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':[''],
|
|
|
|
}
|
|
|
|
|
2010-10-26 21:56:55 +05:30
|
|
|
try:
|
|
|
|
file = open('%s/../MSCpath'%os.path.dirname(os.path.realpath(sys.argv[0])))
|
|
|
|
MSCpath = os.path.normpath(file.readline().strip())
|
|
|
|
file.close()
|
|
|
|
except:
|
|
|
|
MSCpath = '/msc'
|
2010-08-17 23:51:22 +05:30
|
|
|
|
|
|
|
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-10-26 21:56:55 +05:30
|
|
|
try:
|
|
|
|
from py_mentat import *
|
|
|
|
except:
|
|
|
|
print('error: no valid Mentat release found in %s'%MSCpath)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2011-02-04 15:06:26 +05:30
|
|
|
# -----------------------------
|
|
|
|
def readConfig(configFile,ownPath):
|
|
|
|
config = {}
|
|
|
|
configDir = os.path.split(os.path.realpath(ownPath))[0]
|
|
|
|
filename = os.path.join(configDir,configFile)
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
file = open(filename)
|
|
|
|
content = file.readlines()
|
|
|
|
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
|
|
|
|
|
2010-08-17 02:17:27 +05:30
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
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
|
|
|
|
# -----------------------------
|
|
|
|
|
2011-02-04 15:06:26 +05:30
|
|
|
parser = OptionParser(usage="%prog [options] configured scheme | (lower_h,l,s upper_h,l,s)", description = """
|
2010-08-17 02:17:27 +05:30
|
|
|
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]")
|
2011-02-04 15:06:26 +05:30
|
|
|
parser.add_option("-i","--inverse", action = "store_true",
|
|
|
|
dest = "inverse", \
|
|
|
|
help = "invert legend [%default]")
|
2010-08-17 02:17:27 +05:30
|
|
|
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]")
|
2011-02-04 15:06:26 +05:30
|
|
|
parser.add_option("-c", "--config", type='string', \
|
|
|
|
dest = "config",\
|
|
|
|
help = "configuration file [%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]")
|
2011-02-04 15:06:26 +05:30
|
|
|
|
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)
|
2011-02-04 15:06:26 +05:30
|
|
|
parser.set_defaults(config = 'colorMap.config')
|
2010-08-17 02:17:27 +05:30
|
|
|
parser.set_defaults(symmetric = False)
|
2011-02-04 15:06:26 +05:30
|
|
|
parser.set_defaults(inverse = False)
|
|
|
|
parser.set_defaults(verbose = False)
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2010-09-06 22:21:11 +05:30
|
|
|
msg = []
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2011-02-04 15:06:26 +05:30
|
|
|
(options, colors) = parser.parse_args()
|
2010-08-17 02:17:27 +05:30
|
|
|
|
2011-02-04 15:06:26 +05:30
|
|
|
config = readConfig(options.config,sys.argv[0])
|
|
|
|
|
|
|
|
if colors[0] in config:
|
|
|
|
options.symmetric = config[colors[0]]['symmetric']
|
|
|
|
hlsColor_range = [config[colors[0]]['lower'],\
|
|
|
|
config[colors[0]]['upper']]
|
|
|
|
elif len(colors) == 0:
|
|
|
|
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]]
|
|
|
|
elif len(colors) == 2:
|
|
|
|
hlsColor_range = [map(float, colors[i].split(',')) for i in range(2)]
|
|
|
|
else:
|
|
|
|
msg.append('two color tuples required')
|
|
|
|
|
|
|
|
if msg == []:
|
|
|
|
hlsColors_limits = [[0.0,0.0,0.0],[1.0,1.0,1.0]]
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|