colorspace_oklab.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // The OKLab colourspace code is licensed under MIT
  2. // Most code is taken from
  3. // https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
  4. // and
  5. // https://bottosson.github.io/posts/gamutclipping/
  6. //
  7. // Copyright (c) 2021 Björn Ottosson
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. // this software and associated documentation files (the "Software"), to deal in
  11. // the Software without restriction, including without limitation the rights to
  12. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  13. // of the Software, and to permit persons to whom the Software is furnished to do
  14. // so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in all
  17. // copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. // SOFTWARE.
  26. #include "util/colorspace_oklab.hpp"
  27. #include <algorithm>
  28. #include <cmath>
  29. #include <cfloat>
  30. #include "math/util.hpp"
  31. #include "util/log.hpp"
  32. #include "video/color.hpp"
  33. namespace {
  34. struct ColorRGB {
  35. float r, g, b;
  36. bool is_valid() const;
  37. };
  38. bool
  39. ColorRGB::is_valid() const
  40. {
  41. return r >= 0.0f && r <= 1.0f && g >= 0.0f && g <= 1.0f
  42. && b >= 0.0f && b <= 1.0f;
  43. }
  44. struct ColorOKLab {
  45. float L, a, b;
  46. };
  47. ColorRGB srgb_to_linear_srgb(const Color& c)
  48. {
  49. auto to_linear = [&](float channel) -> float {
  50. if (channel <= 0.04045f)
  51. return channel / 12.92f;
  52. else
  53. return powf((channel + 0.055f) / (1.0f + 0.055f), 2.4f);
  54. };
  55. return {to_linear(c.red), to_linear(c.green), to_linear(c.blue)};
  56. }
  57. Color linear_srgb_to_srgb(const ColorRGB& c)
  58. {
  59. auto make_nonlinear = [&](float channel) -> float {
  60. if (channel <= 0.0031308f)
  61. return 12.92f * channel;
  62. else
  63. return (1.0f + 0.055f) * powf(channel, 1.0f / 2.4f) - 0.055f;
  64. };
  65. float r = make_nonlinear(c.r);
  66. float g = make_nonlinear(c.g);
  67. float b = make_nonlinear(c.b);
  68. // The clamping here is only for safety against numerical precision errors.
  69. // r, g and b should already be in [0,1] (at least approximately)
  70. // since they were clipped in the OKLab colourspace.
  71. return Color(math::clamp(r, 0.0f, 1.0f), math::clamp(g, 0.0f, 1.0f),
  72. math::clamp(b, 0.0f, 1.0f));
  73. }
  74. ColorOKLab linear_srgb_to_oklab(const ColorRGB& c)
  75. {
  76. float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
  77. float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
  78. float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
  79. float l_ = cbrtf(l);
  80. float m_ = cbrtf(m);
  81. float s_ = cbrtf(s);
  82. return {
  83. 0.2104542553f*l_ + 0.7936177850f*m_ - 0.0040720468f*s_,
  84. 1.9779984951f*l_ - 2.4285922050f*m_ + 0.4505937099f*s_,
  85. 0.0259040371f*l_ + 0.7827717662f*m_ - 0.8086757660f*s_,
  86. };
  87. }
  88. ColorRGB oklab_to_linear_srgb(const ColorOKLab& c)
  89. {
  90. float l_ = c.L + 0.3963377774f * c.a + 0.2158037573f * c.b;
  91. float m_ = c.L - 0.1055613458f * c.a - 0.0638541728f * c.b;
  92. float s_ = c.L - 0.0894841775f * c.a - 1.2914855480f * c.b;
  93. float l = l_*l_*l_;
  94. float m = m_*m_*m_;
  95. float s = s_*s_*s_;
  96. return {
  97. +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
  98. -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
  99. -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s,
  100. };
  101. }
  102. ColorOKLCh lab_to_lch(const ColorOKLab& c)
  103. {
  104. return ColorOKLCh{c.L, sqrtf(c.a * c.a + c.b * c.b), atan2f(c.b, c.a)};
  105. }
  106. ColorOKLab lch_to_lab(const ColorOKLCh& c)
  107. {
  108. return {c.L, c.C * cosf(c.h), c.C * sinf(c.h)};
  109. }
  110. // Finds the maximum saturation possible for a given hue that fits in sRGB
  111. // Saturation here is defined as S = C/L
  112. // a and b must be normalized so a^2 + b^2 == 1.
  113. float compute_max_saturation(float a, float b)
  114. {
  115. // Max saturation will be when one of r, g or b goes below zero.
  116. // Select different coefficients depending on which component goes below zero first.
  117. float k0, k1, k2, k3, k4, wl, wm, ws;
  118. if (-1.88170328f * a - 0.80936493f * b > 1)
  119. {
  120. // Red component.
  121. k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
  122. wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
  123. }
  124. else if (1.81444104f * a - 1.19445276f * b > 1)
  125. {
  126. // Green component.
  127. k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
  128. wl = -1.2681437731f; wm = +2.6097574011f; ws = -0.3413193965f;
  129. }
  130. else
  131. {
  132. // Blue component.
  133. k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
  134. wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
  135. }
  136. // Approximate max saturation using a polynomial:
  137. float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
  138. // Do one step Halley's method to get closer
  139. // this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
  140. // this should be sufficient for most applications, otherwise do two/three steps.
  141. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  142. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  143. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  144. {
  145. float l_ = 1.f + S * k_l;
  146. float m_ = 1.f + S * k_m;
  147. float s_ = 1.f + S * k_s;
  148. float l = l_ * l_ * l_;
  149. float m = m_ * m_ * m_;
  150. float s = s_ * s_ * s_;
  151. float l_dS = 3.f * k_l * l_ * l_;
  152. float m_dS = 3.f * k_m * m_ * m_;
  153. float s_dS = 3.f * k_s * s_ * s_;
  154. float l_dS2 = 6.f * k_l * k_l * l_;
  155. float m_dS2 = 6.f * k_m * k_m * m_;
  156. float s_dS2 = 6.f * k_s * k_s * s_;
  157. float f = wl * l + wm * m + ws * s;
  158. float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
  159. float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
  160. S = S - f * f1 / (f1*f1 - 0.5f * f * f2);
  161. }
  162. return S;
  163. }
  164. // Finds L_cusp and C_cusp for a given hue
  165. // a and b must be normalized so a^2 + b^2 == 1.
  166. struct OKLabCusp {
  167. float L, C;
  168. };
  169. OKLabCusp find_cusp(float a, float b)
  170. {
  171. // First, find the maximum saturation (saturation S = C/L).
  172. float S_cusp = compute_max_saturation(a, b);
  173. // Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
  174. ColorOKLab c = {1, S_cusp * a, S_cusp * b};
  175. ColorRGB rgb_at_max = oklab_to_linear_srgb(c);
  176. float L_cusp = cbrtf(1.f / std::max(std::max(rgb_at_max.r, rgb_at_max.g),
  177. rgb_at_max.b));
  178. float C_cusp = L_cusp * S_cusp;
  179. return {L_cusp , C_cusp};
  180. }
  181. // Finds intersection of the line defined by
  182. // L = L0 * (1 - t) + t * L1;
  183. // C = t * C1;
  184. // a and b must be normalized so a^2 + b^2 == 1.
  185. float find_gamut_intersection(float a, float b, float L1, float C1, float L0)
  186. {
  187. // Find the cusp of the gamut triangle.
  188. OKLabCusp cusp = find_cusp(a, b);
  189. // Find the intersection for upper and lower half seprately.
  190. float t;
  191. if (((L1 - L0) * cusp.C - (cusp.L - L0) * C1) <= 0.f)
  192. {
  193. // Lower half.
  194. t = cusp.C * L0 / (C1 * cusp.L + cusp.C * (L0 - L1));
  195. }
  196. else
  197. {
  198. // Upper half.
  199. // First intersect with triangle.
  200. t = cusp.C * (L0 - 1.f) / (C1 * (cusp.L - 1.f) + cusp.C * (L0 - L1));
  201. // Then one step Halley's method.
  202. {
  203. float dL = L1 - L0;
  204. float dC = C1;
  205. float k_l = +0.3963377774f * a + 0.2158037573f * b;
  206. float k_m = -0.1055613458f * a - 0.0638541728f * b;
  207. float k_s = -0.0894841775f * a - 1.2914855480f * b;
  208. float l_dt = dL + dC * k_l;
  209. float m_dt = dL + dC * k_m;
  210. float s_dt = dL + dC * k_s;
  211. // If higher accuracy is required, 2 or 3 iterations of the following block can be used:
  212. {
  213. float L = L0 * (1.f - t) + t * L1;
  214. float C = t * C1;
  215. float l_ = L + C * k_l;
  216. float m_ = L + C * k_m;
  217. float s_ = L + C * k_s;
  218. float l = l_ * l_ * l_;
  219. float m = m_ * m_ * m_;
  220. float s = s_ * s_ * s_;
  221. float ldt = 3 * l_dt * l_ * l_;
  222. float mdt = 3 * m_dt * m_ * m_;
  223. float sdt = 3 * s_dt * s_ * s_;
  224. float ldt2 = 6 * l_dt * l_dt * l_;
  225. float mdt2 = 6 * m_dt * m_dt * m_;
  226. float sdt2 = 6 * s_dt * s_dt * s_;
  227. float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1;
  228. float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
  229. float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
  230. float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
  231. float t_r = -r * u_r;
  232. float g = -1.2681437731f * l + 2.6097574011f * m - 0.3413193965f * s - 1;
  233. float g1 = -1.2681437731f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
  234. float g2 = -1.2681437731f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
  235. float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
  236. float t_g = -g * u_g;
  237. b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1;
  238. float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
  239. float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
  240. float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
  241. float t_b = -b * u_b;
  242. t_r = u_r >= 0.f ? t_r : FLT_MAX;
  243. t_g = u_g >= 0.f ? t_g : FLT_MAX;
  244. t_b = u_b >= 0.f ? t_b : FLT_MAX;
  245. t += std::min(t_r, std::min(t_g, t_b));
  246. }
  247. }
  248. }
  249. return t;
  250. }
  251. } // namespace
  252. ColorOKLCh::ColorOKLCh(Color& c) :
  253. L(0.0f),
  254. C(0.0f),
  255. h(0.0f)
  256. {
  257. ColorRGB rgb = srgb_to_linear_srgb(c);
  258. ColorOKLab lab = linear_srgb_to_oklab(rgb);
  259. *this = lab_to_lch(lab);
  260. if (C < 0.00001f)
  261. // Deterministic behaviour when increasing chroma of greyscale colours.
  262. h = 0.0f;
  263. }
  264. Color
  265. ColorOKLCh::to_srgb() const
  266. {
  267. ColorOKLCh c = *this;
  268. ColorOKLab lab = lch_to_lab(c);
  269. ColorRGB rgb = oklab_to_linear_srgb(lab);
  270. if (!rgb.is_valid()) {
  271. c.clip_chroma();
  272. // Gamut clipping; reduce chroma when needed.
  273. lab = lch_to_lab(c);
  274. rgb = oklab_to_linear_srgb(lab);
  275. }
  276. if (!(rgb.r > -0.001f && rgb.r < 1.001f && rgb.g > -0.001f && rgb.g < 1.001f
  277. && rgb.b > -0.001f && rgb.b < 1.001f)) {
  278. log_warning << "Colour out of bounds (after clipping): (" << rgb.r <<
  279. ", " << rgb.g << ", " << rgb.b << ")" << std::endl;
  280. }
  281. return linear_srgb_to_srgb(rgb);
  282. }
  283. float
  284. ColorOKLCh::get_maximum_chroma() const
  285. {
  286. if (C <= 0.0f || L <= 0.0f || L >= 1.0f)
  287. return 0.0f;
  288. return find_gamut_intersection(cosf(h), sinf(h), L, 1.0f, L);
  289. }
  290. float
  291. ColorOKLCh::get_maximum_chroma_any_l() const
  292. {
  293. OKLabCusp cusp = find_cusp(cosf(h), sinf(h));
  294. return cusp.C;
  295. }
  296. void
  297. ColorOKLCh::clip_chroma()
  298. {
  299. // Avoid numerical problems for certain hues of blue.
  300. if (-1.67462f < h && h < -1.67460f)
  301. h = -1.67462f;
  302. L = math::clamp(L, 0.0f, 1.0f);
  303. C = math::clamp(C, 0.0f, get_maximum_chroma());
  304. }
  305. void
  306. ColorOKLCh::clip_lightness()
  307. {
  308. // Avoid numerical problems for certain hues of blue.
  309. if (-1.67462f < h && h < -1.67460f)
  310. h = -1.67462f;
  311. L = math::clamp(L, 0.0f, 1.0f);
  312. ColorOKLab lab = lch_to_lab(*this);
  313. ColorRGB rgb = oklab_to_linear_srgb(lab);
  314. if (rgb.is_valid())
  315. return;
  316. OKLabCusp cusp = find_cusp(lab.a / C, lab.b / C);
  317. if (C >= cusp.C) {
  318. // The cusp is the most colourful point for the given hue.
  319. C = cusp.C;
  320. L = cusp.L;
  321. return;
  322. }
  323. // Select a point inside the triangle defined by (L,C) in {(0,0), (1,0), cusp}
  324. // and then move it further if it's not in the sRGB gamut.
  325. if (L > cusp.L) {
  326. // Reduce L so that it's on the triangle.
  327. L = std::min<float>(L, 1.0f + C * (cusp.L - 1.0f) / cusp.C);
  328. // Reduce L so that it's in the sRGB gamut.
  329. float L0 = -100.0f;
  330. float t = find_gamut_intersection(lab.a / C, lab.b / C, L, C, L0);
  331. L = (1.0f - t) * L0 + t * L;
  332. C *= t;
  333. } else {
  334. // Here the triangle is accurate.
  335. L = std::max<float>(L, C * cusp.L / cusp.C);
  336. }
  337. }
  338. void
  339. ColorOKLCh::clip_adaptive_L0_L_cusp(float alpha)
  340. {
  341. // Avoid numerical problems for certain hues of blue.
  342. if (-1.67462f < h && h < -1.67460f)
  343. h = -1.67462f;
  344. ColorOKLab lab = lch_to_lab(*this);
  345. ColorRGB rgb = oklab_to_linear_srgb(lab);
  346. if (rgb.is_valid())
  347. return;
  348. float a_ = lab.a / C;
  349. float b_ = lab.b / C;
  350. OKLabCusp cusp = find_cusp(a_, b_);
  351. float Ld = L - cusp.L;
  352. float k = 2.f * (Ld > 0 ? 1.f - cusp.L : cusp.L);
  353. float e1 = 0.5f * k + fabsf(Ld) + alpha * C / k;
  354. float sgn = Ld < 0.0f ? -1.0f : 1.0f;
  355. float L0 = cusp.L + 0.5f * (sgn * (e1 - sqrtf(
  356. std::max<float>(e1 * e1 - 2.f * k * fabsf(Ld), 0.0f))));
  357. float t = find_gamut_intersection(a_, b_, L, C, L0);
  358. L = (1.0f - t) * L0 + t * L;
  359. C *= t;
  360. }
  361. /* EOF */