# [vtkusers] How to map colors based on multiple scalars?

Charles Kind charles.kind at bristol.ac.uk
Mon Dec 11 05:00:39 EST 2017

```Lasse,

This one had me stumped for a while too. Below is the code I have used to
implement HSV varying colours over the unit sphere. I am not sure about
extending beyond three variables. First declare an UnsignedCharArray and

def ColorVectors(pd):
numPoints = pd.GetNumberOfPoints()
Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetName("Colors")
#Color by Vector
#Set Hue from planar angle
#Set Value and reverse order [0,1]->[1,0]
for x in range(numPoints):
azi =
math.atan2(pd.GetPointData().GetAbstractArray(0).GetTuple(x)[0],pd.GetPointData().GetAbstractArray(0).GetTuple(x)[1])
+ math.pi
cH = azi / (2*math.pi)
pol =
math.acos(pd.GetPointData().GetAbstractArray(0).GetTuple(x)[2])
cV = ((pol / math.pi)-1)*(-1)
cS  = math.sin(pol)
cR = 255*vtk.vtkMath.HSVToRGB(cH,cS,cV)[0]
cG = 255*vtk.vtkMath.HSVToRGB(cH,cS,cV)[1]
cB = 255*vtk.vtkMath.HSVToRGB(cH,cS,cV)[2]
Colors.InsertNextTuple3(cR, cG, cB)
pd.GetPointData().SetScalars(Colors)
return pd

Then what I did here, as I was colouring vectors and my PolyData contained
points, vectors, and color data was in my glyph creation,

def MakeGlyphs(arrowSource,csG):
glyph3D = vtk.vtkGlyph3D()
glyph3D.SetSourceConnection(arrowSource.GetOutputPort())
glyph3D.SetVectorModeToUseVector()
glyph3D.SetInputData(csG)
glyph3D.SetInputArrayToProcess(0,0,1,0,'m')
glyph3D.SetInputArrayToProcess(0,0,0,0,'colors')
#glyph3D.SetScaleFactor(1e-07)
glyph3D.SetColorModeToColorByScalar()
glyph3D.Update()
return glyph3D

was to tell the glyph which arrays to use and set color mode to scalar. This
worked!!

Good luck,

Charlie

--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
```