-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ok_picker_classic.lua
1948 lines (1701 loc) · 59.1 KB
/
ok_picker_classic.lua
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
dofile("./ok_color.lua")
-- Copyright(c) 2021 Bjorn Ottosson
--
-- 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 noticeand 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.
local defaults <const> = {
base = Color { r = 255, g = 0, b = 0, a = 255 },
colorMode = "HSL",
alpha = 255,
hslHue = 29,
hslSat = 255,
hslLgt = 145,
hsvHue = 29,
hsvSat = 255,
hsvVal = 255,
labLgt = 160,
labA = 225,
labB = 126,
showGradientSettings = false,
gradWidth = 256,
gradHeight = 32,
swatchCount = 7,
hueDir = "NEAR",
showWheelSettings = false,
remapHue = "OKLAB",
size = 256,
hslAxis = "LIGHTNESS",
hsvAxis = "VALUE",
minSat = 0,
maxSat = 250,
minLight = 5,
maxLight = 250,
minValue = 5,
maxValue = 250,
sectorCount = 0,
ringCount = 0,
frames = 32,
fps = 24,
harmonyType = "NONE",
analogies = {
Color { r = 244, g = 0, b = 132 },
Color { r = 200, g = 110, b = 0 }
},
complement = { Color { r = 0, g = 154, b = 172 } },
splits = {
Color { r = 0, g = 159, b = 138 },
Color { r = 0, g = 146, b = 212 }
},
squares = {
Color { r = 127, g = 148, b = 0 },
Color { r = 0, g = 154, b = 172 },
Color { r = 160, g = 88, b = 255 }
},
triads = {
Color { r = 89, g = 123, b = 255 },
Color { r = 0, g = 164, b = 71 }
},
shadingCount = 7,
shadowLight = 0.1,
dayLight = 0.9,
hYel = 110.0 / 360.0,
minChroma = 0.01,
lgtDesatFac = 0.75,
shdDesatFac = 0.75,
srcLightWeight = 0.3333333333333333,
greenHue = 146.0 / 360.0,
minGreenOffset = 0.3,
maxGreenOffset = 0.6,
shadowHue = 291.0 / 360.0,
dayHue = 96.0 / 360.0,
}
local rybHueRemapTable24 <const> = {
0.081186489951701, --ff0000bc
0.088543402729094, --ff0021c9
0.10262364680987, --ff0040d9
0.13403629399108, --ff0066e2
0.17916243354554, --ff008ae7
0.21949160794204, --ff00a8ed
0.25320328186974, --ff05c4f3
0.28293666595694, --ff20e7fe
0.3045570947557, --ff33fefe
0.36503532781409, --ff00ffa1
0.39128411126471, --ff00ff43
0.397020150612, --ff1cff00
0.41735542065514, --ff7aff00
0.48945464109323, --ffd8ff00
0.62758432515209, --ffa68200
0.71322973397372, --ffce6401
0.73231601426016, --fff24100
0.74106567631729, --ffe7241b
0.75326079486957, --ffc61528
0.79484301474327, --ff97043b
0.86508195598932, --ff6a004b
0.94537622442158, --ff510367
1.016379461501466, --ff3c008a
1.062861707421092, --ff2100a7
1.081186489951701, --ff0000bc
}
local rgbHueRemapTable24 <const> = {
0.081186489951701, --ff0000ff
0.095766555555779, --ff0040ff
0.14659018427134, --ff0080ff
0.23391826905505, --ff00bfff
0.30496513648794, --ff00ffff
0.34959701375912, --ff00ffbf
0.37768750974757, --ff00ff80
0.39166456136257, --ff00ff40
0.39586436900711, --ff00ff00
0.40113237655716, --ff40ff00
0.41979583005808, --ff80ff00
0.4629578833159, --ffbfff00
0.54114119023916, --ffffff00
0.64313742175205, --ffffbf00
0.71173624990824, --ffff8000
0.73287799718808, --ffff4000
0.73349594598298, --ffff0000
0.76053438187642, --ffff0040
0.81602187489686, --ffff0080
0.8694399396202, --ffff00bf
0.91207209774771, --ffff00ff
0.95478525659029, --ffbf00ff
1.0072520521085758, --ff8000ff
1.058046802990062, --ff4000ff
1.081186489951701, --ff0000ff
}
local rygbHueRemapTable24 <const> = {
0.072237063394159,
0.095280383412942,
0.13208035564017,
0.16773270414916,
0.20028989647507,
0.23379645010808,
0.26494353051784,
0.29321548131135,
0.32196570822139,
0.34926312018934,
0.38151093005415,
0.40002040933732,
0.43068663559305,
0.45901928198972,
0.49871093920573,
0.54434781369171,
0.5969853889193,
0.67811627795289,
0.73479009641358,
0.78756293049826,
0.83860181840028,
0.89205663524978,
0.95304453455657,
1.021032964650062,
1.072237063394159,
}
---@param ase Color
---@return Color
local function copyColorByValue(ase)
return Color {
r = ase.red,
g = ase.green,
b = ase.blue,
a = ase.alpha
}
end
---@param ase Color
---@return number r
---@return number g
---@return number b
---@nodiscard
local function aseColorToRgb01(ase)
return ase.red / 255.0,
ase.green / 255.0,
ase.blue / 255.0
end
---@param ase Color
---@return Color
local function assignColor(ase)
if ase.alpha > 0 then
return copyColorByValue(ase)
else
return Color { r = 0, g = 0, b = 0, a = 0 }
end
end
---@param ase Color
---@return string
local function colorToHexWeb(ase)
return string.format("%06x",
ase.red << 0x10
| ase.green << 0x08
| ase.blue)
end
---@param sprite Sprite
---@param count integer
---@param duration number
---@return Frame[]
local function createNewFrames(sprite, count, duration)
if not sprite then
app.alert { title = "Error", text = "Sprite could not be found." }
return {}
end
if count < 1 then return {} end
if count > 256 then
local response <const> = app.alert {
title = "Warning",
text = {
string.format(
"This script will create %d frames,",
count),
string.format(
"%d beyond the limit of %d.",
count - 256,
256),
"Do you wish to proceed?"
},
buttons = { "&YES", "&NO" }
}
if response == 2 then
return {}
end
end
local valDur <const> = duration or 1
local valCount = count or 1
if valCount < 1 then valCount = 1 end
---@type Frame[]
local frames <const> = {}
app.transaction("Create Frames", function()
for i = 1, valCount, 1 do
local frame <const> = sprite:newEmptyFrame()
frame.duration = valDur
frames[i] = frame
end
end)
return frames
end
---@param a number
---@param b number
---@param range number
---@return number
local function distAngleUnsigned(a, b, range)
local halfRange <const> = range * 0.5
return halfRange - math.abs(math.abs(
(b % range) - (a % range))
- halfRange)
end
---@param orig number
---@param dest number
---@param t number
---@param range number
---@return number
local function lerpAngleNear(orig, dest, t, range)
local halfRange <const> = range * 0.5
local o <const> = orig % range
local d <const> = dest % range
local diff <const> = d - o
local u <const> = 1.0 - t
if diff == 0.0 then
return o
elseif o < d and diff > halfRange then
return (u * (o + range) + t * d) % range
elseif o > d and diff < -halfRange then
return (u * o + t * (d + range)) % range
else
return u * o + t * d
end
end
---@param orig number
---@param dest number
---@param t number
---@param range number
---@return number
local function lerpAngleCcw(orig, dest, t, range)
local o <const> = orig % range
local d <const> = dest % range
local diff <const> = d - o
local u <const> = 1.0 - t
if diff == 0.0 then
return o
elseif o > d then
return (u * o + t * (d + range)) % range
else
return u * o + t * d
end
end
---@param orig number
---@param dest number
---@param t number
---@param range number
---@return number
local function lerpAngleCw(orig, dest, t, range)
local o <const> = orig % range
local d <const> = dest % range
local diff <const> = d - o
local u <const> = 1.0 - t
if diff == 0.0 then
return d
elseif o < d then
return (u * (o + range) + t * d) % range
else
return u * o + t * d
end
end
local function preserveForeBack()
app.fgColor = copyColorByValue(app.fgColor)
app.command.SwitchColors()
app.fgColor = copyColorByValue(app.fgColor)
app.command.SwitchColors()
end
---@param a number
---@param levels integer
---@return number
local function quantizeSigned(a, levels)
if levels ~= 0 then
return math.floor(0.5 + a * levels) / levels
else
return a
end
end
---@param a number
---@param levels integer
---@return number
local function quantizeUnsigned(a, levels)
if levels > 1 then
return math.max(0.0,
(math.ceil(a * levels) - 1.0)
/ (levels - 1.0))
else
return math.max(0.0, a)
end
end
---@param r number
---@param g number
---@param b number
---@param a integer?
---@return integer
local function srgb01ToHex(r, g, b, a)
return ((a or 255) << 0x18)
| math.floor(math.min(math.max(b, 0.0), 1.0) * 255.0 + 0.5) << 0x10
| math.floor(math.min(math.max(g, 0.0), 1.0) * 255.0 + 0.5) << 0x08
| math.floor(math.min(math.max(r, 0.0), 1.0) * 255.0 + 0.5)
end
---@param r number
---@param g number
---@param b number
---@param a integer?
---@return Color
local function srgb01ToAseColor(r, g, b, a)
return Color {
r = math.floor(math.min(math.max(r, 0.0), 1.0) * 255.0 + 0.5),
g = math.floor(math.min(math.max(g, 0.0), 1.0) * 255.0 + 0.5),
b = math.floor(math.min(math.max(b, 0.0), 1.0) * 255.0 + 0.5),
a = a or 255
}
end
---@param v number
---@return integer
local function round(v)
local iv <const>, fv <const> = math.modf(v)
if iv <= 0 and fv <= -0.5 then
return iv - 1
elseif iv >= 0 and fv >= 0.5 then
return iv + 1
else
return iv
end
end
---@param t number
---@return number
local function zigZag(t)
local a <const> = t * 0.5
local b <const> = a - math.floor(a)
return 1.0 - math.abs(b + b - 1.0)
end
---@param dialog Dialog
---@param shades Color[]
local function updateShades(dialog, shades)
-- TODO: This causes problems with gray patches
-- when using LAB mode.
local args <const> = dialog.data
local alpha <const> = args.alpha --[[@as integer]]
local hslLgt <const> = args.hslLgt --[[@as integer]]
local hslSat <const> = args.hslSat --[[@as integer]]
local hslHue <const> = args.hslHue --[[@as integer]]
local l <const> = math.min(math.max(hslLgt / 255.0, 0.01), 0.99)
local s <const> = hslSat / 255.0
local h <const> = hslHue / 360.0
-- Decide on clockwise or counter-clockwise based
-- on color's warmth or coolness.
-- The LCh hue for yellow is 103 degrees.
local hYel <const> = defaults.hYel
local hBlu <const> = hYel + 0.5
local lerpFunc = nil
if h < hYel or h >= hBlu then
lerpFunc = lerpAngleCcw
else
lerpFunc = lerpAngleCw
end
-- Minimum and maximum light based on place in loop.
local shadowLight <const> = defaults.shadowLight
local dayLight <const> = defaults.dayLight
-- Yellows are very saturated at high light;
-- Desaturate them to get a better shade.
-- Conversely, blues easily fall out of gamut
-- so the shade factor is separate.
local lgtDesatFac <const> = defaults.lgtDesatFac
local shdDesatFac <const> = defaults.shdDesatFac
local minChroma <const> = defaults.minChroma
local cVal <const> = math.max(minChroma, s)
local desatChromaLgt <const> = cVal * lgtDesatFac
local desatChromaShd <const> = cVal * shdDesatFac
-- Amount to mix between base light and loop light.
local srcLightWeight <const> = defaults.srcLightWeight
local cmpLightWeight <const> = 1.0 - srcLightWeight
-- The warm-cool dichotomy works poorly for greens.
-- For that reason, the closer a hue is to green,
-- the more it uses absolute hue shifting.
-- Green is approximately at hue 140.
local offsetMix <const> = 2.0 * distAngleUnsigned(h, defaults.greenHue, 1.0)
local offsetScale <const> = (1.0 - offsetMix) * defaults.maxGreenOffset
+ offsetMix * defaults.minGreenOffset
-- Absolute hues for shadow and light.
-- This could also be combined with the origin hue +/-
-- a shift which is then mixed with the absolute hue.
local shadowHue <const> = defaults.shadowHue
local dayHue <const> = defaults.dayHue
local shadingCount <const> = defaults.shadingCount
local toFac <const> = 1.0 / (shadingCount - 1.0)
for i = 1, shadingCount, 1 do
local iFac <const> = (i - 1) * toFac
local lItr <const> = (1.0 - iFac) * shadowLight
+ iFac * dayLight
-- Idealized hue from violet shadow to
-- off-yellow daylight.
local hAbs <const> = lerpFunc(shadowHue, dayHue, lItr, 1.0)
-- The middle sample should be closest to base color.
-- The fac needs to be 0.0. That's why zigzag is
-- used to convert to an oscillation.
local lMixed <const> = srcLightWeight * l
+ cmpLightWeight * lItr
local lZig <const> = zigZag(lMixed)
local fac <const> = offsetScale * lZig
local hMixed <const> = lerpAngleNear(h, hAbs, fac, 1.0)
-- Desaturate brights and darks.
-- Min chroma gives even grays a slight chroma.
local chromaTarget = desatChromaLgt
if lMixed < 0.5 then chromaTarget = desatChromaShd end
local cMixed = (1.0 - lZig) * cVal + lZig * chromaTarget
cMixed = math.max(minChroma, cMixed)
local r <const>, g <const>, b <const> = ok_color.okhsl_to_srgb(
hMixed, cMixed, lMixed)
local aseColor <const> = srgb01ToAseColor(r, g, b, alpha)
shades[i] = aseColor
end
dialog:modify { id = "shading", colors = shades }
end
---@param dialog Dialog
---@param primary Color
local function updateHarmonies(dialog, primary)
local r01 <const>, g01 <const>, b01 <const> = aseColorToRgb01(primary)
local h <const>, s <const>, l <const> = ok_color.srgb_to_okhsl(r01, g01, b01)
local h30 <const> = 0.08333333333333333
local h90 <const> = 0.25
local h120 <const> = 0.3333333333333333
local h150 <const> = 0.4166666666666667
local h180 <const> = 0.5
local h210 <const> = 0.5833333333333333
local h270 <const> = 0.75
local lOpp <const> = 1.0 - l
local lTri <const> = (2.0 - l) / 3.0
local lAna <const> = (2.0 * l + 0.5) / 3.0
local lSpl <const> = (2.5 - 2.0 * l) / 3.0
local lSqr <const> = 0.5
local rAna0 <const>, gAna0 <const>, bAna0 <const> = ok_color.okhsl_to_srgb(h - h30, s, lAna)
local rAna1 <const>, gAna1 <const>, bAna1 <const> = ok_color.okhsl_to_srgb(h + h30, s, lAna)
local rTri0 <const>, gTri0 <const>, bTri0 <const> = ok_color.okhsl_to_srgb(h - h120, s, lTri)
local rTri1 <const>, gTri1 <const>, bTri1 <const> = ok_color.okhsl_to_srgb(h + h120, s, lTri)
local rSpl0 <const>, gSpl0 <const>, bSpl0 <const> = ok_color.okhsl_to_srgb(h + h150, s, lSpl)
local rSpl1 <const>, gSpl1 <const>, bSpl1 <const> = ok_color.okhsl_to_srgb(h + h210, s, lSpl)
local rSqr0 <const>, gSqr0 <const>, bSqr0 <const> = ok_color.okhsl_to_srgb(h + h90, s, lSqr)
local rSqr1 <const>, gSqr1 <const>, bSqr1 <const> = ok_color.okhsl_to_srgb(h + h180, s, lOpp)
local rSqr2 <const>, gSqr2 <const>, bSqr2 <const> = ok_color.okhsl_to_srgb(h + h270, s, lSqr)
local tris <const> = {
srgb01ToAseColor(rTri0, gTri0, bTri0),
srgb01ToAseColor(rTri1, gTri1, bTri1)
}
local analogues <const> = {
srgb01ToAseColor(rAna0, gAna0, bAna0),
srgb01ToAseColor(rAna1, gAna1, bAna1)
}
local splits <const> = {
srgb01ToAseColor(rSpl0, gSpl0, bSpl0),
srgb01ToAseColor(rSpl1, gSpl1, bSpl1)
}
local squares <const> = {
srgb01ToAseColor(rSqr0, gSqr0, bSqr0),
srgb01ToAseColor(rSqr1, gSqr1, bSqr1),
srgb01ToAseColor(rSqr2, gSqr2, bSqr2)
}
dialog:modify { id = "complement", colors = { squares[2] } }
dialog:modify { id = "triadic", colors = tris }
dialog:modify { id = "analogous", colors = analogues }
dialog:modify { id = "split", colors = splits }
dialog:modify { id = "square", colors = squares }
end
---@param dialog Dialog
---@param l number
---@param a number
---@param b number
local function setLab(dialog, l, a, b)
local labLgtInt <const> = math.floor(l * 255.0 + 0.5)
local labAInt <const> = round(a * 1000.0)
local labBInt <const> = round(b * 1000.0)
dialog:modify { id = "labLgt", value = labLgtInt }
dialog:modify { id = "labA", value = labAInt }
dialog:modify { id = "labB", value = labBInt }
end
---@param dialog Dialog
---@param h number
---@param s number
---@param l number
local function setHsl(dialog, h, s, l)
local hslLgtInt <const> = math.floor(l * 255.0 + 0.5)
local hslSatInt <const> = math.floor(s * 255.0 + 0.5)
local hslHueInt <const> = math.floor(h * 360.0 + 0.5)
if hslSatInt > 0
and hslLgtInt > 0
and hslLgtInt < 255 then
dialog:modify { id = "hslHue", value = hslHueInt }
end
dialog:modify { id = "hslSat", value = hslSatInt }
dialog:modify { id = "hslLgt", value = hslLgtInt }
end
---@param dialog Dialog
---@param h number
---@param s number
---@param v number
local function setHsv(dialog, h, s, v)
local hsvValInt <const> = math.floor(v * 255.0 + 0.5)
local hsvSatInt <const> = math.floor(s * 255.0 + 0.5)
local hsvHueInt <const> = math.floor(h * 360.0 + 0.5)
if hsvSatInt > 0 and hsvValInt > 0 then
dialog:modify { id = "hsvHue", value = hsvHueInt }
end
dialog:modify { id = "hsvSat", value = hsvSatInt }
dialog:modify { id = "hsvVal", value = hsvValInt }
end
---@param dialog Dialog
---@param primary Color
---@param shades Color[]
local function setFromHexStr(dialog, primary, shades)
local args <const> = dialog.data
local hexStr <const> = args.hexCode --[[@as string]]
local s = hexStr
if string.sub(s, 1, 1) == '#' then
s = string.sub(s, 2)
end
local r8, g8, b8, a8 = 0, 0, 0, 0
local sn <const> = tonumber(s, 16)
if sn then
local lens <const> = #s
if lens == 3 then
local r4 <const> = sn >> 0x8 & 0xf
local g4 <const> = sn >> 0x4 & 0xf
local b4 <const> = sn & 0xf
r8 = r4 << 0x4 | r4
g8 = g4 << 0x4 | g4
b8 = b4 << 0x4 | b4
a8 = 255
elseif lens == 4 then
local r5 <const> = sn >> 0xb & 0x1f
local g6 <const> = sn >> 0x5 & 0x3f
local b5 <const> = sn & 0x1f
r8 = math.floor(r5 * 255.0 / 31.0 + 0.5)
g8 = math.floor(g6 * 255.0 / 63.0 + 0.5)
b8 = math.floor(b5 * 255.0 / 31.0 + 0.5)
a8 = 255
elseif lens == 6 then
r8 = sn >> 0x10 & 0xff
g8 = sn >> 0x08 & 0xff
b8 = sn & 0xff
a8 = 255
elseif lens >= 8 then
r8 = sn >> 0x18 & 0xff
g8 = sn >> 0x10 & 0xff
b8 = sn >> 0x08 & 0xff
a8 = sn & 0xff
end
end
if a8 > 0 then
-- Add a previous and mix with previous.
primary = Color { r = r8, g = g8, b = b8, a = a8 }
dialog:modify { id = "baseColor", colors = { primary } }
dialog:modify { id = "alpha", value = a8 }
local r01 <const>, g01 <const>, b01 <const> = aseColorToRgb01(primary)
local l <const>, a <const>, b <const> = ok_color.srgb_to_oklab(r01, g01, b01)
setLab(dialog, l, a, b)
setHsl(dialog, ok_color.oklab_to_okhsl(l, a, b))
setHsv(dialog, ok_color.oklab_to_okhsv(l, a, b))
updateHarmonies(dialog, primary)
updateShades(dialog, shades)
end
end
---@param dialog Dialog
---@param aseColor Color
---@param primary Color
---@param shades Color[]
local function setFromAse(dialog, aseColor, primary, shades)
primary = copyColorByValue(aseColor)
dialog:modify { id = "baseColor", colors = { primary } }
dialog:modify { id = "alpha", value = primary.alpha }
dialog:modify { id = "hexCode", text = colorToHexWeb(primary) }
local r01 <const>, g01 <const>, b01 <const> = aseColorToRgb01(primary)
local l <const>, a <const>, b <const> = ok_color.srgb_to_oklab(r01, g01, b01)
setLab(dialog, l, a, b)
setHsl(dialog, ok_color.oklab_to_okhsl(l, a, b))
setHsv(dialog, ok_color.oklab_to_okhsv(l, a, b))
updateHarmonies(dialog, primary)
updateShades(dialog, shades)
end
---@param dialog Dialog
---@param primary Color
---@param shades Color[]
local function updateColor(dialog, primary, shades)
local args <const> = dialog.data
local alpha <const> = args.alpha --[[@as integer]]
local colorMode <const> = args.colorMode --[[@as string]]
if colorMode == "HSV" then
local hsvHue <const> = args.hsvHue --[[@as integer]]
local hsvSat <const> = args.hsvSat --[[@as integer]]
local hsvVal <const> = args.hsvVal --[[@as integer]]
local l <const>, a <const>, b <const> = ok_color.okhsv_to_oklab(
hsvHue / 360.0, hsvSat / 255.0, hsvVal / 255.0)
local r01 <const>, g01 <const>, b01 <const> = ok_color.oklab_to_srgb(l, a, b)
primary = srgb01ToAseColor(r01, g01, b01, alpha)
-- Update other color sliders.
local hHsl <const>, sHsl <const>, lHsl <const> = ok_color.oklab_to_okhsl(l, a, b)
setHsl(dialog, hHsl, sHsl, lHsl)
setLab(dialog, l, a, b)
elseif colorMode == "LAB" then
local labLgt <const> = args.labLgt --[[@as integer]]
local labA <const> = args.labA --[[@as integer]]
local labB <const> = args.labB --[[@as integer]]
local l <const> = labLgt / 255.0
local a <const> = labA * 0.001
local b <const> = labB * 0.001
local r01 <const>, g01 <const>, b01 <const> = ok_color.oklab_to_srgb(l, a, b)
primary = srgb01ToAseColor(r01, g01, b01, alpha)
-- Update other color sliders.
local hHsl <const>, sHsl <const>, lHsl <const> = ok_color.oklab_to_okhsl(l, a, b)
local hHsv <const>, sHsv <const>, vHsv <const> = ok_color.oklab_to_okhsv(l, a, b)
setHsl(dialog, hHsl, sHsl, lHsl)
setHsv(dialog, hHsv, sHsv, vHsv)
else
local hslHue <const> = args.hslHue --[[@as integer]]
local hslSat <const> = args.hslSat --[[@as integer]]
local hslLgt <const> = args.hslLgt --[[@as integer]]
local l <const>, a <const>, b <const> = ok_color.okhsl_to_oklab(
hslHue / 360.0, hslSat / 255.0, hslLgt / 255.0)
local r01 <const>, g01 <const>, b01 <const> = ok_color.oklab_to_srgb(l, a, b)
primary = srgb01ToAseColor(r01, g01, b01, alpha)
-- Update other color sliders.
local hHsv <const>, sHsv <const>, vHsv <const> = ok_color.oklab_to_okhsv(l, a, b)
setHsv(dialog, hHsv, sHsv, vHsv)
setLab(dialog, l, a, b)
end
dialog:modify {
id = "baseColor",
colors = { primary }
}
dialog:modify {
id = "hexCode",
text = colorToHexWeb(primary)
}
updateHarmonies(dialog, primary)
updateShades(dialog, shades)
end
local colorModes <const> = { "HSL", "HSV", "LAB" }
local harmonies <const> = {
"ANALOGOUS",
"COMPLEMENT",
"NONE",
"SHADING",
"SPLIT",
"SQUARE",
"TRIADIC"
}
local primary <const> = Color { r = 255, g = 0, b = 0 }
local shades <const> = {
Color { r = 113, g = 9, b = 30 },
Color { r = 148, g = 21, b = 43 },
Color { r = 183, g = 37, b = 54 },
Color { r = 214, g = 62, b = 62 },
Color { r = 234, g = 99, b = 78 },
Color { r = 244, g = 139, b = 104 },
Color { r = 248, g = 178, b = 139 }
}
local dlg <const> = Dialog { title = "OkHsl Color Picker" }
dlg:button {
id = "fgGet",
label = "Get:",
text = "&FORE",
focus = false,
onclick = function()
setFromAse(dlg, app.fgColor, primary, shades)
end
}
dlg:button {
id = "bgGet",
text = "&BACK",
focus = false,
onclick = function()
app.command.SwitchColors()
setFromAse(dlg, app.fgColor, primary, shades)
app.command.SwitchColors()
end
}
dlg:newrow { always = false }
dlg:entry {
id = "hexCode",
label = "Hex: #",
text = "ff0000",
focus = false,
onchange = function()
setFromHexStr(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:shades {
id = "baseColor",
label = "Color:",
mode = "pick",
colors = { defaults.base },
onclick = function(ev)
local button <const> = ev.button
if button == MouseButton.LEFT then
app.fgColor = assignColor(ev.color)
elseif button == MouseButton.RIGHT then
app.command.SwitchColors()
app.fgColor = assignColor(ev.color)
app.command.SwitchColors()
end
end
}
dlg:newrow { always = false }
dlg:combobox {
id = "colorMode",
label = "Mode:",
option = defaults.colorMode,
options = colorModes,
onchange = function()
local args <const> = dlg.data
local colorMode <const> = args.colorMode --[[@as string]]
local isHsl = colorMode == "HSL"
dlg:modify { id = "hslHue", visible = isHsl }
dlg:modify { id = "hslSat", visible = isHsl }
dlg:modify { id = "hslLgt", visible = isHsl }
local isHsv = colorMode == "HSV"
dlg:modify { id = "hsvHue", visible = isHsv }
dlg:modify { id = "hsvSat", visible = isHsv }
dlg:modify { id = "hsvVal", visible = isHsv }
local isLab = colorMode == "LAB"
dlg:modify { id = "labLgt", visible = isLab }
dlg:modify { id = "labA", visible = isLab }
dlg:modify { id = "labB", visible = isLab }
local showWheel <const> = args.showWheelSettings --[[@as boolean]]
local hslAxis <const> = args.hslAxis --[[@as string]]
local hsvAxis <const> = args.hsvAxis --[[@as string]]
local isLight <const> = hslAxis == "LIGHTNESS"
local isValue <const> = hsvAxis == "VALUE"
local isSat = false
if isHsl or isLab then
isSat = hslAxis == "SATURATION"
elseif isHsv then
isSat = hsvAxis == "SATURATION"
end
dlg:modify { id = "hslAxis", visible = (isHsl or isLab) and showWheel }
dlg:modify { id = "minLight", visible = (isHsl or isLab) and showWheel and isLight }
dlg:modify { id = "maxLight", visible = (isHsl or isLab) and showWheel and isLight }
dlg:modify { id = "hsvAxis", visible = isHsv and showWheel }
dlg:modify { id = "minValue", visible = isHsv and showWheel and isValue }
dlg:modify { id = "maxValue", visible = isHsv and showWheel and isValue }
dlg:modify { id = "minSat", visible = showWheel and isSat }
dlg:modify { id = "maxSat", visible = showWheel and isSat }
local showGradient <const> = args.showGradientSettings --[[@as boolean]]
if showGradient then
dlg:modify { id = "hueDir", visible = not isLab }
end
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hslHue",
label = "Hue:",
min = 0,
max = 360,
value = defaults.hslHue,
visible = defaults.colorMode == "HSL",
onchange = function()
updateColor(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hslSat",
label = "Saturation:",
min = 0,
max = 255,
value = defaults.hslSat,
visible = defaults.colorMode == "HSL",
onchange = function()
updateColor(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hslLgt",
label = "Lightness:",
min = 0,
max = 255,
value = defaults.hslLgt,
visible = defaults.colorMode == "HSL",
onchange = function()
updateColor(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hsvHue",
label = "Hue:",
min = 0,
max = 360,
value = defaults.hsvHue,
visible = defaults.colorMode == "HSV",
onchange = function()
updateColor(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hsvSat",
label = "Saturation:",
min = 0,
max = 255,
value = defaults.hsvSat,
visible = defaults.colorMode == "HSV",
onchange = function()
updateColor(dlg, primary, shades)
end
}
dlg:newrow { always = false }
dlg:slider {
id = "hsvVal",
label = "Value:",
min = 0,
max = 255,