changed internal color mapping to damask.Colormap class

This commit is contained in:
Philip Eisenlohr 2013-02-02 15:12:43 +00:00
parent 95c74961dd
commit ac562f4b8b
1 changed files with 35 additions and 96 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, os, string, damask import sys, os, string
from colorsys import * import damask
from optparse import OptionParser from optparse import OptionParser
@ -12,15 +12,15 @@ def readConfig(configFile,ownPath):
filename = os.path.join(configDir,configFile) filename = os.path.join(configDir,configFile)
if os.path.isfile(filename): if os.path.isfile(filename):
file = open(filename) file = open(filename)
content = file.readlines() for line in file.readlines():
if not line.startswith('#'):
item = line.split()
for name in item[0].split('|'):
config[name] = {}
config[name]['left'] = map(float,item[1].split(','))
config[name]['right'] = map(float,item[2].split(','))
file.close() file.close()
for line in content:
item = line.split()
config[item[0]] = {}
config[item[0]]['lower'] = map(float,item[1].split(','))
config[item[0]]['upper'] = map(float,item[2].split(','))
config[item[0]]['symmetric'] = len(item) > 3
return config return config
@ -65,28 +65,6 @@ def output(cmds,locals,dest):
# -----------------------------
def interpolate(val0, val1, x):
return val0 + (val1 - val0) * x
# -----------------------------
def syminterpolate(comp, val0, val1, x):
if comp == "hue":
return {True:val0,False:val1}[x<0.5]
if comp == "lightness":
val_middle = 1
elif comp == "saturation":
val_middle = 0
if x < 0.5:
return interpolate(val0, val_middle, 2*x)
else:
return interpolate(val_middle, val1, 2*x-1)
# ----------------------------- # -----------------------------
def colorMap(colors,baseIdx=32): def colorMap(colors,baseIdx=32):
cmds = [ "*color %i %f %f %f"%(idx+baseIdx,color[0],color[1],color[2]) cmds = [ "*color %i %f %f %f"%(idx+baseIdx,color[0],color[1],color[2])
@ -98,22 +76,14 @@ def colorMap(colors,baseIdx=32):
# MAIN FUNCTION STARTS HERE # MAIN FUNCTION STARTS HERE
# ----------------------------- # -----------------------------
parser = OptionParser(usage="%prog [options] configured scheme | (lower_h,l,s upper_h,l,s)", description = """ parser = OptionParser(usage="%prog [options] configured scheme | (lower_h,s,l upper_h,s,l)", description = """
Changes the color map in mentat. Changes the color map in MSC.Mentat.
Interpolates colors between "lower_hls" and "upper_hls". Interpolates colors between "lower_hsl" and "upper_hsl".
For symmetric scales use option "-s".
Example colors:
- Non-symmetric scales: 0.167,0.9,0.1 0.167,0.1,0.9
- Symmetric scales: 0,0.2,0.9 0.333,0.2,0.9
""" + string.replace('$Id$','\n','\\n') """ + string.replace('$Id$','\n','\\n')
) )
parser.add_option("-s","--symmetric", action = "store_true",
dest = "symmetric", \
help = "symmetric legend [%default]")
parser.add_option("-i","--inverse", action = "store_true", parser.add_option("-i","--inverse", action = "store_true",
dest = "inverse", \ dest = "inverse", \
help = "invert legend [%default]") help = "invert legend [%default]")
@ -143,7 +113,6 @@ parser.set_defaults(port = 40007)
parser.set_defaults(baseIdx = 32) parser.set_defaults(baseIdx = 32)
parser.set_defaults(colorcount = 32) parser.set_defaults(colorcount = 32)
parser.set_defaults(config = 'colorMap.config') parser.set_defaults(config = 'colorMap.config')
parser.set_defaults(symmetric = False)
parser.set_defaults(inverse = False) parser.set_defaults(inverse = False)
parser.set_defaults(palette = False) parser.set_defaults(palette = False)
parser.set_defaults(verbose = False) parser.set_defaults(verbose = False)
@ -154,71 +123,41 @@ msg = []
config = readConfig(options.config,sys.argv[0]) config = readConfig(options.config,sys.argv[0])
if len(colors) == 0: if len(colors) > 0 and colors[0] in config:
hlsColor_range = (options.symmetric and [[0,0.2,0.9],[0.333,0.2,0.9]]) or \ left = config[colors[0]][{True:'right',False:'left' }[options.inverse]]
[[0.167,0.9,0.1],[0.167,0.1,0.9]] right = config[colors[0]][{True:'left' ,False:'right'}[options.inverse]]
elif len(colors) == 2: elif len(colors) == 2:
hlsColor_range = [map(float, colors[i].split(',')) for i in range(2)] left = map(float, colors[{True:1,False:0}[options.inverse]].split(','))
elif colors[0] in config: right = map(float, colors[{True:0,False:1}[options.inverse]].split(','))
options.symmetric = config[colors[0]]['symmetric']
hlsColor_range = [config[colors[0]]['lower'],\
config[colors[0]]['upper']]
else: else:
msg.append('two color tuples required') left = {True:[0.0,0.0,0.0],False:[0.0,0.0,1.0]}[options.inverse]
right = {True:[0.0,0.0,1.0],False:[0.0,0.0,0.0]}[options.inverse]
if msg == []: theMap = damask.Colormap(damask.Color('HSL',left),damask.Color('HSL',right))
hlsColors_limits = [[0.0,0.0,0.0],[1.0,1.0,1.0]] theColors = theMap.export(format='list',steps=options.colorcount)
if options.inverse:
hlsColor_range = [hlsColor_range[1],hlsColor_range[0]]
for i in range(2):
for j in range(min(3,len(hlsColor_range[i]))):
if hlsColor_range[i][j] < hlsColors_limits[0][j] or hlsColor_range[i][j] > hlsColors_limits[1][j]:
msg.append('%s of %s color exceeds limit'%(['hue','lightness','saturation'][j],limit))
if msg != []:
parser.error('\n'+'\n'.join(msg)+'\n')
### interpolate hls values
if options.symmetric:
hlsColors = [ [ syminterpolate(comp, hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(options.colorcount-1))
for j,comp in enumerate(["hue","lightness","saturation"]) ]
for idx in range(options.colorcount) ]
else:
hlsColors = [ [ interpolate(hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(options.colorcount-1))
for j,comp in enumerate(["hue","lightness","saturation"]) ]
for idx in range(options.colorcount) ]
### convert to rgb values
rgbColors = [ hls_to_rgb(hlsColor[0], hlsColor[1], hlsColor[2])
for hlsColor in hlsColors ]
if options.palette or options.palettef: if options.palette or options.palettef:
for rgb in rgbColors: for theColor in theColors:
print '\t'.join(map(lambda x: {True: str(int(255*x)), print '\t'.join(map(lambda x: {True: str( x),
False:str( x)}[options.palette],rgb)) False:str(int(255*x))}[options.palettef],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 *
print 'waiting to connect...'
py_connect('',options.port)
print 'connected...'
mentat = True
except: except:
sys.stderr.write('error: no valid Mentat release found\n') sys.stderr.write('warning: no valid Mentat release found\n')
sys.exit(1) mentat = False
outputLocals = {} outputLocals = {}
print 'waiting to connect...' cmds = colorMap(theColors,options.baseIdx)
py_connect('',options.port) if mentat:
print 'connected...' output(['*show_table']+cmds+['*show_model *redraw'],outputLocals,'Mentat')
py_disconnect()
cmds = colorMap(rgbColors,options.baseIdx)
output(['*show_table']+cmds+['*show_model *redraw'],outputLocals,'Mentat')
py_disconnect()
if options.verbose: if options.verbose:
output(cmds,outputLocals,'Stdout') output(cmds,outputLocals,'Stdout')