[vtkusers] Change Contour Colors

Eric E. Monson emonson at cs.duke.edu
Mon Jul 2 09:53:25 EDT 2012


Hey Derek,

First, I think red to blue is the default because the rainbow color map is unfortunately still the default colormap for many VTK routines. Historically this has been the case for much scientific software. Some good people fought at Kitware to have the default colormap changed to blue-white-red a few years back.

Regarding your main question: Of course, this is only one way to do it, but here's the way I usually build colormaps in VTK when I want more control, and especially when I want to use a diverging color scheme. (Sorry for all of the bad abbreviated variable names, but I'm just copying and pasting this from some old code that should be reworked.) This particular color scheme goes to gray in the middle, but you'll get the point.

Talk to you later,
-Eric

===========

lutNum = 256
lut.SetNumberOfTableValues(lutNum)

ctf = vtk.vtkColorTransferFunction()
ctf.SetColorSpaceToDiverging()

cl = []
cl.append([float(cc)/255.0 for cc in [202, 0, 32]])	# Variant of Colorbrewer RdBu 5
cl.append([float(cc)/255.0 for cc in [244, 165, 130]])
cl.append([float(cc)/255.0 for cc in [140, 140, 140]])
cl.append([float(cc)/255.0 for cc in [146, 197, 222]])
cl.append([float(cc)/255.0 for cc in [5, 113, 176]])

vv = [float(xx)/float(len(cl)-1) for xx in range(len(cl))]
vv.reverse()

for pt, color in zip(vv, cl):
	ctf.AddRGBPoint(pt, color[0], color[1], color[2])

for ii, ss in enumerate([float(xx)/float(lutNum) for xx in range(lutNum)]):
	cc = ctf.GetColor(ss)
	lut.SetTableValue(ii, cc[0], cc[1], cc[2], 1.0)

lut.SetRange(-1024,1024)


On Jul 1, 2012, at 5:15 PM, Derek Gaston wrote:

> Hello all.
> 
> I'm writing a bit of custom Python with Qt and VTK to add some visualization to a small PyQt GUI we have.
> 
> I need to read ExodusII and just do some simple contour plotting of what's in there with a color scale/legend (I also draw the edges so you can see the mesh).  I have it basically working at this point (although hardcoded to a specific variable name... but I'll change that later). But I'm hung up at trying to change the colors of the contours.
> 
> All I want is very simple.  Right now it generates from Red to Blue (low to high)... all I need is for it to go from Blue to Red (BTW - why the heck is Red to Blue the default... shouldn't Blue to Red be the default?  I think most people associate Red with a high color... especially if you are plotting temperature... but that's beside the point).
> 
> I've looked at vtkLookupTable and vtkColorTransferFunction... but I can't seem to come up with the right magic.  Basically, what I want is just the simple Blue to Red HSV color bar that you can get in Paraview by choosing that preset.  Any help would be awesome!
> 
> Here is my current code (it's in a Python class that inherits from QWidget and uses a QVTKWidget2 that is initialized elsewhere):
> 
> 
> 
>     self.file_name = file_name
>     reader = vtk.vtkExodusIIReader()
>     reader.SetFileName(self.file_name)
>     reader.UpdateInformation()
>     reader.SetAllArrayStatus(vtk.vtkExodusIIReader.NODAL, 1)
>     reader.SetAllArrayStatus(vtk.vtkExodusIIReader.NODAL_TEMPORAL, 1)
>     reader.SetTimeStep(1)
>     reader.Update()
> 
>     cdp = vtk.vtkCompositeDataPipeline()
>     vtk.vtkAlgorithm.SetDefaultExecutivePrototype(cdp)
> 
>     geom = vtk.vtkCompositeDataGeometryFilter()
>     geom.SetInputConnection(0,reader.GetOutputPort(0));
>     geom.Update()
> 
>     data = geom.GetOutput()
>     data.GetPointData().SetScalars(data.GetPointData().GetArray("u"))
>     mapper = vtk.vtkPolyDataMapper()
>     mapper.SetInput(data)
>     mapper.ScalarVisibilityOn()
>     mapper.SetColorModeToMapScalars()
>     mapper.SetColorMode(2)
>     mapper.SetScalarRange(0,1.0)
> 
>     actor = vtk.vtkActor()
>     actor.SetMapper(mapper)
>     self.renderer.AddActor(actor)
> 
>     edge_geom = vtk.vtkCompositeDataGeometryFilter()
>     edge_geom.SetInputConnection(0,reader.GetOutputPort(0));
>     edge_geom.Update()
> 
>     edges = vtk.vtkExtractEdges()
>     edges.SetInput(edge_geom.GetOutput())
>     edge_mapper = vtk.vtkPolyDataMapper()
>     edge_mapper.SetInput(edges.GetOutput())
> 
>     edge_actor = vtk.vtkActor()
>     edge_actor.SetMapper(edge_mapper)
>     edge_actor.GetProperty().SetColor(0,0,0)
> 
>     self.renderer.AddActor(edge_actor)
> 
>     scalar_bar = vtk.vtkScalarBarActor()
>     scalar_bar.SetLookupTable(mapper.GetLookupTable())
>     scalar_bar.SetTitle("u")
>     scalar_bar.SetNumberOfLabels(4)
> 
>     self.renderer.AddActor2D(scalar_bar)
> 
>     # Avoid z-buffer fighting
>     vtk.vtkPolyDataMapper().SetResolveCoincidentTopologyToPolygonOffset()
> 
> 
>     self.renderer.ResetCamera()
>     self.vtkwidget.updateGL()
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list