Skip to content

ColorMapToLUT

Repository source: ColorMapToLUT


Description

Demonstrate a cone using the vtkDiscretizableColorTransferFunction to generate the colormap.

These two Python functions can be used to generate C++ and Python functions from a JSON or XML colormap. They can then be copied into ColorMapToLUT.cxx, ColorMapToLUT.py or into your own code.

Feel free to use either of these programs to generate different colormaps until you find one you like.

A good initial source for color maps is: SciVisColor -- this will provide you with plenty of XML examples.

ColorMapToLUT_JSON will allow you to select colormaps by name from ParaView Default Colormaps.

Other languages

See (Python), (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ColorMapToLUT.cxx

#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkDiscretizableColorTransferFunction.h>
#include <vtkElevationFilter.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
// #include <vtkSphereSource.h>

#include <array>

namespace {
vtkNew<vtkDiscretizableColorTransferFunction> GetCTF();

}

int main(int, char*[])
{
  std::array<unsigned char, 4> bkg{82, 87, 110, 255};
  vtkNew<vtkNamedColors> colors;
  colors->SetColor("ParaViewBkg", bkg.data());

  vtkNew<vtkRenderer> ren;
  ren->SetBackground(colors->GetColor3d("ParaViewBkg").GetData());
  vtkNew<vtkRenderWindow> renWin;
  renWin->SetSize(640, 480);
  renWin->SetWindowName("ColorMapToLUT");
  renWin->AddRenderer(ren);
  vtkNew<vtkRenderWindowInteractor> iRen;
  iRen->SetRenderWindow(renWin);

  vtkNew<vtkInteractorStyleTrackballCamera> style;
  iRen->SetInteractorStyle(style);

  // vtkNew<vtkSphereSource> sphere;
  // sphere->SetThetaResolution(64);
  // sphere->SetPhiResolution(32);
  // auto bounds = sphere->GetOutput()->GetBounds();

  vtkNew<vtkConeSource> cone;
  cone->SetResolution(6);
  cone->SetDirection(0, 1, 0);
  cone->SetHeight(1);
  cone->Update();
  auto bounds = cone->GetOutput()->GetBounds();

  vtkNew<vtkElevationFilter> elevation_filter;
  elevation_filter->SetLowPoint(0, bounds[2], 0);
  elevation_filter->SetHighPoint(0, bounds[3], 0);
  elevation_filter->SetInputConnection(cone->GetOutputPort());
  // elevation_filter->SetInputConnection(sphere->GetOutputPort());

  auto ctf = GetCTF();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(elevation_filter->GetOutputPort());
  mapper->SetLookupTable(ctf);
  mapper->SetColorModeToMapScalars();
  mapper->InterpolateScalarsBeforeMappingOn();

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  ren->AddActor(actor);

  renWin->Render();
  iRen->Start();

  return EXIT_SUCCESS;
}

namespace {
vtkNew<vtkDiscretizableColorTransferFunction> GetCTF()
{
  // name: Fast, creator: Francesca Samsel and Alan W. Scott
  // interpolationspace: lab, interpolationtype: linear, space: RGB
  // file name: Fast.xml

  vtkNew<vtkDiscretizableColorTransferFunction> ctf;

  ctf->SetColorSpaceToLab();
  ctf->SetScaleToLinear();

  ctf->SetNanColor(0, 0, 0);
  ctf->SetAboveRangeColor(0, 0, 0);
  ctf->UseAboveRangeColorOn();
  ctf->SetBelowRangeColor(0, 0, 0);
  ctf->UseBelowRangeColorOn();

  ctf->AddRGBPoint(0, 0.05639999999999999, 0.05639999999999999, 0.47);
  ctf->AddRGBPoint(0.17159223942480895, 0.24300000000000013, 0.4603500000000004,
                   0.81);
  ctf->AddRGBPoint(0.2984914818394138, 0.3568143826543521, 0.7450246485363142,
                   0.954367702893722);
  ctf->AddRGBPoint(0.4321287371255907, 0.6882, 0.93, 0.9179099999999999);
  ctf->AddRGBPoint(0.5, 0.8994959551205902, 0.944646394975174,
                   0.7686567142818399);
  ctf->AddRGBPoint(0.5882260353170073, 0.957107977357604, 0.8338185108985666,
                   0.5089156299842102);
  ctf->AddRGBPoint(0.7061412605695164, 0.9275207599610714, 0.6214389091739178,
                   0.31535705838676426);
  ctf->AddRGBPoint(0.8476395308725272, 0.8, 0.3520000000000001,
                   0.15999999999999998);
  ctf->AddRGBPoint(1, 0.59, 0.07670000000000013, 0.11947499999999994);

  ctf->SetNumberOfValues(9);
  ctf->DiscretizeOff();

  return ctf;
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ColorMapToLUT)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ColorMapToLUT: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ColorMapToLUT MACOSX_BUNDLE ColorMapToLUT.cxx )
  target_link_libraries(ColorMapToLUT PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ColorMapToLUT
  MODULES ${VTK_LIBRARIES}
)

Download and Build ColorMapToLUT

Click here to download ColorMapToLUT and its CMakeLists.txt file. Once the tarball ColorMapToLUT.tar has been downloaded and extracted,

cd ColorMapToLUT/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ColorMapToLUT

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.