ok_color_shader.h 18 KB

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