hokusai-zero 0.2.6.pre.android → 0.2.6.pre.pinephone2
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.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/Gemfile.lock +0 -2
- data/ast/src/core/input.c +135 -0
- data/ast/src/core/input.h +32 -0
- data/ast/test/hokusai.c +2 -0
- data/ast/test/input.c +44 -0
- data/hokusai.gemspec +1 -2
- data/ui/examples/drag.rb +154 -0
- data/ui/examples/embedded.rb +58 -0
- data/ui/examples/game.rb +143 -0
- data/ui/examples/overlay.rb +233 -0
- data/ui/examples/provider.rb +56 -0
- data/ui/examples/shader/test.rb +145 -0
- data/ui/examples/shader.rb +100 -0
- data/ui/examples/wiki.rb +82 -0
- data/ui/lib/lib_hokusai.rb +20 -1
- data/ui/spec/spec_helper.rb +1 -1
- data/ui/src/hokusai/assets/arrow-down-line.png +0 -0
- data/ui/src/hokusai/assets/arrow-down-wide-line.png +0 -0
- data/ui/src/hokusai/automation/server.rb +2 -3
- data/ui/src/hokusai/backends/embedded/config.rb +48 -0
- data/ui/src/hokusai/backends/embedded/font.rb +112 -0
- data/ui/src/hokusai/backends/embedded/keys.rb +124 -0
- data/ui/src/hokusai/backends/embedded.rb +540 -0
- data/ui/src/hokusai/backends/raylib.rb +80 -5
- data/ui/src/hokusai/blocks/color_picker.rb +1080 -0
- data/ui/src/hokusai/blocks/shader_begin.rb +22 -0
- data/ui/src/hokusai/blocks/shader_end.rb +12 -0
- data/ui/src/hokusai/blocks/texture.rb +23 -0
- data/ui/src/hokusai/commands/rect.rb +10 -1
- data/ui/src/hokusai/commands/shader.rb +33 -0
- data/ui/src/hokusai/commands/texture.rb +26 -0
- data/ui/src/hokusai/commands.rb +22 -0
- data/ui/src/hokusai/event.rb +2 -1
- data/ui/src/hokusai/events/touch.rb +66 -0
- data/ui/src/hokusai/painter.rb +22 -0
- data/ui/src/hokusai/types.rb +89 -0
- data/ui/src/hokusai.rb +4 -9
- data/xmake.lua +1 -1
- metadata +25 -22
- data/ui/src/hokusai/assets/chevron-down.svg +0 -1
@@ -0,0 +1,1080 @@
|
|
1
|
+
class PickerCircle < Hokusai::Block
|
2
|
+
template <<-EOF
|
3
|
+
[template]
|
4
|
+
virtual
|
5
|
+
EOF
|
6
|
+
|
7
|
+
computed! :x
|
8
|
+
computed! :y
|
9
|
+
computed! :color
|
10
|
+
computed! :radius
|
11
|
+
|
12
|
+
def on_mounted
|
13
|
+
node.meta.set_prop(:z, 3);
|
14
|
+
node.meta.set_prop(:ztarget, "root")
|
15
|
+
end
|
16
|
+
|
17
|
+
def render(canvas)
|
18
|
+
draw do
|
19
|
+
circle(x, y - radius, radius + 2.0) do |command|
|
20
|
+
command.color = Hokusai::Color.new(255, 255, 255)
|
21
|
+
end
|
22
|
+
circle(x, y - radius, radius) do |command|
|
23
|
+
command.color = color
|
24
|
+
end
|
25
|
+
|
26
|
+
text("rgb(#{color.r.round(0)},#{color.g.round(0)},#{color.b.round(0)})", x - 90.0, y + radius) do |command|
|
27
|
+
command.size = 12
|
28
|
+
command.color = Hokusai::Color.new(255, 255, 255)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Hokusai::Blocks::ColorPicker < Hokusai::Block
|
35
|
+
style <<-EOF
|
36
|
+
[style]
|
37
|
+
circleStyle {
|
38
|
+
radius: 10.0;
|
39
|
+
}
|
40
|
+
|
41
|
+
cursor {
|
42
|
+
cursor: "none";
|
43
|
+
}
|
44
|
+
|
45
|
+
endStyle {
|
46
|
+
height: 0.0;
|
47
|
+
width: 0.0;
|
48
|
+
}
|
49
|
+
EOF
|
50
|
+
template <<~EOF
|
51
|
+
[template]
|
52
|
+
hblock { }
|
53
|
+
vblock {
|
54
|
+
@mousedown="start_selection"
|
55
|
+
@mousemove="update_selection"
|
56
|
+
}
|
57
|
+
shader_begin {
|
58
|
+
:fragment_shader="picker_shader"
|
59
|
+
:uniforms="values"
|
60
|
+
\ }
|
61
|
+
texture
|
62
|
+
shader_end { ...endStyle }
|
63
|
+
vblock {
|
64
|
+
width="32"
|
65
|
+
cursor="crosshair"
|
66
|
+
}
|
67
|
+
shader_begin {
|
68
|
+
@mousedown="save_position"
|
69
|
+
:fragment_shader="hue_shader"
|
70
|
+
:uniforms="values"
|
71
|
+
}
|
72
|
+
texture
|
73
|
+
shader_end { ...endStyle }
|
74
|
+
vblock { :z="3" ztarget="root"}
|
75
|
+
[if="picking"]
|
76
|
+
pickercircle {
|
77
|
+
...circleStyle
|
78
|
+
:x="pickerx"
|
79
|
+
:y="pickery"
|
80
|
+
:color="color"
|
81
|
+
}
|
82
|
+
EOF
|
83
|
+
|
84
|
+
uses(
|
85
|
+
rect: Hokusai::Blocks::Rect,
|
86
|
+
shader_begin: Hokusai::Blocks::ShaderBegin,
|
87
|
+
shader_end: Hokusai::Blocks::ShaderEnd,
|
88
|
+
texture: Hokusai::Blocks::Texture,
|
89
|
+
hblock: Hokusai::Blocks::Hblock,
|
90
|
+
vblock: Hokusai::Blocks::Vblock,
|
91
|
+
pickercircle: PickerCircle
|
92
|
+
)
|
93
|
+
|
94
|
+
attr_accessor :position, :top, :left, :height, :width, :selecting, :selection,
|
95
|
+
:brightness, :saturation, :pickerx, :pickery
|
96
|
+
|
97
|
+
|
98
|
+
def start_selection(event)
|
99
|
+
if event.left.down
|
100
|
+
self.selecting = true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def picking
|
105
|
+
selecting && pickerx && pickery
|
106
|
+
end
|
107
|
+
|
108
|
+
K1 = 0.206;
|
109
|
+
K2 = 0.03;
|
110
|
+
K3 = (1.0 + K1) / (1.0 + K2);
|
111
|
+
|
112
|
+
def toe_inv(x)
|
113
|
+
(x * x + K1 * x) / (K3 * (x + K2))
|
114
|
+
end
|
115
|
+
|
116
|
+
def compute_max_saturation(a, b)
|
117
|
+
if -1.88170328 * a - 0.80936493 * b > 1.0
|
118
|
+
k0 = +1.19086277
|
119
|
+
k1 = +1.76576728
|
120
|
+
k2 = +0.59662641
|
121
|
+
k3 = +0.75515197
|
122
|
+
k4 = +0.56771245
|
123
|
+
wl = +4.0767416621
|
124
|
+
wm = -3.3077115913
|
125
|
+
ws = +0.2309699292
|
126
|
+
elsif 1.81444104 * a - 1.19445276 * b > 1.0
|
127
|
+
k0 = +0.73956515
|
128
|
+
k1 = -0.45954404
|
129
|
+
k2 = +0.08285427
|
130
|
+
k3 = +0.12541070
|
131
|
+
k4 = +0.14503204
|
132
|
+
wl = -1.2684380046
|
133
|
+
wm = +2.6097574011
|
134
|
+
ws = -0.3413193965
|
135
|
+
else
|
136
|
+
k0 = +1.35733652
|
137
|
+
k1 = -0.00915799
|
138
|
+
k2 = -1.15130210
|
139
|
+
k3 = -0.50559606
|
140
|
+
k4 = +0.00692167
|
141
|
+
wl = -0.0041960863
|
142
|
+
wm = -0.7034186147
|
143
|
+
ws = +1.7076147010
|
144
|
+
end
|
145
|
+
|
146
|
+
sat = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b
|
147
|
+
|
148
|
+
kl = +0.3963377774 * a + 0.2158037573 * b
|
149
|
+
km = -0.1055613458 * a - 0.0638541728 * b
|
150
|
+
ks = -0.0894841775 * a - 1.2914855480 * b
|
151
|
+
|
152
|
+
l_ = 1.0 + sat * kl
|
153
|
+
m_ = 1.0 + sat * km
|
154
|
+
s_ = 1.0 + sat * ks
|
155
|
+
|
156
|
+
l = l_ ** 3
|
157
|
+
m = m_ ** 3
|
158
|
+
s = s_ ** 3
|
159
|
+
|
160
|
+
lds = 3.0 * kl * l_ * l_
|
161
|
+
mds = 3.0 * km * m_ * m_
|
162
|
+
sds = 3.0 * ks * s_ * s_
|
163
|
+
|
164
|
+
lds2 = 6.0 * kl ** 2 * l_
|
165
|
+
mds2 = 6.0 * km ** 2 * m_
|
166
|
+
sds2 = 6.0 * ks ** 2 * s_
|
167
|
+
|
168
|
+
|
169
|
+
f = wl * l + wm * m + ws * s
|
170
|
+
f1 = wl * lds + wm * mds + ws * sds
|
171
|
+
f2 = wl * lds2 + wm * mds2 + ws * sds2
|
172
|
+
|
173
|
+
sat = sat - (f * f1) / (f1 ** 2 - 0.5 * f * f2)
|
174
|
+
|
175
|
+
sat
|
176
|
+
end
|
177
|
+
|
178
|
+
def find_cusp(a, b)
|
179
|
+
s_cusp = compute_max_saturation(a, b)
|
180
|
+
|
181
|
+
rgb = oklab_to_linear_srgb(1.0, s_cusp * a, s_cusp * b)
|
182
|
+
l_cusp = cbrt(1.0 / rgb.max)
|
183
|
+
c_cusp = l_cusp * s_cusp
|
184
|
+
|
185
|
+
[l_cusp, c_cusp]
|
186
|
+
end
|
187
|
+
|
188
|
+
def to_st(cusp)
|
189
|
+
l, c = cusp
|
190
|
+
[c / l, c / (1.0 - l)]
|
191
|
+
end
|
192
|
+
|
193
|
+
def oklab_to_linear_srgb(*lab)
|
194
|
+
r, g, b = lab
|
195
|
+
|
196
|
+
l_ = r + 0.3963377774 * g + 0.2158037573 * b
|
197
|
+
m_ = r - 0.1055613458 * g - 0.0638541728 * b
|
198
|
+
s_ = r - 0.0894841775 * g - 1.2914855480 * b
|
199
|
+
|
200
|
+
l = l_ * l_ * l_
|
201
|
+
m = m_ * m_ * m_
|
202
|
+
s = s_ * s_ * s_
|
203
|
+
|
204
|
+
[
|
205
|
+
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
206
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
207
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
|
208
|
+
]
|
209
|
+
end
|
210
|
+
|
211
|
+
def cbrt(x)
|
212
|
+
(x <=> 0) * (x.abs ** (1.0 / 3.0))
|
213
|
+
end
|
214
|
+
|
215
|
+
def oklab
|
216
|
+
h = hue
|
217
|
+
s = saturation
|
218
|
+
v = brightness
|
219
|
+
|
220
|
+
tau = Math::PI * 2.0
|
221
|
+
|
222
|
+
_a = Math.cos(tau * h)
|
223
|
+
_b = Math.sin(tau * h)
|
224
|
+
|
225
|
+
s_max, t_max = to_st(find_cusp(_a, _b))
|
226
|
+
|
227
|
+
so = 0.5
|
228
|
+
k = 1.0 - so / s_max
|
229
|
+
|
230
|
+
lv = 1.0 - (s * so) / (so + t_max - t_max * k * s)
|
231
|
+
cv = (s * t_max * so) / (so + t_max - t_max * k * s)
|
232
|
+
|
233
|
+
l = v * lv
|
234
|
+
c = v * cv
|
235
|
+
|
236
|
+
lvt = toe_inv(lv)
|
237
|
+
cvt = (cv * lvt) / lv
|
238
|
+
|
239
|
+
l_new = toe_inv(l)
|
240
|
+
c = (c * l_new) / l
|
241
|
+
l = l_new
|
242
|
+
|
243
|
+
rs, gs, bs = oklab_to_linear_srgb(lvt, _a * cvt, _b * cvt)
|
244
|
+
scale_l = cbrt(1.0 / [rs, gs, bs, 0.0].max)
|
245
|
+
|
246
|
+
l = l * scale_l
|
247
|
+
c = c * scale_l
|
248
|
+
|
249
|
+
a = c * _a
|
250
|
+
b = c * _b
|
251
|
+
|
252
|
+
|
253
|
+
l, a, b = oklab_to_linear_srgb(l, a, b)
|
254
|
+
# end
|
255
|
+
[srgb_transfer_function(l), srgb_transfer_function(a), srgb_transfer_function(b)]
|
256
|
+
end
|
257
|
+
|
258
|
+
def srgb_transfer_function(a)
|
259
|
+
0.0031308 >= a ? 12.92 * a : 1.055 * (a ** 0.4166666666666667) - 0.055;
|
260
|
+
end
|
261
|
+
|
262
|
+
def color(alpha = 255)
|
263
|
+
return if brightness.nil? || saturation.nil?
|
264
|
+
r, g, b = oklab
|
265
|
+
|
266
|
+
return Hokusai::Color.new(r * 255, g * 255, b * 255)
|
267
|
+
end
|
268
|
+
|
269
|
+
def update_selection(event)
|
270
|
+
if event.left.down && selecting
|
271
|
+
Hokusai.set_mouse_cursor(:none)
|
272
|
+
w = width - 32.0
|
273
|
+
posx = event.pos.x
|
274
|
+
|
275
|
+
b = ((posx - left) / w)
|
276
|
+
self.pickerx = posx
|
277
|
+
unless b > 1.0 || b < 0.0
|
278
|
+
self.saturation = b
|
279
|
+
end
|
280
|
+
|
281
|
+
posy = event.pos.y
|
282
|
+
t = ((posy - top) / height)
|
283
|
+
self.pickery = posy
|
284
|
+
unless t > 1.0 || t < 0.0
|
285
|
+
self.brightness = 1 - t
|
286
|
+
end
|
287
|
+
else
|
288
|
+
Hokusai.set_mouse_cursor(:pointer)
|
289
|
+
|
290
|
+
self.selecting = false
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
def save_position(event)
|
295
|
+
self.position = [event.pos.x, event.pos.y]
|
296
|
+
end
|
297
|
+
|
298
|
+
def hue
|
299
|
+
return 0.0 if position.nil?
|
300
|
+
|
301
|
+
pos = (position[1] - (top || 0))
|
302
|
+
y = (pos / height)
|
303
|
+
end
|
304
|
+
|
305
|
+
def values
|
306
|
+
return [] unless position
|
307
|
+
|
308
|
+
return if hue > 1 || hue < 0
|
309
|
+
|
310
|
+
{
|
311
|
+
uHue: [hue, Hokusai::SHADER_UNIFORM_FLOAT]
|
312
|
+
}
|
313
|
+
end
|
314
|
+
|
315
|
+
HUE_SHADER = <<~EOF
|
316
|
+
#version 330
|
317
|
+
|
318
|
+
in vec2 fragTexCoord;
|
319
|
+
in vec4 fragColor;
|
320
|
+
|
321
|
+
out vec4 finalColor;
|
322
|
+
|
323
|
+
#define PI 3.1415926535897932384626433832795
|
324
|
+
#define PICKER_SIZE_INV (1.0 / 255.0)
|
325
|
+
|
326
|
+
float hsluv_fromLinear(float c) {
|
327
|
+
return c <= 0.0031308 ? 12.92 * c : 1.055 * pow(c, 1.0 / 2.4) - 0.055;
|
328
|
+
}
|
329
|
+
vec3 hsluv_fromLinear(vec3 c) {
|
330
|
+
return vec3( hsluv_fromLinear(c.r), hsluv_fromLinear(c.g), hsluv_fromLinear(c.b) );
|
331
|
+
}
|
332
|
+
|
333
|
+
vec3 xyzToRgb(vec3 tuple) {
|
334
|
+
const mat3 m = mat3(
|
335
|
+
3.2409699419045214 ,-1.5373831775700935 ,-0.49861076029300328 ,
|
336
|
+
-0.96924363628087983 , 1.8759675015077207 , 0.041555057407175613,
|
337
|
+
0.055630079696993609,-0.20397695888897657, 1.0569715142428786 );
|
338
|
+
|
339
|
+
return hsluv_fromLinear(tuple*m);
|
340
|
+
}
|
341
|
+
|
342
|
+
float hsluv_lToY(float L) {
|
343
|
+
return L <= 8.0 ? L / 903.2962962962963 : pow((L + 16.0) / 116.0, 3.0);
|
344
|
+
}
|
345
|
+
|
346
|
+
vec3 luvToXyz(vec3 tuple) {
|
347
|
+
float L = tuple.x;
|
348
|
+
|
349
|
+
float U = tuple.y / (13.0 * L) + 0.19783000664283681;
|
350
|
+
float V = tuple.z / (13.0 * L) + 0.468319994938791;
|
351
|
+
|
352
|
+
float Y = hsluv_lToY(L);
|
353
|
+
float X = 2.25 * U * Y / V;
|
354
|
+
float Z = (3./V - 5.)*Y - (X/3.);
|
355
|
+
|
356
|
+
return vec3(X, Y, Z);
|
357
|
+
}
|
358
|
+
|
359
|
+
vec3 lchToLuv(vec3 tuple) {
|
360
|
+
float hrad = radians(tuple.b);
|
361
|
+
return vec3(
|
362
|
+
tuple.r,
|
363
|
+
cos(hrad) * tuple.g,
|
364
|
+
sin(hrad) * tuple.g
|
365
|
+
);
|
366
|
+
}
|
367
|
+
|
368
|
+
vec3 lchToRgb(vec3 tuple) {
|
369
|
+
return xyzToRgb(luvToXyz(lchToLuv(tuple)));
|
370
|
+
}
|
371
|
+
|
372
|
+
vec3 hsluv_lengthOfRayUntilIntersect(float theta, vec3 x, vec3 y) {
|
373
|
+
vec3 len = y / (sin(theta) - x * cos(theta));
|
374
|
+
if (len.r < 0.0) {len.r=1000.0;}
|
375
|
+
if (len.g < 0.0) {len.g=1000.0;}
|
376
|
+
if (len.b < 0.0) {len.b=1000.0;}
|
377
|
+
return len;
|
378
|
+
}
|
379
|
+
|
380
|
+
float hsluv_maxChromaForLH(float L, float H) {
|
381
|
+
float hrad = radians(H);
|
382
|
+
|
383
|
+
mat3 m2 = mat3(
|
384
|
+
3.2409699419045214 ,-0.96924363628087983 , 0.055630079696993609,
|
385
|
+
-1.5373831775700935 , 1.8759675015077207 ,-0.20397695888897657 ,
|
386
|
+
-0.49861076029300328 , 0.041555057407175613, 1.0569715142428786
|
387
|
+
);
|
388
|
+
float sub1 = pow(L + 16.0, 3.0) / 1560896.0;
|
389
|
+
float sub2 = sub1 > 0.0088564516790356308 ? sub1 : L / 903.2962962962963;
|
390
|
+
|
391
|
+
vec3 top1 = (284517.0 * m2[0] - 94839.0 * m2[2]) * sub2;
|
392
|
+
vec3 bottom = (632260.0 * m2[2] - 126452.0 * m2[1]) * sub2;
|
393
|
+
vec3 top2 = (838422.0 * m2[2] + 769860.0 * m2[1] + 731718.0 * m2[0]) * L * sub2;
|
394
|
+
|
395
|
+
vec3 bound0x = top1 / bottom;
|
396
|
+
vec3 bound0y = top2 / bottom;
|
397
|
+
|
398
|
+
vec3 bound1x = top1 / (bottom+126452.0);
|
399
|
+
vec3 bound1y = (top2-769860.0*L) / (bottom+126452.0);
|
400
|
+
|
401
|
+
vec3 lengths0 = hsluv_lengthOfRayUntilIntersect(hrad, bound0x, bound0y );
|
402
|
+
vec3 lengths1 = hsluv_lengthOfRayUntilIntersect(hrad, bound1x, bound1y );
|
403
|
+
|
404
|
+
return min(lengths0.r,
|
405
|
+
min(lengths1.r,
|
406
|
+
min(lengths0.g,
|
407
|
+
min(lengths1.g,
|
408
|
+
min(lengths0.b,
|
409
|
+
lengths1.b)))));
|
410
|
+
}
|
411
|
+
|
412
|
+
vec3 hsluvToLch(vec3 tuple) {
|
413
|
+
tuple.g *= hsluv_maxChromaForLH(tuple.b, tuple.r) * .01;
|
414
|
+
return tuple.bgr;
|
415
|
+
}
|
416
|
+
|
417
|
+
vec3 hsluvToRgb(vec3 tuple) {
|
418
|
+
return lchToRgb(hsluvToLch(tuple));
|
419
|
+
}
|
420
|
+
vec3 hsluvToRgb(float x, float y, float z) {return hsluvToRgb( vec3(x,y,z) );}
|
421
|
+
|
422
|
+
void main() {
|
423
|
+
float a_ = cos(2 * PI * fragTexCoord.y);
|
424
|
+
float b_ = sin(2 * PI * fragTexCoord.y);
|
425
|
+
|
426
|
+
float h = fragTexCoord.y;
|
427
|
+
float s = 0.9;
|
428
|
+
float l = 0.65 + 0.20 * b_ - 0.09 * a_;
|
429
|
+
|
430
|
+
vec3 col = hsluvToRgb(h * 360, s * 100, l * 100);
|
431
|
+
finalColor = vec4(col, 1.0);
|
432
|
+
}
|
433
|
+
EOF
|
434
|
+
|
435
|
+
PICKER_SHADER = <<~EOF
|
436
|
+
#version 330
|
437
|
+
|
438
|
+
in vec2 fragTexCoord;
|
439
|
+
in vec4 fragColor;
|
440
|
+
|
441
|
+
uniform float uHue;
|
442
|
+
|
443
|
+
out vec4 finalColor;
|
444
|
+
|
445
|
+
#define M_PI 3.1415926535897932384626433832795
|
446
|
+
|
447
|
+
float cbrt( float x ) {
|
448
|
+
return sign(x)*pow(abs(x),1.0f/3.0f);
|
449
|
+
}
|
450
|
+
|
451
|
+
float srgb_transfer_function(float a) {
|
452
|
+
return .0031308f >= a ? 12.92f * a : 1.055f * pow(a, .4166666666666667f) - .055f;
|
453
|
+
}
|
454
|
+
|
455
|
+
float srgb_transfer_function_inv(float a) {
|
456
|
+
return .04045f < a ? pow((a + .055f) / 1.055f, 2.4f) : a / 12.92f;
|
457
|
+
}
|
458
|
+
|
459
|
+
vec3 linear_srgb_to_oklab(vec3 c) {
|
460
|
+
float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
|
461
|
+
float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
|
462
|
+
float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
|
463
|
+
|
464
|
+
float l_ = cbrt(l);
|
465
|
+
float m_ = cbrt(m);
|
466
|
+
float s_ = cbrt(s);
|
467
|
+
|
468
|
+
return vec3(
|
469
|
+
0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
|
470
|
+
1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
|
471
|
+
0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_
|
472
|
+
);
|
473
|
+
}
|
474
|
+
|
475
|
+
vec3 oklab_to_linear_srgb(vec3 c) {
|
476
|
+
float l_ = c.x + 0.3963377774f * c.y + 0.2158037573f * c.z;
|
477
|
+
float m_ = c.x - 0.1055613458f * c.y - 0.0638541728f * c.z;
|
478
|
+
float s_ = c.x - 0.0894841775f * c.y - 1.2914855480f * c.z;
|
479
|
+
|
480
|
+
float l = l_ * l_ * l_;
|
481
|
+
float m = m_ * m_ * m_;
|
482
|
+
float s = s_ * s_ * s_;
|
483
|
+
|
484
|
+
return vec3(
|
485
|
+
+4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
|
486
|
+
-1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
|
487
|
+
-0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s
|
488
|
+
);
|
489
|
+
}
|
490
|
+
|
491
|
+
// Finds the maximum saturation possible for a given hue that fits in sRGB
|
492
|
+
// Saturation here is defined as S = C/L
|
493
|
+
// a and b must be normalized so a^2 + b^2 == 1
|
494
|
+
float compute_max_saturation(float a, float b) {
|
495
|
+
// Max saturation will be when one of r, g or b goes below zero.
|
496
|
+
|
497
|
+
// Select different coefficients depending on which component goes below zero first
|
498
|
+
float k0, k1, k2, k3, k4, wl, wm, ws;
|
499
|
+
|
500
|
+
if (-1.88170328f * a - 0.80936493f * b > 1.f)
|
501
|
+
{
|
502
|
+
// Red component
|
503
|
+
k0 = +1.19086277f; k1 = +1.76576728f; k2 = +0.59662641f; k3 = +0.75515197f; k4 = +0.56771245f;
|
504
|
+
wl = +4.0767416621f; wm = -3.3077115913f; ws = +0.2309699292f;
|
505
|
+
}
|
506
|
+
else if (1.81444104f * a - 1.19445276f * b > 1.f)
|
507
|
+
{
|
508
|
+
// Green component
|
509
|
+
k0 = +0.73956515f; k1 = -0.45954404f; k2 = +0.08285427f; k3 = +0.12541070f; k4 = +0.14503204f;
|
510
|
+
wl = -1.2684380046f; wm = +2.6097574011f; ws = -0.3413193965f;
|
511
|
+
}
|
512
|
+
else
|
513
|
+
{
|
514
|
+
// Blue component
|
515
|
+
k0 = +1.35733652f; k1 = -0.00915799f; k2 = -1.15130210f; k3 = -0.50559606f; k4 = +0.00692167f;
|
516
|
+
wl = -0.0041960863f; wm = -0.7034186147f; ws = +1.7076147010f;
|
517
|
+
}
|
518
|
+
|
519
|
+
// Approximate max saturation using a polynomial:
|
520
|
+
float S = k0 + k1 * a + k2 * b + k3 * a * a + k4 * a * b;
|
521
|
+
|
522
|
+
// Do one step Halley's method to get closer
|
523
|
+
// this gives an error less than 10e6, except for some blue hues where the dS/dh is close to infinite
|
524
|
+
// this should be sufficient for most applications, otherwise do two/three steps
|
525
|
+
|
526
|
+
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
527
|
+
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
528
|
+
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
529
|
+
|
530
|
+
{
|
531
|
+
float l_ = 1.f + S * k_l;
|
532
|
+
float m_ = 1.f + S * k_m;
|
533
|
+
float s_ = 1.f + S * k_s;
|
534
|
+
|
535
|
+
float l = l_ * l_ * l_;
|
536
|
+
float m = m_ * m_ * m_;
|
537
|
+
float s = s_ * s_ * s_;
|
538
|
+
|
539
|
+
float l_dS = 3.f * k_l * l_ * l_;
|
540
|
+
float m_dS = 3.f * k_m * m_ * m_;
|
541
|
+
float s_dS = 3.f * k_s * s_ * s_;
|
542
|
+
|
543
|
+
float l_dS2 = 6.f * k_l * k_l * l_;
|
544
|
+
float m_dS2 = 6.f * k_m * k_m * m_;
|
545
|
+
float s_dS2 = 6.f * k_s * k_s * s_;
|
546
|
+
|
547
|
+
float f = wl * l + wm * m + ws * s;
|
548
|
+
float f1 = wl * l_dS + wm * m_dS + ws * s_dS;
|
549
|
+
float f2 = wl * l_dS2 + wm * m_dS2 + ws * s_dS2;
|
550
|
+
|
551
|
+
S = S - f * f1 / (f1 * f1 - 0.5f * f * f2);
|
552
|
+
}
|
553
|
+
|
554
|
+
return S;
|
555
|
+
}
|
556
|
+
|
557
|
+
// finds L_cusp and C_cusp for a given hue
|
558
|
+
// a and b must be normalized so a^2 + b^2 == 1
|
559
|
+
vec2 find_cusp(float a, float b) {
|
560
|
+
// First, find the maximum saturation (saturation S = C/L)
|
561
|
+
float S_cusp = compute_max_saturation(a, b);
|
562
|
+
|
563
|
+
// Convert to linear sRGB to find the first point where at least one of r,g or b >= 1:
|
564
|
+
vec3 rgb_at_max = oklab_to_linear_srgb(vec3( 1, S_cusp * a, S_cusp * b ));
|
565
|
+
float L_cusp = cbrt(1.f / max(max(rgb_at_max.r, rgb_at_max.g), rgb_at_max.b));
|
566
|
+
float C_cusp = L_cusp * S_cusp;
|
567
|
+
|
568
|
+
return vec2( L_cusp , C_cusp );
|
569
|
+
}
|
570
|
+
|
571
|
+
// Finds intersection of the line defined by
|
572
|
+
// L = L0 * (1 - t) + t * L1;
|
573
|
+
// C = t * C1;
|
574
|
+
// a and b must be normalized so a^2 + b^2 == 1
|
575
|
+
float find_gamut_intersection(float a, float b, float L1, float C1, float L0, vec2 cusp) {
|
576
|
+
// Find the intersection for upper and lower half seprately
|
577
|
+
float t;
|
578
|
+
if (((L1 - L0) * cusp.y - (cusp.x - L0) * C1) <= 0.f)
|
579
|
+
{
|
580
|
+
// Lower half
|
581
|
+
|
582
|
+
t = cusp.y * L0 / (C1 * cusp.x + cusp.y * (L0 - L1));
|
583
|
+
}
|
584
|
+
else
|
585
|
+
{
|
586
|
+
// Upper half
|
587
|
+
|
588
|
+
// First intersect with triangle
|
589
|
+
t = cusp.y * (L0 - 1.f) / (C1 * (cusp.x - 1.f) + cusp.y * (L0 - L1));
|
590
|
+
|
591
|
+
// Then one step Halley's method
|
592
|
+
{
|
593
|
+
float dL = L1 - L0;
|
594
|
+
float dC = C1;
|
595
|
+
|
596
|
+
float k_l = +0.3963377774f * a + 0.2158037573f * b;
|
597
|
+
float k_m = -0.1055613458f * a - 0.0638541728f * b;
|
598
|
+
float k_s = -0.0894841775f * a - 1.2914855480f * b;
|
599
|
+
|
600
|
+
float l_dt = dL + dC * k_l;
|
601
|
+
float m_dt = dL + dC * k_m;
|
602
|
+
float s_dt = dL + dC * k_s;
|
603
|
+
|
604
|
+
|
605
|
+
// If higher accuracy is required, 2 or 3 iterations of the following block can be used:
|
606
|
+
{
|
607
|
+
float L = L0 * (1.f - t) + t * L1;
|
608
|
+
float C = t * C1;
|
609
|
+
|
610
|
+
float l_ = L + C * k_l;
|
611
|
+
float m_ = L + C * k_m;
|
612
|
+
float s_ = L + C * k_s;
|
613
|
+
|
614
|
+
float l = l_ * l_ * l_;
|
615
|
+
float m = m_ * m_ * m_;
|
616
|
+
float s = s_ * s_ * s_;
|
617
|
+
|
618
|
+
float ldt = 3.f * l_dt * l_ * l_;
|
619
|
+
float mdt = 3.f * m_dt * m_ * m_;
|
620
|
+
float sdt = 3.f * s_dt * s_ * s_;
|
621
|
+
|
622
|
+
float ldt2 = 6.f * l_dt * l_dt * l_;
|
623
|
+
float mdt2 = 6.f * m_dt * m_dt * m_;
|
624
|
+
float sdt2 = 6.f * s_dt * s_dt * s_;
|
625
|
+
|
626
|
+
float r = 4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s - 1.f;
|
627
|
+
float r1 = 4.0767416621f * ldt - 3.3077115913f * mdt + 0.2309699292f * sdt;
|
628
|
+
float r2 = 4.0767416621f * ldt2 - 3.3077115913f * mdt2 + 0.2309699292f * sdt2;
|
629
|
+
|
630
|
+
float u_r = r1 / (r1 * r1 - 0.5f * r * r2);
|
631
|
+
float t_r = -r * u_r;
|
632
|
+
|
633
|
+
float g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s - 1.f;
|
634
|
+
float g1 = -1.2684380046f * ldt + 2.6097574011f * mdt - 0.3413193965f * sdt;
|
635
|
+
float g2 = -1.2684380046f * ldt2 + 2.6097574011f * mdt2 - 0.3413193965f * sdt2;
|
636
|
+
|
637
|
+
float u_g = g1 / (g1 * g1 - 0.5f * g * g2);
|
638
|
+
float t_g = -g * u_g;
|
639
|
+
|
640
|
+
float b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s - 1.f;
|
641
|
+
float b1 = -0.0041960863f * ldt - 0.7034186147f * mdt + 1.7076147010f * sdt;
|
642
|
+
float b2 = -0.0041960863f * ldt2 - 0.7034186147f * mdt2 + 1.7076147010f * sdt2;
|
643
|
+
|
644
|
+
float u_b = b1 / (b1 * b1 - 0.5f * b * b2);
|
645
|
+
float t_b = -b * u_b;
|
646
|
+
|
647
|
+
t_r = u_r >= 0.f ? t_r : 10000.f;
|
648
|
+
t_g = u_g >= 0.f ? t_g : 10000.f;
|
649
|
+
t_b = u_b >= 0.f ? t_b : 10000.f;
|
650
|
+
|
651
|
+
t += min(t_r, min(t_g, t_b));
|
652
|
+
}
|
653
|
+
}
|
654
|
+
}
|
655
|
+
|
656
|
+
return t;
|
657
|
+
}
|
658
|
+
|
659
|
+
float find_gamut_intersection(float a, float b, float L1, float C1, float L0) {
|
660
|
+
// Find the cusp of the gamut triangle
|
661
|
+
vec2 cusp = find_cusp(a, b);
|
662
|
+
|
663
|
+
return find_gamut_intersection(a, b, L1, C1, L0, cusp);
|
664
|
+
}
|
665
|
+
|
666
|
+
vec3 gamut_clip_preserve_chroma(vec3 rgb) {
|
667
|
+
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)
|
668
|
+
return rgb;
|
669
|
+
|
670
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
671
|
+
|
672
|
+
float L = lab.x;
|
673
|
+
float eps = 0.00001f;
|
674
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
675
|
+
float a_ = lab.y / C;
|
676
|
+
float b_ = lab.z / C;
|
677
|
+
|
678
|
+
float L0 = clamp(L, 0.f, 1.f);
|
679
|
+
|
680
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
681
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
682
|
+
float C_clipped = t * C;
|
683
|
+
|
684
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
685
|
+
}
|
686
|
+
|
687
|
+
vec3 gamut_clip_project_to_0_5(vec3 rgb) {
|
688
|
+
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)
|
689
|
+
return rgb;
|
690
|
+
|
691
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
692
|
+
|
693
|
+
float L = lab.x;
|
694
|
+
float eps = 0.00001f;
|
695
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
696
|
+
float a_ = lab.y / C;
|
697
|
+
float b_ = lab.z / C;
|
698
|
+
|
699
|
+
float L0 = 0.5;
|
700
|
+
|
701
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
702
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
703
|
+
float C_clipped = t * C;
|
704
|
+
|
705
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
706
|
+
}
|
707
|
+
|
708
|
+
vec3 gamut_clip_project_to_L_cusp(vec3 rgb) {
|
709
|
+
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)
|
710
|
+
return rgb;
|
711
|
+
|
712
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
713
|
+
|
714
|
+
float L = lab.x;
|
715
|
+
float eps = 0.00001f;
|
716
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
717
|
+
float a_ = lab.y / C;
|
718
|
+
float b_ = lab.z / C;
|
719
|
+
|
720
|
+
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
721
|
+
vec2 cusp = find_cusp(a_, b_);
|
722
|
+
|
723
|
+
float L0 = cusp.x;
|
724
|
+
|
725
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
726
|
+
|
727
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
728
|
+
float C_clipped = t * C;
|
729
|
+
|
730
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
731
|
+
}
|
732
|
+
|
733
|
+
vec3 gamut_clip_adaptive_L0_0_5(vec3 rgb, float alpha) {
|
734
|
+
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)
|
735
|
+
return rgb;
|
736
|
+
|
737
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
738
|
+
|
739
|
+
float L = lab.x;
|
740
|
+
float eps = 0.00001f;
|
741
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
742
|
+
float a_ = lab.y / C;
|
743
|
+
float b_ = lab.z / C;
|
744
|
+
|
745
|
+
float Ld = L - 0.5f;
|
746
|
+
float e1 = 0.5f + abs(Ld) + alpha * C;
|
747
|
+
float L0 = 0.5f * (1.f + sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * abs(Ld))));
|
748
|
+
|
749
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
750
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
751
|
+
float C_clipped = t * C;
|
752
|
+
|
753
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
754
|
+
}
|
755
|
+
|
756
|
+
vec3 gamut_clip_adaptive_L0_L_cusp(vec3 rgb, float alpha) {
|
757
|
+
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)
|
758
|
+
return rgb;
|
759
|
+
|
760
|
+
vec3 lab = linear_srgb_to_oklab(rgb);
|
761
|
+
|
762
|
+
float L = lab.x;
|
763
|
+
float eps = 0.00001f;
|
764
|
+
float C = max(eps, sqrt(lab.y * lab.y + lab.z * lab.z));
|
765
|
+
float a_ = lab.y / C;
|
766
|
+
float b_ = lab.z / C;
|
767
|
+
|
768
|
+
// The cusp is computed here and in find_gamut_intersection, an optimized solution would only compute it once.
|
769
|
+
vec2 cusp = find_cusp(a_, b_);
|
770
|
+
|
771
|
+
float Ld = L - cusp.x;
|
772
|
+
float k = 2.f * (Ld > 0.f ? 1.f - cusp.x : cusp.x);
|
773
|
+
|
774
|
+
float e1 = 0.5f * k + abs(Ld) + alpha * C / k;
|
775
|
+
float L0 = cusp.x + 0.5f * (sign(Ld) * (e1 - sqrt(e1 * e1 - 2.f * k * abs(Ld))));
|
776
|
+
|
777
|
+
float t = find_gamut_intersection(a_, b_, L, C, L0);
|
778
|
+
float L_clipped = L0 * (1.f - t) + t * L;
|
779
|
+
float C_clipped = t * C;
|
780
|
+
|
781
|
+
return oklab_to_linear_srgb(vec3( L_clipped, C_clipped * a_, C_clipped * b_ ));
|
782
|
+
}
|
783
|
+
|
784
|
+
float toe(float x) {
|
785
|
+
float k_1 = 0.206f;
|
786
|
+
float k_2 = 0.03f;
|
787
|
+
float k_3 = (1.f + k_1) / (1.f + k_2);
|
788
|
+
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));
|
789
|
+
}
|
790
|
+
|
791
|
+
float toe_inv(float x) {
|
792
|
+
float k_1 = 0.206f;
|
793
|
+
float k_2 = 0.03f;
|
794
|
+
float k_3 = (1.f + k_1) / (1.f + k_2);
|
795
|
+
return (x * x + k_1 * x) / (k_3 * (x + k_2));
|
796
|
+
}
|
797
|
+
|
798
|
+
vec2 to_ST(vec2 cusp) {
|
799
|
+
float L = cusp.x;
|
800
|
+
float C = cusp.y;
|
801
|
+
return vec2( C / L, C / (1.f - L) );
|
802
|
+
}
|
803
|
+
|
804
|
+
// Returns a smooth approximation of the location of the cusp
|
805
|
+
// This polynomial was created by an optimization process
|
806
|
+
// It has been designed so that S_mid < S_max and T_mid < T_max
|
807
|
+
vec2 get_ST_mid(float a_, float b_) {
|
808
|
+
float S = 0.11516993f + 1.f / (
|
809
|
+
+7.44778970f + 4.15901240f * b_
|
810
|
+
+ a_ * (-2.19557347f + 1.75198401f * b_
|
811
|
+
+ a_ * (-2.13704948f - 10.02301043f * b_
|
812
|
+
+ a_ * (-4.24894561f + 5.38770819f * b_ + 4.69891013f * a_
|
813
|
+
)))
|
814
|
+
);
|
815
|
+
|
816
|
+
float T = 0.11239642f + 1.f / (
|
817
|
+
+1.61320320f - 0.68124379f * b_
|
818
|
+
+ a_ * (+0.40370612f + 0.90148123f * b_
|
819
|
+
+ a_ * (-0.27087943f + 0.61223990f * b_
|
820
|
+
+ a_ * (+0.00299215f - 0.45399568f * b_ - 0.14661872f * a_
|
821
|
+
)))
|
822
|
+
);
|
823
|
+
|
824
|
+
return vec2( S, T );
|
825
|
+
}
|
826
|
+
|
827
|
+
vec3 get_Cs(float L, float a_, float b_) {
|
828
|
+
vec2 cusp = find_cusp(a_, b_);
|
829
|
+
|
830
|
+
float C_max = find_gamut_intersection(a_, b_, L, 1.f, L, cusp);
|
831
|
+
vec2 ST_max = to_ST(cusp);
|
832
|
+
|
833
|
+
// Scale factor to compensate for the curved part of gamut shape:
|
834
|
+
float k = C_max / min((L * ST_max.x), (1.f - L) * ST_max.y);
|
835
|
+
|
836
|
+
float C_mid;
|
837
|
+
{
|
838
|
+
vec2 ST_mid = get_ST_mid(a_, b_);
|
839
|
+
|
840
|
+
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
841
|
+
float C_a = L * ST_mid.x;
|
842
|
+
float C_b = (1.f - L) * ST_mid.y;
|
843
|
+
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))));
|
844
|
+
}
|
845
|
+
|
846
|
+
float C_0;
|
847
|
+
{
|
848
|
+
// for C_0, the shape is independent of hue, so vec2 are constant. Values picked to roughly be the average values of vec2.
|
849
|
+
float C_a = L * 0.4f;
|
850
|
+
float C_b = (1.f - L) * 0.8f;
|
851
|
+
|
852
|
+
// Use a soft minimum function, instead of a sharp triangle shape to get a smooth value for chroma.
|
853
|
+
C_0 = sqrt(1.f / (1.f / (C_a * C_a) + 1.f / (C_b * C_b)));
|
854
|
+
}
|
855
|
+
|
856
|
+
return vec3( C_0, C_mid, C_max );
|
857
|
+
}
|
858
|
+
|
859
|
+
vec3 okhsl_to_srgb(vec3 hsl) {
|
860
|
+
float h = hsl.x;
|
861
|
+
float s = hsl.y;
|
862
|
+
float l = hsl.z;
|
863
|
+
|
864
|
+
if (l == 1.0f)
|
865
|
+
{
|
866
|
+
return vec3( 1.f, 1.f, 1.f );
|
867
|
+
}
|
868
|
+
|
869
|
+
else if (l == 0.f)
|
870
|
+
{
|
871
|
+
return vec3( 0.f, 0.f, 0.f );
|
872
|
+
}
|
873
|
+
|
874
|
+
float a_ = cos(2.f * M_PI * h);
|
875
|
+
float b_ = sin(2.f * M_PI * h);
|
876
|
+
float L = toe_inv(l);
|
877
|
+
|
878
|
+
vec3 cs = get_Cs(L, a_, b_);
|
879
|
+
float C_0 = cs.x;
|
880
|
+
float C_mid = cs.y;
|
881
|
+
float C_max = cs.z;
|
882
|
+
|
883
|
+
float mid = 0.8f;
|
884
|
+
float mid_inv = 1.25f;
|
885
|
+
|
886
|
+
float C, t, k_0, k_1, k_2;
|
887
|
+
|
888
|
+
if (s < mid)
|
889
|
+
{
|
890
|
+
t = mid_inv * s;
|
891
|
+
|
892
|
+
k_1 = mid * C_0;
|
893
|
+
k_2 = (1.f - k_1 / C_mid);
|
894
|
+
|
895
|
+
C = t * k_1 / (1.f - k_2 * t);
|
896
|
+
}
|
897
|
+
else
|
898
|
+
{
|
899
|
+
t = (s - mid)/ (1.f - mid);
|
900
|
+
|
901
|
+
k_0 = C_mid;
|
902
|
+
k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
903
|
+
k_2 = (1.f - (k_1) / (C_max - C_mid));
|
904
|
+
|
905
|
+
C = k_0 + t * k_1 / (1.f - k_2 * t);
|
906
|
+
}
|
907
|
+
|
908
|
+
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
909
|
+
return vec3(
|
910
|
+
srgb_transfer_function(rgb.r),
|
911
|
+
srgb_transfer_function(rgb.g),
|
912
|
+
srgb_transfer_function(rgb.b)
|
913
|
+
);
|
914
|
+
}
|
915
|
+
|
916
|
+
vec3 srgb_to_okhsl(vec3 rgb) {
|
917
|
+
vec3 lab = linear_srgb_to_oklab(vec3(
|
918
|
+
srgb_transfer_function_inv(rgb.r),
|
919
|
+
srgb_transfer_function_inv(rgb.g),
|
920
|
+
srgb_transfer_function_inv(rgb.b)
|
921
|
+
));
|
922
|
+
|
923
|
+
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
924
|
+
float a_ = lab.y / C;
|
925
|
+
float b_ = lab.z / C;
|
926
|
+
|
927
|
+
float L = lab.x;
|
928
|
+
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
929
|
+
|
930
|
+
vec3 cs = get_Cs(L, a_, b_);
|
931
|
+
float C_0 = cs.x;
|
932
|
+
float C_mid = cs.y;
|
933
|
+
float C_max = cs.z;
|
934
|
+
|
935
|
+
// Inverse of the interpolation in okhsl_to_srgb:
|
936
|
+
|
937
|
+
float mid = 0.8f;
|
938
|
+
float mid_inv = 1.25f;
|
939
|
+
|
940
|
+
float s;
|
941
|
+
if (C < C_mid)
|
942
|
+
{
|
943
|
+
float k_1 = mid * C_0;
|
944
|
+
float k_2 = (1.f - k_1 / C_mid);
|
945
|
+
|
946
|
+
float t = C / (k_1 + k_2 * C);
|
947
|
+
s = t * mid;
|
948
|
+
}
|
949
|
+
else
|
950
|
+
{
|
951
|
+
float k_0 = C_mid;
|
952
|
+
float k_1 = (1.f - mid) * C_mid * C_mid * mid_inv * mid_inv / C_0;
|
953
|
+
float k_2 = (1.f - (k_1) / (C_max - C_mid));
|
954
|
+
|
955
|
+
float t = (C - k_0) / (k_1 + k_2 * (C - k_0));
|
956
|
+
s = mid + (1.f - mid) * t;
|
957
|
+
}
|
958
|
+
|
959
|
+
float l = toe(L);
|
960
|
+
return vec3( h, s, l );
|
961
|
+
}
|
962
|
+
|
963
|
+
|
964
|
+
vec3 okhsv_to_srgb(vec3 hsv) {
|
965
|
+
float h = hsv.x;
|
966
|
+
float s = hsv.y;
|
967
|
+
float v = hsv.z;
|
968
|
+
|
969
|
+
float a_ = cos(2.f * M_PI * h);
|
970
|
+
float b_ = sin(2.f * M_PI * h);
|
971
|
+
|
972
|
+
vec2 cusp = find_cusp(a_, b_);
|
973
|
+
vec2 ST_max = to_ST(cusp);
|
974
|
+
float S_max = ST_max.x;
|
975
|
+
float T_max = ST_max.y;
|
976
|
+
float S_0 = 0.5f;
|
977
|
+
float k = 1.f- S_0 / S_max;
|
978
|
+
|
979
|
+
// first we compute L and V as if the gamut is a perfect triangle:
|
980
|
+
|
981
|
+
// L, C when v==1:
|
982
|
+
float L_v = 1.f - s * S_0 / (S_0 + T_max - T_max * k * s);
|
983
|
+
float C_v = s * T_max * S_0 / (S_0 + T_max - T_max * k * s);
|
984
|
+
|
985
|
+
float L = v * L_v;
|
986
|
+
float C = v * C_v;
|
987
|
+
|
988
|
+
// then we compensate for both toe and the curved top part of the triangle:
|
989
|
+
float L_vt = toe_inv(L_v);
|
990
|
+
float C_vt = C_v * L_vt / L_v;
|
991
|
+
|
992
|
+
float L_new = toe_inv(L);
|
993
|
+
C = C * L_new / L;
|
994
|
+
L = L_new;
|
995
|
+
|
996
|
+
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
997
|
+
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
998
|
+
|
999
|
+
L = L * scale_L;
|
1000
|
+
C = C * scale_L;
|
1001
|
+
|
1002
|
+
vec3 rgb = oklab_to_linear_srgb(vec3( L, C * a_, C * b_ ));
|
1003
|
+
return vec3(
|
1004
|
+
srgb_transfer_function(rgb.r),
|
1005
|
+
srgb_transfer_function(rgb.g),
|
1006
|
+
srgb_transfer_function(rgb.b)
|
1007
|
+
);
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
vec3 srgb_to_okhsv(vec3 rgb) {
|
1011
|
+
vec3 lab = linear_srgb_to_oklab(vec3(
|
1012
|
+
srgb_transfer_function_inv(rgb.r),
|
1013
|
+
srgb_transfer_function_inv(rgb.g),
|
1014
|
+
srgb_transfer_function_inv(rgb.b)
|
1015
|
+
));
|
1016
|
+
|
1017
|
+
float C = sqrt(lab.y * lab.y + lab.z * lab.z);
|
1018
|
+
float a_ = lab.y / C;
|
1019
|
+
float b_ = lab.z / C;
|
1020
|
+
|
1021
|
+
float L = lab.x;
|
1022
|
+
float h = 0.5f + 0.5f * atan(-lab.z, -lab.y) / M_PI;
|
1023
|
+
|
1024
|
+
vec2 cusp = find_cusp(a_, b_);
|
1025
|
+
vec2 ST_max = to_ST(cusp);
|
1026
|
+
float S_max = ST_max.x;
|
1027
|
+
float T_max = ST_max.y;
|
1028
|
+
float S_0 = 0.5f;
|
1029
|
+
float k = 1.f - S_0 / S_max;
|
1030
|
+
|
1031
|
+
// first we find L_v, C_v, L_vt and C_vt
|
1032
|
+
|
1033
|
+
float t = T_max / (C + L * T_max);
|
1034
|
+
float L_v = t * L;
|
1035
|
+
float C_v = t * C;
|
1036
|
+
|
1037
|
+
float L_vt = toe_inv(L_v);
|
1038
|
+
float C_vt = C_v * L_vt / L_v;
|
1039
|
+
|
1040
|
+
// we can then use these to invert the step that compensates for the toe and the curved top part of the triangle:
|
1041
|
+
vec3 rgb_scale = oklab_to_linear_srgb(vec3( L_vt, a_ * C_vt, b_ * C_vt ));
|
1042
|
+
float scale_L = cbrt(1.f / max(max(rgb_scale.r, rgb_scale.g), max(rgb_scale.b, 0.f)));
|
1043
|
+
|
1044
|
+
L = L / scale_L;
|
1045
|
+
C = C / scale_L;
|
1046
|
+
|
1047
|
+
C = C * toe(L) / L;
|
1048
|
+
L = toe(L);
|
1049
|
+
|
1050
|
+
// we can now compute v and s:
|
1051
|
+
|
1052
|
+
float v = L / L_v;
|
1053
|
+
float s = (S_0 + T_max) * C_v / ((T_max * S_0) + T_max * k * C_v);
|
1054
|
+
|
1055
|
+
return vec3 (h, s, v );
|
1056
|
+
}
|
1057
|
+
|
1058
|
+
void main() {
|
1059
|
+
vec3 col = okhsv_to_srgb(vec3(uHue, fragTexCoord.x, 1 - fragTexCoord.y));
|
1060
|
+
finalColor = vec4(col, 1.0);
|
1061
|
+
}
|
1062
|
+
EOF
|
1063
|
+
|
1064
|
+
def hue_shader
|
1065
|
+
HUE_SHADER
|
1066
|
+
end
|
1067
|
+
|
1068
|
+
def picker_shader
|
1069
|
+
PICKER_SHADER
|
1070
|
+
end
|
1071
|
+
|
1072
|
+
def render(canvas)
|
1073
|
+
self.top = canvas.y
|
1074
|
+
self.left = canvas.x
|
1075
|
+
self.height = canvas.height
|
1076
|
+
self.width = canvas.width
|
1077
|
+
|
1078
|
+
yield canvas
|
1079
|
+
end
|
1080
|
+
end
|