-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.py
executable file
·434 lines (347 loc) · 13.8 KB
/
color.py
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
from math import radians, cos, sin, sqrt, radians, pow
import math
import colorsys
from colormath.color_objects import (LCHuvColor, LCHabColor, HSLColor,
HSVColor, IPTColor, sRGBColor,
XYZColor, AdobeRGBColor)
from colormath.color_conversions import convert_color
from colorspacious import cspace_convert, CAM02UCS, CAM02SCD, CAM02LCD
def _rgb1_to_rgb255(r, g, b):
return round(r*255), round(g*255), round(b*255)
def _hls_to_yiq(H, L, S):
y = L/100
i = S/100*cos(radians(H))
q = S/100*sin(radians(H))
return y, i, q
def yiq(start, amount, saturation, luminosity):
step = 360/amount
ans = ((start + i*step, luminosity, saturation) for i in range(amount))
ans = (_hls_to_yiq(H, L, S) for H, L, S in ans)
ans = (colorsys.yiq_to_rgb(y, i, q) for y, i, q in ans)
ans = (_rgb1_to_rgb255(r, g, b) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
def yiq_mixer(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = colorsys.rgb_to_hls(rgb1)
x2, y2, z2 = colorsys.rgb_to_hls(rgb2)
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = _hls_to_yiq(*ans)
ans = colorsys.yiq_to_rgb(*ans)
return "#%02x%02x%02x" % ans
def _hls_to_ycbcr(H, L, S):
Y = L/100
Cb = S/100*cos(radians(H))
Cr = S/100*sin(radians(H))
return Y, Cb, Cr
def _rgb1_to_rgb255(r, g, b):
return round(r*255), round(g*255), round(b*255)
def _ycbcr_to_rgb(Y, Cb, Cr):
r = Y - 0.000170345404941155*Cb + 1.40214249139129*Cr
g = Y - 0.345602379281664*Cb - 0.71447536324549*Cr
b = Y + 1.77101547535512*Cb + 9.48244798877522e-5*Cr
r = 0 if r < 0 else 1 if r > 1 else r
g = 0 if g < 0 else 1 if g > 1 else g
b = 0 if b < 0 else 1 if b > 1 else b
return r, g, b
def cat02(start, amount, saturation, luminosity):
step = 360/amount
ans = ((start + i*step, luminosity, saturation) for i in range(amount))
ans = (_hls_to_ycbcr(H, L, S) for H, L, S in ans)
ans = (_ycbcr_to_rgb(Y, Cb, Cr) for Y, Cb, Cr in ans)
ans = (_rgb1_to_rgb255(r, g, b) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
def _ycbcr_to_rgb_2(Y, Cb, Cr):
r = -7.65413663080094*Cb + 3.07695666724817*Cr + 5.909003314299*Y
g = 11.3940532344494*Cb - 2.46426433976758*Cr - 9.14035819725992*Y
b = -2.73991660364845*Cb + 0.387307672519408*Cr + 4.23135488296092*Y
r = 0 if r < 0 else 1 if r > 1 else r
g = 0 if g < 0 else 1 if g > 1 else g
b = 0 if b < 0 else 1 if b > 1 else b
return r, g, b
def ycbcr(start, amount, saturation, luminosity):
step = 360/amount
ans = ((start + i*step, luminosity, saturation) for i in range(amount))
ans = (_hls_to_ycbcr(H, L, S) for H, L, S in ans)
ans = (_ycbcr_to_rgb_2(Y, Cb, Cr) for Y, Cb, Cr in ans)
ans = (_rgb1_to_rgb255(r, g, b) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
def yuv_mixer(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = colorsys.rgb_to_hls(rgb1)
x2, y2, z2 = colorsys.rgb_to_hls(rgb2)
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = _hls_to_yuv(*ans)
ans = _yuv_to_rgb(*ans)
return "#%02x%02x%02x" % ans
def to_hex_rgb(gen):
ans = ((v.clamped_rgb_r, v.clamped_rgb_g,v.clamped_rgb_b) for v in gen)
ans = ((round(r*255), round(g*255), round(b*255)) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
def lchab(l, c, h):
return convert_color(LCHabColor(l, c, h), sRGBColor, target_illuminant="d65")
def lchuv(l, c, h):
return convert_color(LCHuvColor(l, c, h), sRGBColor, target_illuminant="d65")
def hsl(l, c, h):
return convert_color(HSLColor(h, c/100, l/100), sRGBColor, target_illuminant="d65")
def hsv(l, c, h):
return convert_color(HSVColor(h, c/100, l/100), sRGBColor, target_illuminant="d65")
def ipt(l, c, h):
i = l/100
p = c/100*cos(radians(h))
t = c/100*sin(radians(h))
return convert_color(IPTColor(i, p, t), sRGBColor, target_illuminant="d65")
def bt2020color(l, c, h):
return convert_color(BT2020Color(h, c/100, l/100), sRGBColor, target_illuminant="d65")
def create_system(space):
def hex_colors(start, amount, saturation, luminosity):
k = 360/amount
ans = (space(luminosity, saturation, start + i*k) for i in range(amount))
return to_hex_rgb(ans)
return hex_colors
def hex_to_rgb(value):
"""Return (red, green, blue) for the color given as #rrggbb."""
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def hex_to_space(color, space):
color_tuple = hex_to_rgb(color)
values = list(i/255 for i in color_tuple)
return convert_color(sRGBColor(*values), space, target_illuminant="d65").get_value_tuple()
def create_mixer(space):
def mix(color1, color2):
x1, y1, z1 = hex_to_space(color1, space)
x2, y2, z2 = hex_to_space(color2, space)
color3 = space((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = convert_color(color3, sRGBColor, target_illuminant="d65")
ans = (ans.clamped_rgb_r, ans.clamped_rgb_g, ans.clamped_rgb_b)
ans = tuple(round(i*255) for i in ans)
return "#%02x%02x%02x" % ans
return mix
def jch(start, amount, saturation, luminosity):
k = 360/amount
ans = ((luminosity, saturation, start + i*k) for i in range(amount))
ans = (cspace_convert(color, "JCh", "XYZ1") for color in ans)
ans = (XYZColor(*color.tolist()) for color in ans)
ans = (convert_color(color, AdobeRGBColor, target_illuminant="d65") for color in ans)
ans = ((color.clamped_rgb_r, color.clamped_rgb_g, color.clamped_rgb_b) for color in ans)
ans = ((round(r*255), round(g*255), round(b*255)) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
def jch_mixer(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = cspace_convert(rgb1, "sRGB255", "JCh").tolist()
x2, y2, z2 = cspace_convert(rgb2, "sRGB255", "JCh").tolist()
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = cspace_convert(ans, "JCh", "sRGB1").tolist()
ans = sRGBColor(*ans)
ans = (ans.clamped_rgb_r, ans.clamped_rgb_g, ans.clamped_rgb_b)
ans = tuple(round(i*255) for i in ans)
return "#%02x%02x%02x" % ans
def ipt_jch(start, amount, saturation, luminosity):
# Generate colors
k = 360/amount
colors = [(luminosity, saturation, start + i*k) for i in range(amount)]
# From lch to IPT
ans = ((l/100, c/100*cos(radians(h)), c/100*sin(radians(h)))
for l, c, h in colors)
# From IPT to XYZ1
ans = (convert_color(IPTColor(i, p, t), XYZColor, target_illuminant="d65")
for i, p, t in ans)
ipt_colors = (color.get_value_tuple() for color in ans)
# From JCh to XYZ1
ans = (cspace_convert(color, "JCh", "XYZ1") for color in colors)
jch_colors = (color.tolist() for color in ans)
# Compute average
ans = (((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
for (x1, y1, z1), (x2, y2, z2)
in zip(ipt_colors, jch_colors))
# From XYZ1 to sRGB1
ans = (cspace_convert(color, "XYZ1", "sRGB1") for color in ans)
ans = ((color.tolist() for color in ans))
ans = (sRGBColor(*color) for color in ans)
return to_hex_rgb(ans)
def jmh(start, amount, saturation, luminosity):
k = 360/amount
ans = ((luminosity, saturation, start + i*k) for i in range(amount))
ans = (cspace_convert(color, "JMh", "sRGB1") for color in ans)
ans = (color.tolist() for color in ans)
ans = (sRGBColor(*color) for color in ans)
return to_hex_rgb(ans)
def jmh_mixer(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = cspace_convert(rgb1, "sRGB255", "JMh").tolist()
x2, y2, z2 = cspace_convert(rgb2, "sRGB255", "JMh").tolist()
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = cspace_convert(ans, "JMh", "sRGB1").tolist()
ans = sRGBColor(*ans)
ans = (ans.clamped_rgb_r, ans.clamped_rgb_g, ans.clamped_rgb_b)
ans = tuple(round(i*255) for i in ans)
return "#%02x%02x%02x" % ans
def jmh_mixer_2(*colors):
n = len(colors)
ans = (hex_to_rgb(color) for color in colors)
ans = (cspace_convert(color, "sRGB255", "JMh") for color in ans)
ans = (color.tolist() for color in ans)
ans = zip(*ans)
ans = (sum(items)/n for items in ans)
return list(ans)
def create_cie_system(cie_string):
def hex_colors(start, amount, saturation, luminosity):
k = 360/amount
ans = ((luminosity, saturation, start + i*k) for i in range(amount))
ans = (cspace_convert(color, cie_string, "sRGB1") for color in ans)
ans = (color.tolist() for color in ans)
ans = (sRGBColor(*color) for color in ans)
return to_hex_rgb(ans)
return hex_colors
def create_cie_mixer(cie_string):
def mix(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = cspace_convert(rgb1, "sRGB255", cie_string).tolist()
x2, y2, z2 = cspace_convert(rgb2, "sRGB255", cie_string).tolist()
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = cspace_convert(ans, cie_string, "sRGB1").tolist()
ans = sRGBColor(*ans)
ans = (ans.clamped_rgb_r, ans.clamped_rgb_g, ans.clamped_rgb_b)
ans = tuple(round(i*255) for i in ans)
return "#%02x%02x%02x" % ans
return mix
def create_cam02_system(cie_string):
def hex_colors(start, amount, saturation, luminosity):
k = 360/amount
ans = ((luminosity, saturation, start + i*k) for i in range(amount))
ans = ((l, c*cos(radians(h)), c*sin(radians(h))) for l, c, h in ans)
ans = (cspace_convert(color, cie_string, "sRGB1") for color in ans)
ans = (color.tolist() for color in ans)
ans = (sRGBColor(*color) for color in ans)
return to_hex_rgb(ans)
return hex_colors
def create_cam02_mixer(cie_string):
def mix(color1, color2):
rgb1, rgb2 = hex_to_rgb(color1), hex_to_rgb(color2)
x1, y1, z1 = cspace_convert(rgb1, "sRGB255", cie_string).tolist()
x2, y2, z2 = cspace_convert(rgb2, "sRGB255", cie_string).tolist()
ans = ((x1 + x2)/2, (y1 + y2)/2 , (z1 + z2)/2)
ans = cspace_convert(ans, cie_string, "sRGB1").tolist()
ans = sRGBColor(*ans)
ans = (ans.clamped_rgb_r, ans.clamped_rgb_g, ans.clamped_rgb_b)
ans = tuple(round(i*255) for i in ans)
return "#%02x%02x%02x" % ans
return mix
# OSR18a hue angles
R = 0
Y = 66
G = 136
C = 191
B = 246
M = 303
# Delta hue
d_RY = Y - R
d_YG = G - Y
d_GC = C - G
d_CB = B - C
d_BM = M - B
d_MR = 360 - M
# Luminosity
R_L = 0.25212576226954675
Y_L = 0.7200627962473862
G_L = 0.6479370339778395
C_L = 0.6486607905138208
B_L = 0.09072375653598125
M_L = 0.342849518805528
# Slope
s_RY = (Y_L - R_L)/d_RY
s_YG = (G_L - Y_L)/d_YG
s_GC = (C_L - G_L)/d_GC
s_CB = (B_L - C_L)/d_CB
s_BM = (M_L - B_L)/d_BM
s_MR = (R_L - M_L)/d_MR
def _hsl1_to_rgb(h, s, l):
s = s/100
l = l/100
d = l if l <= 0.5 else (1 - l)
while h > 360:
h = h - 360
if R <= h <= Y:
j = l - (-R*s_RY + R_L + h*s_RY - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs((h/d_RY)%2 - 1))
r, g, b = (c + m, x + m, m)
elif Y < h <= G:
j = l - (-Y*s_YG + Y_L + h*s_YG - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs(((G - h)/d_YG)%2 - 1))
r, g, b = (x + m, c + m, m)
elif G < h <= C:
j = l - (-G*s_GC + G_L + h*s_GC - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs(((G - h)/d_GC)%2 - 1))
r, g, b = (m, c + m, x + m)
elif C < h <= B:
j = l - (-C*s_CB + C_L + h*s_CB - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs(((B - h)/d_CB)%2 - 1))
r, g, b = (m, x + m, c + m)
elif B < h <= M:
j = l - (-B*s_BM + B_L + h*s_BM - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs(((B - h)/d_BM)%2 - 1))
r, g, b = (x + m, m, c + m)
elif M < h <= 360:
j = l - (-M*s_MR + M_L + h*s_MR - 0.5)*d*s
c = (1 - abs(2*j - 1))*s
m = j - c/2
x = c*(1 - abs(((360 - h)/d_MR)%2 - 1))
r, g, b = (c + m, m, x + m)
else:
r, g, b = (m, m, m)
return r, g, b
def hsl2(start, amount, saturation, luminosity):
step = 360/amount
ans = ((start + i*step, saturation, luminosity) for i in range(amount))
ans = (_hsl1_to_rgb(h, s, l) for h, s, l in ans)
ans = (_rgb1_to_rgb255(r, g, b) for r, g, b in ans)
ans = (("#%02x%02x%02x" % (r, g, b), r, g, b) for r, g, b in ans)
return ans
space = {
"HSL": create_system(hsl),
"HSL2": hsl2,
"HSV": create_system(hsv),
"YIQ": yiq,
"cat02": cat02,
"YCbCr": cat02,
"LCHab": create_system(lchab),
"LCHuv": create_system(lchuv),
"IPT": create_system(ipt),
"JCh": jch,
"JMh": jmh,
"CIELCHab": create_cie_system("CIELCh"),
"CAM02-UCS": create_cam02_system("CAM02-UCS"),
"CAM02-LCD": create_cam02_system("CAM02-LCD"),
"CAM02-SCD": create_cam02_system("CAM02-SCD"),
"IPT+JCh": ipt_jch,
}
mixer = {
"HSL": create_mixer(HSLColor),
"HSV": create_mixer(HSLColor),
"YIQ": yiq_mixer,
"LCHab": create_mixer(LCHabColor),
"LCHuv": create_mixer(LCHuvColor),
"IPT": create_mixer(IPTColor),
# "BT2020": create_mixer(BT2020Color),
"JCh": jch_mixer,
"JMh": jmh_mixer,
"CIELCHab": create_cie_mixer("CIELCh"),
"CAM02-UCS": create_cam02_mixer("CAM02-UCS"),
"CAM02-LCD": create_cam02_mixer("CAM02-LCD"),
"CAM02-SCD": create_cam02_mixer("CAM02-SCD"),
}