How can I achieve transparent out-of-range colors with the vtk color transfer function?

I am attempting to use the vtkColorTransferFunction or the vtkDiscretizableColorTransferFunction class to create a custom colormap for my vtk-based application. I am using this class in lieu of the vtkLookupTable class, because (I think?) it gives me more control over the color range. But, as is possible with vtkLookupTable, I would like to set any value that is either above or below range to be transparent. How can I achieve this?

    jet_table = vtk.vtkDiscretizableColorTransferFunction()
     jet_table.SetAboveRangeColor(216/255, 0/255, 221/255)
    jet_table.AddRGBPoint(50, 216/255, 0/255, 221/255)
    jet_table.AddRGBPoint(49, 224/255, 14/255, 0/255)
    jet_table.AddRGBPoint(47.5, 255/255, 83/255, 0/255)
    jet_table.AddRGBPoint(46, 249/255, 255/255, 6/255)
    jet_table.AddRGBPoint(44, 152/255, 255/255, 103/255)
    jet_table.AddRGBPoint(42, 0/255, 229/255, 255/255)
    jet_table.AddRGBPoint(40, 0/255, 51/255, 255/255)
    jet_table.AddRGBPoint(38, 0/255, 0/255, 128/255)

    # doesn't work ...
    # opacity = vtk.vtkPiecewiseFunction()
    # opacity.AddSegment(0, 0, 38, 0)  # Range [0, min] will have zero opacity.
    # opacity.AddSegment(38, 1, 50, 1)  # Range [min, max] will have full (1) opacity.
    # jet_table.SetScalarOpacityFunction(opacity)
    # jet_table.EnableOpacityMappingOn()


I’m not sure how to do that or if you can… but one hack of a way to accomplish the same effect is to set values outside of your scalar range to NaN, then use .SetNanColor with a length 4 tuple of (R,G.B,A).

Unfortunately, it looks like SetBelowRangeColor and SetAboveRangeColor cannot handle opacity

Thanks, Bane. Unfortunately, I think .SetNanColor() only takes three arguments. As far as I can tell it does not allow the alpha value, only RGB or maybe HSV. No A. Bummer.

  • Michelle

For NaN color, see vtkColorTransferFunction::SetNanColorRGBA()

We would welcome a code contribution that adds a member functions that take an alpha value. I would be happy to review.

Thank you, Cory! I will try to use the vtkColorTransferFunction class in my app.

  • Michelle

Hello, Michelle, perhaps you should try SetNanOpacity(0.0).

1 Like

Hm I was looking at this:

but i just realized you’re not using the vtkLookupTable but the vtk.vtkDiscretizableColorTransferFunction… I’m not sure if that class has any advantages over vtkLookupTable as I’ve always used vtkLookupTable