2010-08-17 02:17:27 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys, os
|
|
|
|
from colorsys import *
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2010-08-17 23:51:22 +05:30
|
|
|
releases = {'2010':['linux64',''],
|
|
|
|
'2008r1':[''],
|
|
|
|
'2007r1':[''],
|
|
|
|
'2005r3':[''],
|
|
|
|
}
|
|
|
|
|
|
|
|
file = open('%s/../MSCpath'%os.path.dirname(sys.argv[0]))
|
|
|
|
MSCpath = os.path.normpath(file.readline().strip())
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
for release,subdirs in sorted(releases.items(),reverse=True):
|
|
|
|
for subdir in subdirs:
|
|
|
|
libPath = '%s/mentat%s/shlib/%s'%(MSCpath,release,subdir)
|
|
|
|
if os.path.exists(libPath):
|
|
|
|
sys.path.append(libPath)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
from py_mentat import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def outMentat(cmd,locals):
|
|
|
|
if cmd[0:3] == '(!)':
|
|
|
|
exec(cmd[3:])
|
|
|
|
elif cmd[0:3] == '(?)':
|
|
|
|
cmd = eval(cmd[3:])
|
|
|
|
py_send(cmd)
|
|
|
|
else:
|
|
|
|
py_send(cmd)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def outStdout(cmd,locals):
|
|
|
|
if cmd[0:3] == '(!)':
|
|
|
|
exec(cmd[3:])
|
|
|
|
elif cmd[0:3] == '(?)':
|
|
|
|
cmd = eval(cmd[3:])
|
|
|
|
print cmd
|
|
|
|
else:
|
|
|
|
print cmd
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def output(cmds,locals,dest):
|
|
|
|
for cmd in cmds:
|
|
|
|
if isinstance(cmd,list):
|
|
|
|
output(cmd,locals,dest)
|
|
|
|
else:
|
|
|
|
{\
|
|
|
|
'Mentat': outMentat,\
|
|
|
|
'Stdout': outStdout,\
|
|
|
|
}[dest](cmd,locals)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
2010-09-06 22:21:11 +05:30
|
|
|
def interpolate(val0, val1, x):
|
2010-08-17 02:17:27 +05:30
|
|
|
return val0 + (val1 - val0) * x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
2010-09-06 22:21:11 +05:30
|
|
|
def syminterpolate(comp, val0, val1, x):
|
2010-08-17 02:17:27 +05:30
|
|
|
if comp == "hue":
|
2010-09-06 22:21:11 +05:30
|
|
|
return interpolate(val0, val1, x)
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
if comp == "lightness":
|
|
|
|
val_middle = max(0.9, val0, val1)
|
|
|
|
elif comp == "saturation":
|
|
|
|
val_middle = min(0.1, val0, val1)
|
|
|
|
if x < 0.5:
|
2010-09-06 22:21:11 +05:30
|
|
|
return interpolate(val0, val_middle, 2*x)
|
2010-08-17 02:17:27 +05:30
|
|
|
else:
|
2010-09-06 22:21:11 +05:30
|
|
|
return interpolate(val_middle, val1, 2*x-1)
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
2010-09-13 13:01:27 +05:30
|
|
|
def colorMap(colors,baseIdx=32):
|
|
|
|
cmds = [ "*color %i %f %f %f"%(idx+baseIdx,color[0],color[1],color[2])
|
2010-08-17 02:17:27 +05:30
|
|
|
for idx,color in enumerate(colors) ]
|
|
|
|
return cmds
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# MAIN FUNCTION STARTS HERE
|
|
|
|
# -----------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(usage="%prog [options] lower_hls upper_hls", description = """
|
|
|
|
Changes the color map in mentat.
|
|
|
|
|
|
|
|
Interpolates colors between "lower_hls" and "upper_hls".
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
|
|
|
|
parser.add_option("-s","--symmetric", action = "store_true",
|
|
|
|
dest = "symmetric", \
|
|
|
|
help = "symmetric legend [%default]")
|
|
|
|
parser.add_option("-p", "--port", type = "int",\
|
|
|
|
dest = "port",\
|
|
|
|
help = "Mentat connection port [%default]")
|
2010-09-13 13:01:27 +05:30
|
|
|
parser.add_option("-b", "--baseindex", type = "int",\
|
|
|
|
dest = "baseIdx",\
|
|
|
|
help = "base index of colormap [%default]")
|
2010-08-17 02:17:27 +05:30
|
|
|
parser.add_option("-v", "--verbose", action="store_true",\
|
|
|
|
dest = "verbose",\
|
|
|
|
help = "write Mentat command stream also to stdout [%default]")
|
2010-09-06 22:21:11 +05:30
|
|
|
parser.set_defaults(port = 40007)
|
2010-09-13 13:01:27 +05:30
|
|
|
parser.set_defaults(baseIdx = 32)
|
2010-08-17 02:17:27 +05:30
|
|
|
parser.set_defaults(symmetric = False)
|
2010-09-06 22:21:11 +05:30
|
|
|
parser.set_defaults(verbose = False)
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
|
2010-09-06 22:21:11 +05:30
|
|
|
(options, colors) = parser.parse_args()
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### read hlsColors and check if they are valid hls values
|
|
|
|
|
2010-09-06 22:21:11 +05:30
|
|
|
hlsColors_limits = [[0.0,0.0,0.0],[1.0,1.0,1.0]]
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2010-09-06 22:21:11 +05:30
|
|
|
hlsColor_range = (options.symmetric and [[0,0.2,0.9],[0.333,0.2,0.9]]) or \
|
|
|
|
[[0.167,0.9,0.1],[0.167,0.1,0.9]]
|
|
|
|
msg = []
|
|
|
|
for i in [0,1]:
|
|
|
|
if len(colors) > i and colors[i] != None:
|
|
|
|
hlsColor_range[i] = map(float, colors[i].split(','))
|
|
|
|
|
|
|
|
if len(hlsColor_range[i]) != 3:
|
|
|
|
msg.append('please give %s color as three numbers (hue, lightness, saturation)...'%(['lower','upper'][i]))
|
|
|
|
|
|
|
|
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],['lower','upper'][i]))
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
|
2010-09-06 22:21:11 +05:30
|
|
|
if msg != []:
|
|
|
|
parser.error('\n'+'\n'.join(msg)+'\n')
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
### interpolate hls values
|
|
|
|
|
|
|
|
nColors = 32
|
|
|
|
if options.symmetric:
|
2010-09-06 22:21:11 +05:30
|
|
|
hlsColors = [ [ syminterpolate(comp, hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(nColors-1))
|
2010-08-17 02:17:27 +05:30
|
|
|
for j,comp in enumerate(["hue","lightness","saturation"]) ]
|
|
|
|
for idx in range(nColors) ]
|
|
|
|
else:
|
2010-09-06 22:21:11 +05:30
|
|
|
hlsColors = [ [ interpolate(hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(nColors-1))
|
2010-08-17 02:17:27 +05:30
|
|
|
for j,comp in enumerate(["hue","lightness","saturation"]) ]
|
|
|
|
for idx in range(nColors) ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### convert to rgb values
|
|
|
|
|
|
|
|
rgbColors = [ hls_to_rgb(hlsColor[0], hlsColor[1], hlsColor[2])
|
|
|
|
for hlsColor in hlsColors ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### connect to mentat and change colorMap
|
|
|
|
|
|
|
|
outputLocals = {}
|
|
|
|
print 'waiting to connect...'
|
|
|
|
py_connect('',options.port)
|
|
|
|
print 'connected...'
|
|
|
|
|
2010-09-13 13:01:27 +05:30
|
|
|
cmds = colorMap(rgbColors,options.baseIdx)
|
2010-08-17 02:17:27 +05:30
|
|
|
output(cmds,outputLocals,'Mentat')
|
|
|
|
py_disconnect()
|
|
|
|
|
|
|
|
if options.verbose:
|
|
|
|
output(cmds,outputLocals,'Stdout')
|
|
|
|
|
|
|
|
|