ok_color.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // Copyright(c) 2021 Björn Ottosson
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. // this software and associated documentation files(the "Software"), to deal in
  5. // the Software without restriction, including without limitation the rights to
  6. // use, copy, modify, merge, publish, distribute, sublicense, and /or sell copies
  7. // of the Software, and to permit persons to whom the Software is furnished to do
  8. // so, subject to the following conditions :
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef OK_COLOR_H
  19. #define OK_COLOR_H
  20. #include <cmath>
  21. #include <cfloat>
  22. class ok_color
  23. {
  24. public:
  25. struct Lab { float L; float a; float b; };
  26. struct RGB { float r; float g; float b; };
  27. struct HSV { float h; float s; float v; };
  28. struct HSL { float h; float s; float l; };
  29. struct LC { float L; float C; };
  30. // Alternative representation of (L_cusp, C_cusp)
  31. // Encoded so S = C_cusp/L_cusp and T = C_cusp/(1-L_cusp)
  32. // The maximum value for C in the triangle is then found as fmin(S*L, T*(1-L)), for a given L
  33. struct ST { float S; float T; };
  34. static constexpr float pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062f;
  35. static float clamp(float x, float min, float max)
  36. {
  37. if (x < min)
  38. return min;
  39. if (x > max)
  40. return max;
  41. return x;
  42. }
  43. static float sgn(float x)
  44. {
  45. return (float)(0.f < x) - (float)(x < 0.f);
  46. }
  47. static float srgb_transfer_function(float a)
  48. {
  49. return .0031308f >= a ? 12.92f * a : 1.055f * powf(a, .4166666666666667f) - .055f;
  50. }
  51. static float srgb_transfer_function_inv(float a)
  52. {
  53. return .04045f < a ? powf((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
  54. }
  55. static Lab linear_srgb_to_oklab(RGB c)
  56. {
  57. float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
  58. float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
  59. float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
  60. float l_ = cbrtf(l);
  61. float m_ = cbrtf(m);
  62. float s_ = cbrtf(s);
  63. return {
  64. 0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
  65. 1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
  66. 0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_,
  67. };
  68. }
  69. static RGB oklab_to_linear_srgb(Lab c)
  70. {
  71. float l_ = c.L + 0.3963377774f * c.a + 0.2158037573f * c.b;
  72. float m_ = c.L - 0.1055613458f * c.a - 0.0638541728f * c.b;
  73. float s_ = c.L - 0.0894841775f * c.a - 1.2914855480f * c.b;
  74. float l = l_ * l_ * l_;
  75. float m = m_ * m_ * m_;
  76. float s = s_ * s_ * s_;
  77. return {
  78. +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
  79. -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
  80. -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s,
  81. };
  82. }
  83. // Finds the maximum saturation possible for a given hue that fits in sRGB
  84. // Saturation here is defined as S = C/L
  85. // a and b must be normalized so a^2 + b^2 == 1
  86. static float compute_max_saturation(float a, float b)
  87. {
  88. // Max saturation will be when one of r, g or b goes below zero.
  89. // Select different coefficients depending on which component goes below zero first
  90. float k0, k1, k2, k3, k4, wl, wm, ws;
  91. if (-1.88170328f * a - 0.80936493f * b > 1)
  92. {
  93. // Red component
  94. k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
  95. wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
  96. }
  97. else if (1.81444104f * a - 1.19445276f * b > 1)
  98. {
  99. // Green component
  100. k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
  101. wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
  102. }
  103. else
  104. {
  105. // Blue component
  106. k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
  107. wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
  108. }
  109. // Approximate max saturation using a polynomial:
  110. float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
  111. // Do one step Halley's method to get closer
  112. // this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
  113. // this should be sufficient for most applications, otherwise do two/three steps
  114. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  115. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  116. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  117. {
  118. float l_ = 1.f + S * k_l;
  119. float m_ = 1.f + S * k_m;
  120. float s_ = 1.f + S * k_s;
  121. float l = l_ * l_ * l_;
  122. float m = m_ * m_ * m_;
  123. float s = s_ * s_ * s_;
  124. float l_dS = 3.f * k_l * l_ * l_;
  125. float m_dS = 3.f * k_m * m_ * m_;
  126. float s_dS = 3.f * k_s * s_ * s_;
  127. float l_dS2 = 6.f * k_l * k_l * l_;
  128. float m_dS2 = 6.f * k_m * k_m * m_;
  129. float s_dS2 = 6.f * k_s * k_s * s_;
  130. float f = wl * l + wm * m + ws * s;
  131. float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
  132. float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
  133. S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
  134. }
  135. return S;
  136. }
  137. // finds L_cusp and C_cusp for a given hue
  138. // a and b must be normalized so a^2 + b^2 == 1
  139. static LC find_cusp(float a, float b)
  140. {
  141. // First, find the maximum saturation (saturation S = C/L)
  142. float S_cusp = compute_max_saturation(a, b);
  143. // Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
  144. RGB rgb_at_max = oklab_to_linear_srgb({ 1, S_cusp * a, S_cusp * b });
  145. float L_cusp = cbrtf(1.f / fmax(fmax(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
  146. float C_cusp = L_cusp * S_cusp;
  147. return { L_cusp , C_cusp };
  148. }
  149. // Finds intersection of the line defined by
  150. // L = L0 * (1 - t) + t * L1;
  151. // C = t * C1;
  152. // a and b must be normalized so a^2 + b^2 == 1
  153. static float find_gamut_intersection(float a, float b, float L1, float C1, float L0, LC cusp)
  154. {
  155. // Find the intersection for upper and lower half seprately
  156. float t;
  157. if (((L1 - L0) * cusp.C - (cusp.L - L0) * C1) <= 0.f)
  158. {
  159. // Lower half
  160. t = cusp.C * L0 / (C1 * cusp.L + cusp.C * (L0 - L1));
  161. }
  162. else
  163. {
  164. // Upper half
  165. // First intersect with triangle
  166. t = cusp.C * (L0 - 1.f) / (C1 * (cusp.L - 1.f) + cusp.C * (L0 - L1));
  167. // Then one step Halley's method
  168. {
  169. float dL = L1 - L0;
  170. float dC = C1;
  171. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  172. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  173. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  174. float l_dt = dL + dC * k_l;
  175. float m_dt = dL + dC * k_m;
  176. float s_dt = dL + dC * k_s;
  177. // If higher accuracy is required, 2 or 3 iterations of the following block can be used:
  178. {
  179. float L = L0 * (1.f - t) + t * L1;
  180. float C = t * C1;
  181. float l_ = L + C * k_l;
  182. float m_ = L + C * k_m;
  183. float s_ = L + C * k_s;
  184. float l = l_ * l_ * l_;
  185. float m = m_ * m_ * m_;
  186. float s = s_ * s_ * s_;
  187. float ldt = 3 * l_dt * l_ * l_;
  188. float mdt = 3 * m_dt * m_ * m_;
  189. float sdt = 3 * s_dt * s_ * s_;
  190. float ldt2 = 6 * l_dt * l_dt * l_;
  191. float mdt2 = 6 * m_dt * m_dt * m_;
  192. float sdt2 = 6 * s_dt * s_dt * s_;
  193. float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1;
  194. float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
  195. float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
  196. float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
  197. float t_r = -r * u_r;
  198. float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1;
  199. float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
  200. float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
  201. float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
  202. float t_g = -g * u_g;
  203. b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1;
  204. float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
  205. float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
  206. float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
  207. float t_b = -b * u_b;
  208. t_r = u_r >= 0.f ? t_r : FLT_MAX;
  209. t_g = u_g >= 0.f ? t_g : FLT_MAX;
  210. t_b = u_b >= 0.f ? t_b : FLT_MAX;
  211. t += fmin(t_r, fmin(t_g, t_b));
  212. }
  213. }
  214. }
  215. return t;
  216. }
  217. static float find_gamut_intersection(float a, float b, float L1, float C1, float L0)
  218. {
  219. // Find the cusp of the gamut triangle
  220. LC cusp = find_cusp(a, b);
  221. return find_gamut_intersection(a, b, L1, C1, L0, cusp);
  222. }
  223. static RGB gamut_clip_preserve_chroma(RGB rgb)
  224. {
  225. if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
  226. return rgb;
  227. Lab lab = linear_srgb_to_oklab(rgb);
  228. float L = lab.L;
  229. float eps = 0.00001f;
  230. float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
  231. float a_ = lab.a / C;
  232. float b_ = lab.b / C;
  233. float L0 = clamp(L, 0, 1);
  234. float t = find_gamut_intersection(a_, b_, L, C, L0);
  235. float L_clipped = L0 * (1 - t) + t * L;
  236. float C_clipped = t * C;
  237. return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
  238. }
  239. static RGB gamut_clip_project_to_0_5(RGB rgb)
  240. {
  241. if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
  242. return rgb;
  243. Lab lab = linear_srgb_to_oklab(rgb);
  244. float L = lab.L;
  245. float eps = 0.00001f;
  246. float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
  247. float a_ = lab.a / C;
  248. float b_ = lab.b / C;
  249. float L0 = 0.5;
  250. float t = find_gamut_intersection(a_, b_, L, C, L0);
  251. float L_clipped = L0 * (1 - t) + t * L;
  252. float C_clipped = t * C;
  253. return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
  254. }
  255. static RGB gamut_clip_project_to_L_cusp(RGB rgb)
  256. {
  257. if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
  258. return rgb;
  259. Lab lab = linear_srgb_to_oklab(rgb);
  260. float L = lab.L;
  261. float eps = 0.00001f;
  262. float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
  263. float a_ = lab.a / C;
  264. float b_ = lab.b / C;
  265. // The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
  266. LC cusp = find_cusp(a_, b_);
  267. float L0 = cusp.L;
  268. float t = find_gamut_intersection(a_, b_, L, C, L0);
  269. float L_clipped = L0 * (1 - t) + t * L;
  270. float C_clipped = t * C;
  271. return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
  272. }
  273. static RGB gamut_clip_adaptive_L0_0_5(RGB rgb, float alpha = 0.05f)
  274. {
  275. if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
  276. return rgb;
  277. Lab lab = linear_srgb_to_oklab(rgb);
  278. float L = lab.L;
  279. float eps = 0.00001f;
  280. float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
  281. float a_ = lab.a / C;
  282. float b_ = lab.b / C;
  283. float Ld = L - 0.5f;
  284. float e1 = 0.5f + fabs(Ld) + alpha * C;
  285. float L0 = 0.5f * (1.f + sgn(Ld) * (e1 - sqrtf(e1 * e1 - 2.f * fabs(Ld))));
  286. float t = find_gamut_intersection(a_, b_, L, C, L0);
  287. float L_clipped = L0 * (1.f - t) + t * L;
  288. float C_clipped = t * C;
  289. return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
  290. }
  291. static RGB gamut_clip_adaptive_L0_L_cusp(RGB rgb, float alpha = 0.05f)
  292. {
  293. if (rgb.r < 1 && rgb.g < 1 && rgb.b < 1 && rgb.r > 0 && rgb.g > 0 && rgb.b > 0)
  294. return rgb;
  295. Lab lab = linear_srgb_to_oklab(rgb);
  296. float L = lab.L;
  297. float eps = 0.00001f;
  298. float C = fmax(eps, sqrtf(lab.a * lab.a + lab.b * lab.b));
  299. float a_ = lab.a / C;
  300. float b_ = lab.b / C;
  301. // The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
  302. LC cusp = find_cusp(a_, b_);
  303. float Ld = L - cusp.L;
  304. float k = 2.f * (Ld > 0 ? 1.f - cusp.L : cusp.L);
  305. float e1 = 0.5f * k + fabs(Ld) + alpha * C / k;
  306. float L0 = cusp.L + 0.5f * (sgn(Ld) * (e1 - sqrtf(e1 * e1 - 2.f * k * fabs(Ld))));
  307. float t = find_gamut_intersection(a_, b_, L, C, L0);
  308. float L_clipped = L0 * (1.f - t) + t * L;
  309. float C_clipped = t * C;
  310. return oklab_to_linear_srgb({ L_clipped, C_clipped * a_, C_clipped * b_ });
  311. }
  312. static float toe(float x)
  313. {
  314. constexpr float k_1 = 0.206f;
  315. constexpr float k_2 = 0.03f;
  316. constexpr float k_3 = (1.f + k_1) / (1.f + k_2);
  317. return 0.5f * (k_3 * x - k_1 + sqrtf((k_3 * x - k_1) * (k_3 * x - k_1) + 4 * k_2 * k_3 * x));
  318. }
  319. static float toe_inv(float x)
  320. {
  321. constexpr float k_1 = 0.206f;
  322. constexpr float k_2 = 0.03f;
  323. constexpr float k_3 = (1.f + k_1) / (1.f + k_2);
  324. return (x * x + k_1 * x) / (k_3 * (x + k_2));
  325. }
  326. static ST to_ST(LC cusp)
  327. {
  328. float L = cusp.L;
  329. float C = cusp.C;
  330. return { C / L, C / (1 - L) };
  331. }
  332. // Returns a smooth approximation of the location of the cusp
  333. // This polynomial was created by an optimization process
  334. // It has been designed so that S_mid < S_max and T_mid < T_max
  335. static ST get_ST_mid(float a_, float b_)
  336. {
  337. float S = 0.11516993f + 1.f / (
  338. +7.44778970f + 4.15901240f * b_
  339. + a_ * (-2.19557347f + 1.75198401f * b_
  340. + a_ * (-2.13704948f - 10.02301043f * b_
  341. + a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
  342. )))
  343. );
  344. float T = 0.11239642f + 1.f / (
  345. +1.61320320f - 0.68124379f * b_
  346. + a_ * (+0.40370612f + 0.90148123f * b_
  347. + a_ * (-0.27087943f + 0.61223990f * b_
  348. + a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
  349. )))
  350. );
  351. return { S, T };
  352. }
  353. struct Cs { float C_0; float C_mid; float C_max; };
  354. static Cs get_Cs(float L, float a_, float b_)
  355. {
  356. LC cusp = find_cusp(a_, b_);
  357. float C_max = find_gamut_intersection(a_, b_, L, 1, L, cusp);
  358. ST ST_max = to_ST(cusp);
  359. // Scale factor to compensate for the curved part of gamut shape:
  360. float k = C_max / fmin((L * ST_max.S), (1 - L) * ST_max.T);
  361. float C_mid;
  362. {
  363. ST ST_mid = get_ST_mid(a_, b_);
  364. // Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
  365. float C_a = L * ST_mid.S;
  366. float C_b = (1.f - L) * ST_mid.T;
  367. C_mid = 0.9f * k * sqrtf(sqrtf(1.f / (1.f / (C_a * C_a * C_a * C_a) + 1.f / (C_b * C_b * C_b * C_b))));
  368. }
  369. float C_0;
  370. {
  371. // for C_0, the shape is independent of hue, so ST are constant. Values picked to roughly be the average values of ST.
  372. float C_a = L * 0.4f;
  373. float C_b = (1.f - L) * 0.8f;
  374. // Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
  375. C_0 = sqrtf(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
  376. }
  377. return { C_0, C_mid, C_max };
  378. }
  379. static RGB okhsl_to_srgb(HSL hsl)
  380. {
  381. float h = hsl.h;
  382. float s = hsl.s;
  383. float l = hsl.l;
  384. if (l == 1.0f)
  385. {
  386. return { 1.f, 1.f, 1.f };
  387. }
  388. else if (l == 0.f)
  389. {
  390. return { 0.f, 0.f, 0.f };
  391. }
  392. float a_ = cosf(2.f * pi * h);
  393. float b_ = sinf(2.f * pi * h);
  394. float L = toe_inv(l);
  395. Cs cs = get_Cs(L, a_, b_);
  396. float C_0 = cs.C_0;
  397. float C_mid = cs.C_mid;
  398. float C_max = cs.C_max;
  399. float mid = 0.8f;
  400. float mid_inv = 1.25f;
  401. float C, t, k_0, k_1, k_2;
  402. if (s < mid)
  403. {
  404. t = mid_inv * s;
  405. k_1 = mid * C_0;
  406. k_2 = (1.f - k_1 / C_mid);
  407. C = t * k_1 / (1.f - k_2 * t);
  408. }
  409. else
  410. {
  411. t = (s - mid)/ (1 - mid);
  412. k_0 = C_mid;
  413. k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  414. k_2 = (1.f - (k_1) / (C_max - C_mid));
  415. C = k_0 + t * k_1 / (1.f - k_2 * t);
  416. }
  417. RGB rgb = oklab_to_linear_srgb({ L, C * a_, C * b_ });
  418. return {
  419. srgb_transfer_function(rgb.r),
  420. srgb_transfer_function(rgb.g),
  421. srgb_transfer_function(rgb.b),
  422. };
  423. }
  424. static HSL srgb_to_okhsl(RGB rgb)
  425. {
  426. Lab lab = linear_srgb_to_oklab({
  427. srgb_transfer_function_inv(rgb.r),
  428. srgb_transfer_function_inv(rgb.g),
  429. srgb_transfer_function_inv(rgb.b)
  430. });
  431. float C = sqrtf(lab.a * lab.a + lab.b * lab.b);
  432. float a_ = lab.a / C;
  433. float b_ = lab.b / C;
  434. float L = lab.L;
  435. float h = 0.5f + 0.5f * atan2f(-lab.b, -lab.a) / pi;
  436. Cs cs = get_Cs(L, a_, b_);
  437. float C_0 = cs.C_0;
  438. float C_mid = cs.C_mid;
  439. float C_max = cs.C_max;
  440. // Inverse of the interpolation in okhsl_to_srgb:
  441. float mid = 0.8f;
  442. float mid_inv = 1.25f;
  443. float s;
  444. if (C < C_mid)
  445. {
  446. float k_1 = mid * C_0;
  447. float k_2 = (1.f - k_1 / C_mid);
  448. float t = C / (k_1 + k_2 * C);
  449. s = t * mid;
  450. }
  451. else
  452. {
  453. float k_0 = C_mid;
  454. float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
  455. float k_2 = (1.f - (k_1) / (C_max - C_mid));
  456. float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
  457. s = mid + (1.f - mid) * t;
  458. }
  459. float l = toe(L);
  460. return { h, s, l };
  461. }
  462. static RGB okhsv_to_srgb(HSV hsv)
  463. {
  464. float h = hsv.h;
  465. float s = hsv.s;
  466. float v = hsv.v;
  467. float a_ = cosf(2.f * pi * h);
  468. float b_ = sinf(2.f * pi * h);
  469. LC cusp = find_cusp(a_, b_);
  470. ST ST_max = to_ST(cusp);
  471. float S_max = ST_max.S;
  472. float T_max = ST_max.T;
  473. float S_0 = 0.5f;
  474. float k = 1 - S_0 / S_max;
  475. // first we compute L and V as if the gamut is a perfect triangle:
  476. // L, C when v==1:
  477. float L_v = 1 - s * S_0 / (S_0 + T_max - T_max * k * s);
  478. float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
  479. float L = v * L_v;
  480. float C = v * C_v;
  481. // then we compensate for both toe and the curved top part of the triangle:
  482. float L_vt = toe_inv(L_v);
  483. float C_vt = C_v * L_vt / L_v;
  484. float L_new = toe_inv(L);
  485. C = C * L_new / L;
  486. L = L_new;
  487. RGB rgb_scale = oklab_to_linear_srgb({ L_vt, a_ * C_vt, b_ * C_vt });
  488. float scale_L = cbrtf(1.f / fmax(fmax(rgb_scale.r, rgb_scale.g), fmax(rgb_scale.b, 0.f)));
  489. L = L * scale_L;
  490. C = C * scale_L;
  491. RGB rgb = oklab_to_linear_srgb({ L, C * a_, C * b_ });
  492. return {
  493. srgb_transfer_function(rgb.r),
  494. srgb_transfer_function(rgb.g),
  495. srgb_transfer_function(rgb.b),
  496. };
  497. }
  498. static HSV srgb_to_okhsv(RGB rgb)
  499. {
  500. Lab lab = linear_srgb_to_oklab({
  501. srgb_transfer_function_inv(rgb.r),
  502. srgb_transfer_function_inv(rgb.g),
  503. srgb_transfer_function_inv(rgb.b)
  504. });
  505. float C = sqrtf(lab.a * lab.a + lab.b * lab.b);
  506. float a_ = lab.a / C;
  507. float b_ = lab.b / C;
  508. float L = lab.L;
  509. float h = 0.5f + 0.5f * atan2f(-lab.b, -lab.a) / pi;
  510. LC cusp = find_cusp(a_, b_);
  511. ST ST_max = to_ST(cusp);
  512. float S_max = ST_max.S;
  513. float T_max = ST_max.T;
  514. float S_0 = 0.5f;
  515. float k = 1 - S_0 / S_max;
  516. // first we find L_v, C_v, L_vt and C_vt
  517. float t = T_max / (C + L * T_max);
  518. float L_v = t * L;
  519. float C_v = t * C;
  520. float L_vt = toe_inv(L_v);
  521. float C_vt = C_v * L_vt / L_v;
  522. // we can then use these to invert the step that compensates for the toe and the curved top part of the triangle:
  523. RGB rgb_scale = oklab_to_linear_srgb({ L_vt, a_ * C_vt, b_ * C_vt });
  524. float scale_L = cbrtf(1.f / fmax(fmax(rgb_scale.r, rgb_scale.g), fmax(rgb_scale.b, 0.f)));
  525. L = L / scale_L;
  526. C = C / scale_L;
  527. C = C * toe(L) / L;
  528. L = toe(L);
  529. // we can now compute v and s:
  530. float v = L / L_v;
  531. float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
  532. return { h, s, v };
  533. }
  534. };
  535. #endif // OK_COLOR_H