put colormap related functions into library, renamed and added scripts for creation of colormaps

This commit is contained in:
Mahesh Balasubramaniam 2013-01-04 13:57:36 +00:00
parent 6ab79d5593
commit 820b8eb2c1
6 changed files with 300 additions and 249 deletions

View File

@ -4,6 +4,7 @@ import sys
from .environment import Environment # only one class
from .asciitable import ASCIItable # only one class
from .config import Material # will be extended to debug and numerics
from .colormaps import Colormaps # only one class
#from .block import Block # only one class
from .result import Result # one class with subclasses
from .geometry import Geometry # one class with subclasses

233
lib/damask/colormaps.py Normal file
View File

@ -0,0 +1,233 @@
#!/usr/bin/env python
class Colormaps():
'''
Funtionality to create colormaps
'''
# from http://code.activestate.com/recipes/121574-matrix-vector-multiplication/
def matmult(self, m, v):
nrows = len(m)
w = [None] * nrows
for row in range(nrows):
w[row] = reduce(lambda x,y: x+y, map(lambda x,y: x*y, m[row], v))
return w
# convert H(ue) S(aturation) L(uminance) to R(ot) G(elb) B(lau)
# with S,L,R,G,B running from 0 to 1, H running from 0 to 360
# from http://en.wikipedia.org/wiki/HSL_and_HSV
def HSL2RGB(self,HSL):
RGB = [0.0,0.0,0.0]
H_strich = HSL[0]/60.0
c = (1.0- abs(2.0 * HSL[2] - 1.0))*HSL[1]
x = c*(1.0- abs(H_strich%2-1.0))
m = HSL[2] -.5*c
if (0.0 <= H_strich)and(H_strich<1.0):
RGB[0] = c + m
RGB[1] = x + m
RGB[2] = 0.0 + m
elif (1.0 <= H_strich)and(H_strich<2.0):
RGB[0] = x + m
RGB[1] = c + m
RGB[2] = 0.0 + m
elif (2.0 <= H_strich)and(H_strich<3.0):
RGB[0] = 0.0 + m
RGB[1] = c + m
RGB[2] = x + m
elif (3.0 <= H_strich)and(H_strich<4.0):
RGB[0] = 0.0 + m
RGB[1] = x + m
RGB[2] = c + m
elif (4.0 <= H_strich)and(H_strich<5.0):
RGB[0] = x + m
RGB[1] = 0.0 + m
RGB[2] = c + m
elif (5.0 <= H_strich)and(H_strich<=6.0):
RGB[0] = c + m
RGB[1] = 0.0 + m
RGB[2] = x + m
for i in range(3):
RGB[i] = min(RGB[i],1.0)
RGB[i] = max(RGB[i],0.0)
return RGB
# convert R(ot) G(elb) B(lau) to H(ue) S(aturation) L(uminance)
# with S,L,R,G,B running from 0 to 1, H running from 0 to 360
# from http://130.113.54.154/~monger/hsl-rgb.html
def RGB2HSL(self,RGB):
HSL = [0.0,0.0,0.0]
maxcolor = max(RGB)
mincolor = min(RGB)
HSL[2] = (maxcolor + mincolor)/2.0
if(mincolor == maxcolor):
HSL[0] = 0.0
HSL[1] = 0.0
else:
if (HSL[2]<0.5):
HSL[1] = (maxcolor - mincolor)/(maxcolor + mincolor)
else:
HSL[1] = (maxcolor - mincolor)/(2.0 -maxcolor -mincolor)
if (maxcolor == RGB[0]):
HSL[0] = 0.0 + (RGB[1] - RGB[2])/(maxcolor - mincolor)
elif (maxcolor == RGB[1]):
HSL[0] = 2.0 + (RGB[2] - RGB[0])/(maxcolor - mincolor)
elif (maxcolor == RGB[2]):
HSL[0] = 4.0 + (RGB[0] - RGB[1])/(maxcolor - mincolor)
HSL[0] = HSL[0]*60.0
if (HSL[0] < 0.0):
HSL[0] = HSL[0] + 360.0
for i in range(2):
HSL[i+1] = min(HSL[i+1],1.0)
HSL[i+1] = max(HSL[i+1],0.0)
return HSL
# convert R(ot) G(elb) B(lau) to CIE XYZ
# with all values in the range of 0 to 1
# from http://www.cs.rit.edu/~ncs/color/t_convert.html
def RGB2XYZ(self,RGB):
XYZ = [0.0,0.0,0.0]
RGB_lin = [0.0,0.0,0.0]
for i in range(3):
if (RGB[i] > 0.04045):
RGB_lin[i] = ((RGB[i]+0.0555)/1.0555)**2.4
else:
RGB_lin[i] = RGB[i]/12.92
convert =[[0.412453,0.357580,0.180423],[0.212671,0.715160,0.072169],[0.019334,0.119193,0.950227]]
XYZ = self.matmult(convert,RGB_lin)
for i in range(3):
XYZ[i] = min(XYZ[i],1.0)
XYZ[i] = max(XYZ[i],0.0)
return XYZ
# convert CIE XYZ R(ot) G(elb) B(lau)
# with all values in the range of 0 to 1
# from http://www.cs.rit.edu/~ncs/color/t_convert.html
def XYZ2RGB(self,XYZ):
RGB_lin = [0.0,0.0,0.0]
RGB = [0.0,0.0,0.0]
convert =[[3.240479,-1.537150,-0.498535],[-0.969256,1.875992,0.041556],[0.055648,-0.204043,1.057311]]
RGB_lin = self.matmult(convert,XYZ)
for i in range(3):
if (RGB_lin[i] > 0.0031308):
RGB[i] = ((RGB_lin[i])**(1.0/2.4))*1.0555-0.0555
else:
RGB[i] = RGB_lin[i]*12.92
for i in range(3):
RGB[i] = min(RGB[i],1.0)
RGB[i] = max(RGB[i],0.0)
maxVal = RGB[0] # clipping clolors according to the display gamut
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
# convert CIE Lab to CIE XYZ
# with XYZ in the range of 0 to 1
# from http://www.easyrgb.com/index.php?X=MATH&H=07#text7
def CIELab2XYZ(self,Lab,white):
# ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer = 2, Illuminant = D65
XYZ = [0.0,0.0,0.0]
var_Y = ( Lab[0] + 16 ) / 116
var_X = Lab[1] / 500 + var_Y
var_Z = var_Y - Lab[2] / 200
if ( var_Y**3 > 0.008856 ):
var_Y = var_Y**3
else:
var_Y = ( var_Y-16/116) / 7.787
if ( var_X**3 > 0.008856 ):
var_X = var_X**3
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
# convert CIE XYZ to CIE Lab
# 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
def XYZ2CIELab(self,XYZ,white):
# ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer = 2, Illuminant = D65
Lab = [0.0,0.0,0.0]
var_X = XYZ[0]/white[0]
var_Y = XYZ[1]/white[1]
var_Z = XYZ[2]/white[2]
if ( var_X > 0.008856 ):
var_X = var_X**(1.0/3.0)
else:
var_X = ( 7.787 * var_X ) + (16.0/116.0)
if ( var_Y > 0.008856 ):
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
# convert Cie Lab to msh colorspace
# from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
def CIELab2Msh(self,Lab):
import math
Msh = [0.0,0.0,0.0]
Msh[0] = math.sqrt(Lab[0]**2.0 + Lab[1]**2.0 + Lab[2]**2.0)
if (Msh[0] != 0.0) and (Msh[0] > 0.001):
Msh[1] = math.acos(Lab[0]/Msh[0])
if (Lab[1] != 0.0) and (Msh[1] > 0.001):
Msh[2] = math.atan2(Lab[2],Lab[1])
return Msh
# convert msh colorspace to Cie Lab
# from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
def Msh2CIELab(self,Msh):
import math
Lab = [0.0,0.0,0.0]
Lab[0] = Msh[0] * math.cos(Msh[1])
Lab[1] = Msh[0] * math.sin(Msh[1]) * math.cos(Msh[2])
Lab[2] = Msh[0] * math.sin(Msh[1]) * math.sin(Msh[2])
return Lab
def write_gsmh(self,RGB_vector,name):
colormap = open(str(name) + '.map',"w")
colormap.write('View.ColorTable = {\n')
for i in range(len(RGB_vector)-1):
colormap.write('{'+str((RGB_vector[0][i])*255.0)+','+str((RGB_vector[0][i])*255.0)+','+str((RGB_vector[0][i])*255.0)+'},\n')
colormap.write('{'+str((RGB_vector[0][-1])*255.0)+','+str((RGB_vector[0][-1])*255.0)+','+str((RGB_vector[0][-1])*255.0)+'}}')
file.close(colormap)
def write_paraview(self,RGB_vector,name):
colormap = open(str(name) + '.xml',"w")
colormap.write('<ColorMap name="'+ str(name)+ '" space="RGB">\n')
for i in range(len(RGB_vector[0])):
colormap.write('<Point x="'+str(i)+'" o="1" r="'+str(RGB_vector[0][i])+'" g="'+str(RGB_vector[1][i])+'" b="'+str(RGB_vector[2][i])+'"/>\n')
colormap.write('</ColorMap>')
file.close(colormap)
def write_raw(self,RGB_vector,name):
colormap = open(str(name) + '.colormap',"w")
colormap.write('ColorMap name = ' + str(name)+'\n')
for i in range(len(RGB_vector)):
colormap.write(str(RGB_vector[0][i])+'\t'+str(RGB_vector[1][i])+'\t'+str(RGB_vector[2][i])+'\n')
file.close(colormap)

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python
import math, convert_colormodels, colormap_io, string, sys
import math, string, sys
from damask import Colormaps
from optparse import OptionParser, Option
# -----------------------------
@ -32,7 +32,7 @@ parser = OptionParser(option_class=extendableOption, usage='%prog options [file[
Add column(s) containing Cauchy stress based on given column(s) of
deformation gradient and first Piola--Kirchhoff stress.
""" + string.replace('$Id: addCauchy.py 1278 2012-02-07 13:09:10Z MPIE\c.kords $','\n','\\n')
""" + string.replace('$Id$','\n','\\n')
)
parser.add_option('-l','--left', dest='left', type='float', nargs=3, \
@ -61,10 +61,11 @@ if filenames == []:
# -----------------------------------------------------------------------------------------------------
myColorMap = Colormaps()
def rad_dif(Msh1,Msh2,white):
HSL1 = convert_colormodels.RGB2HSL(convert_colormodels.XYZ2RGB(convert_colormodels.CIELab2XYZ(convert_colormodels.Msh2CIELab(Msh1),white)))
HSL2 = convert_colormodels.RGB2HSL(convert_colormodels.XYZ2RGB(convert_colormodels.CIELab2XYZ(convert_colormodels.Msh2CIELab(Msh2),white)))
HSL1 = myColorMap.RGB2HSL(myColorMap.XYZ2RGB(myColorMap.CIELab2XYZ(myColorMap.Msh2CIELab(Msh1),white)))
HSL2 = myColorMap.RGB2HSL(myColorMap.XYZ2RGB(myColorMap.CIELab2XYZ(myColorMap.Msh2CIELab(Msh2),white)))
return abs(HSL1[0]*math.pi/180.0-HSL2[0]*math.pi/180.0)
def adjust_hue(Msh_sat,M_unsat):
@ -79,8 +80,8 @@ def adjust_hue(Msh_sat,M_unsat):
def interpolate_color(RGB1,RGB2,white,interp):
Msh1 = convert_colormodels.CIELab2Msh(convert_colormodels.XYZ2CIELab(convert_colormodels.RGB2XYZ(RGB1),white))
Msh2 = convert_colormodels.CIELab2Msh(convert_colormodels.XYZ2CIELab(convert_colormodels.RGB2XYZ(RGB2),white))
Msh1 = myColorMap.CIELab2Msh(myColorMap.XYZ2CIELab(myColorMap.RGB2XYZ(RGB1),white))
Msh2 = myColorMap.CIELab2Msh(myColorMap.XYZ2CIELab(myColorMap.RGB2XYZ(RGB2),white))
Msh_mid = [0.0,0.0,0.0]
if ((Msh1[1] > 0.05 and Msh2[1] > 0.05) and rad_dif(Msh1,Msh2,white) > math.pi/3.0):
Msh_mid[0] = max(Msh1[0],Msh2[0],88.0)
@ -100,7 +101,7 @@ def interpolate_color(RGB1,RGB2,white,interp):
Msh2[2] = adjust_hue(Msh1,Msh2[0])
for i in range(3):
Msh_mid[i] = (1.0-interp)*Msh1[i] + interp* Msh2[i]
return convert_colormodels.XYZ2RGB(convert_colormodels.CIELab2XYZ(convert_colormodels.Msh2CIELab(Msh_mid),white))
return myColorMap.XYZ2RGB(myColorMap.CIELab2XYZ(myColorMap.Msh2CIELab(Msh_mid),white))
white = [0.950456, 1.0, 1.088754]
interpolatorArray = []
@ -114,8 +115,9 @@ for i in interpolatorArray:
rMatrix.append(color[0])
gMatrix.append(color[1])
bMatrix.append(color[2])
colorMatrix = [rMatrix,gMatrix,bMatrix]
if options.outtype.lower() == 'paraview':
colormap_io.write_paraview(colorMatrix,filenames[0])
myColorMap.write_paraview(colorMatrix,filenames[0])

View File

@ -1,34 +0,0 @@
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
def write_gsmh(RGB_vector,name):
colormap = open(str(name) + '.map',"w")
colormap.write('View.ColorTable = {\n')
for i in range(len(RGB_vector)-1):
colormap.write('{'+str((RGB_vector[0][i])*255.0)+','+str((RGB_vector[0][i])*255.0)+','+str((RGB_vector[0][i])*255.0)+'},\n')
colormap.write('{'+str((RGB_vector[0][-1])*255.0)+','+str((RGB_vector[0][-1])*255.0)+','+str((RGB_vector[0][-1])*255.0)+'}}')
file.close(colormap)
def write_paraview(RGB_vector,name):
colormap = open(str(name) + '.xml',"w")
colormap.write('<ColorMap name="'+ str(name)+ '" space="RGB">\n')
for i in range(len(RGB_vector[0])):
colormap.write('<Point x="'+str(i)+'" o="1" r="'+str(RGB_vector[0][i])+'" g="'+str(RGB_vector[1][i])+'" b="'+str(RGB_vector[2][i])+'"/>\n')
colormap.write('</ColorMap>')
file.close(colormap)
def write_paraview2(RGB_vector,name):
colormap = open(str(name) + '.xml',"w")
colormap.write('<ColorMap name = "'+ str(name)+ '" space = "RGB">\n')
for i in range(len(RGB_vector)/3):
colormap.write('<Point x="'+str(i)+'" o="1" r="'+str(RGB_vector[i*3])+'" g="'+str(RGB_vector[i*3+1])+'" b="'+str(RGB_vector[i*3+2])+'"/>\n')
colormap.write('</ColorMap>')
file.close(colormap)
def write_raw(RGB_vector,name):
colormap = open(str(name) + '.colormap',"w")
colormap.write('ColorMap name = ' + str(name)+'\n')
for i in range(len(RGB_vector)):
colormap.write(str(RGB_vector[0][i])+'\t'+str(RGB_vector[1][i])+'\t'+str(RGB_vector[2][i])+'\n')
file.close(colormap)

View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
import math, convert_colormodels, colormap_io, string, sys
def adjust_hue(Msh_sat,M_unsat):
if ( Msh_sat[0] >= (M_unsat-0.1) ):
return Msh_sat[2]
else:
hSpin = Msh_sat[1]*math.sqrt((M_unsat)**2.0-(Msh_sat[0])**2)/(Msh_sat[0]*math.sin(Msh_sat[1]))
if Msh_sat[2] > - math.pi/3.0:
return Msh_sat[2] + hSpin
else:
return Msh_sat[2] - hSpin
def interpolate_color(RGB1,white,interp):
Msh1 = convert_colormodels.CIELab2Msh(convert_colormodels.XYZ2CIELab(convert_colormodels.RGB2XYZ(RGB1),white))
Msh2 = [0.0,0.0,0.0]
Msh_mid = [0.0,0.0,0.0]
if ((Msh1[1] > 0.05 and Msh2[1] > 0.05):
Msh_mid[0] = max(Msh1[0],Msh2[0],88.0)
if interp <=1:
Msh2[0] = Msh_mid[0]
Msh2[1] = 0.0
Msh2[2] = 0.0
interp = 2.0*interp
else:
print 'Interpolation factor value must be within 0 and 1!'
if (Msh1[1] < 0.05):
Msh1[2] = adjust_hue(Msh2,Msh1[0])
else:
print 'The Saturation value of the given color is aggreable!'
for i in range(3):
Msh_mid[i] = (1.0-interp)*Msh1[i] + interp* Msh2[i]
return convert_colormodels.XYZ2RGB(convert_colormodels.CIELab2XYZ(convert_colormodels.Msh2CIELab(Msh_mid),white))
ex1 = [46/255 139/255 87/255]
interpolatorArray = []
for i in range(options.steps+1): interpolatorArray.append(float(i)/options.steps)
rMatrix = []
gMatrix = []
bMatrix = []
for i in interpolatorArray:
step_no = str(interpolatorArray.index(i))
color = interpolate_color(options.left,options.right,white,i)
rMatrix.append(color[0])
gMatrix.append(color[1])
bMatrix.append(color[2])
print 'step no: %s'%step_no
print color[0], color[1], color[2]
colorMatrix = [rMatrix,gMatrix,bMatrix]

View File

@ -1,206 +0,0 @@
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import math
# from http://code.activestate.com/recipes/121574-matrix-vector-multiplication/
def matmult(m, v):
nrows = len(m)
w = [None] * nrows
for row in range(nrows):
w[row] = reduce(lambda x,y: x+y, map(lambda x,y: x*y, m[row], v))
return w
# convert H(ue) S(aturation) L(uminance) to R(ot) G(elb) B(lau)
# with S,L,R,G,B running from 0 to 1, H running from 0 to 360
# from http://en.wikipedia.org/wiki/HSL_and_HSV
def HSL2RGB(HSL):
RGB = [0.0,0.0,0.0]
H_strich = HSL[0]/60.0
c = (1.0- abs(2.0 * HSL[2] - 1.0))*HSL[1]
x = c*(1.0- abs(H_strich%2-1.0))
m = HSL[2] -.5*c
if (0.0 <= H_strich)and(H_strich<1.0):
RGB[0] = c + m
RGB[1] = x + m
RGB[2] = 0.0 + m
elif (1.0 <= H_strich)and(H_strich<2.0):
RGB[0] = x + m
RGB[1] = c + m
RGB[2] = 0.0 + m
elif (2.0 <= H_strich)and(H_strich<3.0):
RGB[0] = 0.0 + m
RGB[1] = c + m
RGB[2] = x + m
elif (3.0 <= H_strich)and(H_strich<4.0):
RGB[0] = 0.0 + m
RGB[1] = x + m
RGB[2] = c + m
elif (4.0 <= H_strich)and(H_strich<5.0):
RGB[0] = x + m
RGB[1] = 0.0 + m
RGB[2] = c + m
elif (5.0 <= H_strich)and(H_strich<=6.0):
RGB[0] = c + m
RGB[1] = 0.0 + m
RGB[2] = x + m
for i in range(3):
RGB[i] = min(RGB[i],1.0)
RGB[i] = max(RGB[i],0.0)
return RGB
# convert R(ot) G(elb) B(lau) to H(ue) S(aturation) L(uminance)
# with S,L,R,G,B running from 0 to 1, H running from 0 to 360
# from http://130.113.54.154/~monger/hsl-rgb.html
def RGB2HSL(RGB):
HSL = [0.0,0.0,0.0]
maxcolor = max(RGB)
mincolor = min(RGB)
HSL[2] = (maxcolor + mincolor)/2.0
if(mincolor == maxcolor):
HSL[0] = 0.0
HSL[1] = 0.0
else:
if (HSL[2]<0.5):
HSL[1] = (maxcolor - mincolor)/(maxcolor + mincolor)
else:
HSL[1] = (maxcolor - mincolor)/(2.0 -maxcolor -mincolor)
if (maxcolor == RGB[0]):
HSL[0] = 0.0 + (RGB[1] - RGB[2])/(maxcolor - mincolor)
elif (maxcolor == RGB[1]):
HSL[0] = 2.0 + (RGB[2] - RGB[0])/(maxcolor - mincolor)
elif (maxcolor == RGB[2]):
HSL[0] = 4.0 + (RGB[0] - RGB[1])/(maxcolor - mincolor)
HSL[0] = HSL[0]*60.0
if (HSL[0] < 0.0):
HSL[0] = HSL[0] + 360.0
for i in range(2):
HSL[i+1] = min(HSL[i+1],1.0)
HSL[i+1] = max(HSL[i+1],0.0)
return HSL
# convert R(ot) G(elb) B(lau) to CIE XYZ
# with all values in the range of 0 to 1
# from http://www.cs.rit.edu/~ncs/color/t_convert.html
def RGB2XYZ(RGB):
XYZ = [0.0,0.0,0.0]
RGB_lin = [0.0,0.0,0.0]
for i in range(3):
if (RGB[i] > 0.04045):
RGB_lin[i] = ((RGB[i]+0.0555)/1.0555)**2.4
else:
RGB_lin[i] = RGB[i]/12.92
convert =[[0.412453,0.357580,0.180423],[0.212671,0.715160,0.072169],[0.019334,0.119193,0.950227]]
XYZ = matmult(convert,RGB_lin)
for i in range(3):
XYZ[i] = min(XYZ[i],1.0)
XYZ[i] = max(XYZ[i],0.0)
return XYZ
# convert CIE XYZ R(ot) G(elb) B(lau)
# with all values in the range of 0 to 1
# from http://www.cs.rit.edu/~ncs/color/t_convert.html
def XYZ2RGB(XYZ):
RGB_lin = [0.0,0.0,0.0]
RGB = [0.0,0.0,0.0]
convert =[[3.240479,-1.537150,-0.498535],[-0.969256,1.875992,0.041556],[0.055648,-0.204043,1.057311]]
RGB_lin = matmult(convert,XYZ)
for i in range(3):
if (RGB_lin[i] > 0.0031308):
RGB[i] = ((RGB_lin[i])**(1.0/2.4))*1.0555-0.0555
else:
RGB[i] = RGB_lin[i]*12.92
for i in range(3):
RGB[i] = min(RGB[i],1.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
# convert CIE Lab to CIE XYZ
# with XYZ in the range of 0 to 1
# from http://www.easyrgb.com/index.php?X=MATH&H=07#text7
def CIELab2XYZ(Lab,white):
# ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer= 2°, Illuminant= D65
XYZ = [0.0,0.0,0.0]
var_Y = ( Lab[0] + 16 ) / 116
var_X = Lab[1] / 500 + var_Y
var_Z = var_Y - Lab[2] / 200
if ( var_Y**3 > 0.008856 ):
var_Y = var_Y**3
else:
var_Y = ( var_Y-16/116) / 7.787
if ( var_X**3 > 0.008856 ):
var_X = var_X**3
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
# convert CIE XYZ to CIE Lab
# 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
def XYZ2CIELab(XYZ,white):
# ref_white_XYZ = [.95047, 1.00000, 1.08883] # Observer= 2°, Illuminant= D65
Lab = [0.0,0.0,0.0]
var_X = XYZ[0]/white[0]
var_Y = XYZ[1]/white[1]
var_Z = XYZ[2]/white[2]
if ( var_X > 0.008856 ):
var_X = var_X**(1.0/3.0)
else:
var_X = ( 7.787 * var_X ) + (16.0/116.0)
if ( var_Y > 0.008856 ):
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
# convert Cie Lab to msh colorspace
# from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
def CIELab2Msh(Lab):
Msh = [0.0,0.0,0.0]
Msh[0] = math.sqrt(Lab[0]**2.0 + Lab[1]**2.0 + Lab[2]**2.0)
if (Msh[0] != 0.0) and (Msh[0] > 0.001):
Msh[1] = math.acos(Lab[0]/Msh[0])
if (Lab[1] != 0.0) and (Msh[1] > 0.001):
Msh[2] = math.atan2(Lab[2],Lab[1])
return Msh
# convert msh colorspace to Cie Lab
# from http://www.cs.unm.edu/~kmorel/documents/ColorMaps/DivergingColorMapWorkshop.xls
def Msh2CIELab(Msh):
Lab = [0.0,0.0,0.0]
Lab[0] = Msh[0] * math.cos(Msh[1])
Lab[1] = Msh[0] * math.sin(Msh[1]) * math.cos(Msh[2])
Lab[2] = Msh[0] * math.sin(Msh[1]) * math.sin(Msh[2])
return Lab