forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.d
1147 lines (952 loc) · 26 KB
/
color.d
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
///
module arsd.color;
@safe:
// importing phobos explodes the size of this code 10x, so not doing it.
private {
real toInternal(T)(string s) {
real accumulator = 0.0;
size_t i = s.length;
foreach(idx, c; s) {
if(c >= '0' && c <= '9') {
accumulator *= 10;
accumulator += c - '0';
} else if(c == '.') {
i = idx + 1;
break;
} else
throw new Exception("bad char to make real from " ~ s);
}
real accumulator2 = 0.0;
real count = 1;
foreach(c; s[i .. $]) {
if(c >= '0' && c <= '9') {
accumulator2 *= 10;
accumulator2 += c - '0';
count *= 10;
} else
throw new Exception("bad char to make real from " ~ s);
}
return accumulator + accumulator2 / count;
}
@trusted
string toInternal(T)(int a) {
if(a == 0)
return "0";
char[] ret;
while(a) {
ret ~= (a % 10) + '0';
a /= 10;
}
for(int i = 0; i < ret.length / 2; i++) {
char c = ret[i];
ret[i] = ret[$ - i - 1];
ret[$ - i - 1] = c;
}
return cast(string) ret;
}
string toInternal(T)(real a) {
// a simplifying assumption here is the fact that we only use this in one place: toInternal!string(cast(real) a / 255)
// thus we know this will always be between 0.0 and 1.0, inclusive.
if(a <= 0.0)
return "0.0";
if(a >= 1.0)
return "1.0";
string ret = "0.";
// I wonder if I can handle round off error any better. Phobos does, but that isn't worth 100 KB of code.
int amt = cast(int)(a * 1000);
return ret ~ toInternal!string(amt);
}
real absInternal(real a) { return a < 0 ? -a : a; }
real minInternal(real a, real b, real c) {
auto m = a;
if(b < m) m = b;
if(c < m) m = c;
return m;
}
real maxInternal(real a, real b, real c) {
auto m = a;
if(b > m) m = b;
if(c > m) m = c;
return m;
}
bool startsWithInternal(string a, string b) {
return (a.length >= b.length && a[0 .. b.length] == b);
}
string[] splitInternal(string a, char c) {
string[] ret;
size_t previous = 0;
foreach(i, char ch; a) {
if(ch == c) {
ret ~= a[previous .. i];
previous = i + 1;
}
}
if(previous != a.length)
ret ~= a[previous .. $];
return ret;
}
string stripInternal(string s) {
foreach(i, char c; s)
if(c != ' ' && c != '\t' && c != '\n') {
s = s[i .. $];
break;
}
for(int a = cast(int)(s.length - 1); a > 0; a--) {
char c = s[a];
if(c != ' ' && c != '\t' && c != '\n') {
s = s[0 .. a + 1];
break;
}
}
return s;
}
}
// done with mini-phobos
/// Represents an RGBA color
struct Color {
union {
ubyte[4] components; ///
struct {
ubyte r; /// red
ubyte g; /// green
ubyte b; /// blue
ubyte a; /// alpha. 255 == opaque
}
uint asUint; ///
}
// this makes sure they are in range before casting
static Color fromIntegers(int red, int green, int blue, int alpha = 255) {
if(red < 0) red = 0; if(red > 255) red = 255;
if(green < 0) green = 0; if(green > 255) green = 255;
if(blue < 0) blue = 0; if(blue > 255) blue = 255;
if(alpha < 0) alpha = 0; if(alpha > 255) alpha = 255;
return Color(red, green, blue, alpha);
}
/// .
this(int red, int green, int blue, int alpha = 255) {
// workaround dmd bug 10937
if(__ctfe)
this.components[0] = cast(ubyte) red;
else
this.r = cast(ubyte) red;
this.g = cast(ubyte) green;
this.b = cast(ubyte) blue;
this.a = cast(ubyte) alpha;
}
/// Convenience functions for common color names
static Color transparent() { return Color(0, 0, 0, 0); }
static Color white() { return Color(255, 255, 255); } ///.
static Color black() { return Color(0, 0, 0); } ///.
static Color red() { return Color(255, 0, 0); } ///.
static Color green() { return Color(0, 255, 0); } ///.
static Color blue() { return Color(0, 0, 255); } ///.
static Color yellow() { return Color(255, 255, 0); } ///.
static Color teal() { return Color(0, 255, 255); } ///.
static Color purple() { return Color(255, 0, 255); } ///.
/*
ubyte[4] toRgbaArray() {
return [r,g,b,a];
}
*/
/// Makes a string that matches CSS syntax for websites
string toCssString() {
if(a == 255)
return "#" ~ toHexInternal(r) ~ toHexInternal(g) ~ toHexInternal(b);
else {
return "rgba("~toInternal!string(r)~", "~toInternal!string(g)~", "~toInternal!string(b)~", "~toInternal!string(cast(real)a / 255.0)~")";
}
}
/// Makes a hex string RRGGBBAA (aa only present if it is not 255)
string toString() {
if(a == 255)
return toCssString()[1 .. $];
else
return toRgbaHexString();
}
/// returns RRGGBBAA, even if a== 255
string toRgbaHexString() {
return toHexInternal(r) ~ toHexInternal(g) ~ toHexInternal(b) ~ toHexInternal(a);
}
/// Gets a color by name, iff the name is one of the static members listed above
static Color fromNameString(string s) {
Color c;
foreach(member; __traits(allMembers, Color)) {
static if(__traits(compiles, c = __traits(getMember, Color, member))) {
if(s == member)
return __traits(getMember, Color, member);
}
}
throw new Exception("Unknown color " ~ s);
}
/// Reads a CSS style string to get the color. Understands #rrggbb, rgba(), hsl(), and rrggbbaa
static Color fromString(string s) {
s = s.stripInternal();
Color c;
c.a = 255;
// trying named colors via the static no-arg methods here
foreach(member; __traits(allMembers, Color)) {
static if(__traits(compiles, c = __traits(getMember, Color, member))) {
if(s == member)
return __traits(getMember, Color, member);
}
}
// try various notations borrowed from CSS (though a little extended)
// hsl(h,s,l,a) where h is degrees and s,l,a are 0 >= x <= 1.0
if(s.startsWithInternal("hsl(") || s.startsWithInternal("hsla(")) {
assert(s[$-1] == ')');
s = s[s.startsWithInternal("hsl(") ? 4 : 5 .. $ - 1]; // the closing paren
real[3] hsl;
ubyte a = 255;
auto parts = s.splitInternal(',');
foreach(i, part; parts) {
if(i < 3)
hsl[i] = toInternal!real(part.stripInternal);
else
a = cast(ubyte) (toInternal!real(part.stripInternal) * 255);
}
c = .fromHsl(hsl);
c.a = a;
return c;
}
// rgb(r,g,b,a) where r,g,b are 0-255 and a is 0-1.0
if(s.startsWithInternal("rgb(") || s.startsWithInternal("rgba(")) {
assert(s[$-1] == ')');
s = s[s.startsWithInternal("rgb(") ? 4 : 5 .. $ - 1]; // the closing paren
auto parts = s.splitInternal(',');
foreach(i, part; parts) {
// lol the loop-switch pattern
auto v = toInternal!real(part.stripInternal);
switch(i) {
case 0: // red
c.r = cast(ubyte) v;
break;
case 1:
c.g = cast(ubyte) v;
break;
case 2:
c.b = cast(ubyte) v;
break;
case 3:
c.a = cast(ubyte) (v * 255);
break;
default: // ignore
}
}
return c;
}
// otherwise let's try it as a hex string, really loosely
if(s.length && s[0] == '#')
s = s[1 .. $];
// not a built in... do it as a hex string
if(s.length >= 2) {
c.r = fromHexInternal(s[0 .. 2]);
s = s[2 .. $];
}
if(s.length >= 2) {
c.g = fromHexInternal(s[0 .. 2]);
s = s[2 .. $];
}
if(s.length >= 2) {
c.b = fromHexInternal(s[0 .. 2]);
s = s[2 .. $];
}
if(s.length >= 2) {
c.a = fromHexInternal(s[0 .. 2]);
s = s[2 .. $];
}
return c;
}
/// from hsl
static Color fromHsl(real h, real s, real l) {
return .fromHsl(h, s, l);
}
}
private string toHexInternal(ubyte b) {
string s;
if(b < 16)
s ~= '0';
else {
ubyte t = (b & 0xf0) >> 4;
if(t >= 10)
s ~= 'A' + t - 10;
else
s ~= '0' + t;
b &= 0x0f;
}
if(b >= 10)
s ~= 'A' + b - 10;
else
s ~= '0' + b;
return s;
}
private ubyte fromHexInternal(string s) {
int result = 0;
int exp = 1;
//foreach(c; retro(s)) { // FIXME: retro doesn't work right in dtojs
foreach_reverse(c; s) {
if(c >= 'A' && c <= 'F')
result += exp * (c - 'A' + 10);
else if(c >= 'a' && c <= 'f')
result += exp * (c - 'a' + 10);
else if(c >= '0' && c <= '9')
result += exp * (c - '0');
else
// throw new Exception("invalid hex character: " ~ cast(char) c);
return 0;
exp *= 16;
}
return cast(ubyte) result;
}
/// Converts hsl to rgb
Color fromHsl(real[3] hsl) {
return fromHsl(hsl[0], hsl[1], hsl[2]);
}
/// Converts hsl to rgb
Color fromHsl(real h, real s, real l, real a = 255) {
h = h % 360;
real C = (1 - absInternal(2 * l - 1)) * s;
real hPrime = h / 60;
real X = C * (1 - absInternal(hPrime % 2 - 1));
real r, g, b;
if(h is real.nan)
r = g = b = 0;
else if (hPrime >= 0 && hPrime < 1) {
r = C;
g = X;
b = 0;
} else if (hPrime >= 1 && hPrime < 2) {
r = X;
g = C;
b = 0;
} else if (hPrime >= 2 && hPrime < 3) {
r = 0;
g = C;
b = X;
} else if (hPrime >= 3 && hPrime < 4) {
r = 0;
g = X;
b = C;
} else if (hPrime >= 4 && hPrime < 5) {
r = X;
g = 0;
b = C;
} else if (hPrime >= 5 && hPrime < 6) {
r = C;
g = 0;
b = X;
}
real m = l - C / 2;
r += m;
g += m;
b += m;
return Color(
cast(ubyte)(r * 255),
cast(ubyte)(g * 255),
cast(ubyte)(b * 255),
cast(ubyte)(a));
}
/// Converts an RGB color into an HSL triplet. useWeightedLightness will try to get a better value for luminosity for the human eye, which is more sensitive to green than red and more to red than blue. If it is false, it just does average of the rgb.
real[3] toHsl(Color c, bool useWeightedLightness = false) {
real r1 = cast(real) c.r / 255;
real g1 = cast(real) c.g / 255;
real b1 = cast(real) c.b / 255;
real maxColor = maxInternal(r1, g1, b1);
real minColor = minInternal(r1, g1, b1);
real L = (maxColor + minColor) / 2 ;
if(useWeightedLightness) {
// the colors don't affect the eye equally
// this is a little more accurate than plain HSL numbers
L = 0.2126*r1 + 0.7152*g1 + 0.0722*b1;
}
real S = 0;
real H = 0;
if(maxColor != minColor) {
if(L < 0.5) {
S = (maxColor - minColor) / (maxColor + minColor);
} else {
S = (maxColor - minColor) / (2.0 - maxColor - minColor);
}
if(r1 == maxColor) {
H = (g1-b1) / (maxColor - minColor);
} else if(g1 == maxColor) {
H = 2.0 + (b1 - r1) / (maxColor - minColor);
} else {
H = 4.0 + (r1 - g1) / (maxColor - minColor);
}
}
H = H * 60;
if(H < 0){
H += 360;
}
return [H, S, L];
}
/// .
Color lighten(Color c, real percentage) {
auto hsl = toHsl(c);
hsl[2] *= (1 + percentage);
if(hsl[2] > 1)
hsl[2] = 1;
return fromHsl(hsl);
}
/// .
Color darken(Color c, real percentage) {
auto hsl = toHsl(c);
hsl[2] *= (1 - percentage);
return fromHsl(hsl);
}
/// for light colors, call darken. for dark colors, call lighten.
/// The goal: get toward center grey.
Color moderate(Color c, real percentage) {
auto hsl = toHsl(c);
if(hsl[2] > 0.5)
hsl[2] *= (1 - percentage);
else {
if(hsl[2] <= 0.01) // if we are given black, moderating it means getting *something* out
hsl[2] = percentage;
else
hsl[2] *= (1 + percentage);
}
if(hsl[2] > 1)
hsl[2] = 1;
return fromHsl(hsl);
}
/// the opposite of moderate. Make darks darker and lights lighter
Color extremify(Color c, real percentage) {
auto hsl = toHsl(c, true);
if(hsl[2] < 0.5)
hsl[2] *= (1 - percentage);
else
hsl[2] *= (1 + percentage);
if(hsl[2] > 1)
hsl[2] = 1;
return fromHsl(hsl);
}
/// Move around the lightness wheel, trying not to break on moderate things
Color oppositeLightness(Color c) {
auto hsl = toHsl(c);
auto original = hsl[2];
if(original > 0.4 && original < 0.6)
hsl[2] = 0.8 - original; // so it isn't quite the same
else
hsl[2] = 1 - original;
return fromHsl(hsl);
}
/// Try to determine a text color - either white or black - based on the input
Color makeTextColor(Color c) {
auto hsl = toHsl(c, true); // give green a bonus for contrast
if(hsl[2] > 0.71)
return Color(0, 0, 0);
else
return Color(255, 255, 255);
}
// These provide functional access to hsl manipulation; useful if you need a delegate
Color setLightness(Color c, real lightness) {
auto hsl = toHsl(c);
hsl[2] = lightness;
return fromHsl(hsl);
}
///
Color rotateHue(Color c, real degrees) {
auto hsl = toHsl(c);
hsl[0] += degrees;
return fromHsl(hsl);
}
///
Color setHue(Color c, real hue) {
auto hsl = toHsl(c);
hsl[0] = hue;
return fromHsl(hsl);
}
///
Color desaturate(Color c, real percentage) {
auto hsl = toHsl(c);
hsl[1] *= (1 - percentage);
return fromHsl(hsl);
}
///
Color saturate(Color c, real percentage) {
auto hsl = toHsl(c);
hsl[1] *= (1 + percentage);
if(hsl[1] > 1)
hsl[1] = 1;
return fromHsl(hsl);
}
///
Color setSaturation(Color c, real saturation) {
auto hsl = toHsl(c);
hsl[1] = saturation;
return fromHsl(hsl);
}
/*
void main(string[] args) {
auto color1 = toHsl(Color(255, 0, 0));
auto color = fromHsl(color1[0] + 60, color1[1], color1[2]);
writefln("#%02x%02x%02x", color.r, color.g, color.b);
}
*/
/* Color algebra functions */
/* Alpha putpixel looks like this:
void putPixel(Image i, Color c) {
Color b;
b.r = i.data[(y * i.width + x) * bpp + 0];
b.g = i.data[(y * i.width + x) * bpp + 1];
b.b = i.data[(y * i.width + x) * bpp + 2];
b.a = i.data[(y * i.width + x) * bpp + 3];
float ca = cast(float) c.a / 255;
i.data[(y * i.width + x) * bpp + 0] = alpha(c.r, ca, b.r);
i.data[(y * i.width + x) * bpp + 1] = alpha(c.g, ca, b.g);
i.data[(y * i.width + x) * bpp + 2] = alpha(c.b, ca, b.b);
i.data[(y * i.width + x) * bpp + 3] = alpha(c.a, ca, b.a);
}
ubyte alpha(ubyte c1, float alpha, ubyte onto) {
auto got = (1 - alpha) * onto + alpha * c1;
if(got > 255)
return 255;
return cast(ubyte) got;
}
So, given the background color and the resultant color, what was
composited on to it?
*/
///
ubyte unalpha(ubyte colorYouHave, float alpha, ubyte backgroundColor) {
// resultingColor = (1-alpha) * backgroundColor + alpha * answer
auto resultingColorf = cast(float) colorYouHave;
auto backgroundColorf = cast(float) backgroundColor;
auto answer = (resultingColorf - backgroundColorf + alpha * backgroundColorf) / alpha;
if(answer > 255)
return 255;
if(answer < 0)
return 0;
return cast(ubyte) answer;
}
///
ubyte makeAlpha(ubyte colorYouHave, ubyte backgroundColor/*, ubyte foreground = 0x00*/) {
//auto foregroundf = cast(float) foreground;
auto foregroundf = 0.00f;
auto colorYouHavef = cast(float) colorYouHave;
auto backgroundColorf = cast(float) backgroundColor;
// colorYouHave = backgroundColorf - alpha * backgroundColorf + alpha * foregroundf
auto alphaf = 1 - colorYouHave / backgroundColorf;
alphaf *= 255;
if(alphaf < 0)
return 0;
if(alphaf > 255)
return 255;
return cast(ubyte) alphaf;
}
int fromHex(string s) {
int result = 0;
int exp = 1;
// foreach(c; retro(s)) {
foreach_reverse(c; s) {
if(c >= 'A' && c <= 'F')
result += exp * (c - 'A' + 10);
else if(c >= 'a' && c <= 'f')
result += exp * (c - 'a' + 10);
else if(c >= '0' && c <= '9')
result += exp * (c - '0');
else
throw new Exception("invalid hex character: " ~ cast(char) c);
exp *= 16;
}
return result;
}
///
Color colorFromString(string s) {
if(s.length == 0)
return Color(0,0,0,255);
if(s[0] == '#')
s = s[1..$];
assert(s.length == 6 || s.length == 8);
Color c;
c.r = cast(ubyte) fromHex(s[0..2]);
c.g = cast(ubyte) fromHex(s[2..4]);
c.b = cast(ubyte) fromHex(s[4..6]);
if(s.length == 8)
c.a = cast(ubyte) fromHex(s[6..8]);
else
c.a = 255;
return c;
}
/*
import browser.window;
import std.conv;
void main() {
import browser.document;
foreach(ele; document.querySelectorAll("input")) {
ele.addEventListener("change", {
auto h = toInternal!real(document.querySelector("input[name=h]").value);
auto s = toInternal!real(document.querySelector("input[name=s]").value);
auto l = toInternal!real(document.querySelector("input[name=l]").value);
Color c = Color.fromHsl(h, s, l);
auto e = document.getElementById("example");
e.style.backgroundColor = c.toCssString();
// JSElement __js_this;
// __js_this.style.backgroundColor = c.toCssString();
}, false);
}
}
*/
/**
This provides two image classes and a bunch of functions that work on them.
Why are they separate classes? I think the operations on the two of them
are necessarily different. There's a whole bunch of operations that only
really work on truecolor (blurs, gradients), and a few that only work
on indexed images (palette swaps).
Even putpixel is pretty different. On indexed, it is a palette entry's
index number. On truecolor, it is the actual color.
A greyscale image is the weird thing in the middle. It is truecolor, but
fits in the same size as indexed. Still, I'd say it is a specialization
of truecolor.
There is a subset that works on both
*/
/// An image in memory
interface MemoryImage {
//IndexedImage convertToIndexedImage() const;
//TrueColorImage convertToTrueColor() const;
/// gets it as a TrueColorImage. May return this or may do a conversion and return a new image
TrueColorImage getAsTrueColorImage();
/// Image width, in pixels
int width() const;
/// Image height, in pixels
int height() const;
}
/// An image that consists of indexes into a color palette. Use getAsTrueColorImage() if you don't care about palettes
class IndexedImage : MemoryImage {
bool hasAlpha;
/// .
Color[] palette;
/// the data as indexes into the palette. Stored left to right, top to bottom, no padding.
ubyte[] data;
/// .
override int width() const {
return _width;
}
/// .
override int height() const {
return _height;
}
private int _width;
private int _height;
/// .
this(int w, int h) {
_width = w;
_height = h;
data = new ubyte[w*h];
}
/*
void resize(int w, int h, bool scale) {
}
*/
/// returns a new image
override TrueColorImage getAsTrueColorImage() {
return convertToTrueColor();
}
/// Creates a new TrueColorImage based on this data
TrueColorImage convertToTrueColor() const {
auto tci = new TrueColorImage(width, height);
foreach(i, b; data) {
/*
if(b >= palette.length) {
string fuckyou;
fuckyou ~= b + '0';
fuckyou ~= " ";
fuckyou ~= palette.length + '0';
assert(0, fuckyou);
}
*/
tci.imageData.colors[i] = palette[b];
}
return tci;
}
/// Gets an exact match, if possible, adds if not. See also: the findNearestColor free function.
ubyte getOrAddColor(Color c) {
foreach(i, co; palette) {
if(c == co)
return cast(ubyte) i;
}
return addColor(c);
}
/// Number of colors currently in the palette (note: palette entries are not necessarily used in the image data)
int numColors() const {
return cast(int) palette.length;
}
/// Adds an entry to the palette, returning its inded
ubyte addColor(Color c) {
assert(palette.length < 256);
if(c.a != 255)
hasAlpha = true;
palette ~= c;
return cast(ubyte) (palette.length - 1);
}
}
/// An RGBA array of image data. Use the free function quantize() to convert to an IndexedImage
class TrueColorImage : MemoryImage {
// bool hasAlpha;
// bool isGreyscale;
//ubyte[] data; // stored as rgba quads, upper left to right to bottom
/// .
struct Data {
ubyte[] bytes; /// the data as rgba bytes. Stored left to right, top to bottom, no padding.
// the union is no good because the length of the struct is wrong!
/// the same data as Color structs
@trusted // the cast here is typically unsafe, but it is ok
// here because I guarantee the layout, note the static assert below
@property inout(Color)[] colors() inout {
return cast(inout(Color)[]) bytes;
}
static assert(Color.sizeof == 4);
}
/// .
Data imageData;
alias imageData.bytes data;
int _width;
int _height;
/// .
override int width() const { return _width; }
///.
override int height() const { return _height; }
/// .
this(int w, int h) {
_width = w;
_height = h;
imageData.bytes = new ubyte[w*h*4];
}
/// Creates with existing data. The data pointer is stored here.
this(int w, int h, ubyte[] data) {
_width = w;
_height = h;
assert(data.length == w * h * 4);
imageData.bytes = data;
}
/// Returns this
override TrueColorImage getAsTrueColorImage() {
return this;
}
}
/// Converts true color to an indexed image. It uses palette as the starting point, adding entries
/// until maxColors as needed. If palette is null, it creates a whole new palette.
///
/// After quantizing the image, it applies a dithering algorithm.
///
/// This is not written for speed.
IndexedImage quantize(in TrueColorImage img, Color[] palette = null, in int maxColors = 256)
// this is just because IndexedImage assumes ubyte palette values
in { assert(maxColors <= 256); }
body {
int[Color] uses;
foreach(pixel; img.imageData.colors) {
if(auto i = pixel in uses) {
(*i)++;
} else {
uses[pixel] = 1;
}
}
struct ColorUse {
Color c;
int uses;
//string toString() { import std.conv; return c.toCssString() ~ " x " ~ to!string(uses); }
int opCmp(ref const ColorUse co) const {
return co.uses - uses;
}
}
ColorUse[] sorted;
foreach(color, count; uses)
sorted ~= ColorUse(color, count);
uses = null;
version(no_phobos)
sorted = sorted.sort;
else {
import std.algorithm : sort;
sort(sorted);
}
ubyte[Color] paletteAssignments;
foreach(idx, entry; palette)
paletteAssignments[entry] = cast(ubyte) idx;
// For the color assignments from the image, I do multiple passes, decreasing the acceptable
// distance each time until we're full.
// This is probably really slow.... but meh it gives pretty good results.
auto ddiff = 32;
outer: for(int d1 = 128; d1 >= 0; d1 -= ddiff) {
auto minDist = d1*d1;
if(d1 <= 64)
ddiff = 16;
if(d1 <= 32)
ddiff = 8;
foreach(possibility; sorted) {
if(palette.length == maxColors)
break;
if(palette.length) {
auto co = palette[findNearestColor(palette, possibility.c)];
auto pixel = possibility.c;
auto dr = cast(int) co.r - pixel.r;
auto dg = cast(int) co.g - pixel.g;
auto db = cast(int) co.b - pixel.b;
auto dist = dr*dr + dg*dg + db*db;
// not good enough variety to justify an allocation yet
if(dist < minDist)
continue;
}
paletteAssignments[possibility.c] = cast(ubyte) palette.length;
palette ~= possibility.c;
}
}
// Final pass: just fill in any remaining space with the leftover common colors
while(palette.length < maxColors && sorted.length) {
if(sorted[0].c !in paletteAssignments) {
paletteAssignments[sorted[0].c] = cast(ubyte) palette.length;
palette ~= sorted[0].c;
}
sorted = sorted[1 .. $];
}
bool wasPerfect = true;
auto newImage = new IndexedImage(img.width, img.height);
newImage.palette = palette;
foreach(idx, pixel; img.imageData.colors) {
if(auto p = pixel in paletteAssignments)
newImage.data[idx] = *p;
else {
// gotta find the closest one...
newImage.data[idx] = findNearestColor(palette, pixel);
wasPerfect = false;
}
}
if(!wasPerfect)
floydSteinbergDither(newImage, img);
return newImage;
}
/// Finds the best match for pixel in palette (currently by checking for minimum euclidean distance in rgb colorspace)
ubyte findNearestColor(in Color[] palette, in Color pixel) {
int best = 0;
int bestDistance = int.max;
foreach(pe, co; palette) {
auto dr = cast(int) co.r - pixel.r;
auto dg = cast(int) co.g - pixel.g;
auto db = cast(int) co.b - pixel.b;
int dist = dr*dr + dg*dg + db*db;
if(dist < bestDistance) {
best = cast(int) pe;
bestDistance = dist;
}
}
return cast(ubyte) best;
}
/+
// Quantizing and dithering test program