-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorop.py
229 lines (181 loc) · 5.43 KB
/
colorop.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
"""
Color operations.
RGB -> XYZ -> L*ab
+ CIEDE2000 itself.
All credits go to http://easyrgb.com
"""
import math
REF_X = 95.047
REF_Y = 100.000
REF_Z = 108.883
def xyz_color_norm(color):
"""Helper function for normalizing color during conversion to XYZ"""
if color > 0.04045:
color = math.pow(((color + 0.055) / 1.055), 2.4)
else:
color = color / 12.92
return color * 100
def rgb_to_xyz(ro, go, bo):
""""Converts given RGB color to XYZ"""
r, g, b = [
xyz_color_norm(x / 255.0)
for x in (ro, go, bo)
]
return (
r * 0.4124 + g * 0.3576 + b * 0.1805,
r * 0.2126 + g * 0.7152 + b * 0.0722,
r * 0.0193 + g * 0.1192 + b * 0.9505
)
def laab_color_norm(c):
"""Helper function for normalizing color during conversion to L*ab"""
if c > 0.008856:
return math.pow(c, 1 / 3.0)
else:
return (7.787 * c) + (16 / 116.0)
def xyz_to_laab(x, y, z):
"""Converts given XYZ color to L*ab"""
var_x = laab_color_norm(x / REF_X)
var_y = laab_color_norm(y / REF_Y)
var_z = laab_color_norm(z / REF_Z)
return (
(116 * var_y) - 16,
500 * (var_x - var_y),
200 * (var_y - var_z)
)
def cie_lab_2hue(a, b):
"""Color difference helper"""
bias = 0
if a >= 0 and b == 0:
return 0
if a < 0 and b == 0:
return 180
if a == 0 and b > 0:
return 90
if a == 0 and b < 0:
return 270
if a > 0 and b > 0:
bias = 0
if a < 0:
bias = 180
if a > 0 and b < 0:
bias = 360
return (math.degrees(math.atan(b / float(a))) + bias)
def color_diff_rgb(c1, c2, whtl=1, whtc=1, whth=1):
"""Converts colors from RGB to XYZ before passing them to color_diff_xyz"""
c1 = rgb_to_xyz(*c1)
c2 = rgb_to_xyz(*c2)
return color_diff_xyz(c1, c2, whtl, whtc, whth)
def color_diff_xyz(c1, c2, whtl=1, whtc=1, whth=1):
"""Converts colors from XYZ to L*ab before passing them to color_diff_laab"""
c1 = xyz_to_laab(*c1)
c2 = xyz_to_laab(*c2)
return color_diff_laab(c1, c2, whtl, whtc, whth)
def color_diff_laab(c1, c2, whtl=1, whtc=1, whth=1):
"""Computes difference between L*ab colors with CIEDE2000 algorithm"""
ciea1, cieb1, ciel1 = c1
ciea2, cieb2, ciel2 = c2
xC1 = math.sqrt(ciea1 * ciea1 + cieb1 * cieb1)
xC2 = math.sqrt(ciea2 * ciea2 + cieb2 * cieb2)
xCX = (xC1 + xC2) / 2.0
xGX = 0.5 * (1 - math.sqrt(math.pow(xCX, 7) / (math.pow(xCX, 7) + math.pow(25, 7))))
xNN = (1 + xGX) * ciea1
xC1 = math.sqrt(xNN * xNN + cieb1 * cieb1)
xH1 = cie_lab_2hue(xNN, cieb1)
xNN = (1 + xGX) * ciea2
xC2 = math.sqrt(xNN * xNN + cieb2 * cieb2)
xH2 = cie_lab_2hue(xNN, cieb2)
xDL = ciel2 - ciel1
xDC = xC2 - xC1
if xC1 * xC2 == 0:
xDH = 0
else:
xNN = round(xH2 - xH1, 12)
if abs(xNN) <= 180:
xDH = xH2 - xH1
elif xNN > 180:
xDH = xH2 - xH1 - 360
else:
xDH = xH2 - xH1 + 360
xDH = 2 * math.sqrt(xC1 * xC2) * math.sin(math.radians(xDH / 2.0))
xLX = (ciel1 + ciel2) / 2.0
xCY = (xC1 + xC2) / 2.0
if xC1 * xC2 == 0:
xHX = xH1 + xH2
else:
xNN = abs(round(xH1 - xH2, 12))
if xNN > 180:
if xH2 + xH1 < 360:
xHX = xH1 + xH2 + 360
else:
xHX = xH1 + xH2 - 360
else:
xHX = xH1 + xH2
xHX /= 2
xTX = 1 - 0.17 * math.cos(math.radians(xHX - 30)) + 0.24 \
* math.cos(math.radians(2 * xHX)) + 0.32 \
* math.cos(math.radians(3 * xHX + 6)) - 0.20 \
* math.cos(math.radians(4 * xHX - 63))
xPH = 30 * math.exp(-((xHX - 275) / 25.0) * ((xHX - 275) / 25.0))
xRC = 2 * math.sqrt(math.pow(xCY, 7) / (math.pow(xCY, 7) + math.pow(25, 7)))
xSL = 1 + ((0.015 * ((xLX - 50) * (xLX - 50))) /
math.sqrt(20 + ((xLX - 50) * (xLX - 50))))
xSC = 1 + 0.045 * xCY
xSH = 1 + 0.015 * xCY * xTX
xRT = - math.sin(math.radians(2 * xPH)) * xRC
xDL = xDL / (whtl * xSL)
xDC = xDC / (whtc * xSC)
xDH = xDH / (whth * xSH)
return math.sqrt(math.pow(xDL, 2) + math.pow(xDC, 2) + math.pow(xDH, 2) + xRT * xDC * xDH)
def rgb_to_hsl(r, g, b):
r, g, b = r / 255.0, g / 255.0, b / 255.0
minc = min(r, g, b)
maxc = max(r, g, b)
d = maxc - minc
if d == 0:
h = 0
elif maxc == r:
h = ((g - b) / d) % 6
elif maxc == g:
h = (b - r) / d + 2
else:
h = (r - g) / d + 4
h *= (1 / 3.0)
if h < 0:
h += 1
l = (maxc + minc) / 2.0
if d == 0:
s = 0
else:
s = d / (1 - abs(2 * l - 1))
return (h, s, l)
def hsl_to_rgb(h, s, l):
c = (1 - abs(2 * l - 1)) * s
hh = h * (1 / 3.0)
x = c * (1 - abs(hh % 2 - 1))
r, g, b = 0, 0, 0
if hh >= 0 and hh < 1:
r, g = c, x
elif hh >= 1 and hh < 2:
r, g = x, c
elif hh >= 2 and hh < 3:
g, b = c, x
elif hh >= 3 and hh < 4:
g, b = x, c
elif hh >= 4 and hh < 5:
r, b = x, c
else:
r, b = c, x
m = l - c / 2.0
return (
int(math.ceil((r + m) * 255)),
int(math.ceil((g + m) * 255)),
int(math.ceil((b + m) * 255))
)
def opposite_hsl(h, s, l):
h += 0.5
if h > 1:
h -= 1
return h, s, l
def opposite_rgb(r, g, b):
"""Simple opposite color calculation"""
return hsl_to_rgb(*opposite_hsl(*rgb_to_hsl(r, g, b)))