-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ok_picker.lua
2244 lines (1905 loc) · 71.6 KB
/
ok_picker.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 axes <const> = { "LIGHTNESS", "SATURATION" }
local harmonyTypes <const> = {
"ANALOGOUS",
"COMPLEMENT",
"NONE",
"SHADING",
"SPLIT",
"SQUARE",
"TETRADIC",
"TRIADIC"
}
local huePresets <const> = { "CCW", "CW", "FAR", "NEAR" }
local tau <const> = 6.2831853071796
local oneTau <const> = 0.1591549430919
local sqrt32 <const> = 0.86602540378444
local abs <const> = math.abs
local ceil <const> = math.ceil
local floor <const> = math.floor
local max <const> = math.max
local min <const> = math.min
local screenScale = 1
if app.preferences then
local generalPrefs <const> = app.preferences.general
if generalPrefs then
local ssCand <const> = generalPrefs.screen_scale --[[@as integer]]
if ssCand and ssCand > 0 then
screenScale = ssCand
end
end
end
local defaults <const> = {
-- TODO: Ultimately, this would work better as a single canvas,
-- not muliple ones, unless you can figure out how to make them
-- not rescale.
wCanvas = max(16, 200 // screenScale),
hCanvasAxis = max(6, 12 // screenScale),
hCanvasAlpha = max(6, 12 // screenScale),
hCanvasCircle = max(16, 200 // screenScale),
hCanvasHarmony = max(6, 12 // screenScale),
aCheck = 0.5,
bCheck = 0.8,
wCheck = max(1, 6 // screenScale),
hCheck = max(1, 6 // screenScale),
reticleSize = max(3, 8 // screenScale),
reticleStroke = max(1, 1 // screenScale),
harmonyReticleSize = max(2, 4 // screenScale),
harmonyReticleStroke = 1,
swatchSize = max(4, 17 // screenScale),
textDisplayLimit = 50,
radiansOffset = math.rad(60),
useBack = false,
useSat = false,
satAxis = 1.0,
lightAxis = 0.5,
harmonyType = "NONE",
showHarmonyOnWheel = true,
shadingCount = 7,
shadowLight = 0.1,
dayLight = 0.9,
shadowHue = 291.0 / 360.0,
dayHue = 96.0 / 360.0,
hYel = 109.79 / 360.0,
srcLightWeight = 0.3333333333333333,
greenHue = 146.0 / 360.0,
minGreenOffset = 0.3,
maxGreenOffset = 0.6,
minChroma = 0.01,
lgtDesatFac = 0.75,
shdDesatFac = 0.75,
foreKey = "FO&RE",
backKey = "&BACK",
sampleKey = "S&LE",
gradientKey = "&GRADIENT",
optionsKey = "&+",
exitKey = "&X",
showForeButton = true,
showBackButton = true,
showSampleButton = false,
showGradientButton = false,
showExitButton = true,
swatchCount = 5,
huePreset = "NEAR",
rBitDepth = 8,
gBitDepth = 8,
bBitDepth = 8,
tBitDepth = 8,
keyShiftAmount = 0.01,
hLevels = 24,
sLevels = 12,
lLevels = 12,
}
local active <const> = {
radiansOffset = defaults.radiansOffset,
useSat = defaults.useSat,
wCanvasCircle = defaults.wCanvas,
hCanvasCircle = defaults.hCanvasCircle,
triggerCircleRepaint = true,
byteStrCircle = "",
wCanvasAxis = defaults.wCanvas,
hCanvasAxis = defaults.hCanvasAxis,
triggerAxisRepaint = true,
byteStrAxis = "",
wCanvasAlpha = defaults.wCanvas,
hCanvasAlpha = defaults.hCanvasAlpha,
triggerAlphaRepaint = true,
byteStrAlpha = "",
wCanvasHarmony = defaults.wCanvas,
hCanvasHarmony = defaults.hCanvasHarmony,
triggerHarmonyRepaint = false,
byteStrHarmony = "",
harmonyType = defaults.harmonyType,
showHarmonyOnWheel = defaults.showHarmonyOnWheel,
useBack = defaults.useBack,
satAxis = defaults.satAxis,
lightAxis = defaults.lightAxis,
rBitDepth = defaults.rBitDepth,
gBitDepth = defaults.gBitDepth,
bBitDepth = defaults.bBitDepth,
tBitDepth = defaults.tBitDepth,
hueFore = 0.0,
satFore = 1.0,
lightFore = 0.5,
redFore = 1.0,
greenFore = 1.0,
blueFore = 1.0,
alphaFore = 1.0,
hueBack = 0.5,
satBack = 1.0,
lightBack = 0.5,
redBack = 0.0,
greenBack = 0.0,
blueBack = 0.0,
alphaBack = 1.0,
hLevels = defaults.hLevels,
sLevels = defaults.sLevels,
lLevels = defaults.lLevels,
}
---@param a number value
---@param levels number levels
---@param delta number inverse levels
---@return number
---@nodiscard
local function quantizeSignedInternal(a, levels, delta)
return floor(0.5 + a * levels) * delta
end
---@param a number value
---@param levels number levels
---@return number
---@nodiscard
local function quantizeSigned(a, levels)
if levels ~= 0 then
return quantizeSignedInternal(
a, levels, 1.0 / levels)
end
return a
end
---@param a number value
---@param levels number levels
---@param delta number inverse levels
---@return number
---@nodiscard
local function quantizeUnsignedInternal(a, levels, delta)
return max(0.0, (ceil(a * levels) - 1.0) * delta)
end
---@param a number value
---@param levels number levels
---@return number
---@nodiscard
local function quantizeUnsigned(a, levels)
if levels > 1 then
return quantizeUnsignedInternal(
a, levels, 1.0 / (levels - 1.0))
end
return max(0.0, a)
end
---@param a number
---@param b number
---@param range number
---@return number
---@nodiscard
local function distAngleUnsigned(a, b, range)
local halfRange <const> = range * 0.5
return halfRange - abs(abs(
(b % range) - (a % range))
- halfRange)
end
---@param orig number origin angle
---@param dest number destination angle
---@param t number factor
---@param range number? range
---@return number
---@nodiscard
local function lerpAngleCcw(orig, dest, t, range)
local valRange <const> = range or 360.0
local o <const> = orig % valRange
local d <const> = dest % valRange
local diff <const> = d - o
if diff == 0.0 then return o end
local u <const> = 1.0 - t
if o > d then
return (u * o + t * (d + valRange)) % valRange
else
return u * o + t * d
end
end
---@param orig number origin angle
---@param dest number destination angle
---@param t number factor
---@param range number? range
---@return number
---@nodiscard
local function lerpAngleCw(orig, dest, t, range)
local valRange <const> = range or 360.0
local o <const> = orig % valRange
local d <const> = dest % valRange
local diff <const> = d - o
if diff == 0.0 then return d end
local u <const> = 1.0 - t
if o < d then
return (u * (o + valRange) + t * d) % valRange
else
return u * o + t * d
end
end
---@param orig number origin angle
---@param dest number destination angle
---@param t number factor
---@param range number? range
---@return number
---@nodiscard
local function lerpAngleFar(orig, dest, t, range)
local valRange <const> = range or 360.0
local halfRange <const> = valRange * 0.5
local o <const> = orig % valRange
local d <const> = dest % valRange
local diff <const> = d - o
local u <const> = 1.0 - t
if diff == 0.0 or (o < d and diff < halfRange) then
return (u * (o + valRange) + t * d) % valRange
elseif o > d and diff > -halfRange then
return (u * o + t * (d + valRange)) % valRange
else
return u * o + t * d
end
end
---@param orig number origin angle
---@param dest number destination angle
---@param t number factor
---@param range number? range
---@return number
---@nodiscard
local function lerpAngleNear(orig, dest, t, range)
local valRange <const> = range or 360.0
local o <const> = orig % valRange
local d <const> = dest % valRange
local diff <const> = d - o
if diff == 0.0 then return o end
local u <const> = 1.0 - t
local halfRange <const> = valRange * 0.5
if o < d and diff > halfRange then
return (u * (o + valRange) + t * d) % valRange
elseif o > d and diff < -halfRange then
return (u * o + t * (d + valRange)) % valRange
else
return u * o + t * d
end
end
---@param t number
---@return number
---@nodiscard
local function zigZag(t)
local a <const> = t * 0.5
local b <const> = a - floor(a)
return 1.0 - abs(b + b - 1.0)
end
---@param h number hue
---@param s number saturation
---@param l number lightness
---@return integer r8
---@return integer g8
---@return integer b8
---@return number r01
---@return number g01
---@return number b01
local function okhslToRgb24(h, s, l)
local r01 <const>, g01 <const>, b01 <const> = ok_color.okhsl_to_srgb(
h, s, l)
-- Values still go out of gamut, particularly for
-- saturated blues at medium light.
local r01cl = min(max(r01, 0), 1)
local g01cl = min(max(g01, 0), 1)
local b01cl = min(max(b01, 0), 1)
local r8 <const> = floor(r01cl * 255 + 0.5)
local g8 <const> = floor(g01cl * 255 + 0.5)
local b8 <const> = floor(b01cl * 255 + 0.5)
return r8, g8, b8, r01cl, g01cl, b01cl
end
---@param event { context: GraphicsContext }
local function onPaintAlpha(event)
local ctx <const> = event.context
ctx.antialias = false
ctx.blendMode = BlendMode.SRC
local wCanvas <const> = ctx.width
local hCanvas <const> = ctx.height
if wCanvas <= 1 or hCanvas <= 1 then return end
local useBack <const> = active.useBack
local alphaActive <const> = useBack
and active.alphaBack
or active.alphaFore
local needsRepaint <const> = active.triggerAlphaRepaint
or active.wCanvasAlpha ~= wCanvas
or active.hCanvasAlpha ~= hCanvas
active.wCanvasAlpha = wCanvas
active.hCanvasAlpha = hCanvas
if needsRepaint then
---@type string[]
local byteStrs <const> = {}
local strpack <const> = string.pack
local redActive <const> = useBack
and active.redBack
or active.redFore
local greenActive <const> = useBack
and active.greenBack
or active.greenFore
local blueActive <const> = useBack
and active.blueBack
or active.blueFore
local wCheck <const> = defaults.wCheck
local hCheck <const> = defaults.hCheck
-- local aCheck <const> = 0x80 / 0xff
-- local bCheck <const> = 0xca / 0xff
local aCheck <const> = defaults.aCheck
local bCheck <const> = defaults.bCheck
local xToFac <const> = 1.0 / (wCanvas - 1.0)
local lenCanvas <const> = wCanvas * hCanvas
local i = 0
while i < lenCanvas do
local y <const> = i // wCanvas
local x <const> = i % wCanvas
local t <const> = x * xToFac
local cCheck = bCheck
if (((x // wCheck) + (y // hCheck)) % 2) ~= 1 then
cCheck = aCheck
end
local ucCheck <const> = (1.0 - t) * cCheck
local rMix <const> = ucCheck + t * redActive
local gMix <const> = ucCheck + t * greenActive
local bMix <const> = ucCheck + t * blueActive
local r8 <const> = floor(rMix * 255 + 0.5)
local g8 <const> = floor(gMix * 255 + 0.5)
local b8 <const> = floor(bMix * 255 + 0.5)
local byteStr <const> = strpack("B B B B", r8, g8, b8, 255)
i = i + 1
byteStrs[i] = byteStr
end -- End image loop.
active.byteStrAlpha = table.concat(byteStrs)
active.triggerAlphaRepaint = false
end -- End needs repaint.
-- Draw alpha canvas.
local imgSpec <const> = ImageSpec {
width = wCanvas,
height = hCanvas,
transparentColor = 0,
colorMode = ColorMode.RGB
}
local img <const> = Image(imgSpec)
img.bytes = active.byteStrAlpha
local drawRect <const> = Rectangle(0, 0, wCanvas, hCanvas)
ctx:drawImage(img, drawRect, drawRect)
local xReticle <const> = floor(alphaActive * (wCanvas - 1.0) + 0.5)
local yReticle <const> = hCanvas // 2
local reticleSize <const> = defaults.reticleSize
local reticleHalf <const> = reticleSize // 2
local reticleColor <const> = Color(255, 255, 255, 255)
ctx.color = reticleColor
ctx.strokeWidth = defaults.reticleStroke
ctx:strokeRect(Rectangle(
xReticle - reticleHalf, yReticle - reticleHalf,
reticleSize, reticleSize))
end
---@param event { context: GraphicsContext }
local function onPaintAxis(event)
local ctx <const> = event.context
ctx.antialias = false
ctx.blendMode = BlendMode.SRC
local wCanvas <const> = ctx.width
local hCanvas <const> = ctx.height
if wCanvas <= 1 or hCanvas <= 1 then return end
local useSat <const> = active.useSat
local useBack <const> = active.useBack
local satAxis <const> = active.satAxis
local lightAxis <const> = active.lightAxis
local hueFore <const> = active.hueFore
local satFore <const> = active.satFore
local lightFore <const> = active.lightFore
local hueBack <const> = active.hueBack
local satBack <const> = active.satBack
local lightBack <const> = active.lightBack
local hueActive <const> = useBack and hueBack or hueFore
local satActive <const> = useBack and satBack or satFore
local lightActive <const> = useBack and lightBack or lightFore
local needsRepaint <const> = active.triggerAxisRepaint
or active.wCanvasAxis ~= wCanvas
or active.hCanvasAxis ~= hCanvas
or (useSat and satAxis ~= satActive
or lightAxis ~= lightActive)
active.wCanvasAxis = wCanvas
active.hCanvasAxis = hCanvas
if needsRepaint then
---@type string[]
local byteStrs <const> = {}
local strpack <const> = string.pack
local xToFac <const> = 1.0 / (wCanvas - 1.0)
local x = 0
while x < wCanvas do
local fac <const> = x * xToFac
local xs <const> = useSat and fac or satActive
local xl <const> = useSat and lightActive or fac
local r8 <const>, g8 <const>, b8 <const>,
_ <const>, _ <const>, _ <const> = okhslToRgb24(
hueActive, xs, xl)
local byteStr <const> = strpack("B B B B", r8, g8, b8, 255)
x = x + 1
byteStrs[x] = byteStr
end -- End image loop.
active.byteStrAxis = table.concat(byteStrs)
active.triggerAxisRepaint = false
end -- End needs repaint.
-- Draw axis canvas.
local imgSpec <const> = ImageSpec {
width = wCanvas,
height = 1,
transparentColor = 0,
colorMode = ColorMode.RGB
}
local img <const> = Image(imgSpec)
img.bytes = active.byteStrAxis
ctx:drawImage(img,
Rectangle(0, 0, wCanvas, 1),
Rectangle(0, 0, wCanvas, hCanvas))
-- Draw reticle.
local x01 <const> = useSat and satAxis or lightAxis
local xReticle <const> = floor(x01 * (wCanvas - 1.0) + 0.5)
local yReticle <const> = hCanvas // 2
local reticleSize <const> = defaults.reticleSize
local reticleHalf <const> = reticleSize // 2
local comparisand <const> = useSat and lightActive or lightAxis
local aseWhite <const> = Color { r = 255, g = 255, b = 255, a = 255 }
local aseBlack <const> = Color { r = 0, g = 0, b = 0, a = 255 }
local reticleColor <const> = comparisand < 0.5
and aseWhite
or aseBlack
ctx.color = reticleColor
ctx.strokeWidth = defaults.reticleStroke
ctx:strokeRect(Rectangle(
xReticle - reticleHalf, yReticle - reticleHalf,
reticleSize, reticleSize))
if (not useSat)
and active.showHarmonyOnWheel then
local harmonyType <const> = active.harmonyType
local harmRetSize = defaults.harmonyReticleSize
local harmRetHalf = harmRetSize // 2
local harmRetColor <const> = lightActive < 0.5
and aseBlack
or aseWhite
ctx.color = harmRetColor
ctx.strokeWidth = defaults.harmonyReticleStroke
if harmonyType == "ANALOGOUS" then
local lAna <const> = (lightActive * 2.0 + 0.5) / 3.0
ctx:strokeRect(Rectangle(
floor(lAna * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
elseif harmonyType == "COMPLEMENT" then
local lCmp <const> = 1.0 - lightActive
ctx:strokeRect(Rectangle(
floor(lCmp * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
elseif harmonyType == "SPLIT" then
local lSpl <const> = (2.5 - lightActive * 2.0) / 3.0
ctx:strokeRect(Rectangle(
floor(lSpl * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
elseif harmonyType == "SQUARE" then
local lCmp <const> = 1.0 - lightActive
local lSqr <const> = 0.5
ctx:strokeRect(Rectangle(
floor(lCmp * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
ctx:strokeRect(Rectangle(
floor(lSqr * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
elseif harmonyType == "TETRADIC" then
local lTri <const> = (2.0 - lightActive) / 3.0
local lCmp <const> = 1.0 - lightActive
local lTet <const> = (1.0 + lightActive) / 3.0
ctx:strokeRect(Rectangle(
floor(lTri * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
ctx:strokeRect(Rectangle(
floor(lCmp * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
ctx:strokeRect(Rectangle(
floor(lTet * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
elseif harmonyType == "TRIADIC" then
local lTri <const> = (2.0 - lightActive) / 3.0
ctx:strokeRect(Rectangle(
floor(lTri * (wCanvas - 1.0) + 0.5) - harmRetHalf,
yReticle - harmRetHalf,
harmRetSize, harmRetSize))
end
end
end
---@param event { context: GraphicsContext }
local function onPaintCircle(event)
local ctx <const> = event.context
ctx.antialias = false
ctx.blendMode = BlendMode.SRC
local wCanvas <const> = ctx.width
local hCanvas <const> = ctx.height
if wCanvas <= 1 or hCanvas <= 1 then return end
local needsRepaint <const> = active.triggerCircleRepaint
or active.wCanvasCircle ~= wCanvas
or active.hCanvasCircle ~= hCanvas
active.wCanvasCircle = wCanvas
active.hCanvasCircle = hCanvas
local xCenter <const> = wCanvas * 0.5
local yCenter <const> = hCanvas * 0.5
local shortEdge <const> = min(wCanvas, hCanvas)
local radiusCanvas <const> = (shortEdge - 1.0) * 0.5
local themeColors <const> = app.theme.color
local radiansOffset <const> = active.radiansOffset
local useSat <const> = active.useSat
local useBack <const> = active.useBack
local hueFore <const> = active.hueFore
local satFore <const> = active.satFore
local lightFore <const> = active.lightFore
local hueBack <const> = active.hueBack
local satBack <const> = active.satBack
local lightBack <const> = active.lightBack
local hueActive <const> = useBack and hueBack or hueFore
local satActive <const> = useBack and satBack or satFore
local lightActive <const> = useBack and lightBack or lightFore
if needsRepaint then
---@type string[]
local byteStrs <const> = {}
-- Cache method used in while loop.
local strpack <const> = string.pack
local atan2 <const> = math.atan
local sqrt <const> = math.sqrt
-- local diamCanvasInv <const> = 1.0 / diamCanvas
local radiusCanvasInv <const> = 1.0 / radiusCanvas
local bkgColor <const> = themeColors.window_face
local packZero <const> = strpack("B B B B",
bkgColor.red, bkgColor.green, bkgColor.blue, 255)
local satAxis <const> = active.satAxis
local lightAxis <const> = active.lightAxis
local lenCanvas <const> = wCanvas * hCanvas
local i = 0
while i < lenCanvas do
local x <const> = (i % wCanvas - xCenter) * radiusCanvasInv
local y <const> = (yCenter - i // wCanvas) * radiusCanvasInv
local sqMag <const> = x * x + y * y
local byteStr = packZero
if sqMag <= 1.0 then
local radiansSigned <const> = atan2(y, x)
local rSgnOffset <const> = radiansSigned + radiansOffset
local rUnsigned <const> = rSgnOffset % tau
local hue <const> = rUnsigned * oneTau
-- If you want to support quantize, use signed q here.
local mag <const> = sqrt(sqMag)
local light <const> = useSat and 1.0 - mag or lightAxis
local sat <const> = useSat and satAxis or mag
-- If you want to support quantize, use unsigned q here.
local r8 <const>, g8 <const>, b8 <const>,
_ <const>, _ <const>, _ <const> = okhslToRgb24(
hue, sat, light)
byteStr = strpack("B B B B", r8, g8, b8, 255)
end -- End within circle.
i = i + 1
byteStrs[i] = byteStr
end -- End image loop.
active.byteStrCircle = table.concat(byteStrs)
active.triggerCircleRepaint = false
end -- End needs repaint.
-- Draw picker canvas.
local imgSpec <const> = ImageSpec {
width = wCanvas,
height = hCanvas,
transparentColor = 0,
colorMode = ColorMode.RGB
}
local img <const> = Image(imgSpec)
img.bytes = active.byteStrCircle
local drawRect <const> = Rectangle(0, 0, wCanvas, hCanvas)
ctx:drawImage(img, drawRect, drawRect)
local swatchSize <const> = defaults.swatchSize
local offset <const> = swatchSize // 2
local rBitDepth <const> = active.rBitDepth
local gBitDepth <const> = active.gBitDepth
local bBitDepth <const> = active.bBitDepth
local rLevels <const> = 1 << rBitDepth
local gLevels <const> = 1 << gBitDepth
local bLevels <const> = 1 << bBitDepth
local redBack <const> = active.redBack
local greenBack <const> = active.greenBack
local blueBack <const> = active.blueBack
local redFore <const> = active.redFore
local greenFore <const> = active.greenFore
local blueFore <const> = active.blueFore
-- Draw background color swatch.
ctx.color = Color {
-- r = floor(quantizeUnsigned(redBack, rLevels) * 255 + 0.5),
-- g = floor(quantizeUnsigned(greenBack, gLevels) * 255 + 0.5),
-- b = floor(quantizeUnsigned(blueBack, bLevels) * 255 + 0.5),
r = floor(redBack * 255 + 0.5),
g = floor(greenBack * 255 + 0.5),
b = floor(blueBack * 255 + 0.5),
a = 255
}
ctx:fillRect(Rectangle(
offset, hCanvas - swatchSize - 1,
swatchSize, swatchSize))
-- Draw foreground color swatch.
ctx.color = Color {
-- r = floor(quantizeUnsigned(redFore, rLevels) * 255 + 0.5),
-- g = floor(quantizeUnsigned(greenFore, gLevels) * 255 + 0.5),
-- b = floor(quantizeUnsigned(blueFore, bLevels) * 255 + 0.5),
r = floor(redFore * 255 + 0.5),
g = floor(greenFore * 255 + 0.5),
b = floor(blueFore * 255 + 0.5),
a = 255
}
ctx:fillRect(Rectangle(
0, hCanvas - swatchSize - 1 - offset,
swatchSize, swatchSize))
-- Draw reticle.
local magActive <const> = useSat
and 1.0 - lightActive
or satActive
local magCanvas <const> = magActive * radiusCanvas
local radiansActive <const> = hueActive * tau - radiansOffset
local xReticle <const> = math.cos(radiansActive)
local yReticle <const> = math.sin(radiansActive)
local reticleSize <const> = defaults.reticleSize
local reticleHalf <const> = reticleSize // 2
local aseWhite <const> = Color { r = 255, g = 255, b = 255, a = 255 }
local aseBlack <const> = Color { r = 0, g = 0, b = 0, a = 255 }
local reticleColor <const> = lightActive < 0.5
and aseWhite
or aseBlack
ctx.color = reticleColor
ctx.strokeWidth = defaults.reticleStroke
ctx:strokeRect(Rectangle(
(xCenter + xReticle * magCanvas) - reticleHalf,
(yCenter - yReticle * magCanvas) - reticleHalf,
reticleSize, reticleSize))
-- Draw harmony reticles.
local harmonyType <const> = active.harmonyType
local showHarmonyOnWheel <const> = active.showHarmonyOnWheel
if showHarmonyOnWheel
and harmonyType ~= "NONE"
and harmonyType ~= "SHADING" then
local harmRetSize = defaults.harmonyReticleSize
local harmRetHalf = harmRetSize // 2
---@type Point[]
local pts <const> = {}
if harmonyType == "ANALOGOUS" then
-- 30, 330 degrees
local rt32x <const> = sqrt32 * xReticle
local rt32y <const> = sqrt32 * yReticle
local halfx <const> = 0.5 * xReticle
local halfy <const> = 0.5 * yReticle
local xAna0 <const>, yAna0 <const> = rt32x - halfy, rt32y + halfx
local xAna1 <const>, yAna1 <const> = rt32x + halfy, rt32y - halfx
local lAna <const> = useSat
and ((magActive * 2.0 + 0.5) / 3.0) * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter + xAna0 * lAna) - harmRetHalf,
(yCenter - yAna0 * lAna) - harmRetHalf)
pts[2] = Point(
(xCenter + xAna1 * lAna) - harmRetHalf,
(yCenter - yAna1 * lAna) - harmRetHalf)
elseif harmonyType == "COMPLEMENT" then
-- 180 degrees
local lCmp <const> = useSat
and (1.0 - magActive) * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter - xReticle * lCmp) - harmRetHalf,
(yCenter + yReticle * lCmp) - harmRetHalf)
elseif harmonyType == "SPLIT" then
-- 150, 210 degrees
local rt32x <const> = -sqrt32 * xReticle
local rt32y <const> = -sqrt32 * yReticle
local halfx <const> = 0.5 * xReticle
local halfy <const> = 0.5 * yReticle
local xSpl0 <const>, ySpl0 <const> = rt32x - halfy, rt32y + halfx
local xSpl1 <const>, ySpl1 <const> = rt32x + halfy, rt32y - halfx
local lSpl <const> = useSat
and ((2.5 - magActive * 2.0) / 3.0) * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter + xSpl0 * lSpl) - harmRetHalf,
(yCenter - ySpl0 * lSpl) - harmRetHalf)
pts[2] = Point(
(xCenter + xSpl1 * lSpl) - harmRetHalf,
(yCenter - ySpl1 * lSpl) - harmRetHalf)
elseif harmonyType == "SQUARE" then
-- 90, 180, 270 degrees
local lCmp <const> = useSat
and (1.0 - magActive) * radiusCanvas
or magCanvas
local lSqr <const> = useSat
and 0.5 * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter - yReticle * lSqr) - harmRetHalf,
(yCenter - xReticle * lSqr) - harmRetHalf)
pts[2] = Point(
(xCenter - xReticle * lCmp) - harmRetHalf,
(yCenter + yReticle * lCmp) - harmRetHalf)
pts[3] = Point(
(xCenter + yReticle * lSqr) - harmRetHalf,
(yCenter + xReticle * lSqr) - harmRetHalf)
elseif harmonyType == "TETRADIC" then
-- 120, 300 degrees
local rt32x <const> = sqrt32 * xReticle
local rt32y <const> = sqrt32 * yReticle
local halfx <const> = 0.5 * xReticle
local halfy <const> = 0.5 * yReticle
local xTet0 <const>, yTet0 <const> = -halfx - rt32y, -halfy + rt32x
local xTet2 <const>, yTet2 <const> = halfx + rt32y, halfy - rt32x
local lTri <const> = useSat
and ((2.0 - magActive) / 3.0) * radiusCanvas
or magCanvas
local lCmp <const> = useSat
and (1.0 - magActive) * radiusCanvas
or magCanvas
local lTet <const> = useSat
and ((1.0 + magActive) / 3.0) * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter + xTet0 * lTri) - harmRetHalf,
(yCenter - yTet0 * lTri) - harmRetHalf)
pts[2] = Point(
(xCenter - xReticle * lCmp) - harmRetHalf,
(yCenter + yReticle * lCmp) - harmRetHalf)
pts[3] = Point(
(xCenter + xTet2 * lTet) - harmRetHalf,
(yCenter - yTet2 * lTet) - harmRetHalf)
elseif harmonyType == "TRIADIC" then
-- 120, 240 degrees
local rt32x <const> = sqrt32 * xReticle
local rt32y <const> = sqrt32 * yReticle
local halfx <const> = -0.5 * xReticle
local halfy <const> = -0.5 * yReticle
local xTri0 <const>, yTri0 <const> = halfx - rt32y, halfy + rt32x
local xTri1 <const>, yTri1 <const> = halfx + rt32y, halfy - rt32x
local lTri <const> = useSat
and ((2.0 - magActive) / 3.0) * radiusCanvas
or magCanvas
pts[1] = Point(
(xCenter + xTri0 * lTri) - harmRetHalf,
(yCenter - yTri0 * lTri) - harmRetHalf)
pts[2] = Point(
(xCenter + xTri1 * lTri) - harmRetHalf,
(yCenter - yTri1 * lTri) - harmRetHalf)
end
local harmRetColor <const> = lightActive < 0.5
and aseBlack
or aseWhite
ctx.color = harmRetColor
ctx.strokeWidth = defaults.harmonyReticleStroke
local lenPts <const> = #pts
local i = 0
while i < lenPts do
i = i + 1
local pt <const> = pts[i]
ctx:strokeRect(Rectangle(
pt.x, pt.y, harmRetSize, harmRetSize))
end
end
-- Draw diagnostic text.
if (wCanvas - hCanvas) > defaults.textDisplayLimit then
local textSize <const> = ctx:measureText("E")
local yIncr <const> = textSize.height + 4
local textColor <const> = themeColors.text
ctx.color = textColor
if lightActive > 0.0
and lightActive < 1.0 then
if satActive > 0.0 then
ctx:fillText(string.format(
"H: %.2f", hueActive * 360), 2, 2)
end
ctx:fillText(string.format(
"S: %.2f%%", satActive * 100), 2, 2 + yIncr)
end
ctx:fillText(string.format(
"L: %.2f%%", lightActive * 100), 2, 2 + yIncr * 2)
local redActive <const> = useBack
and redBack
or redFore
local greenActive <const> = useBack
and greenBack
or greenFore
local blueActive <const> = useBack
and blueBack
or blueFore
local alphaActive <const> = useBack
and active.alphaBack
or active.alphaFore
ctx:fillText(string.format(
"R: %.2f%%", redActive * 100), 2, 2 + yIncr * 4)
ctx:fillText(string.format(
"G: %.2f%%", greenActive * 100), 2, 2 + yIncr * 5)
ctx:fillText(string.format(
"B: %.2f%%", blueActive * 100), 2, 2 + yIncr * 6)
ctx:fillText(string.format(
"A: %.2f%%", alphaActive * 100), 2, 2 + yIncr * 8)
local bShift <const> = 0
local gShift <const> = bShift + bBitDepth
local rShift <const> = gShift + gBitDepth
local hexPad <const> = ceil((rShift + rBitDepth) * 0.25)
local hex <const> = floor(redActive * (rLevels - 1) + 0.5) << rShift
| floor(greenActive * (gLevels - 1) + 0.5) << gShift
| floor(blueActive * (bLevels - 1) + 0.5) << bShift
ctx:fillText(string.format("#%0" .. hexPad .. "X", hex),