palette-shader 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +237 -0
- package/dist/palette-shader.d.ts +167 -0
- package/dist/palette-shader.js +1691 -0
- package/dist/palette-shader.js.map +1 -0
- package/dist/palette-shader.umd.cjs +1027 -0
- package/dist/palette-shader.umd.cjs.map +1 -0
- package/package.json +49 -0
- package/src/index.ts +485 -0
- package/src/shaders/closestColor.frag.glsl +41 -0
- package/src/shaders/deltaE.frag.glsl +146 -0
- package/src/shaders/hsl2rgb.frag.glsl +4 -0
- package/src/shaders/hsv2rgb.frag.glsl +5 -0
- package/src/shaders/lch2rgb.frag.glsl +31 -0
- package/src/shaders/oklab.frag.glsl +653 -0
- package/src/shaders/srgb2rgb.frag.glsl +4 -0
- package/src/types.ts +22 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// slightly rearranged vector components so it matches with LCH
|
|
2
|
+
// M_PI and srgb_transfer_function are provided by oklab.frag.glsl (included before this file)
|
|
3
|
+
vec3 lch2rgb(vec3 lch) {
|
|
4
|
+
lch.y *= 0.34;
|
|
5
|
+
|
|
6
|
+
vec3 lab = vec3(
|
|
7
|
+
lch.x,
|
|
8
|
+
lch.y * cos(lch.z * M_PI*2.0),
|
|
9
|
+
lch.y * sin(lch.z * M_PI*2.0)
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
vec3 lms = vec3(
|
|
13
|
+
lab.x + 0.3963377774f * lab.y + 0.2158037573f * lab.z,
|
|
14
|
+
lab.x - 0.1055613458f * lab.y - 0.0638541728f * lab.z,
|
|
15
|
+
lab.x - 0.0894841775f * lab.y - 1.2914855480f * lab.z
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
lms = pow(max(lms, vec3(0.0)), vec3(3.0));
|
|
19
|
+
|
|
20
|
+
vec3 rgb = vec3(
|
|
21
|
+
+4.0767416621f * lms.x - 3.3077115913f * lms.y + 0.2309699292f * lms.z,
|
|
22
|
+
-1.2684380046f * lms.x + 2.6097574011f * lms.y - 0.3413193965f * lms.z,
|
|
23
|
+
-0.0041960863f * lms.x - 0.7034186147f * lms.y + 1.7076147010f * lms.z
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
return vec3(
|
|
27
|
+
srgb_transfer_function(rgb.r),
|
|
28
|
+
srgb_transfer_function(rgb.g),
|
|
29
|
+
srgb_transfer_function(rgb.b)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,653 @@
|
|
|
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 softwareand 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 noticeand 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
|
+
|
|
19
|
+
#define M_PI 3.1415926535897932384626433832795
|
|
20
|
+
|
|
21
|
+
float cbrt( float x )
|
|
22
|
+
{
|
|
23
|
+
return sign(x)*pow(abs(x),1.0f/3.0f);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
float srgb_transfer_function(float a)
|
|
27
|
+
{
|
|
28
|
+
return .0031308f >= a ? 12.92f * a : 1.055f * pow(a, .4166666666666667f) - .055f;
|
|
29
|
+
}
|
|
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
|
+
|
|
36
|
+
vec3 linear_srgb_to_oklab(vec3 c)
|
|
37
|
+
{
|
|
38
|
+
float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
|
|
39
|
+
float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
|
|
40
|
+
float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
|
|
41
|
+
|
|
42
|
+
float l_ = cbrt(l);
|
|
43
|
+
float m_ = cbrt(m);
|
|
44
|
+
float s_ = cbrt(s);
|
|
45
|
+
|
|
46
|
+
return vec3(
|
|
47
|
+
0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
|
|
48
|
+
1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
|
|
49
|
+
0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
vec3 oklab_to_linear_srgb(vec3 c)
|
|
54
|
+
{
|
|
55
|
+
float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
|
|
56
|
+
float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
|
|
57
|
+
float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
|
|
58
|
+
|
|
59
|
+
float l = l_ * l_ * l_;
|
|
60
|
+
float m = m_ * m_ * m_;
|
|
61
|
+
float s = s_ * s_ * s_;
|
|
62
|
+
|
|
63
|
+
return vec3(
|
|
64
|
+
+4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
|
|
65
|
+
-1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
|
|
66
|
+
-0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Finds the maximum saturation possible for a given hue that fits in sRGB
|
|
71
|
+
// Saturation here is defined as S = C/L
|
|
72
|
+
// a and b must be normalized so a^2 + b^2 == 1
|
|
73
|
+
float compute_max_saturation(float a, float b)
|
|
74
|
+
{
|
|
75
|
+
// Max saturation will be when one of r, g or b goes below zero.
|
|
76
|
+
|
|
77
|
+
// Select different coefficients depending on which component goes below zero first
|
|
78
|
+
float k0, k1, k2, k3, k4, wl, wm, ws;
|
|
79
|
+
|
|
80
|
+
if (-1.88170328f * a - 0.80936493f * b > 1.f)
|
|
81
|
+
{
|
|
82
|
+
// Red component
|
|
83
|
+
k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
|
|
84
|
+
wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
|
|
85
|
+
}
|
|
86
|
+
else if (1.81444104f * a - 1.19445276f * b > 1.f)
|
|
87
|
+
{
|
|
88
|
+
// Green component
|
|
89
|
+
k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
|
|
90
|
+
wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
{
|
|
94
|
+
// Blue component
|
|
95
|
+
k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
|
|
96
|
+
wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Approximate max saturation using a polynomial:
|
|
100
|
+
float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
|
|
101
|
+
|
|
102
|
+
// Do one step Halley's method to get closer
|
|
103
|
+
// this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
|
|
104
|
+
// this should be sufficient for most applications, otherwise do two/three steps
|
|
105
|
+
|
|
106
|
+
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
|
107
|
+
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
|
108
|
+
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
|
109
|
+
|
|
110
|
+
{
|
|
111
|
+
float l_ = 1.f + S * k_l;
|
|
112
|
+
float m_ = 1.f + S * k_m;
|
|
113
|
+
float s_ = 1.f + S * k_s;
|
|
114
|
+
|
|
115
|
+
float l = l_ * l_ * l_;
|
|
116
|
+
float m = m_ * m_ * m_;
|
|
117
|
+
float s = s_ * s_ * s_;
|
|
118
|
+
|
|
119
|
+
float l_dS = 3.f * k_l * l_ * l_;
|
|
120
|
+
float m_dS = 3.f * k_m * m_ * m_;
|
|
121
|
+
float s_dS = 3.f * k_s * s_ * s_;
|
|
122
|
+
|
|
123
|
+
float l_dS2 = 6.f * k_l * k_l * l_;
|
|
124
|
+
float m_dS2 = 6.f * k_m * k_m * m_;
|
|
125
|
+
float s_dS2 = 6.f * k_s * k_s * s_;
|
|
126
|
+
|
|
127
|
+
float f = wl * l + wm * m + ws * s;
|
|
128
|
+
float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
|
|
129
|
+
float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
|
|
130
|
+
|
|
131
|
+
S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return S;
|
|
135
|
+
}
|
|
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
|
+
vec2 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
|
+
|
|
144
|
+
// Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
|
|
145
|
+
vec3 rgb_at_max = oklab_to_linear_srgb(vec3( 1, S_cusp * a, S_cusp * b ));
|
|
146
|
+
float L_cusp = cbrt(1.f / max(max(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
|
|
147
|
+
float C_cusp = L_cusp * S_cusp;
|
|
148
|
+
|
|
149
|
+
return vec2( L_cusp , C_cusp );
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Finds intersection of the line defined by
|
|
153
|
+
// L = L0 * (1 - t) + t * L1;
|
|
154
|
+
// C = t * C1;
|
|
155
|
+
// a and b must be normalized so a^2 + b^2 == 1
|
|
156
|
+
float find_gamut_intersection(float a, float b, float L1, float C1, float L0, vec2 cusp)
|
|
157
|
+
{
|
|
158
|
+
// Find the intersection for upper and lower half seprately
|
|
159
|
+
float t;
|
|
160
|
+
if (((L1 - L0) * cusp.y - (cusp.x - L0) * C1) <= 0.f)
|
|
161
|
+
{
|
|
162
|
+
// Lower half
|
|
163
|
+
|
|
164
|
+
t = cusp.y * L0 / (C1 * cusp.x + cusp.y * (L0 - L1));
|
|
165
|
+
}
|
|
166
|
+
else
|
|
167
|
+
{
|
|
168
|
+
// Upper half
|
|
169
|
+
|
|
170
|
+
// First intersect with triangle
|
|
171
|
+
t = cusp.y * (L0 - 1.f) / (C1 * (cusp.x - 1.f) + cusp.y * (L0 - L1));
|
|
172
|
+
|
|
173
|
+
// Then one step Halley's method
|
|
174
|
+
{
|
|
175
|
+
float dL = L1 - L0;
|
|
176
|
+
float dC = C1;
|
|
177
|
+
|
|
178
|
+
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
|
179
|
+
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
|
180
|
+
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
|
181
|
+
|
|
182
|
+
float l_dt = dL + dC * k_l;
|
|
183
|
+
float m_dt = dL + dC * k_m;
|
|
184
|
+
float s_dt = dL + dC * k_s;
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
// If higher accuracy is required, 2 or 3 iterations of the following block can be used:
|
|
188
|
+
{
|
|
189
|
+
float L = L0 * (1.f - t) + t * L1;
|
|
190
|
+
float C = t * C1;
|
|
191
|
+
|
|
192
|
+
float l_ = L + C * k_l;
|
|
193
|
+
float m_ = L + C * k_m;
|
|
194
|
+
float s_ = L + C * k_s;
|
|
195
|
+
|
|
196
|
+
float l = l_ * l_ * l_;
|
|
197
|
+
float m = m_ * m_ * m_;
|
|
198
|
+
float s = s_ * s_ * s_;
|
|
199
|
+
|
|
200
|
+
float ldt = 3.f * l_dt * l_ * l_;
|
|
201
|
+
float mdt = 3.f * m_dt * m_ * m_;
|
|
202
|
+
float sdt = 3.f * s_dt * s_ * s_;
|
|
203
|
+
|
|
204
|
+
float ldt2 = 6.f * l_dt * l_dt * l_;
|
|
205
|
+
float mdt2 = 6.f * m_dt * m_dt * m_;
|
|
206
|
+
float sdt2 = 6.f * s_dt * s_dt * s_;
|
|
207
|
+
|
|
208
|
+
float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1.f;
|
|
209
|
+
float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
|
|
210
|
+
float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
|
|
211
|
+
|
|
212
|
+
float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
|
|
213
|
+
float t_r = -r * u_r;
|
|
214
|
+
|
|
215
|
+
float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1.f;
|
|
216
|
+
float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
|
|
217
|
+
float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
|
|
218
|
+
|
|
219
|
+
float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
|
|
220
|
+
float t_g = -g * u_g;
|
|
221
|
+
|
|
222
|
+
float b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1.f;
|
|
223
|
+
float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
|
|
224
|
+
float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
|
|
225
|
+
|
|
226
|
+
float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
|
|
227
|
+
float t_b = -b * u_b;
|
|
228
|
+
|
|
229
|
+
t_r = u_r >= 0.f ? t_r : 10000.f;
|
|
230
|
+
t_g = u_g >= 0.f ? t_g : 10000.f;
|
|
231
|
+
t_b = u_b >= 0.f ? t_b : 10000.f;
|
|
232
|
+
|
|
233
|
+
t += min(t_r, min(t_g, t_b));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return t;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
float find_gamut_intersection(float a, float b, float L1, float C1, float L0)
|
|
242
|
+
{
|
|
243
|
+
// Find the cusp of the gamut triangle
|
|
244
|
+
vec2 cusp = find_cusp(a, b);
|
|
245
|
+
|
|
246
|
+
return find_gamut_intersection(a, b, L1, C1, L0, cusp);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
vec3 gamut_clip_preserve_chroma(vec3 rgb)
|
|
250
|
+
{
|
|
251
|
+
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)
|
|
252
|
+
return rgb;
|
|
253
|
+
|
|
254
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
|
255
|
+
|
|
256
|
+
float L = lab.x;
|
|
257
|
+
float eps = 0.00001f;
|
|
258
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
|
259
|
+
float a_ = lab.y / C;
|
|
260
|
+
float b_ = lab.z / C;
|
|
261
|
+
|
|
262
|
+
float L0 = clamp(L, 0.f, 1.f);
|
|
263
|
+
|
|
264
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
|
265
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
|
266
|
+
float C_clipped = t * C;
|
|
267
|
+
|
|
268
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
vec3 gamut_clip_project_to_0_5(vec3 rgb)
|
|
272
|
+
{
|
|
273
|
+
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)
|
|
274
|
+
return rgb;
|
|
275
|
+
|
|
276
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
|
277
|
+
|
|
278
|
+
float L = lab.x;
|
|
279
|
+
float eps = 0.00001f;
|
|
280
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
|
281
|
+
float a_ = lab.y / C;
|
|
282
|
+
float b_ = lab.z / C;
|
|
283
|
+
|
|
284
|
+
float L0 = 0.5;
|
|
285
|
+
|
|
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
|
+
|
|
290
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
vec3 gamut_clip_project_to_L_cusp(vec3 rgb)
|
|
294
|
+
{
|
|
295
|
+
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)
|
|
296
|
+
return rgb;
|
|
297
|
+
|
|
298
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
|
299
|
+
|
|
300
|
+
float L = lab.x;
|
|
301
|
+
float eps = 0.00001f;
|
|
302
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
|
303
|
+
float a_ = lab.y / C;
|
|
304
|
+
float b_ = lab.z / C;
|
|
305
|
+
|
|
306
|
+
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
|
307
|
+
vec2 cusp = find_cusp(a_, b_);
|
|
308
|
+
|
|
309
|
+
float L0 = cusp.x;
|
|
310
|
+
|
|
311
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
|
312
|
+
|
|
313
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
|
314
|
+
float C_clipped = t * C;
|
|
315
|
+
|
|
316
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
vec3 gamut_clip_adaptive_L0_0_5(vec3 rgb, float alpha)
|
|
320
|
+
{
|
|
321
|
+
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)
|
|
322
|
+
return rgb;
|
|
323
|
+
|
|
324
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
|
325
|
+
|
|
326
|
+
float L = lab.x;
|
|
327
|
+
float eps = 0.00001f;
|
|
328
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
|
329
|
+
float a_ = lab.y / C;
|
|
330
|
+
float b_ = lab.z / C;
|
|
331
|
+
|
|
332
|
+
float Ld = L - 0.5f;
|
|
333
|
+
float e1 = 0.5f + abs(Ld) + alpha * C;
|
|
334
|
+
float L0 = 0.5f * (1.f + sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * abs(Ld))));
|
|
335
|
+
|
|
336
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
|
337
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
|
338
|
+
float C_clipped = t * C;
|
|
339
|
+
|
|
340
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
vec3 gamut_clip_adaptive_L0_L_cusp(vec3 rgb, float alpha)
|
|
344
|
+
{
|
|
345
|
+
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)
|
|
346
|
+
return rgb;
|
|
347
|
+
|
|
348
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
|
349
|
+
|
|
350
|
+
float L = lab.x;
|
|
351
|
+
float eps = 0.00001f;
|
|
352
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
|
353
|
+
float a_ = lab.y / C;
|
|
354
|
+
float b_ = lab.z / C;
|
|
355
|
+
|
|
356
|
+
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
|
357
|
+
vec2 cusp = find_cusp(a_, b_);
|
|
358
|
+
|
|
359
|
+
float Ld = L - cusp.x;
|
|
360
|
+
float k = 2.f * (Ld > 0.f ? 1.f - cusp.x : cusp.x);
|
|
361
|
+
|
|
362
|
+
float e1 = 0.5f * k + abs(Ld) + alpha * C / k;
|
|
363
|
+
float L0 = cusp.x + 0.5f * (sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * k * abs(Ld))));
|
|
364
|
+
|
|
365
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
|
366
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
|
367
|
+
float C_clipped = t * C;
|
|
368
|
+
|
|
369
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
float toe(float x)
|
|
373
|
+
{
|
|
374
|
+
float k_1 = 0.206f;
|
|
375
|
+
float k_2 = 0.03f;
|
|
376
|
+
float k_3 = (1.f + k_1) / (1.f + k_2);
|
|
377
|
+
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));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
float toe_inv(float x)
|
|
381
|
+
{
|
|
382
|
+
float k_1 = 0.206f;
|
|
383
|
+
float k_2 = 0.03f;
|
|
384
|
+
float k_3 = (1.f + k_1) / (1.f + k_2);
|
|
385
|
+
return (x * x + k_1 * x) / (k_3 * (x + k_2));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
vec2 to_ST(vec2 cusp)
|
|
389
|
+
{
|
|
390
|
+
float L = cusp.x;
|
|
391
|
+
float C = cusp.y;
|
|
392
|
+
return vec2( C / L, C / (1.f - L) );
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Returns a smooth approximation of the location of the cusp
|
|
396
|
+
// This polynomial was created by an optimization process
|
|
397
|
+
// It has been designed so that S_mid < S_max and T_mid < T_max
|
|
398
|
+
vec2 get_ST_mid(float a_, float b_)
|
|
399
|
+
{
|
|
400
|
+
float S = 0.11516993f + 1.f / (
|
|
401
|
+
+7.44778970f + 4.15901240f * b_
|
|
402
|
+
+ a_ * (-2.19557347f + 1.75198401f * b_
|
|
403
|
+
+ a_ * (-2.13704948f - 10.02301043f * b_
|
|
404
|
+
+ a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
|
|
405
|
+
)))
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
float T = 0.11239642f + 1.f / (
|
|
409
|
+
+1.61320320f - 0.68124379f * b_
|
|
410
|
+
+ a_ * (+0.40370612f + 0.90148123f * b_
|
|
411
|
+
+ a_ * (-0.27087943f + 0.61223990f * b_
|
|
412
|
+
+ a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
|
|
413
|
+
)))
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
return vec2( S, T );
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
vec3 get_Cs(float L, float a_, float b_)
|
|
420
|
+
{
|
|
421
|
+
vec2 cusp = find_cusp(a_, b_);
|
|
422
|
+
|
|
423
|
+
float C_max = find_gamut_intersection(a_, b_, L, 1.f, L, cusp);
|
|
424
|
+
vec2 ST_max = to_ST(cusp);
|
|
425
|
+
|
|
426
|
+
// Scale factor to compensate for the curved part of gamut shape:
|
|
427
|
+
float k = C_max / min((L * ST_max.x), (1.f - L) * ST_max.y);
|
|
428
|
+
|
|
429
|
+
float C_mid;
|
|
430
|
+
{
|
|
431
|
+
vec2 ST_mid = get_ST_mid(a_, b_);
|
|
432
|
+
|
|
433
|
+
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
|
434
|
+
float C_a = L * ST_mid.x;
|
|
435
|
+
float C_b = (1.f - L) * ST_mid.y;
|
|
436
|
+
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))));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
float C_0;
|
|
440
|
+
{
|
|
441
|
+
// for C_0, the shape is independent of hue, so vec2 are constant. Values picked to roughly be the average values of vec2.
|
|
442
|
+
float C_a = L * 0.4f;
|
|
443
|
+
float C_b = (1.f - L) * 0.8f;
|
|
444
|
+
|
|
445
|
+
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
|
446
|
+
C_0 = sqrt(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return vec3( C_0, C_mid, C_max );
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
vec3 okhsl_to_srgb(vec3 hsl)
|
|
453
|
+
{
|
|
454
|
+
float h = hsl.x;
|
|
455
|
+
float s = hsl.y;
|
|
456
|
+
float l = hsl.z;
|
|
457
|
+
|
|
458
|
+
if (l == 1.0f)
|
|
459
|
+
{
|
|
460
|
+
return vec3( 1.f, 1.f, 1.f );
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
else if (l == 0.f)
|
|
464
|
+
{
|
|
465
|
+
return vec3( 0.f, 0.f, 0.f );
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
float a_ = cos(2.f * M_PI * h);
|
|
469
|
+
float b_ = sin(2.f * M_PI * h);
|
|
470
|
+
float L = toe_inv(l);
|
|
471
|
+
|
|
472
|
+
vec3 cs = get_Cs(L, a_, b_);
|
|
473
|
+
float C_0 = cs.x;
|
|
474
|
+
float C_mid = cs.y;
|
|
475
|
+
float C_max = cs.z;
|
|
476
|
+
|
|
477
|
+
float mid = 0.8f;
|
|
478
|
+
float mid_inv = 1.25f;
|
|
479
|
+
|
|
480
|
+
float C, t, k_0, k_1, k_2;
|
|
481
|
+
|
|
482
|
+
if (s < mid)
|
|
483
|
+
{
|
|
484
|
+
t = mid_inv * s;
|
|
485
|
+
|
|
486
|
+
k_1 = mid * C_0;
|
|
487
|
+
k_2 = (1.f - k_1 / C_mid);
|
|
488
|
+
|
|
489
|
+
C = t * k_1 / (1.f - k_2 * t);
|
|
490
|
+
}
|
|
491
|
+
else
|
|
492
|
+
{
|
|
493
|
+
t = (s - mid)/ (1.f - mid);
|
|
494
|
+
|
|
495
|
+
k_0 = C_mid;
|
|
496
|
+
k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
|
497
|
+
k_2 = (1.f - (k_1) / (C_max - C_mid));
|
|
498
|
+
|
|
499
|
+
C = k_0 + t * k_1 / (1.f - k_2 * t);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
|
503
|
+
return vec3(
|
|
504
|
+
srgb_transfer_function(rgb.r),
|
|
505
|
+
srgb_transfer_function(rgb.g),
|
|
506
|
+
srgb_transfer_function(rgb.b)
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
vec3 srgb_to_okhsl(vec3 rgb)
|
|
511
|
+
{
|
|
512
|
+
vec3 lab = linear_srgb_to_oklab(vec3(
|
|
513
|
+
srgb_transfer_function_inv(rgb.r),
|
|
514
|
+
srgb_transfer_function_inv(rgb.g),
|
|
515
|
+
srgb_transfer_function_inv(rgb.b)
|
|
516
|
+
));
|
|
517
|
+
|
|
518
|
+
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
|
519
|
+
float a_ = lab.y / C;
|
|
520
|
+
float b_ = lab.z / C;
|
|
521
|
+
|
|
522
|
+
float L = lab.x;
|
|
523
|
+
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
|
524
|
+
|
|
525
|
+
vec3 cs = get_Cs(L, a_, b_);
|
|
526
|
+
float C_0 = cs.x;
|
|
527
|
+
float C_mid = cs.y;
|
|
528
|
+
float C_max = cs.z;
|
|
529
|
+
|
|
530
|
+
// Inverse of the interpolation in okhsl_to_srgb:
|
|
531
|
+
|
|
532
|
+
float mid = 0.8f;
|
|
533
|
+
float mid_inv = 1.25f;
|
|
534
|
+
|
|
535
|
+
float s;
|
|
536
|
+
if (C < C_mid)
|
|
537
|
+
{
|
|
538
|
+
float k_1 = mid * C_0;
|
|
539
|
+
float k_2 = (1.f - k_1 / C_mid);
|
|
540
|
+
|
|
541
|
+
float t = C / (k_1 + k_2 * C);
|
|
542
|
+
s = t * mid;
|
|
543
|
+
}
|
|
544
|
+
else
|
|
545
|
+
{
|
|
546
|
+
float k_0 = C_mid;
|
|
547
|
+
float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
|
548
|
+
float k_2 = (1.f - (k_1) / (C_max - C_mid));
|
|
549
|
+
|
|
550
|
+
float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
|
|
551
|
+
s = mid + (1.f - mid) * t;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
float l = toe(L);
|
|
555
|
+
return vec3( h, s, l );
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
vec3 okhsv_to_srgb(vec3 hsv)
|
|
560
|
+
{
|
|
561
|
+
float h = hsv.x;
|
|
562
|
+
float s = hsv.y;
|
|
563
|
+
float v = hsv.z;
|
|
564
|
+
|
|
565
|
+
float a_ = cos(2.f * M_PI * h);
|
|
566
|
+
float b_ = sin(2.f * M_PI * h);
|
|
567
|
+
|
|
568
|
+
vec2 cusp = find_cusp(a_, b_);
|
|
569
|
+
vec2 ST_max = to_ST(cusp);
|
|
570
|
+
float S_max = ST_max.x;
|
|
571
|
+
float T_max = ST_max.y;
|
|
572
|
+
float S_0 = 0.5f;
|
|
573
|
+
float k = 1.f- S_0 / S_max;
|
|
574
|
+
|
|
575
|
+
// first we compute L and V as if the gamut is a perfect triangle:
|
|
576
|
+
|
|
577
|
+
// L, C when v==1:
|
|
578
|
+
float L_v = 1.f - s * S_0 / (S_0 + T_max - T_max * k * s);
|
|
579
|
+
float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
|
|
580
|
+
|
|
581
|
+
float L = v * L_v;
|
|
582
|
+
float C = v * C_v;
|
|
583
|
+
|
|
584
|
+
// then we compensate for both toe and the curved top part of the triangle:
|
|
585
|
+
float L_vt = toe_inv(L_v);
|
|
586
|
+
float C_vt = C_v * L_vt / L_v;
|
|
587
|
+
|
|
588
|
+
float L_new = toe_inv(L);
|
|
589
|
+
C = C * L_new / L;
|
|
590
|
+
L = L_new;
|
|
591
|
+
|
|
592
|
+
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
|
593
|
+
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
|
594
|
+
|
|
595
|
+
L = L * scale_L;
|
|
596
|
+
C = C * scale_L;
|
|
597
|
+
|
|
598
|
+
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
|
599
|
+
return vec3(
|
|
600
|
+
srgb_transfer_function(rgb.r),
|
|
601
|
+
srgb_transfer_function(rgb.g),
|
|
602
|
+
srgb_transfer_function(rgb.b)
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
vec3 srgb_to_okhsv(vec3 rgb)
|
|
607
|
+
{
|
|
608
|
+
vec3 lab = linear_srgb_to_oklab(vec3(
|
|
609
|
+
srgb_transfer_function_inv(rgb.r),
|
|
610
|
+
srgb_transfer_function_inv(rgb.g),
|
|
611
|
+
srgb_transfer_function_inv(rgb.b)
|
|
612
|
+
));
|
|
613
|
+
|
|
614
|
+
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
|
615
|
+
float a_ = lab.y / C;
|
|
616
|
+
float b_ = lab.z / C;
|
|
617
|
+
|
|
618
|
+
float L = lab.x;
|
|
619
|
+
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
|
620
|
+
|
|
621
|
+
vec2 cusp = find_cusp(a_, b_);
|
|
622
|
+
vec2 ST_max = to_ST(cusp);
|
|
623
|
+
float S_max = ST_max.x;
|
|
624
|
+
float T_max = ST_max.y;
|
|
625
|
+
float S_0 = 0.5f;
|
|
626
|
+
float k = 1.f - S_0 / S_max;
|
|
627
|
+
|
|
628
|
+
// first we find L_v, C_v, L_vt and C_vt
|
|
629
|
+
|
|
630
|
+
float t = T_max / (C + L * T_max);
|
|
631
|
+
float L_v = t * L;
|
|
632
|
+
float C_v = t * C;
|
|
633
|
+
|
|
634
|
+
float L_vt = toe_inv(L_v);
|
|
635
|
+
float C_vt = C_v * L_vt / L_v;
|
|
636
|
+
|
|
637
|
+
// we can then use these to invert the step that compensates for the toe and the curved top part of the triangle:
|
|
638
|
+
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
|
639
|
+
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
|
640
|
+
|
|
641
|
+
L = L / scale_L;
|
|
642
|
+
C = C / scale_L;
|
|
643
|
+
|
|
644
|
+
C = C * toe(L) / L;
|
|
645
|
+
L = toe(L);
|
|
646
|
+
|
|
647
|
+
// we can now compute v and s:
|
|
648
|
+
|
|
649
|
+
float v = L / L_v;
|
|
650
|
+
float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
|
|
651
|
+
|
|
652
|
+
return vec3 (h, s, v );
|
|
653
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// https://lygia.xyz/
|
|
2
|
+
float srgb2rgb(const in float v) { return (v < 0.04045) ? v * 0.0773993808 : pow((v + 0.055) * 0.947867298578199, 2.4); }
|
|
3
|
+
vec3 srgb2rgb(const in vec3 srgb) { return vec3(srgb2rgb(srgb.r), srgb2rgb(srgb.g), srgb2rgb(srgb.b)); }
|
|
4
|
+
vec4 srgb2rgb(const in vec4 srgb) { return vec4(srgb2rgb(srgb.rgb), srgb.a); }
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type ColorString = string;
|
|
2
|
+
export type ColorList = ColorString[];
|
|
3
|
+
|
|
4
|
+
export type SupportedColorModels = 'hsv' | 'okhsv' | 'hsl' | 'okhsl' | 'oklch';
|
|
5
|
+
export type Axis = 'x' | 'y' | 'z';
|
|
6
|
+
export type DistanceMetric = 'rgb' | 'oklab' | 'deltaE76' | 'deltaE94' | 'deltaE2000' | 'kotsarenkoRamos';
|
|
7
|
+
|
|
8
|
+
export type PaletteVizOptions = {
|
|
9
|
+
palette?: ColorList;
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
pixelRatio?: number;
|
|
13
|
+
container?: HTMLElement;
|
|
14
|
+
// shader options
|
|
15
|
+
colorModel?: SupportedColorModels;
|
|
16
|
+
distanceMetric?: DistanceMetric;
|
|
17
|
+
isPolar?: boolean;
|
|
18
|
+
axis?: Axis;
|
|
19
|
+
position?: number;
|
|
20
|
+
invertLightness?: boolean;
|
|
21
|
+
showRaw?: boolean;
|
|
22
|
+
};
|