forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.d
298 lines (241 loc) · 5.92 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
module arsd.color;
// NOTE: this is obsolete. use color.d instead.
import std.stdio;
import std.math;
import std.conv;
import std.algorithm;
struct Color {
ubyte r;
ubyte g;
ubyte b;
ubyte a;
this(int red, int green, int blue, int alpha = 255) {
this.r = cast(ubyte) red;
this.g = cast(ubyte) green;
this.b = cast(ubyte) blue;
this.a = cast(ubyte) alpha;
}
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);
}
string toString() {
import std.string;
if(a == 255)
return format("%02x%02x%02x", r, g, b);
else
return format("%02x%02x%02x%02x", r, g, b, a);
}
}
Color fromHsl(real[3] hsl) {
return fromHsl(hsl[0], hsl[1], hsl[2]);
}
Color fromHsl(real h, real s, real l) {
h = h % 360;
real C = (1 - abs(2 * l - 1)) * s;
real hPrime = h / 60;
real X = C * (1 - abs(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),
255);
}
real[3] toHsl(Color c) {
real r1 = cast(real) c.r / 255;
real g1 = cast(real) c.g / 255;
real b1 = cast(real) c.b / 255;
real maxColor = max(r1, g1, b1);
real minColor = min(r1, g1, b1);
real L = (maxColor + minColor) / 2 ;
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
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);
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);
}
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;
}