summaryrefslogtreecommitdiffstats
path: root/oklab.cginc
blob: 4c3342851362631ced813d3be605c1d11e5bac31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * MIT License
 *
 * Copyright (c) 2023 yum_food
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE
 * SOFTWARE.
 */

#ifndef __OKLAB_INC
#define __OKLAB_INC

#if defined(_OKLAB)

// Utilities relating to the OKLAB color space, as defined here:
//   https://bottosson.github.io/posts/oklab/
// Code is derived from the samples there, which are in the public domain.

// Weights: https://en.wikipedia.org/wiki/SRGB
float3 LRGBtoXYZ(float3 c) {
  float3x3 rgb_to_xyz = float3x3(
      0.4124, 0.3576, 0.1805,
      0.2126, 0.7152, 0.0722,
      0.0193, 0.1192, 0.9505
      );
  return mul(rgb_to_xyz, c);
}

// Weights: https://en.wikipedia.org/wiki/SRGB
float3 XYZtoLRGB(float3 c) {
  float3x3 xyz_to_rgb = float3x3(
      3.24062548, -1.53720797, -0.4986286,
      -0.96893071,  1.87575606,  0.04151752,
      0.05571012, -0.20402105,  1.05699594
      );
  return mul(xyz_to_rgb, c);
}

// Source: https://bottosson.github.io/posts/oklab/
float3 XYZtoOKLAB(float3 c) {
  float3x3 m1 = float3x3(
      0.8189, 0.3618, -0.1288,
      0.0329, 0.9293, 0.0361,
      0.0482, 0.2643, 0.6338
      );
  float3x3 m2 = float3x3(
      0.2104, 0.7936, -0.0040,
      1.9779, -2.4285, 0.4505,
      0.0259, 0.7827, -0.8086
      );
  c = mul(m1, c);
  c = pow(c, 0.33333333333);
  return mul(m2, c);
}

// Source: https://bottosson.github.io/posts/oklab/
float3 OKLABtoXYZ(float3 c) {
  float3x3 m1i = float3x3(
      1.22700842, -0.5576564 ,  0.28111404,
      -0.04047048,  1.11219073, -0.07157255,
      -0.07643651, -0.42138367,  1.58625265
      );
  float3x3 m2i = float3x3(
      1.00003964,  0.39638005,  0.21589049,
      0.99998945, -0.10553958, -0.06374665,
      0.99999105, -0.08946276, -1.291495
      );
  c = mul(m2i, c);
  c = pow(c, 3);
  return mul(m1i, c);
}

// Source: https://bottosson.github.io/posts/oklab/
float3 OKLABtoOKLCH(float3 c) {
  float c_ = length(c.yz);
  float h_ = atan2(c.z, c.y);
  return float3(c.x, c_, h_);
}

// Source: https://bottosson.github.io/posts/oklab/
// Note: hue must be in units of radians.
float3 OKLCHtoOKLAB(float3 c) {
  float a = c.y * cos(c.z);
  float b = c.y * sin(c.z);
  return float3(c.x, a, b);
}

float3 LRGBtoOKLAB(float3 c) {
  return XYZtoOKLAB(LRGBtoXYZ(c));
}

float3 OKLABtoLRGB(float3 c) {
  return XYZtoLRGB(OKLABtoXYZ(c));
}

float3 LRGBtoOKLCH(float3 c) {
  return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c)));
}

float3 OKLCHtoLRGB(float3 c) {
  return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c)));
}

#endif  // _OKLAB

#endif  // __OKLAB_INC