-
Notifications
You must be signed in to change notification settings - Fork 2
/
oklab.html
290 lines (230 loc) · 10 KB
/
oklab.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<html>
<body>
Suppose an author specified<br><tt>oklch(90% 90% 0deg)</tt><img width=1px src="gradient-pq.avif">
<p style="background-color:color(display-p3 1 0.7753 0.8575);">
If they're authoring this on a P3 display, this is the color that they will see.
</p>
<p style="background-color:rgb(100% 78.52% 86.15%);">
If they're authoring this on an sRGB display, this is the color that they will see.
</p>
<p style="background-color:oklch(90% 90% 0deg);">
But this is the true color that they specified!
One day, their content will be displayed on a device where that color can be produced!
</p>
<p>
What will we do then?
</p>
<!--
<p style="background-color:color(display-p3 1 0.7362 0.8525);">
If they're authoring this on a P3 display, this is the color that they will see.
</p>
<p style="background-color:color(srgb 1 0.7168 0.8560);">
If they're authoring this on an sRGB display, this is the color that they will see.
</p>
<p style="background-color:color(srgb 1.1209 0.7168 0.8560);">
But this is the true color that they specified!
One day, their content will be displayed on a device where that color can be produced!
</p>
<p>
What will we do then?
</p>
-->
<!--
<p>
Say to them "tough, you should have only specified P3 colors when you designed your page?"
I reject this idea.
</p>
<p>
Should we have instead have provided them with a color space with built-in guardrails to work in?
I think yes!
</p>
<p>
We can either update <tt>oklch</tt> to have built-in guardrails, so that it can never specify a color outside of an unsafe gamut.
Or, we can introduce safe color spaces like <a href="https://bottosson.github.io/posts/colorpicker/"><tt>okhsv</tt></a>, along with variants that are P3-safe (say, <tt>okhsv-p3</tt>).
</p>
-->
<!--
And the result will look very different from what they wanted!!
<div style="width: 100%; display: table;">
<div style="display: table-row">
<div style="width: 50%; display: table-cell;">
With gamut mapping:
<p>
<span style="background-color:color(rec2020 1 0.7282 0.8325);">
An author would see this on a Rec2020 display.
</span>
</p>
<p>
<span style="background-color:color(display-p3 1 0.7753 0.8575);">
An author would see this on a P3 display.
</span>
</p>
<p>
<span style="background-color:rgb(100% 78.52% 86.15%);">
An author would see this on a sRGB display.
</p>
</div>
<div style="display: table-cell;">
If <tt>oklab</tt> were to have gamut mapping baked in:
<p>
<span style="background-color:color(rec2020 1 0.7282 0.8325);">
This would be real color's appearance.
</span>
</p>
<p>
<span style="background-color:color(display-p3 1 0.7352 0.8525);">
An author would see this on a P3 display.
</span>
</p>
<p>
<span style="background-color:color(srgb 1 0.7168 0.8560);">
An author would see this on an sRGB display.
</span>
</p>
</div>
</div>
</div>
-->
<!--
<p style="background-color:color(rec2020 1.0 0.4343 0.7975);">
This is oklch(90% 90% 0deg) clamped to Rec2020 to color(rec2020 1.0 0.4343 0.7975).
</p>
<p style="background-color:color(display-p3 1.0 0.346 0.825);">
This is oklch(90% 90% 0deg) clamped to P3 to color(display-p3 1.0 0.346 0.825).
</p>
<p>
<div style="width:50px; display:inline-block;">A</div>
<div style="background-color:color(rec2020 0.8543 0.8543 0.8543); width:40px; display:inline-block;"> 0%</div>
<div style="background-color:color(rec2020 0.9087 0.8216 0.8482); width:40px; display:inline-block;"> 10%</div>
<div style="background-color:color(rec2020 0.9612 0.7867 0.8420); width:40px; display:inline-block;"> 20%</div>
<div style="background-color:color(rec2020 1.0000 0.7494 0.8359); width:40px; display:inline-block;"> 30%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:40px; display:inline-block;"> 40%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:40px; display:inline-block;"> 50%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:40px; display:inline-block;"> 60%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:40px; display:inline-block;"> 70%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:40px; display:inline-block;"> 80%</div>
<div style="background-color:color(rec2020 1.0000 0.7282 0.8325); width:40px; display:inline-block;"> 90%</div>
<div style="background-color:color(rec2020 1.0000 0.7281 0.8325); width:50px; display:inline-block;">100%</div>
</p>
<p>
<div style="width:50px; display:inline-block;">B</div>
<div style="background-color:oklch(90% 0% 0deg); width:40px; display:inline-block;"> 0%</div>
<div style="background-color:oklch(90% 10% 0deg); width:40px; display:inline-block;"> 10%</div>
<div style="background-color:oklch(90% 20% 0deg); width:40px; display:inline-block;"> 20%</div>
<div style="background-color:oklch(90% 30% 0deg); width:40px; display:inline-block;"> 30%</div>
<div style="background-color:oklch(90% 40% 0deg); width:40px; display:inline-block;"> 40%</div>
<div style="background-color:oklch(90% 50% 0deg); width:40px; display:inline-block;"> 50%</div>
<div style="background-color:oklch(90% 60% 0deg); width:40px; display:inline-block;"> 60%</div>
<div style="background-color:oklch(90% 70% 0deg); width:40px; display:inline-block;"> 70%</div>
<div style="background-color:oklch(90% 80% 0deg); width:40px; display:inline-block;"> 80%</div>
<div style="background-color:oklch(90% 90% 0deg); width:40px; display:inline-block;"> 90%</div>
<div style="background-color:oklch(90% 100% 0deg); width:50px; display:inline-block;">100%</div>
</p>
<p>
<div style="width:50px; display:inline-block;">C</div>
<div style="background-color:color(display-p3 0.87 0.87 0.87); width:40px; display:inline-block;"> 0%</div>
<div style="background-color:color(display-p3 0.95 0.84 0.87); width:40px; display:inline-block;"> 10%</div>
<div style="background-color:color(display-p3 1.00 0.80 0.86); width:40px; display:inline-block;"> 20%</div>
<div style="background-color:color(display-p3 1.00 0.76 0.85); width:40px; display:inline-block;"> 30%</div>
<div style="background-color:color(display-p3 1.00 0.73 0.84); width:40px; display:inline-block;"> 40%</div>
<div style="background-color:color(display-p3 1.00 0.69 0.82); width:40px; display:inline-block;"> 50%</div>
<div style="background-color:color(display-p3 1.00 0.66 0.80); width:40px; display:inline-block;"> 60%</div>
<div style="background-color:color(display-p3 1.00 0.62 0.78); width:40px; display:inline-block;"> 70%</div>
<div style="background-color:color(display-p3 1.00 0.58 0.76); width:40px; display:inline-block;"> 80%</div>
<div style="background-color:color(display-p3 1.00 0.53 0.74); width:40px; display:inline-block;"> 90%</div>
<div style="background-color:color(display-p3 1.00 0.48 0.72); width:50px; display:inline-block;">100%</div>
</p>
<p>
Right after this is a 1x1 PQ image, that, because of bugs, causes CSS colors to be un-clamped from the SDR range ... this needs to be fixed, but until it is fixed, it allows visualizing colors that go out of the SDR gamut.
<img width=1px src="gradient-pq.avif">
</p>
<!--
$export $pure half3 $css_gamut_map_oklab(half3 oklab) {
// Constants for the normal vector of the plane formed by white, black, and
// the specified vertex of the gamut.
half2 normal_R = half2(0.409702, -0.912219);
half2 normal_M = half2(-0.397919, -0.917421);
half2 normal_B = half2(-0.906800, 0.421562);
half2 normal_C = half2(-0.171122, 0.985250);
half2 normal_G = half2(0.460276, 0.887776);
half2 normal_Y = half2(0.947925, 0.318495);
// For the triangles formed by white (W) or black (K) with the vertices
// of Yellow and Red (YR), Red and Magenta (RM), etc, the constants to be
// used to compute the intersection of a line of constant hue and luminance
// with that plane.
half c0_YR = 0.091132;
half2 cW_YR = half2(0.070370, 0.034139);
half2 cK_YR = half2(0.018170, 0.378550);
half c0_RM = 0.113902;
half2 cW_RM = half2(0.090836, 0.036251);
half2 cK_RM = half2(0.226781, 0.018764);
half c0_MB = 0.161739;
half2 cW_MB = half2(-0.008202, -0.264819);
half2 cK_MB = half2( 0.187156, -0.284304);
half c0_BC = 0.102047;
half2 cW_BC = half2(-0.014804, -0.162608);
half2 cK_BC = half2(-0.276786, 0.004193);
half c0_CG = 0.092029;
half2 cW_CG = half2(-0.038533, -0.001650);
half2 cK_CG = half2(-0.232572, -0.094331);
half c0_GY = 0.081709;
half2 cW_GY = half2(-0.034601, -0.002215);
half2 cK_GY = half2( 0.012185, 0.338031);
half L = oklab.r;
half one_minus_L = 1.0 - oklab.r;
half2 ab = half2(oklab.y, oklab.z);
// Find the planes to intersect with and set the constants based on those
// planes.
half c0;
half2 cW;
half2 cK;
if (dot(ab, normal_R) < 0.0) {
if (dot(ab, normal_G) < 0.0) {
if (dot(ab, normal_C) < 0.0) {
c0 = c0_BC; cW = cW_BC; cK = cK_BC;
} else {
c0 = c0_CG; cW = cW_CG; cK = cK_CG;
}
} else {
if (dot(ab, normal_Y) < 0.0) {
c0 = c0_GY; cW = cW_GY; cK = cK_GY;
} else {
c0 = c0_YR; cW = cW_YR; cK = cK_YR;
}
}
} else {
if (dot(ab, normal_B) < 0.0) {
if (dot(ab, normal_M) < 0.0) {
c0 = c0_RM; cW = cW_RM; cK = cK_RM;
} else {
c0 = c0_MB; cW = cW_MB; cK = cK_MB;
}
} else {
c0 = c0_BC; cW = cW_BC; cK = cK_BC;
}
}
// Perform the intersection.
half alpha = 1.0;
// Intersect with the plane with white.
half w_denom = dot(cW, ab);
if (w_denom > 0.0) {
half w_num = c0*one_minus_L;
if (w_num < w_denom) {
alpha = min(alpha, w_num / w_denom);
}
}
// Intersect with the plane with black.
half k_denom = dot(cK, ab);
if (k_denom > 0.0) {
half k_num = c0*L;
if (k_num < k_denom) {
alpha = min(alpha, k_num / k_denom);
}
}
// Attenuate the ab coordinate by alpha.
oklab.yz *= alpha;
return oklab;
}
-->
</body>
</html>