# invert a lookup table

**URL:** https://discourse.vtk.org/t/invert-a-lookup-table/1210
**Category:** Support
**Created:** [June 25, 2019, 2:07pm UTC](https://discourse.vtk.org/t/invert-a-lookup-table/1210 "2019-06-25T14:07:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)
#### Post date: [June 25, 2019, 2:07pm UTC](https://discourse.vtk.org/t/invert-a-lookup-table/1210/1 "2019-06-25T14:07:44Z")

</div>

Is there a function to invert a lookup table from vtk (vtk.js) ?

This is a button to do that from paraview:  
 ![image](https://discourse.vtk.org/uploads/default/original/1X/10cbee6e4b193d3fa0baab19dfbb7381a2ff749b.png)

---

<div class="post-metadata">

### Author: ![PBrockmann](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pbrockmann/32/524_2.png) [@PBrockmann](https://discourse.vtk.org/u/PBrockmann)
#### Post date: [June 25, 2019, 3:26pm UTC](https://discourse.vtk.org/t/invert-a-lookup-table/1210/2 "2019-06-25T15:26:26Z")

</div>

```auto
var colorMap = $("#selectColorMap").val();
var lookup = vtk.Rendering.Core.vtkColorTransferFunction.newInstance();
var preset = vtk.Rendering.Core.vtkColorTransferFunction.vtkColorMaps.getPresetByName(colorMap);

var table = preset.RGBPoints;
var tableInvert = [];
for (var i = table.length/4-1; i >= 0; i--) {
     tableInvert.push(-1*table[i*4], table[i*4+1], table[i*4+2], table[i*4+3]);
}
preset.RGBPoints = tableInvert;

lookup.applyColorMap(preset);
lookup.setNanColor(1.0, 1.0, 1.0, 1.0);
lookup.build();

```

will do it.  
Is there a better way ?

---

<div class="post-metadata">

### Author: ![Aron\_Helser](https://discourse.vtk.org/user_avatar/discourse.vtk.org/aron_helser/32/14_2.png) [@Aron\_Helser](https://discourse.vtk.org/u/Aron_Helser)
#### Post date: [June 15, 2023, 10:54pm UTC](https://discourse.vtk.org/t/invert-a-lookup-table/1210/3 "2023-06-15T22:54:44Z")

</div>

I was looking into this, too, and there still doesn’t seem to be a way to invert a colormap in VTK proper. The ParaView button ends up calling `vtkSMTransferFunctionProxy::InvertTransferFunction()` which does a loop like this:

```auto
  for (size_t cc = 0; cc < points.size(); cc++)
  {
    double& x = points[cc].GetData()[0];
    x = (1.0 - x);
  }

```

and then sets those points on the LUT proxy. So pretty close to your solution, except `1 - x` instead of `-x`.

---

<div class="post-metadata">

### Author: ![marlon.ma](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marlon.ma/32/6091_2.png) [@marlon.ma](https://discourse.vtk.org/u/marlon.ma)
#### Post date: [June 16, 2023, 1:17am UTC](https://discourse.vtk.org/t/invert-a-lookup-table/1210/4 "2023-06-16T01:17:06Z")

</div>

if (lut)  
{  
int index = lut-\>GetNumberOfTableValues();  
unsigned char swap[4];  
size\_t num = 4 \* sizeof(unsigned char);  
vtkUnsignedCharArray\* table = lut-\>GetTable();  
for (int count = 0; count \< --index; count++)  
{  
unsigned char\* rgba1 = table-\>GetPointer(4 \* count);  
unsigned char\* rgba2 = table-\>GetPointer(4 \* index);  
memcpy(swap, rgba1, num);  
memcpy(rgba1, rgba2, num);  
memcpy(rgba2, swap, num);  
}

```
	// force the lookuptable to update its InsertTime to avoid
	// rebuilding the array
	double temp[4];
	lut->GetTableValue(0, temp);
	lut->SetTableValue(0, temp);
} have a try

```
