# ColorCells

VTKExamples/Cxx/PolyData/ColorCells

### Description

This example shows how to assign specific colors to individual cells of a vtkPolyData using a vtkLookupTable. The vtkCellData has a vtkFloatArray that contains a scalar values.

Seealso

The example ColorCellsWithRGB creates a vtkCellData with a vtkUnsignedCharArray that contains rgb values.

### Code

ColorCells.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>

    #include <vtkLookupTable.h>
    #include <vtkFloatArray.h>
    #include <vtkCellData.h>
    #include <vtkPolyData.h>
    #include <vtkPlaneSource.h>

    #include <vtkNamedColors.h>

    #include <algorithm>

    int main(int , char *[])
    {
      // Provide some geometry
      int resolution = 3;
      vtkSmartPointer<vtkPlaneSource> aPlane =
        vtkSmartPointer<vtkPlaneSource>::New();
      aPlane->SetXResolution(resolution);
      aPlane->SetYResolution(resolution);

      // Create cell data
      vtkSmartPointer<vtkFloatArray> cellData =
        vtkSmartPointer<vtkFloatArray>::New();
      for (int i = 0; i < resolution * resolution; i++)
      {
        cellData->InsertNextValue(i + 1);
      }

      // Create a lookup table to map cell data to colors
      vtkSmartPointer<vtkLookupTable> lut =
        vtkSmartPointer<vtkLookupTable>::New();
      int tableSize = std::max(resolution*resolution + 1, 10);
      lut->SetNumberOfTableValues(tableSize);
      lut->Build();

      // Fill in a few known colors, the rest will be generated if needed
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();
      lut->SetTableValue(0, colors->GetColor4d("Black").GetData());
      lut->SetTableValue(1, colors->GetColor4d("Banana").GetData());
      lut->SetTableValue(2, colors->GetColor4d("Tomato").GetData());
      lut->SetTableValue(3, colors->GetColor4d("Wheat").GetData());
      lut->SetTableValue(4, colors->GetColor4d("Lavender").GetData());
      lut->SetTableValue(5, colors->GetColor4d("Flesh").GetData());
      lut->SetTableValue(6, colors->GetColor4d("Raspberry").GetData());
      lut->SetTableValue(7, colors->GetColor4d("Salmon").GetData());
      lut->SetTableValue(8, colors->GetColor4d("Mint").GetData());
      lut->SetTableValue(9, colors->GetColor4d("Peacock").GetData());

      aPlane->Update(); // Force an update so we can set cell data
      aPlane->GetOutput()->GetCellData()->SetScalars(cellData);

      // Setup actor and mapper
      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection(aPlane->GetOutputPort());
      mapper->SetScalarRange(0, tableSize - 1);
      mapper->SetLookupTable(lut);

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);

      // Setup render window, renderer, and interactor
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);
      renderer->AddActor(actor);
      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(ColorCells)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(ColorCells MACOSX_BUNDLE ColorCells.cxx )

    target_link_libraries(ColorCells ${VTK_LIBRARIES})

### Download and Build ColorCells

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

    cd ColorCells/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:

    ./ColorCells

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