parent
c2ae657f5b
commit
1dddfa040e
|
@ -10,7 +10,6 @@ class Color():
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
model = 'RGB',
|
model = 'RGB',
|
||||||
color = np.zeros(3,'d')):
|
color = np.zeros(3,'d')):
|
||||||
|
@ -49,20 +48,17 @@ class Color():
|
||||||
self.color = np.array(color,'d')
|
self.color = np.array(color,'d')
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
"""Color model and values."""
|
"""Color model and values."""
|
||||||
return 'Model: %s Color: %s'%(self.model,str(self.color))
|
return 'Model: %s Color: %s'%(self.model,str(self.color))
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Color model and values."""
|
"""Color model and values."""
|
||||||
return self.__repr__()
|
return self.__repr__()
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
def convert_to(self,toModel = 'RGB'):
|
||||||
def convertTo(self,toModel = 'RGB'):
|
|
||||||
"""
|
"""
|
||||||
Change the color model permanently.
|
Change the color model permanently.
|
||||||
|
|
||||||
|
@ -88,8 +84,7 @@ class Color():
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
def express_as(self,asModel = 'RGB'):
|
||||||
def expressAs(self,asModel = 'RGB'):
|
|
||||||
"""
|
"""
|
||||||
Return the color in a different model.
|
Return the color in a different model.
|
||||||
|
|
||||||
|
@ -99,7 +94,7 @@ class Color():
|
||||||
color model
|
color model
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return self.__class__(self.model,self.color).convertTo(asModel)
|
return self.__class__(self.model,self.color).convert_to(asModel)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -293,6 +288,7 @@ class Color():
|
||||||
self.model = converted.model
|
self.model = converted.model
|
||||||
self.color = converted.color
|
self.color = converted.color
|
||||||
|
|
||||||
|
|
||||||
def _XYZ2CIELAB(self):
|
def _XYZ2CIELAB(self):
|
||||||
"""
|
"""
|
||||||
Convert CIE XYZ to CIE Lab.
|
Convert CIE XYZ to CIE Lab.
|
||||||
|
@ -498,13 +494,13 @@ class Colormap():
|
||||||
def interpolate_linear(lo, hi, frac):
|
def interpolate_linear(lo, hi, frac):
|
||||||
"""Linear interpolation between lo and hi color at given fraction; output in model of lo color."""
|
"""Linear interpolation between lo and hi color at given fraction; output in model of lo color."""
|
||||||
interpolation = (1.0 - frac) * np.array(lo.color[:]) \
|
interpolation = (1.0 - frac) * np.array(lo.color[:]) \
|
||||||
+ frac * np.array(hi.expressAs(lo.model).color[:])
|
+ frac * np.array(hi.express_as(lo.model).color[:])
|
||||||
|
|
||||||
return Color(lo.model,interpolation)
|
return Color(lo.model,interpolation)
|
||||||
|
|
||||||
if self.interpolate == 'perceptualuniform':
|
if self.interpolate == 'perceptualuniform':
|
||||||
return interpolate_Msh(self.left.expressAs('MSH').color,
|
return interpolate_Msh(self.left.express_as('MSH').color,
|
||||||
self.right.expressAs('MSH').color,fraction)
|
self.right.express_as('MSH').color,fraction)
|
||||||
elif self.interpolate == 'linear':
|
elif self.interpolate == 'linear':
|
||||||
return interpolate_linear(self.left,
|
return interpolate_linear(self.left,
|
||||||
self.right,fraction)
|
self.right,fraction)
|
||||||
|
@ -528,7 +524,7 @@ class Colormap():
|
||||||
"""
|
"""
|
||||||
format = format.lower() # consistent comparison basis
|
format = format.lower() # consistent comparison basis
|
||||||
frac = 0.5*(np.array(crop) + 1.0) # rescale crop range to fractions
|
frac = 0.5*(np.array(crop) + 1.0) # rescale crop range to fractions
|
||||||
colors = [self.color(float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]).expressAs(model).color for i in range(steps)]
|
colors = [self.color(float(i)/(steps-1)*(frac[1]-frac[0])+frac[0]).express_as(model).color for i in range(steps)]
|
||||||
if format == 'paraview':
|
if format == 'paraview':
|
||||||
colormap = ['[\n {{\n "ColorSpace": "RGB", "Name": "{}", "DefaultMap": true,\n "RGBPoints" : ['.format(name)] \
|
colormap = ['[\n {{\n "ColorSpace": "RGB", "Name": "{}", "DefaultMap": true,\n "RGBPoints" : ['.format(name)] \
|
||||||
+ [' {:4d},{:8.6f},{:8.6f},{:8.6f},'.format(i,color[0],color[1],color[2],) \
|
+ [' {:4d},{:8.6f},{:8.6f},{:8.6f},'.format(i,color[0],color[1],color[2],) \
|
||||||
|
|
|
@ -38,7 +38,6 @@ class bcolors:
|
||||||
self.CROSSOUT = ''
|
self.CROSSOUT = ''
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def srepr(arg,glue = '\n'):
|
def srepr(arg,glue = '\n'):
|
||||||
"""
|
"""
|
||||||
Join arguments as individual lines.
|
Join arguments as individual lines.
|
||||||
|
@ -57,7 +56,7 @@ def srepr(arg,glue = '\n'):
|
||||||
return glue.join(str(x) for x in arg)
|
return glue.join(str(x) for x in arg)
|
||||||
return arg if isinstance(arg,str) else repr(arg)
|
return arg if isinstance(arg,str) else repr(arg)
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def croak(what, newline = True):
|
def croak(what, newline = True):
|
||||||
"""
|
"""
|
||||||
Write formated to stderr.
|
Write formated to stderr.
|
||||||
|
@ -70,11 +69,11 @@ def croak(what, newline = True):
|
||||||
Separate items of what by newline. Defaults to True.
|
Separate items of what by newline. Defaults to True.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if what is not None:
|
if not what:
|
||||||
sys.stderr.write(srepr(what,glue = '\n') + ('\n' if newline else ''))
|
sys.stderr.write(srepr(what,glue = '\n') + ('\n' if newline else ''))
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def report(who = None,
|
def report(who = None,
|
||||||
what = None):
|
what = None):
|
||||||
"""
|
"""
|
||||||
|
@ -86,27 +85,25 @@ def report(who = None,
|
||||||
croak( (emph(who)+': ' if who is not None else '') + (what if what is not None else '') + '\n' )
|
croak( (emph(who)+': ' if who is not None else '') + (what if what is not None else '') + '\n' )
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def emph(what):
|
def emph(what):
|
||||||
"""Formats string with emphasis."""
|
"""Formats string with emphasis."""
|
||||||
return bcolors.BOLD+srepr(what)+bcolors.ENDC
|
return bcolors.BOLD+srepr(what)+bcolors.ENDC
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def deemph(what):
|
def deemph(what):
|
||||||
"""Formats string with deemphasis."""
|
"""Formats string with deemphasis."""
|
||||||
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def delete(what):
|
def delete(what):
|
||||||
"""Formats string as deleted."""
|
"""Formats string as deleted."""
|
||||||
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def strikeout(what):
|
def strikeout(what):
|
||||||
"""Formats string as strikeout."""
|
"""Formats string as strikeout."""
|
||||||
return bcolors.CROSSOUT+srepr(what)+bcolors.ENDC
|
return bcolors.CROSSOUT+srepr(what)+bcolors.ENDC
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
def execute(cmd,
|
def execute(cmd,
|
||||||
streamIn = None,
|
streamIn = None,
|
||||||
wd = './',
|
wd = './',
|
||||||
|
@ -140,10 +137,11 @@ def execute(cmd,
|
||||||
out = out.decode('utf-8').replace('\x08','')
|
out = out.decode('utf-8').replace('\x08','')
|
||||||
error = error.decode('utf-8').replace('\x08','')
|
error = error.decode('utf-8').replace('\x08','')
|
||||||
os.chdir(initialPath)
|
os.chdir(initialPath)
|
||||||
if process.returncode != 0: raise RuntimeError('{} failed with returncode {}'.format(cmd,process.returncode))
|
if process.returncode != 0:
|
||||||
|
raise RuntimeError('{} failed with returncode {}'.format(cmd,process.returncode))
|
||||||
return out,error
|
return out,error
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
class extendableOption(Option):
|
class extendableOption(Option):
|
||||||
"""
|
"""
|
||||||
Used for definition of new option parser action 'extend', which enables to take multiple option arguments.
|
Used for definition of new option parser action 'extend', which enables to take multiple option arguments.
|
||||||
|
@ -164,11 +162,13 @@ class extendableOption(Option):
|
||||||
else:
|
else:
|
||||||
Option.take_action(self, action, dest, opt, value, values, parser)
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
||||||
|
|
||||||
# from https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
|
|
||||||
def progressBar(iteration, total, prefix='', bar_length=50):
|
def progressBar(iteration, total, prefix='', bar_length=50):
|
||||||
"""
|
"""
|
||||||
Call in a loop to create terminal progress bar.
|
Call in a loop to create terminal progress bar.
|
||||||
|
|
||||||
|
From https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
iteration : int
|
iteration : int
|
||||||
|
@ -202,7 +202,8 @@ def progressBar(iteration, total, prefix='', bar_length=50):
|
||||||
|
|
||||||
sys.stderr.write('\r{} {} {}'.format(prefix, bar, remaining_time)),
|
sys.stderr.write('\r{} {} {}'.format(prefix, bar, remaining_time)),
|
||||||
|
|
||||||
if iteration == total: sys.stderr.write('\n')
|
if iteration == total:
|
||||||
|
sys.stderr.write('\n')
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue