Paraview has nice lookup tables, is there an easy way to use them in VTK?

Paraview has a nice set of preset lookup tables, is there any way to use those in VTK?

Here’s a few of them that aren’t too long:

from vtkmodules.vtkRenderingCore import vtkColorTransferFunction

def table_factory(name):
    "Return one of a few example color lookup tables."
    ctf = vtkColorTransferFunction()
    match name:
        case "CoolToWarm":
            ctf.SetColorSpaceToDiverging()
            ctf.AddRGBPoint(0.0, 0.23137, 0.29803, 0.75294)
            ctf.AddRGBPoint(0.5, 0.86499, 0.86499, 0.86499)
            ctf.AddRGBPoint(1.0, 0.70588, 0.01568, 0.14901)
            ctf.SetNanColor(1.0, 1.0, 0.0)

        case "Blackbody":
            ctf.AddRGBPoint(0.0, 0.00000, 0.00000, 0.00000)
            ctf.AddRGBPoint(0.4, 0.90196, 0.00000, 0.00000)
            ctf.AddRGBPoint(0.8, 0.90196, 0.90196, 0.00000)
            ctf.AddRGBPoint(1.0, 1.00000, 1.00000, 1.00000)
            ctf.SetNanColor(0.00000, 0.49803, 1.00000)

        case "XRay":
            ctf.AddRGBPoint(0.0, 1.00000, 1.00000, 1.00000)
            ctf.AddRGBPoint(1.0, 0.00000, 0.00000, 0.00000)
            ctf.SetNanColor(1.0, 0.0, 0.0)

        case "BlackBlueWhite":
            ctf.AddRGBPoint(0.000, 0.00000, 0.00000, 0.00000)
            ctf.AddRGBPoint(0.333, 0.00000, 0.00000, 0.50196)
            ctf.AddRGBPoint(0.666, 0.00000, 0.50196, 1.00000)
            ctf.AddRGBPoint(1.000, 1.00000, 1.00000, 1.00000)
            ctf.SetNanColor(1.0, 1.0, 0.0)

        case _:
            raise ValueError("unexpected lookup table name")

    return ctf

t1 = table_factory("CoolToWarm")
t2 = table_factory("Blackbody")
t3 = table_factory("XRay")
t4 = table_factory("BlackBlueWhite")

There is a function to export color maps in ParaView out to a JSON representation. In the UI, go to the Choose Preset dialog, click on the color map you want to export, and click the “Export” button. “X- Ray” looks like:

[
        {
                "ColorSpace" : "RGB",
                "DefaultMap" : true,
                "Name" : "X Ray",
                "NanColor" : 
                [
                        1,
                        0,
                        0
                ],
                "RGBPoints" : 
                [
                        0,
                        1,
                        1,
                        1,
                        1,
                        0,
                        0,
                        0
                ]
        }
]

Note: the RGBPoints array is interleaved as XRGB 4-tuples where X is the data value that maps to the RGB color.

You can use the Python json module to import that file and set the RGB points and color space like you are doing in your example script.

A lot of the work has already been done for you:

2 Likes

The info provided by Cory is, of course, correct. However, some people may find this example confusing because the RGB values are integers. Here is some more info from PresetColorTables.cxx

static constexpr ColorTable<2> XRay = {
  vtkm::cont::ColorSpace::RGB,
  { 0, 1, 1, 1,
    1, 0, 0, 0
  }
};

static constexpr ColorTable<4> BlackBlueWhite = {
  vtkm::cont::ColorSpace::RGB,
  { 0, 0, 0, 0,
    0.333, 0, 0, 0.501960784314,
    0.666, 0, 0.501960784314, 1,
    1, 1, 1, 1
  }
};

Thanks, @whereismymoney, that is an important detail!

Brilliant! I didn’t know about those examples (clearly).