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

**URL:** https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257
**Category:** Support
**Created:** [December 11, 2019, 8:01pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257 "2019-12-11T20:01:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [December 11, 2019, 8:01pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/1 "2019-12-11T20:01:29Z")

</div>

Hello,  
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.SetColorSpaceToRGB()
    jet_table.SetScaleToLinear()
     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()

```

Thanks!  
Michelle

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [December 12, 2019, 5:23am UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/2 "2019-12-12T05:23:33Z")

</div>

> [@MichelleKline](#):
>
> I would like to set any value that is either above or below range to be transparent. How can I achieve this?

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

---

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [December 12, 2019, 2:57pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/4 "2019-12-12T14:57:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [December 12, 2019, 2:59pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/5 "2019-12-12T14:59:53Z")

</div>

For NaN color, see [vtkColorTransferFunction::SetNanColorRGBA()](https://vtk.org/doc/nightly/html/classvtkColorTransferFunction.html#a00eb8f47912a4f14647d34f7cbbd47fb)

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

---

<div class="post-metadata">

### Author: ![MichelleKline](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michellekline/32/2155_2.png) [@MichelleKline](https://discourse.vtk.org/u/MichelleKline)
#### Post date: [December 12, 2019, 3:05pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/6 "2019-12-12T15:05:07Z")

</div>

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

- Michelle

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [December 13, 2019, 10:05pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/7 "2019-12-13T22:05:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [December 13, 2019, 10:27pm UTC](https://discourse.vtk.org/t/how-can-i-achieve-transparent-out-of-range-colors-with-the-vtk-color-transfer-function/2257/8 "2019-12-13T22:27:17Z")

</div>

> [@MichelleKline](#):
>
> 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.

Hm I was looking at this: [VTK: vtkLookupTable Class Reference](https://vtk.org/doc/nightly/html/classvtkLookupTable.html#a76fc100e67f14c4dc3744a76b23d3652)

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`
