#!/usr/bin/env python import sys, os from colorsys import * from optparse import OptionParser 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 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 # ----------------------------- def interpolate(val0, val1, x): return val0 + (val1 - val0) * x # ----------------------------- def syminterpolate(comp, val0, val1, x): if comp == "hue": return interpolate(val0, val1, x) if comp == "lightness": val_middle = max(0.9, val0, val1) elif comp == "saturation": val_middle = min(0.1, val0, val1) 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): cmds = [ "*color %i %f %f %f"%(idx+baseIdx,color[0],color[1],color[2]) 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]") parser.add_option("-b", "--baseindex", type = "int",\ dest = "baseIdx",\ help = "base index of colormap [%default]") parser.add_option("-v", "--verbose", action="store_true",\ dest = "verbose",\ help = "write Mentat command stream also to stdout [%default]") parser.set_defaults(port = 40007) parser.set_defaults(baseIdx = 32) parser.set_defaults(symmetric = False) parser.set_defaults(verbose = False) (options, colors) = parser.parse_args() ### read hlsColors and check if they are valid hls values hlsColors_limits = [[0.0,0.0,0.0],[1.0,1.0,1.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]] 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])) if msg != []: parser.error('\n'+'\n'.join(msg)+'\n') ### interpolate hls values nColors = 32 if options.symmetric: hlsColors = [ [ syminterpolate(comp, hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(nColors-1)) for j,comp in enumerate(["hue","lightness","saturation"]) ] for idx in range(nColors) ] else: hlsColors = [ [ interpolate(hlsColor_range[0][j], hlsColor_range[1][j], float(idx)/(nColors-1)) 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...' cmds = colorMap(rgbColors,options.baseIdx) output(cmds,outputLocals,'Mentat') py_disconnect() if options.verbose: output(cmds,outputLocals,'Stdout')