oklab.glsl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // https://bottosson.github.io/posts/oklab/
  2. float cbrtf(float x) {
  3. return pow(x, 1.0 / 3.0);
  4. }
  5. vec3 rgbToOklab(vec3 c)
  6. {
  7. float l = 0.4122214708 * c.r + 0.5363325363 * c.g + 0.0514459929 * c.b;
  8. float m = 0.2119034982 * c.r + 0.6806995451 * c.g + 0.1073969566 * c.b;
  9. float s = 0.0883024619 * c.r + 0.2817188376 * c.g + 0.6299787005 * c.b;
  10. float l_ = cbrtf(l);
  11. float m_ = cbrtf(m);
  12. float s_ = cbrtf(s);
  13. return vec3(
  14. 0.2104542553*l_ + 0.7936177850*m_ - 0.0040720468*s_,
  15. 1.9779984951*l_ - 2.4285922050*m_ + 0.4505937099*s_,
  16. 0.0259040371*l_ + 0.7827717662*m_ - 0.8086757660*s_
  17. );
  18. }
  19. vec3 oklabToRGB(vec3 c)
  20. {
  21. float l_ = c.x + 0.3963377774 * c.y + 0.2158037573 * c.z;
  22. float m_ = c.x - 0.1055613458 * c.y - 0.0638541728 * c.z;
  23. float s_ = c.x - 0.0894841775 * c.y - 1.2914855480 * c.z;
  24. float l = l_*l_*l_;
  25. float m = m_*m_*m_;
  26. float s = s_*s_*s_;
  27. return vec3(
  28. +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
  29. -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
  30. -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
  31. );
  32. }
  33. #ifndef USE_OKLAB
  34. #define col3 vec3
  35. #else
  36. vec3 col3(float r, float g, float b) {
  37. return rgbToOklab(vec3(r, g, b));
  38. }
  39. vec3 col3(vec3 v) {
  40. return rgbToOklab(v);
  41. }
  42. vec3 col3(float v) {
  43. return rgbToOklab(vec3(v));
  44. }
  45. #endif