moved "predefined" to colormap init

cleaned interpolate to not change color model

fixed color range cropping issue
This commit is contained in:
Philip Eisenlohr 2014-02-28 16:09:01 +00:00
parent 63569b19b6
commit 6fd8407d6e
2 changed files with 61 additions and 53 deletions

View File

@ -313,8 +313,14 @@ class Colormap():
left = Color('RGB',[1,1,1]), left = Color('RGB',[1,1,1]),
right = Color('RGB',[0,0,0]), right = Color('RGB',[0,0,0]),
interpolate = 'perceptualuniform', interpolate = 'perceptualuniform',
predefined = None
): ):
if str(predefined).lower() in self.__predefined__:
left = self.__predefined__[predefined.lower()]['left']
right= self.__predefined__[predefined.lower()]['right']
interpolate = self.__predefined__[predefined.lower()]['interpolate']
if left.__class__.__name__ != 'Color': if left.__class__.__name__ != 'Color':
left = Color() left = Color()
if right.__class__.__name__ != 'Color': if right.__class__.__name__ != 'Color':
@ -325,28 +331,21 @@ class Colormap():
self.interpolate = interpolate self.interpolate = interpolate
# ------------------------------------------------------------------
def __repr__(self):
return 'Left: %s Right: %s'%(self.left,self.right)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def invert(self): def invert(self):
temp = self.left (self.left, self.right) = (self.right, self.left)
self.left = self.right
self.right = temp
return self return self
# ------------------------------------------------------------------
def usePredefined(self,name='bluered'):
if name.lower() not in self.__predefined__:
raise KeyError('colormap "%s" is not defined, use one of "%s"'%(name,'" "'.join(self.__predefined__.keys())))
self.left = self.__predefined__[name.lower()]['left']
self.right= self.__predefined__[name.lower()]['right']
self.interpolate = self.__predefined__[name.lower()]['interpolate']
return self
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def export(self,name = 'uniformPerceptualColorMap',\ def export(self,name = 'uniformPerceptualColorMap',\
format = 'paraview',\ format = 'paraview',\
steps = 10,\ steps = 2,\
crop = [-1.0,1.0], crop = [-1.0,1.0],
model = 'RGB'): model = 'RGB'):
''' '''
@ -354,19 +353,18 @@ class Colormap():
arguments: name, format, steps, crop. arguments: name, format, steps, crop.
format is one of (paraview, gmsh, raw, list). format is one of (paraview, gmsh, raw, list).
crop selects a (sub)range in [-1.0,1.0]. crop selects a (sub)range in [-1.0,1.0].
generates generates sequential map if one limiting color is either white or black,
sequential map if one limiting color is either white or black, diverging map otherwise.
diverging map otherwise.
''' '''
import copy,numpy,math import copy,numpy,math
def interpolate_Msh(lo,hi,frac,model='RGB'): def interpolate_Msh(lo, hi, frac):
def rad_diff(a,b): def rad_diff(a,b):
return abs(a[2]-b[2]) return abs(a[2]-b[2])
def adjust_hue(Msh_sat,Msh_unsat): # if saturation of one of the two colors is too less than the other, hue of the less def adjust_hue(Msh_sat, Msh_unsat): # if saturation of one of the two colors is too less than the other, hue of the less
if Msh_sat[0] >= Msh_unsat[0]: if Msh_sat[0] >= Msh_unsat[0]:
return Msh_sat[2] return Msh_sat[2]
else: else:
@ -381,23 +379,28 @@ class Colormap():
M_mid = max(Msh1[0],Msh2[0],88.0) M_mid = max(Msh1[0],Msh2[0],88.0)
if frac < 0.5: if frac < 0.5:
Msh2 = numpy.array([M_mid,0.0,0.0],'d') Msh2 = numpy.array([M_mid,0.0,0.0],'d')
frac = 2.0*frac frac *= 2.0
else: else:
Msh1 = numpy.array([M_mid,0.0,0.0],'d') Msh1 = numpy.array([M_mid,0.0,0.0],'d')
frac = 2.0*frac - 1.0 frac = 2.0*frac - 1.0
if Msh1[1] < 0.05 and Msh2[1] > 0.05: Msh1[2] = adjust_hue(Msh2,Msh1) if Msh1[1] < 0.05 and Msh2[1] > 0.05: Msh1[2] = adjust_hue(Msh2,Msh1)
elif Msh1[1] > 0.05 and Msh2[1] < 0.05: Msh2[2] = adjust_hue(Msh1,Msh2) elif Msh1[1] > 0.05 and Msh2[1] < 0.05: Msh2[2] = adjust_hue(Msh1,Msh2)
Msh = (1.0-frac)*Msh1 + frac*Msh2 Msh = (1.0 - frac) * Msh1 + frac * Msh2
return Color('MSH',Msh).convertTo(model)
def linearInterpolate(lo,hi,frac,model='RGB'): return Color('MSH',Msh)
color1 = numpy.array(lo.color[:])
color2 = numpy.array(hi.expressAs(lo.model).color[:]) def interpolate_linear(lo, hi, frac):
color = (1.0 - frac) * color1 + frac * color2 '''
return Color(lo.model,color).convertTo(model) linearly interpolate color at given fraction between lower and higher color in model of lower color
'''
interpolation = (1.0 - frac) * numpy.array(lo.color[:]) \
+ frac * numpy.array(hi.expressAs(lo.model).color[:])
return Color(lo.model,interpolation)
def write_paraview(RGB_vector): def write_paraview(RGB_vector):
colormap ='<ColorMap name="'+str(name)+'" space="RGB">\n' colormap ='<ColorMap name="'+str(name)+'" space="Diverging">\n'
for i in range(len(RGB_vector)): for i in range(len(RGB_vector)):
colormap+='<Point x="'+str(i)+'" o="1" r="'+str(RGB_vector[i][0])+'" g="'+str(RGB_vector[i][1])+'" b="'+str(RGB_vector[i][2])+'"/>\n' colormap+='<Point x="'+str(i)+'" o="1" r="'+str(RGB_vector[i][0])+'" g="'+str(RGB_vector[i][1])+'" b="'+str(RGB_vector[i][2])+'"/>\n'
colormap+='</ColorMap>' colormap+='</ColorMap>'
@ -418,23 +421,25 @@ class Colormap():
colors = [] colors = []
totalSteps = int(2.0*steps/(crop[1] - crop[0])) frac = (numpy.array(crop) + 1.0)/2.0
for i in range(totalSteps): if self.interpolate == 'perceptualuniform':
if self.interpolate == 'perceptualuniform': for i in range(steps):
color = interpolate_Msh(self.left.expressAs('MSH').color,self.right.expressAs('MSH').color,float(i)/(totalSteps-1),model) colors.append(interpolate_Msh(self.left.expressAs('MSH').color,
elif self.interpolate == 'linear': self.right.expressAs('MSH').color,
color = linearInterpolate(self.left,self.right,float(i)/(totalSteps-1),model) float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]))
else: elif self.interpolate == 'linear':
raise NameError('unknown interpolation method') for i in range(steps):
colors.append(color.color) colors.append(interpolate_linear(self.left,
self.right,
float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]))
else:
raise NameError('unknown interpolation method')
leftIndex = int(round((crop[0]-(-1.0))/(2.0/(totalSteps-1))))
rightIndex = leftIndex + steps
return {\ return {\
'paraview': write_paraview, 'paraview': write_paraview,
'gmsh': write_gmsh, 'gmsh': write_gmsh,
'gom': write_GOM, 'gom': write_GOM,
'raw': write_raw, 'raw': write_raw,
'list': lambda x: x, 'list': lambda x: x,
}[format.lower()](colors[max(leftIndex,0):min(rightIndex,totalSteps)]) }[format.lower()](map(lambda x:x.expressAs(model).color,colors))

View File

@ -56,7 +56,7 @@ def colorMap(colors,baseIdx=32):
# MAIN FUNCTION STARTS HERE # MAIN FUNCTION STARTS HERE
# ----------------------------- # -----------------------------
parser = OptionParser(usage="%prog [options] configured scheme | (lower_h,s,l upper_h,s,l)", description = """ parser = OptionParser(usage="%prog [options] predefinedScheme | (lower_h,s,l upper_h,s,l)", description = """
Changes the color map in MSC.Mentat. Changes the color map in MSC.Mentat.
Interpolates colors between "lower_hsl" and "upper_hsl". Interpolates colors between "lower_hsl" and "upper_hsl".
@ -84,7 +84,7 @@ parser.add_option("-n", "--colorcount", type = "int",\
help = "number of colors [%default]") help = "number of colors [%default]")
parser.add_option("-v", "--verbose", action="store_true",\ parser.add_option("-v", "--verbose", action="store_true",\
dest = "verbose",\ dest = "verbose",\
help = "write Mentat command stream also to stdout [%default]") help = "write Mentat command stream also to STDOUT [%default]")
parser.set_defaults(port = 40007) parser.set_defaults(port = 40007)
parser.set_defaults(baseIdx = 32) parser.set_defaults(baseIdx = 32)
@ -98,16 +98,19 @@ msg = []
(options, colors) = parser.parse_args() (options, colors) = parser.parse_args()
if len(colors) > 0 and colors[0] in damask.Colormap.__predefined__: if len(colors) == 0:
theMap = damask.Colormap().usePredefined(colors[0]) parser.error('missing color information')
elif len(colors) == 1:
theMap = damask.Colormap(predefined = colors[0])
elif len(colors) == 2:
theMap = damask.Colormap(damask.Color('HSL',map(float, colors[0].split(','))),
damask.Color('HSL',map(float, colors[1].split(','))) )
else: else:
if len(colors) == 2: theMap = damask.Colormap()
left = map(float, colors[0].split(','))
right = map(float, colors[1].split(','))
else:
left = [0.0,0.0,1.0]
right = [0.0,0.0,0.0]
theMap = damask.Colormap(damask.Color('HSL',left),damask.Color('HSL',right))
if options.inverse: if options.inverse:
theMap = theMap.invert() theMap = theMap.invert()
@ -117,7 +120,7 @@ elif options.palette:
for theColor in theMap.export(format='list',steps=options.colorcount): for theColor in theMap.export(format='list',steps=options.colorcount):
print '\t'.join(map(lambda x: str(int(255*x)),theColor)) print '\t'.join(map(lambda x: str(int(255*x)),theColor))
else: else:
### connect to mentat and change colorMap ### connect to Mentat and change colorMap
sys.path.append(damask.solver.Marc().libraryPath('../../')) sys.path.append(damask.solver.Marc().libraryPath('../../'))
try: try:
from py_mentat import * from py_mentat import *