-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaseSwatchIo.lua
More file actions
1781 lines (1540 loc) · 59.9 KB
/
Copy pathaseSwatchIo.lua
File metadata and controls
1781 lines (1540 loc) · 59.9 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
--[[
ACO & ASE:
https://medium.com/swlh/mastering-adobe-color-file-formats-d29e43fde8eb
ACO:
https://devblog.cyotek.com/post/reading-photoshop-color-swatch-aco-files-using-csharp
https://devblog.cyotek.com/post/writing-photoshop-color-swatch-aco-files-using-csharp
GIMP Lab format support:
https://gitlab.gnome.org/GNOME/gimp/blob/gimp-2-10/app/core/gimppalette-load.c#L413
https://gitlab.gnome.org/GNOME/gimp/-/merge_requests/849
Krita Lab format support:
https://github.com/KDE/krita/blob/master/libs/pigment/resources/KoColorSet.cpp#L1658
Adobe spec:
https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1055819
ASE:
https://devblog.cyotek.com/post/reading-adobe-swatch-exchange-ase-files-using-csharp
https://devblog.cyotek.com/post/writing-adobe-swatch-exchange-ase-files-using-csharp
GIMP ASE support (NEW):
https://gitlab.gnome.org/GNOME/gimp/blob/gimp-2-10/app/core/gimppalette-load.c#L931
CIE-sRGB, CIE-AdobeRGB formulae:
https://www.easyrgb.com/en/math.php
For Bradford adapted, D65 to D50, matrices found in icc profiles, see:
http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
Display P3 conversions to and from CIE XYZ:
https://www.w3.org/TR/css-color-4/#color-conversion-code
https://fujiwaratko.sakura.ne.jp/infosci/colorspace/colorspace2_e.html
Ase files can be downloaded from https://color.adobe.com/ .
This confirms that LAB format for Krita is right and GIMP is wrong.
Bug report: https://gitlab.gnome.org/GNOME/gimp/-/issues/12478
]]
local colorFormats <const> = { "CMYK", "GRAY", "HSB", "LAB", "RGB" }
local colorSpaces <const> = { "ADOBE_RGB", "DISPLAY_P3", "S_RGB" }
local externalRefs <const> = { "GIMP", "KRITA", "OTHER" }
local fileExts <const> = { "aco", "act", "ase" }
local grayMethods <const> = { "HSI", "HSL", "HSV", "LUMA" }
local defaults <const> = {
name = "Palette",
colorFormat = "RGB",
colorSpace = "S_RGB",
grayMethod = "LUMA",
externalRef = "KRITA",
preserveIndices = false
}
---@param l number
---@param a number
---@param b number
---@return number x
---@return number y
---@return number z
---@nodiscard
local function cieLabToCieXyz(l, a, b)
local y = (l + 16.0) * 0.008620689655172414
local x = a * 0.002 + y
local z = y - b * 0.005
local ye3 <const> = (y * y) * y
local xe3 <const> = (x * x) * x
local ze3 <const> = (z * z) * z
y = ye3 > 0.008856 and ye3
or (y - 0.13793103448275862) * 0.12841751101180157
x = xe3 > 0.008856 and xe3
or (x - 0.13793103448275862) * 0.12841751101180157
z = ze3 > 0.008856 and ze3
or (z - 0.13793103448275862) * 0.12841751101180157
x = x * 0.95047
z = z * 1.08883
return x, y, z
end
---@param y number
---@return number
---@nodiscard
local function cieLumToAdobeGray(y)
return y ^ 0.4547069271758437
end
---@param y number
---@return number
---@nodiscard
local function cieLumTosGray(y)
return y <= 0.0031308 and y * 12.92
or (y ^ 0.41666666666667) * 1.055 - 0.055
end
---@param x number
---@param y number
---@param z number
---@return number l
---@return number a
---@return number b
---@nodiscard
local function cieXyzToCieLab(x, y, z)
local vx = x * 1.0521110608435826
vx = vx > 0.008856
and vx ^ 0.3333333333333333
or 7.787 * vx + 0.13793103448275862
local vy = y
vy = vy > 0.008856
and vy ^ 0.3333333333333333
or 7.787 * vy + 0.13793103448275862
local vz = z * 0.9184170164304805
vz = vz > 0.008856
and vz ^ 0.3333333333333333
or 7.787 * vz + 0.13793103448275862
local l <const> = 116.0 * vy - 16.0
local a <const> = 500.0 * (vx - vy)
local b <const> = 200.0 * (vy - vz)
return l, a, b
end
---@param x number
---@param y number
---@param z number
---@return number r01Lnear
---@return number g01Lnear
---@return number b01Lnear
---@nodiscard
local function cieXyzToLinearAdobeRgb(x, y, z)
return 2.04137 * x - 0.56495 * y - 0.34469 * z,
-0.96927 * x + 1.87601 * y + 0.04156 * z,
0.01345 * x - 0.11839 * y + 1.01541 * z
end
---@param x number
---@param y number
---@param z number
---@return number r01Lnear
---@return number g01Lnear
---@return number b01Lnear
---@nodiscard
local function cieXyzToLinearP3(x, y, z)
return 2.493497 * x - 0.9313836 * y - 0.4027108 * z,
-0.829489 * x + 1.7626641 * y + 0.023624687 * z,
0.03584583 * x - 0.07617239 * y + 0.9568845 * z
end
---@param x number
---@param y number
---@param z number
---@return number r01Linear
---@return number g01Linear
---@return number b01Linear
---@nodiscard
local function cieXyzToLinearsRgb(x, y, z)
return 3.2408123 * x - 1.5373085 * y - 0.49858654 * z,
-0.969243 * x + 1.8759663 * y + 0.041555032 * z,
0.0556384 * x - 0.20400746 * y + 1.0571296 * z
end
---@param c number
---@param m number
---@param y number
---@param k number
---@return number r01Gamma
---@return number g01Gamma
---@return number b01Gamma
---@nodiscard
local function cmykToRgb(c, m, y, k)
local u <const> = 1.0 - k
local r01 <const> = (1.0 - c) * u
local g01 <const> = (1.0 - m) * u
local b01 <const> = (1.0 - y) * u
return r01, g01, b01
end
---@param r01Gamma number
---@param g01Gamma number
---@param b01Gamma number
---@return number r01Linear
---@return number g01Linear
---@return number b01Linear
---@nodiscard
local function gammaAdobeRgbToLinearAdobeRgb(r01Gamma, g01Gamma, b01Gamma)
local r01Linear <const> = r01Gamma ^ 2.19921875
local g01Linear <const> = g01Gamma ^ 2.19921875
local b01Linear <const> = b01Gamma ^ 2.19921875
return r01Linear ~= r01Linear and 0.0 or r01Linear,
g01Linear ~= g01Linear and 0.0 or g01Linear,
b01Linear ~= b01Linear and 0.0 or b01Linear
end
---@param r01Gamma number
---@param g01Gamma number
---@param b01Gamma number
---@return number r01Linear
---@return number g01Linear
---@return number b01Linear
---@nodiscard
local function gammasRgbToLinearsRgb(r01Gamma, g01Gamma, b01Gamma)
local r01Linear <const> = r01Gamma <= 0.04045
and r01Gamma * 0.077399380804954
or ((r01Gamma + 0.055) * 0.9478672985782) ^ 2.4
local g01Linear <const> = g01Gamma <= 0.04045
and g01Gamma * 0.077399380804954
or ((g01Gamma + 0.055) * 0.9478672985782) ^ 2.4
local b01Linear <const> = b01Gamma <= 0.04045
and b01Gamma * 0.077399380804954
or ((b01Gamma + 0.055) * 0.9478672985782) ^ 2.4
return r01Linear, g01Linear, b01Linear
end
---@param r01 number
---@param g01 number
---@param b01 number
---@return number
---@nodiscard
local function grayMethodHsi(r01, g01, b01)
return (r01 + g01 + b01) / 3.0
end
---@param r01 number
---@param g01 number
---@param b01 number
---@return number
---@nodiscard
local function grayMethodHsl(r01, g01, b01)
return (math.min(r01, g01, b01) + math.max(r01, g01, b01)) * 0.5
end
---@param r01 number
---@param g01 number
---@param b01 number
---@return number
---@nodiscard
local function grayMethodHsv(r01, g01, b01)
return math.max(r01, g01, b01)
end
---@param hue number
---@param sat number
---@param val number
---@return number r01Gamma
---@return number g01Gamma
---@return number b01Gamma
---@nodiscard
local function hsvToRgb(hue, sat, val)
local h <const> = (hue % 1.0) * 6.0
local s <const> = math.min(math.max(sat, 0.0), 1.0)
local v <const> = math.min(math.max(val, 0.0), 1.0)
local sector <const> = math.floor(h)
local secf <const> = sector + 0.0
local tint1 <const> = v * (1.0 - s)
local tint2 <const> = v * (1.0 - s * (h - secf))
local tint3 <const> = v * (1.0 - s * (1.0 + secf - h))
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 r01Linear number
---@param g01Linear number
---@param b01Linear number
---@return number r01Gamma
---@return number g01Gamma
---@return number b01Gamma
---@nodiscard
local function linearAdobeRgbToGammaAdobeRgb(r01Linear, g01Linear, b01Linear)
local r01Gamma <const> = r01Linear ^ 0.4547069271758437
local g01Gamma <const> = g01Linear ^ 0.4547069271758437
local b01Gamma <const> = b01Linear ^ 0.4547069271758437
return r01Gamma ~= r01Gamma and 0.0 or r01Gamma,
g01Gamma ~= g01Gamma and 0.0 or g01Gamma,
b01Gamma ~= b01Gamma and 0.0 or b01Gamma
end
---@param r01Linear number
---@param g01Linear number
---@param b01Linear number
---@return number x
---@return number y
---@return number z
---@nodiscard
local function linearAdobeRgbToCieXyz(r01Linear, g01Linear, b01Linear)
return 0.57667 * r01Linear + 0.18555 * g01Linear + 0.18819 * b01Linear,
0.29738 * r01Linear + 0.62735 * g01Linear + 0.07527 * b01Linear,
0.02703 * r01Linear + 0.07069 * g01Linear + 0.99110 * b01Linear
end
---@param r01Linear number
---@param g01Linear number
---@param b01Linear number
---@return number x
---@return number y
---@return number z
---@nodiscard
local function linearP3ToCieXyz(r01Linear, g01Linear, b01Linear)
return 0.48657095 * r01Linear + 0.2656677 * g01Linear + 0.19821729 * b01Linear,
0.22897457 * r01Linear + 0.69173855 * g01Linear + 0.07928691 * b01Linear,
0.0 * r01Linear + 0.04511338 * g01Linear + 1.0439444 * b01Linear
end
---@param r01Linear number
---@param g01Linear number
---@param b01Linear number
---@return number x
---@return number y
---@return number z
---@nodiscard
local function linearsRgbToCieXyz(r01Linear, g01Linear, b01Linear)
return 0.41241086 * r01Linear + 0.35758457 * g01Linear + 0.1804538 * b01Linear,
0.21264935 * r01Linear + 0.71516913 * g01Linear + 0.07218152 * b01Linear,
0.019331759 * r01Linear + 0.11919486 * g01Linear + 0.95039004 * b01Linear
end
---@param r01Linear number
---@param g01Linear number
---@param b01Linear number
---@return number r01Gamma
---@return number g01Gamma
---@return number b01Gamma
---@nodiscard
local function linearsRgbToGammasRgb(r01Linear, g01Linear, b01Linear)
local r01Gamma <const> = r01Linear <= 0.0031308
and r01Linear * 12.92
or (r01Linear ^ 0.41666666666667) * 1.055 - 0.055
local g01Gamma <const> = g01Linear <= 0.0031308
and g01Linear * 12.92
or (g01Linear ^ 0.41666666666667) * 1.055 - 0.055
local b01Gamma <const> = b01Linear <= 0.0031308
and b01Linear * 12.92
or (b01Linear ^ 0.41666666666667) * 1.055 - 0.055
return r01Gamma, g01Gamma, b01Gamma
end
---@param fileData string
---@param colorSpace "ADOBE_RGB"|"DISPLAY_P3"|"S_RGB"
---@param externalRef "GIMP"|"KRITA"
---@return Color[]
---@nodiscard
local function readAco(fileData, colorSpace, externalRef)
---@type Color[]
local aseColors <const> = { Color { r = 0, g = 0, b = 0, a = 0 } }
local strsub <const> = string.sub
local strunpack <const> = string.unpack
local floor <const> = math.floor
local max <const> = math.max
local min <const> = math.min
local isAdobe <const> = colorSpace == "ADOBE_RGB"
local isP3 <const> = colorSpace == "DISPLAY_P3"
local isKrita <const> = externalRef == "KRITA"
local exponent = 1.0
if isKrita then exponent = 1.0 / 2.2 end
local fmtGry <const> = 0x0008
local fmtLab <const> = 0x0007
local fmtCmyk <const> = 0x0002
local fmtHsb <const> = 0x0001
local initHead <const> = strunpack(">I2", strsub(fileData, 1, 2))
local numColors <const> = strunpack(">I2", strsub(fileData, 3, 4))
local initIsV2 <const> = initHead == 0x0002
local blockLen = 10
-- print(string.format("%02X", initHead))
-- print(initIsV2)
-- print(string.format("numColors: %d", numColors))
local i = 0
local j = 5
while i < numColors do
i = i + 1
-- print(string.format("\ni: %d, j: %d", i, j))
local fmt <const> = strunpack(">I2", strsub(fileData, j, j + 1))
local upkwStr <const> = strsub(fileData, j + 2, j + 3)
local upkxStr <const> = strsub(fileData, j + 4, j + 5)
local upkyStr <const> = strsub(fileData, j + 6, j + 7)
local upkzStr <const> = strsub(fileData, j + 8, j + 9)
local upkw <const> = strunpack(">I2", upkwStr)
local upkx = strunpack(">I2", upkxStr)
local upky = strunpack(">I2", upkyStr)
local upkz <const> = strunpack(">I2", upkzStr)
-- print(string.format("fmt: %d (0x%02x)", fmt, fmt))
-- print(string.format("upw: %d (0x%02x)", upkw, upkw))
-- print(string.format("upx: %d (0x%02x)", upkx, upkx))
-- print(string.format("upy: %d (0x%02x)", upky, upky))
-- print(string.format("upz: %d (0x%02x)", upkz, upkz))
local r01 = 0.0
local g01 = 0.0
local b01 = 0.0
if fmt == fmtGry then
local gray <const> = (upkw * 0.0001) ^ exponent
r01 = gray
g01 = gray
b01 = gray
-- print(strfmt("GRAY: %.3f", gray))
elseif fmt == fmtLab then
local l = 0.0
local a = 0.0
local b = 0.0
if isKrita then
l = upky / 655.35
a = (upkx - 32768) / 257.0
b = (upkw - 32768) / 257.0
else
upkx = strunpack(">i2", upkxStr)
upky = strunpack(">i2", upkyStr)
l = upkw * 0.01
a = upkx * 0.01
b = upky * 0.01
end
local x <const>, y <const>, z <const> = cieLabToCieXyz(l, a, b)
local r01Linear = 0.0
local g01Linear = 0.0
local b01Linear = 0.0
if isAdobe then
r01Linear, g01Linear, b01Linear = cieXyzToLinearAdobeRgb(x, y, z)
r01, g01, b01 = linearAdobeRgbToGammaAdobeRgb(r01Linear, g01Linear, b01Linear)
elseif isP3 then
r01Linear, g01Linear, b01Linear = cieXyzToLinearP3(x, y, z)
r01, g01, b01 = linearsRgbToGammasRgb(r01Linear, g01Linear, b01Linear)
else
r01Linear, g01Linear, b01Linear = cieXyzToLinearsRgb(x, y, z)
r01, g01, b01 = linearsRgbToGammasRgb(r01Linear, g01Linear, b01Linear)
end
-- print(strfmt(
-- "LAB: l: %.3f, a: %.3f, b: %.3f",
-- l, a, b))
elseif fmt == fmtCmyk then
local c <const> = 1.0 - upkw / 65535.0
local m <const> = 1.0 - upkx / 65535.0
local y <const> = 1.0 - upky / 65535.0
local k <const> = 1.0 - upkz / 65535.0
r01, g01, b01 = cmykToRgb(c, m, y, k)
-- print(strfmt(
-- "CMYK: c: %.3f, m: %.3f, y: %.3f, k: %.3f",
-- c, m, y, k))
elseif fmt == fmtHsb then
local hue <const> = upkw / 65535.0
local saturation <const> = upkx / 65535.0
local value <const> = upky / 65535.0
r01, g01, b01 = hsvToRgb(hue, saturation, value)
-- print(strfmt(
-- "HSB: h: %.3f, s: %.3f, b: %.3f",
-- hue, saturation, value))
else
r01 = upkw / 65535.0
g01 = upkx / 65535.0
b01 = upky / 65535.0
-- print(strfmt(
-- "RGB: r01: %.3f, g01: %.3f, b01: %.3f",
-- r01, g01, b01))
end
local r8 <const> = floor(min(max(r01, 0.0), 1.0) * 255.0 + 0.5)
local g8 <const> = floor(min(max(g01, 0.0), 1.0) * 255.0 + 0.5)
local b8 <const> = floor(min(max(b01, 0.0), 1.0) * 255.0 + 0.5)
-- print(string.format(
-- "r8: %d, g8: %d, b8: %d, (#%06x)",
-- r8, g8, b8, r8 << 0x10 | g8 << 0x08 | b8))
local aseColor <const> = Color { r = r8, g = g8, b = b8, a = 255 }
aseColors[1 + i] = aseColor
if initIsV2 then
-- Slots 10 and 11 are used by a constant 0.
-- Length of the name is in characters, which are utf16, plus
-- a terminal zero.
-- local spacer = strunpack(">I2", strsub(fileData, j + 10, j + 11))
local lenName <const> = strunpack(">I2", strsub(fileData, j + 12, j + 13))
-- print(string.format("spacer: 0x%02X", spacer))
-- print(string.format("lenName: %d (0x%02X)", lenName, lenName))
blockLen = 10
+ 2 -- spacer
+ 2 -- string length
+ (lenName + 1) * 2
end
j = j + blockLen
end
return aseColors
end
---@param fileData string
---@param preserveIndices boolean
---@return Color[]
---@nodiscard
local function readAct(fileData, preserveIndices)
local lenFileData <const> = #fileData
local is772 <const> = lenFileData == 772
local numColors = 256
-- local alphaIndex = -1
if is772 then
-- Neither GIMP nor Krita supports 772, so this was tested with
-- palettes from https://fornaxvoid.com/colorpalettes/ .
local ncParsed <const> = string.unpack(">I2", string.sub(fileData, 769, 770))
numColors = math.min(ncParsed, 256)
-- print(string.format(
-- "ncParsed: %d, numColors: %d",
-- ncParsed, numColors))
-- local aiParsed <const> = string.unpack(">I2", string.sub(fileData, 771, 772))
-- if aiParsed > 0 and aiParsed < numColors then
-- alphaIndex = aiParsed
-- end
-- print(string.format(
-- "aiParsed: %d, alphaIndex: %d",
-- aiParsed, alphaIndex))
end
local strbyte <const> = string.byte
---@type table<integer, integer[]>
local hexDict <const> = {}
local uniqueCount = 0
local i = 0
local j = 0
while i < numColors do
-- if i ~= alphaIndex then
local r <const>, g <const>, b <const> = strbyte(fileData, 1 + j, 3 + j)
local hex <const> = 0xff000000 | b << 0x10 | g << 0x08 | r
if hexDict[hex] then
local indices <const> = hexDict[hex]
indices[#indices + 1] = 1 + i
else
hexDict[hex] = { 1 + i }
uniqueCount = uniqueCount + 1
end
-- end
i = i + 1
j = j + 3
end
---@type Color[]
local aseColors <const> = {}
if preserveIndices then
for hex, idcs in pairs(hexDict) do
local r <const> = hex & 0xff
local g <const> = hex >> 0x08 & 0xff
local b <const> = hex >> 0x10 & 0xff
local aseColor <const> = Color { r = r, g = g, b = b, a = 255 }
local lenIdcs <const> = #idcs
local k = 0
while k < lenIdcs do
k = k + 1
local idx <const> = idcs[k]
aseColors[idx] = aseColor
end
end
else
---@type integer[]
local sortedSet <const> = {}
for hex, _ in pairs(hexDict) do
sortedSet[#sortedSet + 1] = hex
end
table.sort(sortedSet, function(a, b)
return hexDict[a][1] < hexDict[b][1]
end)
local k = 0
while k < uniqueCount do
k = k + 1
local hex <const> = sortedSet[k]
local r <const> = hex & 0xff
local g <const> = hex >> 0x08 & 0xff
local b <const> = hex >> 0x10 & 0xff
aseColors[k] = Color { r = r, g = g, b = b, a = 255 }
end
if #aseColors < 256 then
table.insert(aseColors, 1, Color { r = 0, g = 0, b = 0, a = 0 })
end
end
return aseColors
end
---@param fileData string
---@param colorSpace "ADOBE_RGB"|"DISPLAY_P3"|"S_RGB"
---@param externalRef "GIMP"|"KRITA"|"OTHER"
---@return Color[]
---@nodiscard
local function readAse(fileData, colorSpace, externalRef)
---@type Color[]
local aseColors <const> = { Color { r = 0, g = 0, b = 0, a = 0 } }
local strlower <const> = string.lower
local strsub <const> = string.sub
local strunpack <const> = string.unpack
local floor <const> = math.floor
local max <const> = math.max
local min <const> = math.min
local lScalar <const> = 100.0
local isGimp <const> = externalRef == "GIMP"
local lenFileData <const> = #fileData
local isAdobe <const> = colorSpace == "ADOBE_RGB"
local isP3 <const> = colorSpace == "DISPLAY_P3"
local groupNesting = 0
-- Ignore version block (0010) in chars 5, 6, 7, 8.
-- local numColors = strunpack(">i4", strsub(fileData, 9, 12))
-- print("numColors: " .. numColors)
local i = 13
while i < lenFileData do
local blockLen = 2
local blockHeader <const> = strunpack(">i2", strsub(fileData, i, i + 1))
local isGroup <const> = blockHeader == 0xc001
local isEntry <const> = blockHeader == 0x0001
if isGroup or isEntry then
-- Excludes header start.
blockLen = strunpack(">i4", strsub(fileData, i + 2, i + 5))
-- print("blockLen: " .. blockLen)
-- The length is the number of chars in a string. The string
-- is encoded in UTF-16, so it'd be 2x number of binary chars.
-- A terminal zero is included in the length.
--
-- Ase files downloaded from Lospec use their lower-case 6 digit
-- hexadecimal value as a name, e.g., "aabbcc".
local lenChars16 <const> = strunpack(">i2", strsub(fileData, i + 6, i + 7))
-- print("lenChars16: " .. lenChars16)
-- local nameChars = {}
-- local j = 0
-- while j < lenChars16 do
-- local k = i + 8 + j * 2
-- local bin16 = strsub(fileData, k, k + 1)
-- local int16 = strunpack(">i2", bin16)
-- local char8 = ""
-- if int16 then char8 = strchar(int16) end
-- j = j + 1
-- nameChars[j] = char8
-- print("bin16: " .. bin16)
-- print("int16: " .. int16)
-- print(strfmt("char16: \"%s\"", char8))
-- end
-- local name = tconcat(nameChars, "")
-- print("name: " .. name)
if isEntry then
-- print("Color block.")
local iOffset <const> = lenChars16 * 2 + i
-- Color formats do not need to be unpacked, since they are
-- human readable strings.
local colorFormat <const> = strlower(strsub(fileData, iOffset + 8, iOffset + 11))
if colorFormat == "rgb " then
-- print("RGB color space.")
local r01 <const> = strunpack(">f", strsub(fileData, iOffset + 12, iOffset + 15))
local g01 <const> = strunpack(">f", strsub(fileData, iOffset + 16, iOffset + 19))
local b01 <const> = strunpack(">f", strsub(fileData, iOffset + 20, iOffset + 23))
-- print(strfmt("%.6f, %.6f, %.6f", r01, g01, b01))
aseColors[#aseColors + 1] = Color {
r = floor(min(max(r01, 0.0), 1.0) * 255.0 + 0.5),
g = floor(min(max(g01, 0.0), 1.0) * 255.0 + 0.5),
b = floor(min(max(b01, 0.0), 1.0) * 255.0 + 0.5),
a = 255
}
elseif colorFormat == "cmyk" then
-- print("CMYK color space")
local c <const> = strunpack(">f", strsub(fileData, iOffset + 12, iOffset + 15))
local m <const> = strunpack(">f", strsub(fileData, iOffset + 16, iOffset + 19))
local y <const> = strunpack(">f", strsub(fileData, iOffset + 20, iOffset + 23))
local k <const> = strunpack(">f", strsub(fileData, iOffset + 24, iOffset + 27))
local r01, g01, b01 = cmykToRgb(c, m, y, k)
if isGimp then
if isAdobe then
r01, g01, b01 = linearAdobeRgbToGammaAdobeRgb(r01, g01, b01)
elseif isP3 then
r01, g01, b01 = linearsRgbToGammasRgb(r01, g01, b01)
else
r01, g01, b01 = linearsRgbToGammasRgb(r01, g01, b01)
end
end
aseColors[#aseColors + 1] = Color {
r = floor(min(max(r01, 0.0), 1.0) * 255.0 + 0.5),
g = floor(min(max(g01, 0.0), 1.0) * 255.0 + 0.5),
b = floor(min(max(b01, 0.0), 1.0) * 255.0 + 0.5),
a = 255
}
elseif colorFormat == "lab " then
-- print("Lab color space")
local l <const> = strunpack(">f", strsub(fileData, iOffset + 12, iOffset + 15))
local a <const> = strunpack(">f", strsub(fileData, iOffset + 16, iOffset + 19))
local b <const> = strunpack(">f", strsub(fileData, iOffset + 20, iOffset + 23))
local x <const>, y <const>, z <const> = cieLabToCieXyz(l * lScalar, a, b)
local r01Linear = 0.0
local g01Linear = 0.0
local b01Linear = 0.0
local r01Gamma = 0.0
local g01Gamma = 0.0
local b01Gamma = 0.0
if isAdobe then
r01Linear, g01Linear, b01Linear = cieXyzToLinearAdobeRgb(x, y, z)
r01Gamma, g01Gamma, b01Gamma = linearAdobeRgbToGammaAdobeRgb(r01Linear, g01Linear, b01Linear)
elseif isP3 then
r01Linear, g01Linear, b01Linear = cieXyzToLinearP3(x, y, z)
r01Gamma, g01Gamma, b01Gamma = linearsRgbToGammasRgb(r01Linear, g01Linear, b01Linear)
else
r01Linear, g01Linear, b01Linear = cieXyzToLinearsRgb(x, y, z)
r01Gamma, g01Gamma, b01Gamma = linearsRgbToGammasRgb(r01Linear, g01Linear, b01Linear)
end
aseColors[#aseColors + 1] = Color {
r = floor(min(max(r01Gamma, 0.0), 1.0) * 255.0 + 0.5),
g = floor(min(max(g01Gamma, 0.0), 1.0) * 255.0 + 0.5),
b = floor(min(max(b01Gamma, 0.0), 1.0) * 255.0 + 0.5),
a = 255
}
elseif colorFormat == "gray" then
-- print("Gray color space")
local v01 <const> = strunpack(">f", strsub(fileData, iOffset + 12, iOffset + 15))
local v8 <const> = floor(min(max(v01, 0.0), 1.0) * 255.0 + 0.5)
aseColors[#aseColors + 1] = Color { r = v8, g = v8, b = v8, a = 255 }
end
else
-- print("Group begin block.")
groupNesting = groupNesting + 1
end
-- Include block header in length.
blockLen = blockLen + 2
elseif blockHeader == 0xc002 then
-- print("Group end block.")
groupNesting = groupNesting - 1
end
i = i + blockLen
end
return aseColors
end
---@param r01 number
---@param g01 number
---@param b01 number
---@param gray number
---@return number c
---@return number m
---@return number y
---@return number k
---@nodiscard
local function rgbToCmyk(r01, g01, b01, gray)
local c = 0.0
local m = 0.0
local y = 0.0
local k <const> = 1.0 - gray
if k ~= 1.0 then
local scalar <const> = 1.0 / (1.0 - k)
c = (1.0 - r01 - k) * scalar
m = (1.0 - g01 - k) * scalar
y = (1.0 - b01 - k) * scalar
end
return c, m, y, k
end
---@param r01 number
---@param g01 number
---@param b01 number
---@return number h
---@return number s
---@return number v
---@nodiscard
local function rgbToHsv(r01, g01, b01)
local gbmx <const> = math.max(g01, b01)
local gbmn <const> = math.min(g01, b01)
local mx <const> = math.max(r01, gbmx)
if mx < 0.00392156862745098 then
return 0.0, 0.0, 0.0
end
local mn <const> = math.min(r01, gbmn)
local diff <const> = mx - mn
if diff < 0.00392156862745098 then
local light = (mx + mn) * 0.5
if light > 0.996078431372549 then
return 0.0, 0.0, 1.0
end
return 0.0, 0.0, mx
end
local hue = 0.0
if r01 == mx then
hue = (g01 - b01) / diff
if g01 < b01 then hue = hue + 6.0 end
elseif g01 == mx then
hue = 2.0 + (b01 - r01) / diff
else
hue = 4.0 + (r01 - g01) / diff
end
return hue / 6.0, diff / mx, mx
end
---@param palette Palette
---@return string
---@nodiscard
local function writeAct(palette)
---@type table<integer, integer>
local hexDict <const> = {}
local uniqueCount = 0
local lenPalette <const> = #palette
local h = 0
while h < lenPalette do
local aseColor <const> = palette:getColor(h)
if aseColor.alpha > 0 then
local r8 <const> = aseColor.red
local g8 <const> = aseColor.green
local b8 <const> = aseColor.blue
local hex <const> = 0xff000000 | b8 << 0x10 | g8 << 0x08 | r8
if uniqueCount < 256 and (not hexDict[hex]) then
hexDict[hex] = uniqueCount
uniqueCount = uniqueCount + 1
end
end
h = h + 1
end
---@type string[]
local binWords <const> = {}
local strchar <const> = string.char
for hex, idx in pairs(hexDict) do
local r8 <const> = hex & 0xff
local g8 <const> = hex >> 0x08 & 0xff
local b8 <const> = hex >> 0x10 & 0xff
local j <const> = idx * 3
binWords[1 + j] = strchar(r8)
binWords[2 + j] = strchar(g8)
binWords[3 + j] = strchar(b8)
end
local char0 <const> = strchar(0)
while #binWords < 768 do binWords[#binWords + 1] = char0 end
return table.concat(binWords, "")
end
---@param palette Palette
---@param colorFormat "CMYK"|"GRAY"|"HSB"|"LAB"|"RGB"
---@param colorSpace "ADOBE_RGB"|"DISPLAY_P3"|"S_RGB"
---@param grayMethod "HSI"|"HSL"|"HSV"|"LUMA"
---@param externalRef "GIMP"|"KRITA"
---@return string
---@nodiscard
local function writeAco(
palette,
colorFormat,
colorSpace,
grayMethod,
externalRef)
-- Cache commonly used methods.
local strpack <const> = string.pack
local tconcat <const> = table.concat
local tinsert <const> = table.insert
local floor <const> = math.floor
local max <const> = math.max
local min <const> = math.min
local lenPalette <const> = #palette
---@type string[]
local binWords <const> = {}
local numColors = 0
local writeLab <const> = colorFormat == "LAB"
local writeGry <const> = colorFormat == "GRAY"
local writeCmyk <const> = colorFormat == "CMYK"
local writeHsb <const> = colorFormat == "HSB"
local calcLinear <const> = writeLab or writeGry or writeCmyk
local isAdobe <const> = colorSpace == "ADOBE_RGB"
local isP3 <const> = colorSpace == "DISPLAY_P3"
local isKrita <const> = externalRef == "KRITA"
-- Krita and GIMP treat gray differently
local exponent = 1.0
if isKrita then exponent = 2.2 end
local isGryHsv <const> = grayMethod == "HSV"
local isGryHsi <const> = grayMethod == "HSI"
local isGryHsl <const> = grayMethod == "HSL"
local isGryLuma <const> = grayMethod == "LUMA"
local isGryAdobeY <const> = isAdobe and isGryLuma
local isGryP3Y <const> = isP3 and isGryLuma
local pkZero <const> = strpack(">I2", 0)
local pkColorFormat = strpack(">I2", 0)
if writeGry then
pkColorFormat = strpack(">I2", 8)
elseif writeLab then
pkColorFormat = strpack(">I2", 7)
elseif writeCmyk then
pkColorFormat = strpack(">I2", 2)
elseif writeHsb then
pkColorFormat = strpack(">I2", 1)
end
local i = 0
while i < lenPalette do
local aseColor <const> = palette:getColor(i)
if aseColor.alpha > 0 then
-- Unpack color.
local r8 <const> = aseColor.red
local g8 <const> = aseColor.green
local b8 <const> = aseColor.blue
local r01Gamma <const> = r8 / 255.0
local g01Gamma <const> = g8 / 255.0
local b01Gamma <const> = b8 / 255.0
local pkw = pkZero
local pkx = pkZero
local pky = pkZero
local pkz = pkZero
if calcLinear then
local r01Linear = 0.0
local g01Linear = 0.0
local b01Linear = 0.0
local xCie = 0.0
local yCie = 0.0
local zCie = 0.0
if isAdobe then
r01Linear, g01Linear, b01Linear = gammaAdobeRgbToLinearAdobeRgb(r01Gamma, g01Gamma, b01Gamma)
xCie, yCie, zCie = linearAdobeRgbToCieXyz(r01Linear, g01Linear, b01Linear)
elseif isP3 then
r01Linear, g01Linear, b01Linear = gammasRgbToLinearsRgb(r01Gamma, g01Gamma, b01Gamma)
xCie, yCie, zCie = linearP3ToCieXyz(r01Linear, g01Linear, b01Linear)
else
r01Linear, g01Linear, b01Linear = gammasRgbToLinearsRgb(r01Gamma, g01Gamma, b01Gamma)
xCie, yCie, zCie = linearsRgbToCieXyz(r01Linear, g01Linear, b01Linear)
end
if writeGry then
local gray = 0.0