How to preserve the original cell color when using scalars to change the partial cell color?

Hello, these code was used to change partial cells’s color:

void coloringSelectedCells() {
vtkNew nc;
auto orbg = property()->GetColor();
double originalColor[4] = { orbg[0] * 255, orbg[1] * 255, orbg[2] * 255, 1 };
double newColor[3] = {0,255,0};

auto size = data()->GetNumberOfCells();

//setup scalars
vtkNew scalars;

//preserve surface color
for (vtkIdType i = 0; i < size; i++) {
scalars->SetTuple(i, originalColor);

//set cell color
for (auto const id : selectedCells) {
scalars->SetTuple(id, newColor);


Only partial cells(selected cells) are expected to set to new color,and others to preserve the original color,but reuslt is selectedCells do get set to expected color,but the color of other cells don’t get preserved but change to unexpected blue color,question is how to preserve original color for these partical cells?

Hi, Alex,

Scalars don’t work that way for most applications. If I understood it right, you set a 4-double scalar field expecting it to be directly mapped to colors, when just one of the tuple fields is used to set the color of the cell. This is what almost all applictions do and it’s likely what your code is doing.

Maybe you can achieve the desired behavior by building a vtkLookupTable via a vtkScalarsToColors, using one of the overrides of its MapVectorsThroughTable() method ( So far, I never used such method nor I could find any examples.



If you want to directly set a color per cell, you can call mapper->SetColorModeToDirectScalars(), then mapper->SelectColorArray("ConeColors") with the name of the cell data array that has the colors. If it is a double array, colors should be in the range 0-1. Examples that use SetColorModeToDirectScalars() should help.