37 |
LAB = RGBtoLAB(RGB) |
LAB = RGBtoLAB(RGB) |
38 |
return math.atan2(LAB[2],LAB[1]) |
return math.atan2(LAB[2],LAB[1]) |
39 |
|
|
|
|
|
|
colors = [ (1,0,0), (1,.5,0), |
|
|
(1,1,0), #(.5,1,0), |
|
|
(0,1,0), (0,1,1), |
|
|
(0,0,1), (1,0,1) ] |
|
|
angles = map(LABhue, colors) |
|
|
angles = map(lambda a: a + (a < 0) * 2 * math.pi, angles) |
|
|
angles.sort() |
|
|
angles += [angles[0] + 2 * math.pi] |
|
|
|
|
|
def getangle(f): |
|
|
f *= len(colors) |
|
|
index = int(f) % len(colors) |
|
|
fract = f - int(f) |
|
|
return (1 - fract) * angles[index] + fract * angles[index + 1] |
|
|
|
|
|
|
|
40 |
def abdiff(a, b): |
def abdiff(a, b): |
41 |
u = RGBtoLAB(a) |
u = RGBtoLAB(a) |
42 |
v = RGBtoLAB(b) |
v = RGBtoLAB(b) |
130 |
Wg = 0.715160 |
Wg = 0.715160 |
131 |
Wb = 0.072169 |
Wb = 0.072169 |
132 |
|
|
133 |
|
Wr,Wg,Wb = 0.3, 0.59, 0.11 |
134 |
|
|
135 |
def YSTtoRGB(v): |
def YSTtoRGB(v): |
136 |
n = 1.0 / (Wr+Wg+Wb) |
n = 1.0 / (Wr+Wg+Wb) |
137 |
mat = [ [n, n*(Wg+Wb), n*(Wb - Wg) / math.sqrt(3) ], |
mat = [ [n, n*(Wg+Wb), n*(Wb - Wg) / math.sqrt(3) ], |
178 |
return YSTtoRGB((Y,r*S,r*T)) |
return YSTtoRGB((Y,r*S,r*T)) |
179 |
|
|
180 |
|
|
|
|
|
181 |
def YSThue(RGB): |
def YSThue(RGB): |
182 |
YST = RGBtoYST(RGB) |
YST = RGBtoYST(RGB) |
183 |
return math.atan2(YST[2],YST[1]) |
return math.atan2(YST[2],YST[1]) |
184 |
|
|
185 |
|
|
186 |
|
def YtoL(Y): |
187 |
|
""" |
188 |
|
Convert linear luminance into perceptual lightness (CIE L*) |
189 |
|
Y: lightness between 0 and 1 |
190 |
|
returns: luminance between 0 and 100 |
191 |
|
""" |
192 |
|
if Y <= (216./24389): |
193 |
|
return Y * (24389./27) |
194 |
|
else: |
195 |
|
return 116 * pow(Y, 1./3) - 16 |
196 |
|
|
197 |
|
def LtoY(L): |
198 |
""" |
""" |
199 |
# Kluge: Emulate LAB color space using YST hues |
Convert perceptual lightness (CIE L*) into linear luminance |
200 |
angles = map(YSThue, colors) |
L: luminance between 0 and 100 |
201 |
angles = map(lambda a: a + (a < 0) * 2 * math.pi, angles) |
returns: lightness between 0 and 1 |
202 |
angles.sort() |
""" |
203 |
angles += [angles[0] + 2 * math.pi] |
if L <= 8: |
204 |
|
return L * (27./24389) |
205 |
LABclamp = clampSat |
else: |
206 |
LABtoRGB = lambda lab: YSTtoRGB((lab[0]*.01,lab[1]*.01,lab[2]*.01)) |
return pow((L + 16.0) / 116, 3) |
|
|
|
|
def RGBtoLAB(rgb): |
|
|
yst = RGBtoYST(rgb) |
|
|
return [ -16 + 116 * pow(yst[0], 1./3), 100 * yst[1], 100 * yst[2] ] |
|
|
|
|
|
def LABtoRGB(lab): |
|
|
yst = [ pow((lab[0] + 16.0) / 116, 3), lab[1] / 100.0, lab[2] / 100.0 ] |
|
|
return YSTtoRGB(yst) |
|
|
""" |
|