HSV Modulate
Contents
HSV Modulate Python Expression
Example python expression that modulates the 'Value' component of the HSV representation of a colormap by a given variable.
Start by opening the the rect2d.silo example dataset.
Setup expressions
Create two standard expressions:
- Vector Mesh Variable: d_color = colorlookup(d,"hot")
- Scalar Mesh Variable: p_norm = (p-1.054)/(42.25-1.054)
(p_norm simply normalizes the data extents of p to a range between 0.0 and 1.0)
Create a python expression:
- Vector Mesh Variable: d_modulated_by_p with arguments d_color, p_norm and the following python code:
from math import floor
def rgb_to_hsv(r,g,b):
vmin = min(r, g, b)
vmax = max(r, g, b)
v = vmax
delta = vmax - vmin
if vmax != 0:
s = delta / vmax
else:
# r = g = b = 0 s = 0, v is undefined
s = 0.0;
h = -1.0;
return h,s,v
if r == vmax:
h = ( g - b ) / delta # between yellow & magenta
elif g == vmax:
h = 2 + ( b - r ) / delta # between cyan & yellow
else:
h = 4 + ( r - g ) / delta # between magenta & cyan
h *= 60.0 # degrees
if h < 0:
h += 360.0;
return h,s,v
def hsv_to_rgb(h,s,v):
h /= 60.0 # sector 0 to 5
i = floor(h)
f = h - i # factorial part of h
p = v * ( 1 - s )
q = v * ( 1 - s * f )
t = v * ( 1 - s * ( 1 - f ) )
if s == 0:
# achromatic (grey)
r = v
g = v
b = v
elif i == 0:
r = v
g = t
b = p
elif i == 1:
r = q
g = v
b = p
elif i == 2:
r = p
g = v
b = t
elif i == 3:
r = p
g = q
b = v
elif i == 4:
r = t
g = p
b = v
else:
r = v
g = p
b = q
return r,g,b
def do_modulate(r,g,b,m):
h,s,v = rgb_to_hsv(r,g,b)
v *= m
rn,gn,bn = hsv_to_rgb(h,s,v)
return rn,gn,bn
class ValueModulate(SimplePythonExpression):
def __init__(self):
SimplePythonExpression.__init__(self)
self.name = "ValueModulate"
self.description = "Modulate HSV 'Value' by another mesh var."
self.output_is_point_var = False
self.output_dimension = 3
def derive_variable(self,ds_in,domain_id):
ncells = ds_in.GetNumberOfCells()
res = vtk.vtkFloatArray()
res.SetNumberOfComponents(3)
res.SetNumberOfTuples(ncells)
# get the input vector
rgb_vec = ds_in.GetCellData().GetArray(self.input_var_names[0])
mod_val = ds_in.GetCellData().GetArray(self.input_var_names[1])
# do the conversion for each cell
for i in xrange(ncells):
cell = ds_in.GetCell(i)
r,g,b = rgb_vec.GetTuple3(i)
m = mod_val.GetTuple1(i)
h,s,v = do_modulate(r,g,b,m)
res.SetTuple3(i, h,s,v)
return res
py_filter = ValueModulate
Your Expressions Window will look simlar to the following:
Plot result
Now create a Truecolor Plot of "d_modulated_by_p" and you should see the following:
Invert the modulation
Redefine p_norm to be:
- Scalar Mesh Variable: p_norm = 1.0 - (p-1.054)/(42.25-1.054)
Clear and redraw your Truecolor plot and you should see the following: