changed some of the color-space conversion formulae.

This commit is contained in:
Mahesh Balasubramaniam 2013-01-04 12:00:10 +00:00
parent 738f363263
commit 7157e8f03c
1 changed files with 62 additions and 29 deletions

View File

@ -113,43 +113,76 @@ def XYZ2RGB(XYZ):
for i in range(3): for i in range(3):
RGB[i] = min(RGB[i],1.0) RGB[i] = min(RGB[i],1.0)
RGB[i] = max(RGB[i],0.0) RGB[i] = max(RGB[i],0.0)
maxVal = RGB[0]
if (maxVal < RGB[1]): maxVal = RGB[1]
if (maxVal < RGB[2]): maxVal = RGB[2]
if (maxVal > 1.0):
RGB[0] = RGB[0]/maxVal
RGB[1] = RGB[1]/maxVal
RGB[2] = RGB[2]/maxVal
return RGB return RGB
# convert CIE Lab to CIE XYZ # convert CIE Lab to CIE XYZ
# with XYZ in the range of 0 to 1 # with XYZ in the range of 0 to 1
# from http://en.wikipedia.org/wiki/Lab_color_space, http://www.cs.rit.edu/~ncs/color/t_convert.html # from http://www.easyrgb.com/index.php?X=MATH&H=07#text7
def CIELab2XYZ(Lab,white): def CIELab2XYZ(Lab,white):
# ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer= 2°, Illuminant= D65
XYZ = [0.0,0.0,0.0] XYZ = [0.0,0.0,0.0]
temp = [0.0,0.0,0.0] var_Y = ( Lab[0] + 16 ) / 116
temp[0] = 1.0/116.0 *(Lab[0] + 16.0) + 1/500.0 * Lab[1] var_X = Lab[1] / 500 + var_Y
temp[1] = 1.0/116.0 *(Lab[0] + 16.0) var_Z = var_Y - Lab[2] / 200
temp[2] = 1.0/116.0 *(Lab[0] + 16.0) - 1/200.0 * Lab[2]
for i in range(3): if ( var_Y**3 > 0.008856 ):
if (temp[i] > 6.0/29.0): var_Y = var_Y**3
temp[i] = temp[i]**(3.0)
else: else:
temp[i] =3 * (6.0/29.0)**2 * (temp[i]- 4.0/29.0) var_Y = ( var_Y-16/116) / 7.787
XYZ[i] = white[i] * temp[i]
for i in range(3): if ( var_X**3 > 0.008856 ):
XYZ[i] = min(XYZ[i],1.0) var_X = var_X**3
XYZ[i] = max(XYZ[i],0.0) else:
var_X = ( var_X-16/116) / 7.787
if ( var_Z**3 > 0.008856 ):
var_Z = var_Z**3
else:
var_Z = ( var_Z-16/116) / 7.787
XYZ[0] = white[0]*var_X
XYZ[1] = white[1]*var_Y
XYZ[2] = white[2]*var_Z
return XYZ return XYZ
# convert CIE XYZ to CIE Lab # convert CIE XYZ to CIE Lab
# with XYZ in the range of 0 to 1 # with XYZ in the range of 0 to 1
# from http://en.wikipedia.org/wiki/Lab_color_space, http://www.cs.rit.edu/~ncs/color/t_convert.html # from http://en.wikipedia.org/wiki/Lab_color_space, http://www.cs.rit.edu/~ncs/color/t_convert.html
def XYZ2CIELab(XYZ,white): def XYZ2CIELab(XYZ,white):
temp = [0.0,0.0,0.0] # ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer= 2°, Illuminant= D65
Lab = [0.0,0.0,0.0] Lab = [0.0,0.0,0.0]
for i in range(3): var_X = XYZ[0]/white[0]
temp[i] = XYZ[i]/white[i] var_Y = XYZ[1]/white[1]
if (temp[i] > (6.0/29.0)**3.0): var_Z = XYZ[2]/white[2]
temp[i] = temp[i]**(1.0/3.0)
if ( var_X > 0.008856 ):
var_X = var_X**(1.0/3.0)
else: else:
temp[i] = 1.0/3.0 * (29.0/6.0)**2.0 * temp[i] + 4.0/29.0 var_X = ( 7.787 * var_X ) + (16.0/116.0)
Lab[0] = 116.0 * temp[1] - 16.0
Lab[1] = 500.0 *(temp[0] - temp[1]) if ( var_Y > 0.008856 ):
Lab[2] = 200.0 *(temp[1] - temp[2]) var_Y = var_Y**(1.0/3.0)
else:
var_Y = ( 7.787 * var_Y ) + (16.0/116.0)
if ( var_Z > 0.008856 ):
var_Z = var_Z**(1.0/3.0)
else:
var_Z = (7.787*var_Z)+(16.0/116.0)
Lab[0] = (116.0 * var_Y) - 16.0
Lab[1] = 500.0 * (var_X - var_Y)
Lab[2] = 200.0 * (var_Y - var_Z)
return Lab return Lab
# convert Cie Lab to msh colorspace # convert Cie Lab to msh colorspace
@ -157,9 +190,9 @@ def XYZ2CIELab(XYZ,white):
def CIELab2Msh(Lab): def CIELab2Msh(Lab):
Msh = [0.0,0.0,0.0] Msh = [0.0,0.0,0.0]
Msh[0] = math.sqrt(Lab[0]**2.0 + Lab[1]**2.0 + Lab[2]**2.0) Msh[0] = math.sqrt(Lab[0]**2.0 + Lab[1]**2.0 + Lab[2]**2.0)
if (Msh[0] != 0.0): if (Msh[0] != 0.0) and (Msh[0] > 0.001):
Msh[1] = math.acos(Lab[0]/Msh[0]) Msh[1] = math.acos(Lab[0]/Msh[0])
if (Lab[1] != 0.0): if (Lab[1] != 0.0) and (Msh[1] > 0.001):
Msh[2] = math.atan2(Lab[2],Lab[1]) Msh[2] = math.atan2(Lab[2],Lab[1])
return Msh return Msh