[vtkusers] Re: Colorscale Problem

Kenneth Sloan kennethrsloan at gmail.com
Wed Jan 31 10:09:56 EST 2007


Just a guess, but...

Colors are represented as points in RGB space.  Transitions are done  
by linear interpolation in that space.  The line from Blue to Red  
does not go through Yellow!  The line from Blue to Red goes through  
Purple!

Consider:  Red is <1.0, 0.0, 0.0>
            Blue is <0.0, 0.0, 1.0>

Midway between Red and Blue is <0.5, 0.0, 0.5>

Yellow (on the other hand) is <0.5, 0.5, 0.0>

Don't worry - it stumped Newton, for awhile.

The key point is that "color is NOT wavelength".  Linear  
interpolation in RGB space is not the same as linear interpolation  
along the spectrum.

If you want your scale bar to show the spectrum from Blue to Red, you  
will need to break up the scale bar and pick color values from the  
spectrum such that linear interpolation between these points gives a  
good approximation to the spectrum.

A common scheme is:
      <0.0, 0.0, 1.0> --> <0.0, 1.0, 0.0> --> <1.0, 0.0, 0.0>.

The above path is fairly crude - but I suggest that you start there  
and then refine it.

Looking more closely, I see that you are trying to get:
      <0.0, 0.0, 1.0> --> <1.0, 1.0, 0.0> --> <1.0, 0.0, 0.0>

That's OK - but now you still have the problem that rendering a  
single element with
<0.0, 0.0, 1.0> at one end and <1.0, 0.0, 0.0> at the other end  WILL  
NOT go through <1.0, 1.0, 0.0>.
Instead, the renderer will linearly interpolate from <0.0, 0.0, 1.0>  
to <1.0, 0.0, 0.0> and the color you will see in the  middle of the  
element will be <0.5, 0.0, 0.5>.

If that is the color you see - then my guess is correct and my  
explanation should also be correct.  If not, then I have guessed wrong.

To fix this, break your scale bar into two pieces.  In general, your  
scale bar element will need to have n-1 pieces when you have n  
distinct colors added to your colorTransferFunction.

So, the short and direct answer to your question is: the renderer  
does not interpolate values and then pick a color for each point  
along the way - the renderer picks colors at each end and  
interpolates *colors* from one end to the other.  You can thank  
Gouraud for this.

On Jan 31, 2007, at 2:39 AM, WolfgangZillig wrote:

> Hello,
>
> has nobody an idea why I get this strange color mapping with my  
> colorTransferFunction? Why can't I have a color transition from  
> blue over yellow to red in one element of my unstructured grid.
>
> Kind regards
> WolfgangZillig
>
>
>
> WolfgangZillig schrieb:
>> Hello,
>> I want to generate some grapfics from results from a finite  
>> element calculation. It works quite well but when I tried to  
>> generate a scalebar (out of a manually entered grid) I got some  
>> strange results. I want to have the color rangeing from red over  
>> yellow to blue. I've defined this range by:
>>     min=0.50
>>     max=0.80
>>     avg=(min+max)/2.0
>>     aTriangleMapper.SetInput(aTriangleGrid)
>>     colorTransferFunction = vtk.vtkColorTransferFunction()
>>     colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)
>>     colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)
>>     colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)
>>     aTriangleMapper.SetLookupTable(colorTransferFunction)
>> It looks for me that this range can not be drawn in one element of  
>> my grid. In the output from my calculations this is not a problem  
>> as there is not a that huge difference over one single element but  
>> in the mesh of my scalbar I got strange results. See included  
>> pictures.
>> So my question is: is this a limitation of vtk or do I do  
>> something wrong?
>> Kind regards
>> Wolfgang Zillig
>> --------------------------------------------------------------------- 
>> ---
>> --------------------------------------------------------------------- 
>> ---
>> --------------------------------------------------------------------- 
>> ---
>> #!/usr/bin/env python
>> import vtk,copy
>> from vtk.util.colors import *
>> scalars = vtk.vtkFloatArray()
>> scalars.InsertNextValue(0.8)
>> scalars.InsertNextValue(0.8)
>> scalars.InsertNextValue(0.5)
>> scalars.InsertNextValue(0.5)
>> trianglePoints = vtk.vtkPoints()
>> trianglePoints.InsertPoint(0, 0, 0, 0)
>> trianglePoints.InsertPoint(1, 1, 0, 0)
>> trianglePoints.InsertPoint(2, 0, 1, 0)
>> trianglePoints.InsertPoint(3, 1, 1, 0)
>> aTriangleGrid = vtk.vtkUnstructuredGrid()
>> aTriangle = vtk.vtkTriangle()
>> aTriangle.GetPointIds().SetId(0, 0)
>> aTriangle.GetPointIds().SetId(1, 1)
>> aTriangle.GetPointIds().SetId(2, 2)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangle.GetPointIds().SetId(0, 1)
>> aTriangle.GetPointIds().SetId(1, 3)
>> aTriangle.GetPointIds().SetId(2, 2)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangleGrid.SetPoints(trianglePoints)
>> aTriangleGrid.GetPointData().SetScalars(scalars)
>> aTriangleMapper = vtk.vtkDataSetMapper()
>> # colorTransferFunction
>> min=0.50
>> max=0.80
>> avg=(min+max)/2.0
>> aTriangleMapper.SetInput(aTriangleGrid)
>> colorTransferFunction = vtk.vtkColorTransferFunction()
>> colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)
>> colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)
>> colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)
>> aTriangleMapper.SetLookupTable(colorTransferFunction)
>> aTriangleActor = vtk.vtkActor()
>> aTriangleActor.SetMapper(aTriangleMapper)
>> aTriangleActor.AddPosition(0, 0, 0)
>> # aTriangleActor.GetProperty().SetRepresentationToWireframe()
>> # Create the usual rendering stuff.
>> ren = vtk.vtkRenderer()
>> renWin = vtk.vtkRenderWindow()
>> renWin.AddRenderer(ren)
>> renWin.SetSize(300, 300)
>> iren = vtk.vtkRenderWindowInteractor()
>> interactor=vtk.vtkInteractorStyleTerrain()
>> iren.SetInteractorStyle(interactor)
>> iren.SetRenderWindow(renWin)
>> ren.SetBackground(1, 1, 1)
>> ren.AddActor(aTriangleActor)
>> ren.ResetCamera()
>> ren.GetActiveCamera().Azimuth(0)
>> ren.GetActiveCamera().Elevation(0)
>> # ren.GetActiveCamera().Dolly(2.8)
>> ren.ResetCameraClippingRange()
>> # Render the scene and start interaction.
>> iren.Initialize()
>> interactor=vtk.vtkInteractorStyleTerrain()
>> iren.SetInteractorStyle(interactor)
>> renWin.Render()
>> iren.Start()
>> renderLarge = vtk.vtkRenderLargeImage()
>> renderLarge.SetInput(ren)
>> renderLarge.SetMagnification(1)
>> # We write out the image which causes the rendering to occur. If you
>> # watch your screen you might see the pieces being rendered right
>> # after one another.
>> writer = vtk.vtkPNGWriter()
>> writer.SetInputConnection(renderLarge.GetOutputPort())
>> writer.SetFileName("colorscale_false.png")
>> writer.Write()
>> --------------------------------------------------------------------- 
>> ---
>> --------------------------------------------------------------------- 
>> ---
>> --------------------------------------------------------------------- 
>> ---
>> #!/usr/bin/env python
>> import vtk,copy
>> from vtk.util.colors import *
>> scalars = vtk.vtkFloatArray()
>> scalars.InsertNextValue(0.8)
>> scalars.InsertNextValue(0.8)
>> scalars.InsertNextValue(0.5)
>> scalars.InsertNextValue(0.5)
>> scalars.InsertNextValue(0.65)
>> scalars.InsertNextValue(0.65)
>> trianglePoints = vtk.vtkPoints()
>> trianglePoints.InsertPoint(0, 0, 0, 0)
>> trianglePoints.InsertPoint(1, 1, 0, 0)
>> trianglePoints.InsertPoint(2, 0, 1, 0)
>> trianglePoints.InsertPoint(3, 1, 1, 0)
>> trianglePoints.InsertPoint(4, 0, 0.5, 0)
>> trianglePoints.InsertPoint(5, 1, 0.5, 0)
>> aTriangleGrid = vtk.vtkUnstructuredGrid()
>> aTriangle = vtk.vtkTriangle()
>> aTriangle.GetPointIds().SetId(0, 0)
>> aTriangle.GetPointIds().SetId(1, 1)
>> aTriangle.GetPointIds().SetId(2, 4)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangle.GetPointIds().SetId(0, 1)
>> aTriangle.GetPointIds().SetId(1, 5)
>> aTriangle.GetPointIds().SetId(2, 4)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangle.GetPointIds().SetId(0, 4)
>> aTriangle.GetPointIds().SetId(1, 5)
>> aTriangle.GetPointIds().SetId(2, 2)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangle.GetPointIds().SetId(0, 5)
>> aTriangle.GetPointIds().SetId(1, 3)
>> aTriangle.GetPointIds().SetId(2, 2)
>> aTriangleGrid.InsertNextCell(aTriangle.GetCellType 
>> (),aTriangle.GetPointIds())
>> aTriangleGrid.SetPoints(trianglePoints)
>> aTriangleGrid.GetPointData().SetScalars(scalars)
>> aTriangleMapper = vtk.vtkDataSetMapper()
>> min=0.50
>> max=0.80
>> avg=(min+max)/2.0
>> aTriangleMapper.SetInput(aTriangleGrid)
>> colorTransferFunction = vtk.vtkColorTransferFunction()
>> colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)
>> colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)
>> colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)
>> aTriangleMapper.SetLookupTable(colorTransferFunction)
>> aTriangleActor = vtk.vtkActor()
>> aTriangleActor.SetMapper(aTriangleMapper)
>> aTriangleActor.AddPosition(0, 0, 0)
>> # aTriangleActor.GetProperty().SetRepresentationToWireframe()
>> # Create the usual rendering stuff.
>> ren = vtk.vtkRenderer()
>> renWin = vtk.vtkRenderWindow()
>> renWin.AddRenderer(ren)
>> renWin.SetSize(300, 300)
>> iren = vtk.vtkRenderWindowInteractor()
>> interactor=vtk.vtkInteractorStyleTerrain()
>> iren.SetInteractorStyle(interactor)
>> iren.SetRenderWindow(renWin)
>> ren.SetBackground(1, 1, 1)
>> ren.AddActor(aTriangleActor)
>> ren.ResetCamera()
>> ren.GetActiveCamera().Azimuth(0)
>> ren.GetActiveCamera().Elevation(0)
>> # ren.GetActiveCamera().Dolly(2.8)
>> ren.ResetCameraClippingRange()
>> # Render the scene and start interaction.
>> iren.Initialize()
>> interactor=vtk.vtkInteractorStyleTerrain()
>> iren.SetInteractorStyle(interactor)
>> renWin.Render()
>> iren.Start()
>> renderLarge = vtk.vtkRenderLargeImage()
>> renderLarge.SetInput(ren)
>> renderLarge.SetMagnification(1)
>> # We write out the image which causes the rendering to occur. If you
>> # watch your screen you might see the pieces being rendered right
>> # after one another.
>> writer = vtk.vtkPNGWriter()
>> writer.SetInputConnection(renderLarge.GetOutputPort())
>> writer.SetFileName("colorscale_right.png")
>> writer.Write()
>> --------------------------------------------------------------------- 
>> ---
>> _______________________________________________
>> This is the private VTK discussion list. Please keep messages on- 
>> topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>
> _______________________________________________
> This is the private VTK discussion list. Please keep messages on- 
> topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

--
Kenneth Sloan                                           
KennethRSloan at gmail.com
Computer and Information Sciences                        +1-205-934-2213
University of Alabama at Birmingham              FAX +1-205-934-5473
Birmingham, AL 35294-1170                http://www.cis.uab.edu/sloan/





More information about the vtkusers mailing list