-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaseTriPicker.lua
More file actions
2013 lines (1676 loc) · 60.8 KB
/
Copy pathaseTriPicker.lua
File metadata and controls
2013 lines (1676 loc) · 60.8 KB
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
local tau <const> = 6.2831853071796
local oneTau <const> = 0.1591549430919
local sqrt3_2 <const> = 0.86602540378444
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> = {
-- Might be nice to have an inner radius where the mouse stops responding
-- to hue ring shift... but it causes ring to jump when mouse moves from
-- deadzone back into into valid magnitude.
lockTriRot = false,
ringInEdge = 0.88,
angOffsetRadians = 0.5235987755983,
angLockRadians = 0.0,
wCanvasMain = math.max(16, 200 // screenScale),
hCanvasMain = math.max(16, 200 // screenScale),
wCanvasAlpha = math.max(16, 200 // screenScale),
hCanvasAlpha = math.max(8, 12 // screenScale),
wCanvasHex = math.max(16, 100 // screenScale),
hCanvasHex = math.max(4, 16 // screenScale),
reticleSize = math.max(3, 6 // screenScale),
reticleStroke = math.max(1, 1 // screenScale),
swatchSize = math.max(4, 17 // screenScale),
swatchMargin = math.max(3, 6 // screenScale),
showAlphaBar = false,
showForeButton = true,
showBackButton = true,
showPalAppendButton = false,
showSampleButton = false,
showHexButton = false,
showExitButton = true,
rBitDepth = 8,
gBitDepth = 8,
bBitDepth = 8,
bitDepthsNote = "Format: RGB888",
hue = 0.0,
sat = 1.0,
val = 1.0,
alpha = 1.0,
red = 1.0,
green = 0.0,
blue = 0.0,
hexCode = "000000",
textDisplayLimit = 50,
shiftLevels = 24,
foreKey = "&FORE",
backKey = "&BACK",
palAppendKey = "A&PPEND",
sampleKey = "S&LE",
hexKey = "&HEX",
optionsKey = "&+",
exitKey = "&X",
hueStep = 0.0013180565309174,
satStep = 1.0 / 255.0,
valStep = 1.0 / 255.0,
shiftScalar = 5.0,
-- https://developer.mozilla.org/en-US/docs/Web/
-- API/UI_Events/Keyboard_event_code_values
-- The issue with arrow keys is that when a selection is being
-- transformed, these inputs get eaten. Other keys, like WASD
-- and QE interfere with magic wand, etc. For now, saturation
-- nudging needs Alt key.
hueIncrKey = "ArrowRight",
hueDecrKey = "ArrowLeft",
satIncrKey = "ArrowUp",
satDecrKey = "ArrowDown",
valIncrKey = "ArrowUp",
valDecrKey = "ArrowDown",
aCheck = 0.25,
bCheck = 0.4,
wCheck = math.max(1, 6 // screenScale),
hCheck = math.max(1, 6 // screenScale),
}
local active <const> = {
lockTriRot = defaults.lockTriRot,
ringInEdge = defaults.ringInEdge,
angOffsetRadians = defaults.angOffsetRadians,
angLockRadians = 0.0,
wCanvasMain = defaults.wCanvasMain,
hCanvasMain = defaults.hCanvasMain,
triggerRingRepaint = true,
ringBytes = "",
triggerTriRepaint = true,
triBytes = "",
showAlphaBar = defaults.showAlphaBar,
wCanvasAlpha = defaults.wCanvasAlpha,
hCanvasAlpha = defaults.hCanvasAlpha,
triggerAlphaRepaint = true,
byteStrAlpha = "",
rBitDepth = 8,
gBitDepth = 8,
bBitDepth = 8,
rMax = 255.0,
gMax = 255.0,
bMax = 255.0,
hueFore = defaults.hue,
satFore = defaults.sat,
valFore = defaults.val,
hqFore = defaults.hue,
sqFore = defaults.sat,
vqFore = defaults.val,
redFore = defaults.red,
greenFore = defaults.green,
blueFore = defaults.blue,
alphaFore = defaults.alpha,
hueBack = defaults.hue + 0.5,
satBack = defaults.sat,
valBack = defaults.val,
hqBack = defaults.hue + 0.5,
sqBack = defaults.sat,
vqBack = defaults.val,
redBack = 1.0 - defaults.red,
greenBack = 1.0 - defaults.green,
blueBack = 1.0 - defaults.blue,
alphaBack = defaults.alpha,
isBackActive = false,
mouseDownRing = false,
mouseDownTri = false,
}
---@param r01 number
---@param g01 number
---@param b01 number
---@return string
local function rgbToHexStr(r01, g01, b01)
local rBitDepth <const> = active.rBitDepth
local gBitDepth <const> = active.gBitDepth
local bBitDepth <const> = active.bBitDepth
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
local is555 <const> = rBitDepth == 5
and gBitDepth == 5
and bBitDepth == 5
local shift0 <const> = is555 and 0 or bBitDepth + gBitDepth
local shift1 <const> = bBitDepth
local shift2 <const> = is555 and bBitDepth + gBitDepth or 0
local hex <const> = math.floor(r01 * rMax + 0.5) << shift0
| math.floor(g01 * gMax + 0.5) << shift1
| math.floor(b01 * bMax + 0.5) << shift2
local hexPad <const> = math.ceil((rBitDepth
+ gBitDepth
+ bBitDepth) * 0.25)
local hexStr <const> = string.format("%0" .. hexPad .. "X", hex)
return hexStr
end
---@param hexCode string
---@return boolean isValid
---@return number r
---@return number g
---@return number b
local function hexStrToRgb(hexCode)
local hexCodeVerif = hexCode
if string.sub(hexCode, 1, 1) == '#' then
hexCodeVerif = string.sub(hexCode, 2)
end
local hcParsed <const> = tonumber(hexCodeVerif, 16)
if not hcParsed then return false, 0.0, 0.0, 0.0 end
-- print(string.format("hcParsed: %08x", hcParsed))
local rBitDepth <const> = active.rBitDepth
local gBitDepth <const> = active.gBitDepth
local bBitDepth <const> = active.bBitDepth
-- print(string.format(
-- "rBitDepth: %d, gBitDepth: %d, bBitDepth: %d",
-- rBitDepth, gBitDepth, bBitDepth))
local rShift <const> = bBitDepth + gBitDepth
local gShift <const> = bBitDepth
local bShift <const> = 0
-- print(string.format(
-- "rShift: %d, gShift: %d, bShift: %d",
-- rShift, gShift, bShift))
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
-- print(string.format(
-- "rMax: %d, gMax: %d, bMax: %d",
-- rMax, gMax, bMax))
-- It's possible in, e.g., RGB555, to enter a code such as
-- #ABCD which exceeds the limit and will be truncated to #2BCD,
-- causing confusion.
-- local hcLimit <const> = (rMax << rShift)
-- | (gMax << gShift)
-- | (bMax << bShift)
local rx = (hcParsed >> rShift) & rMax
local gx <const> = (hcParsed >> gShift) & gMax
local bx = (hcParsed >> bShift) & bMax
local is555 <const> = rBitDepth == 5
and gBitDepth == 5
and bBitDepth == 5
if is555 then rx, bx = bx, rx end
-- print(string.format(
-- "rx: %d (0x%x), gx: %d (0x%x), bx: %d (0x%x)",
-- rx, rx, gx, gx, bx, bx))
local r01 <const> = rMax > 0.0 and rx / rMax or 0.0
local g01 <const> = gMax > 0.0 and gx / gMax or 0.0
local b01 <const> = bMax > 0.0 and bx / bMax or 0.0
-- print(string.format(
-- "r01: %03f, g01: %03f, b01: %03f",
-- r01, g01, b01))
return true, r01, g01, b01
end
---@param h number
---@param s number
---@param v number
---@return number r
---@return number g
---@return number b
local function hsvToRgb(h, s, v)
local h6 <const> = h * 6.0
local sector <const> = math.floor(h6)
local tint1 <const> = v * (1.0 - s)
local tint2 <const> = v * (1.0 - s * (h6 - sector))
local tint3 <const> = v * (1.0 - s * (1.0 + sector - h6))
if sector == 0 then
return v, tint3, tint1
elseif sector == 1 then
return tint2, v, tint1
elseif sector == 2 then
return tint1, v, tint3
elseif sector == 3 then
return tint1, tint2, v
elseif sector == 4 then
return tint3, tint1, v
elseif sector == 5 then
return v, tint1, tint2
elseif sector == 6 then
return v, tint3, tint1
end
return 0.0, 0.0, 0.0
end
---@param r number
---@param g number
---@param b number
---@return number h
---@return number s
---@return number v
local function rgbToHsv(r, g, b)
local gbmx <const> = math.max(g, b)
local gbmn <const> = math.min(g, b)
local mx <const> = math.max(r, gbmx)
if mx <= 0.0 then return 0.0, 0.0, 0.0 end
local mn <const> = math.min(r, gbmn)
local chroma <const> = mx - mn
if chroma <= 0.0 then return 0.0, 0.0, mx end
local hue = 0.0
if r == mx then
hue = (g - b) / chroma
if g < b then hue = hue + 6.0 end
elseif g == mx then
hue = 2.0 + (b - r) / chroma
elseif b == mx then
hue = 4.0 + (r - g) / chroma
end
return hue / 6.0, chroma / mx, mx
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 needsRepaint <const> = active.triggerAlphaRepaint
or active.wCanvasAlpha ~= wCanvas
or active.hCanvasAlpha ~= hCanvas
active.wCanvasAlpha = wCanvas
active.hCanvasAlpha = hCanvas
local isBackActive <const> = active.isBackActive
local redActive <const> = isBackActive
and active.redBack
or active.redFore
local greenActive <const> = isBackActive
and active.greenBack
or active.greenFore
local blueActive <const> = isBackActive
and active.blueBack
or active.blueFore
local alphaActive <const> = isBackActive
and active.alphaBack
or active.alphaFore
if needsRepaint then
---@type string[]
local byteStrs <const> = {}
local strpack <const> = string.pack
local floor <const> = math.floor
local wCheck <const> = defaults.wCheck
local hCheck <const> = defaults.hCheck
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 byteStr <const> = strpack("B B B B",
floor((ucCheck + t * redActive) * 255 + 0.5),
floor((ucCheck + t * greenActive) * 255 + 0.5),
floor((ucCheck + t * blueActive) * 255 + 0.5),
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> = math.floor(alphaActive * (wCanvas - 1.0) + 0.5)
local yReticle <const> = hCanvas // 2
local reticleSize <const> = defaults.reticleSize
local reticleHalf <const> = reticleSize // 2
local relLum <const> = (0.30 * redActive
+ 0.59 * greenActive
+ 0.11 * blueActive)
local reticleColor <const> = relLum < 0.5 and
Color { r = 255, g = 255, b = 255, a = 255 }
or Color { r = 0, g = 0, b = 0, a = 255 }
ctx.color = reticleColor
ctx.strokeWidth = defaults.reticleStroke
ctx:strokeRect(Rectangle(
xReticle - reticleHalf, yReticle - reticleHalf,
reticleSize, reticleSize))
end
---@param event { context: GraphicsContext }
local function onPaintMain(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 needsRepaintResize <const> = active.wCanvasMain ~= wCanvas
or active.hCanvasMain ~= hCanvas
local needsRingRepaint <const> = active.triggerRingRepaint
or needsRepaintResize
local needsTriRepaint <const> = active.triggerTriRepaint
or needsRepaintResize
active.wCanvasMain = wCanvas
active.hCanvasMain = hCanvas
local angOffsetRadians <const> = active.angOffsetRadians
local lockTriRot <const> = active.lockTriRot
local ringInEdge <const> = active.ringInEdge
local sqRie <const> = ringInEdge * ringInEdge
local xCenter <const> = wCanvas * 0.5
local yCenter <const> = hCanvas * 0.5
local shortEdge <const> = math.min(wCanvas, hCanvas)
local rCanvas <const> = (shortEdge - 1.0) * 0.5
local rCanvasInv <const> = rCanvas ~= 0.0 and 1.0 / rCanvas or 0.0
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
local rRatio <const> = 255.0 / rMax
local gRatio <const> = 255.0 / gMax
local bRatio <const> = 255.0 / bMax
local redBack <const> = active.redBack
local greenBack <const> = active.greenBack
local blueBack <const> = active.blueBack
local alphaBack <const> = active.alphaBack
local redFore <const> = active.redFore
local greenFore <const> = active.greenFore
local blueFore <const> = active.blueFore
local alphaFore <const> = active.alphaFore
local isBackActive <const> = active.isBackActive
local hueActive = isBackActive
and active.hueBack
or active.hueFore
-- Find main point of the triangle.
local thetaActive <const> = hueActive * tau
local thetaTri <const> = lockTriRot
and active.angLockRadians
or thetaActive - angOffsetRadians
local xTri1 <const> = ringInEdge * math.cos(thetaTri)
local yTri1 <const> = ringInEdge * math.sin(thetaTri)
-- Find the other two triangle points, 120 degrees away.
local rt32x <const> = sqrt3_2 * xTri1
local rt32y <const> = sqrt3_2 * yTri1
local halfx <const> = -0.5 * xTri1
local halfy <const> = -0.5 * yTri1
local xTri2 <const> = halfx - rt32y
local yTri2 <const> = halfy + rt32x
local xTri3 <const> = halfx + rt32y
local yTri3 <const> = halfy - rt32x
-- For calculation of barycentric coordinates.
-- Cf. https://codeplea.com/triangular-interpolation
local xDiff2_3 <const> = xTri2 - xTri3
local yDiff2_3 <const> = yTri2 - yTri3
local xDiff1_3 <const> = xTri1 - xTri3
local yDiff1_3 <const> = yTri1 - yTri3
local xDiff3_2 <const> = xTri3 - xTri2
local yDiff3_1 <const> = yTri3 - yTri1
local xDiff1_2 <const> = xTri1 - xTri2
local yDiff1_2 <const> = yTri1 - yTri2
local bwDenom <const> = yDiff2_3 * xDiff1_3 + xDiff3_2 * yDiff1_3
local bwDnmInv <const> = bwDenom ~= 0.0 and 1.0 / bwDenom or 0.0
-- Cache methods used in while loop.
local strpack <const> = string.pack
local floor <const> = math.floor
local atan <const> = math.atan
local rBase <const>,
gBase <const>,
bBase <const> = hsvToRgb(hueActive, 1.0, 1.0)
local packAlpha <const> = strpack("B B B B", 0, 0, 0, 0)
local lenCanvas <const> = wCanvas * hCanvas
if needsRingRepaint then
---@type string[]
local ringByteStrs <const> = {}
local i = 0
while i < lenCanvas do
local ringByteStr = packAlpha
local yCanvas <const> = i // wCanvas
local yDlt <const> = yCenter - yCanvas
local yNrm <const> = yDlt * rCanvasInv
local xCanvas <const> = i % wCanvas
local xDlt <const> = xCanvas - xCenter
local xNrm <const> = xDlt * rCanvasInv
local sqMag <const> = xNrm * xNrm + yNrm * yNrm
if sqMag >= sqRie and sqMag <= 1.0 then
local angSigned <const> = atan(yNrm, xNrm)
local angRotated <const> = angOffsetRadians + angSigned
local angUnSigned <const> = angRotated % tau
local hueWheel <const> = angUnSigned * oneTau
local rWheel <const>,
gWheel <const>,
bWheel <const> = hsvToRgb(hueWheel, 1.0, 1.0)
ringByteStr = strpack("B B B B",
floor(floor(rWheel * rMax + 0.5) * rRatio + 0.5),
floor(floor(gWheel * gMax + 0.5) * gRatio + 0.5),
floor(floor(bWheel * bMax + 0.5) * bRatio + 0.5),
255)
end
i = i + 1
ringByteStrs[i] = ringByteStr
end
active.ringBytes = table.concat(ringByteStrs)
active.triggerRingRepaint = false
end
if needsTriRepaint then
---@type string[]
local triByteStrs <const> = {}
local j = 0
while j < lenCanvas do
local triByteStr = packAlpha
local yCanvas <const> = j // wCanvas
local yDlt <const> = yCenter - yCanvas
local yNrm <const> = yDlt * rCanvasInv
local xCanvas <const> = j % wCanvas
local xDlt <const> = xCanvas - xCenter
local xNrm <const> = xDlt * rCanvasInv
local sqMag <const> = xNrm * xNrm + yNrm * yNrm
if sqMag < sqRie then
local xbw <const> = xNrm - xTri3
local ybw <const> = yNrm - yTri3
local w1 <const> = (yDiff2_3 * xbw + xDiff3_2 * ybw) * bwDnmInv
local w2 <const> = (yDiff3_1 * xbw + xDiff1_3 * ybw) * bwDnmInv
local w3 <const> = 1.0 - w1 - w2
if w1 >= 0.0 and w1 <= 1.0
and w2 >= 0.0 and w2 <= 1.0
and w3 >= 0.0 and w3 <= 1.0 then
local wSum <const> = w1 + w2 + w3
local wSumInv <const> = wSum ~= 0.0 and 1.0 / wSum or 0.0
-- w2 is white, w3 is black.
-- Black saturation is undefined in HSV.
local v <const> = (w1 + w2) * wSumInv
local u <const> = (w1 + w3) * wSumInv
local diagSq <const> = u * u + v * v
local coeff <const> = diagSq <= 0.0 and 0.0 or w2
local rTri <const> = (w1 * rBase + coeff) * wSumInv
local gTri <const> = (w1 * gBase + coeff) * wSumInv
local bTri <const> = (w1 * bBase + coeff) * wSumInv
triByteStr = strpack("B B B B",
floor(floor(rTri * rMax + 0.5) * rRatio + 0.5),
floor(floor(gTri * gMax + 0.5) * gRatio + 0.5),
floor(floor(bTri * bMax + 0.5) * bRatio + 0.5),
255)
end -- End ws in bounds.
end
j = j + 1
triByteStrs[j] = triByteStr
end
active.triBytes = table.concat(triByteStrs)
active.triggerTriRepaint = false
end
-- Draw picker canvas.
local imgSpec <const> = ImageSpec {
width = wCanvas,
height = hCanvas,
transparentColor = 0,
colorMode = ColorMode.RGB
}
local imgRing <const> = Image(imgSpec)
imgRing.bytes = active.ringBytes
local imgTri <const> = Image(imgSpec)
imgTri.bytes = active.triBytes
local themeColors <const> = app.theme.color
local bkgColor <const> = themeColors.window_face
local imgComp <const> = Image(imgSpec)
imgComp:clear(bkgColor)
imgComp:drawImage(imgRing, Point(0, 0), 255, BlendMode.DST_OVER)
imgComp:drawImage(imgTri, Point(0, 0), 255, BlendMode.DST_OVER)
local drawRect <const> = Rectangle(0, 0, wCanvas, hCanvas)
ctx:drawImage(imgComp, drawRect, drawRect)
-- Draw reticle.
local satActive <const> = isBackActive
and active.satBack
or active.satFore
local valActive <const> = isBackActive
and active.valBack
or active.valFore
local sv <const> = satActive * valActive
local xRet01 <const> = xTri3
+ xDiff2_3 * valActive
+ xDiff1_2 * sv
local yRet01 <const> = yTri3
+ yDiff2_3 * valActive
+ yDiff1_2 * sv
local xReticle <const> = xCenter + xRet01 * rCanvas
local yReticle <const> = yCenter - yRet01 * rCanvas
local reticleSize <const> = defaults.reticleSize
local reticleHalf <const> = reticleSize * 0.5
ctx.color = valActive < 0.5 and Color { r = 255, g = 255, b = 255, a = 255 }
or Color { r = 0, g = 0, b = 0, a = 255 }
ctx.strokeWidth = defaults.reticleStroke
ctx:strokeRect(Rectangle(
math.floor(xReticle - reticleHalf),
math.floor(yReticle - reticleHalf),
reticleSize, reticleSize))
if lockTriRot then
-- Draw hue reticle.
local cosa <const> = math.cos(thetaActive - angOffsetRadians)
local sina <const> = math.sin(thetaActive - angOffsetRadians)
local ringInner = rCanvas * ringInEdge
ctx.strokeWidth = 3
ctx.color = Color { r = 255, g = 255, b = 255, a = 255 }
ctx:moveTo(xCenter + ringInner * cosa, yCenter - ringInner * sina)
ctx:lineTo(xCenter + rCanvas * cosa, yCenter - rCanvas * sina)
ctx:stroke()
end
local swatchSize <const> = defaults.swatchSize
local swatchMargin <const> = defaults.swatchMargin
local offset <const> = swatchSize // 2
local xSwatch <const> = wCanvas - swatchSize - swatchMargin
local ySwatch <const> = hCanvas - swatchSize - swatchMargin
-- Draw background color swatch.
local r8Back <const> = math.floor(redBack * 255 + 0.5)
local g8Back <const> = math.floor(greenBack * 255 + 0.5)
local b8Back <const> = math.floor(blueBack * 255 + 0.5)
ctx.color = Color { r = r8Back, g = g8Back, b = b8Back, a = 255 }
ctx:fillRect(Rectangle(
xSwatch - offset, ySwatch,
swatchSize, swatchSize))
-- Draw foreground color swatch.
local r8Fore <const> = math.floor(redFore * 255 + 0.5)
local g8Fore <const> = math.floor(greenFore * 255 + 0.5)
local b8Fore <const> = math.floor(blueFore * 255 + 0.5)
ctx.color = Color { r = r8Fore, g = g8Fore, b = b8Fore, a = 255 }
ctx:fillRect(Rectangle(
xSwatch, ySwatch - offset,
swatchSize, swatchSize))
-- If dialog is wide enough, draw diagnostic text.
if (wCanvas - hCanvas) > defaults.textDisplayLimit
and rCanvas > (swatchSize + swatchSize) then
local textColor <const> = themeColors.text
ctx.color = textColor
local textSize <const> = ctx:measureText("E")
local yIncr <const> = textSize.height + 4
local hqActive <const> = isBackActive
and active.hqBack
or active.hqFore
local sqActive <const> = isBackActive
and active.sqBack
or active.sqFore
local vqActive <const> = isBackActive
and active.vqBack
or active.vqFore
if vqActive > 0.0 and sqActive > 0.0 then
ctx:fillText(string.format(
"H: %.2f", hqActive * 360), 2, 2)
end
if vqActive > 0.0 then
ctx:fillText(string.format(
"S: %.2f%%", sqActive * 100), 2, 2 + yIncr)
end
ctx:fillText(string.format(
"V: %.2f%%", vqActive * 100), 2, 2 + yIncr * 2)
local redActive <const> = isBackActive
and redBack
or redFore
local greenActive <const> = isBackActive
and greenBack
or greenFore
local blueActive <const> = isBackActive
and blueBack
or blueFore
local showAlphaBar <const> = active.showAlphaBar
local alphaActive <const> = isBackActive
and alphaBack
or 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)
if showAlphaBar then
ctx:fillText(string.format(
"A: %.2f%%", alphaActive * 100), 2, 2 + yIncr * 8)
end
local hexStr <const> = rgbToHexStr(redActive, greenActive, blueActive)
local hexVertOffset <const> = showAlphaBar and 10 or 8
ctx:fillText('#' .. hexStr,
2, 2 + yIncr * hexVertOffset)
end
end
---@param r01 number
---@param g01 number
---@param b01 number
---@param t01 number
---@param isBackActive boolean
local function updateQuantizedRgb(r01, g01, b01, t01, isBackActive)
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
local rq <const> = math.floor(r01 * rMax + 0.5) / rMax
local gq <const> = math.floor(g01 * gMax + 0.5) / gMax
local bq <const> = math.floor(b01 * bMax + 0.5) / bMax
active[isBackActive and "redBack" or "redFore"] = rq
active[isBackActive and "greenBack" or "greenFore"] = gq
active[isBackActive and "blueBack" or "blueFore"] = bq
local hq <const>, sq <const>, vq <const> = rgbToHsv(rq, gq, bq)
if vq > 0.0 then
if sq > 0.0 then
active[isBackActive and "hqBack" or "hqFore"] = hq
end
active[isBackActive and "sqBack" or "sqFore"] = sq
end
active[isBackActive and "vqBack" or "vqFore"] = vq
local showAlphaBar <const> = active.showAlphaBar
local r8 <const> = math.floor(rq * 255.0 + 0.5)
local g8 <const> = math.floor(gq * 255.0 + 0.5)
local b8 <const> = math.floor(bq * 255.0 + 0.5)
local t8 <const> = showAlphaBar
and math.floor(t01 * 255.0 + 0.5)
or 255
if isBackActive then
app.command.SwitchColors()
app.fgColor = Color { r = r8, g = g8, b = b8, a = t8 }
app.command.SwitchColors()
else
app.fgColor = Color { r = r8, g = g8, b = b8, a = t8 }
end
end
---@param ho number
---@param so number
---@param vo number
---@param isBackActive boolean
local function updateQuantizedHsv(ho, so, vo, isBackActive)
local r01 <const>, g01 <const>, b01 <const> = hsvToRgb(ho, so, vo)
local t01 <const> = isBackActive
and active.alphaBack
or active.alphaFore
updateQuantizedRgb(r01, g01, b01, t01, isBackActive)
end
---@param hue number
local function updateFromHue(hue)
local isBackActive <const> = active.isBackActive
active[isBackActive and "hueBack" or "hueFore"] = hue
local satWheel <const> = isBackActive
and active.satBack
or active.satFore
local valWheel <const> = isBackActive
and active.valBack
or active.valFore
updateQuantizedHsv(hue, satWheel, valWheel, isBackActive)
end
---@param sat number
local function updateFromSat(sat)
local isBackActive <const> = active.isBackActive
active[isBackActive and "satBack" or "satFore"] = sat
local hueWheel <const> = isBackActive
and active.hueBack
or active.hueFore
local valWheel <const> = isBackActive
and active.valBack
or active.valFore
updateQuantizedHsv(hueWheel, sat, valWheel, isBackActive)
end
---@param val number
local function updateFromVal(val)
local isBackActive <const> = active.isBackActive
active[isBackActive and "valBack" or "valFore"] = val
local hueWheel <const> = isBackActive
and active.hueBack
or active.hueFore
local satWheel <const> = isBackActive
and active.satBack
or active.satFore
updateQuantizedHsv(hueWheel, satWheel, val, isBackActive)
end
local function updateFromBitDepth()
local r01Fore <const> = active.redFore
local g01Fore <const> = active.greenFore
local b01Fore <const> = active.blueFore
local t01Fore <const> = active.alphaFore
updateQuantizedRgb(r01Fore, g01Fore, b01Fore, t01Fore, false)
local r01Back <const> = active.redBack
local g01Back <const> = active.greenBack
local b01Back <const> = active.blueBack
local t01Back <const> = active.alphaBack
updateQuantizedRgb(r01Back, g01Back, b01Back, t01Back, true)
end
---@param r8 integer
---@param g8 integer
---@param b8 integer
---@param t8 integer
---@param isBackActive boolean
local function updateFromAse(r8, g8, b8, t8, isBackActive)
local r01 <const>,
g01 <const>,
b01 <const> = r8 / 255.0, g8 / 255.0, b8 / 255.0
local ho <const>, so <const>, vo <const> = rgbToHsv(r01, g01, b01)
if vo > 0.0 then
if so > 0.0 then
active[isBackActive and "hueBack" or "hueFore"] = ho
end
active[isBackActive and "satBack" or "satFore"] = so
end
active[isBackActive and "valBack" or "valFore"] = vo
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
local rq <const> = math.floor((r01) * rMax + 0.5) / rMax
local gq <const> = math.floor((g01) * gMax + 0.5) / gMax
local bq <const> = math.floor((b01) * bMax + 0.5) / bMax
active[isBackActive and "redBack" or "redFore"] = rq
active[isBackActive and "greenBack" or "greenFore"] = gq
active[isBackActive and "blueBack" or "blueFore"] = bq
active[isBackActive and "alphaBack" or "alphaFore"] = t8 / 255.0
local hq <const>, sq <const>, vq <const> = rgbToHsv(rq, gq, bq)
if vq > 0.0 then
if sq > 0.0 then
active[isBackActive and "hqBack" or "hqFore"] = hq
end
active[isBackActive and "sqBack" or "sqFore"] = sq
end
active[isBackActive and "vqBack" or "vqFore"] = vq
end
local dlgMain <const> = Dialog {
title = "Triangle Color Picker"
}
local dlgOptions <const> = Dialog {
title = "Triangle Options",
parent = dlgMain
}
local dlgHex <const> = Dialog {
title = "Triangle Hex Code",
parent = dlgMain
}
---@param r01 number
---@param g01 number
---@param b01 number
---@param t01 number
local function updateHexDisplay(r01, g01, b01, t01)
local rMax <const> = active.rMax
local gMax <const> = active.gMax
local bMax <const> = active.bMax
local r8 <const> = math.floor(r01 * 255 + 0.5)
local g8 <const> = math.floor(g01 * 255 + 0.5)
local b8 <const> = math.floor(b01 * 255 + 0.5)
local rx <const> = math.floor(r01 * rMax + 0.5)
local gx <const> = math.floor(g01 * gMax + 0.5)
local bx <const> = math.floor(b01 * bMax + 0.5)
local showAlphaBar <const> = active.showAlphaBar
local t8 <const> = showAlphaBar
and math.floor(t01 * 255.0 + 0.5)
or 255
dlgHex:modify {
id = "rNote",
text = string.format("R: %d / %d", rx, rMax)
}
dlgHex:modify {
id = "gNote",
text = string.format("G: %d / %d", gx, gMax)
}
dlgHex:modify {
id = "bNote",
text = string.format("B: %d / %d", bx, bMax)
}
dlgHex:modify {
id = "previewColor",
colors = { Color { r = r8, g = g8, b = b8, a = t8 } }
}
end
---@param event KeyEvent
local function onKeyDownMain(event)
local isBackActive <const> = active.isBackActive
local hueActive <const> = isBackActive
and active.hueBack
or active.hueFore
local satActive <const> = isBackActive
and active.satBack
or active.satFore