invert a lookup table

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

This is a button to do that from paraview:
image

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 ?

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:

  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.

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