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

**URL:** https://discourse.vtk.org/t/how-to-preserve-the-original-cell-color-when-using-scalars-to-change-the-partial-cell-color/7685
**Category:** Support
**Created:** [January 20, 2022, 8:18am UTC](https://discourse.vtk.org/t/how-to-preserve-the-original-cell-color-when-using-scalars-to-change-the-partial-cell-color/7685 "2022-01-20T08:18:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![AlexLuya](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alexluya/32/4036_2.png) [@AlexLuya](https://discourse.vtk.org/u/AlexLuya)
#### Post date: [January 20, 2022, 8:18am UTC](https://discourse.vtk.org/t/how-to-preserve-the-original-cell-color-when-using-scalars-to-change-the-partial-cell-color/7685/1 "2022-01-20T08:18:14Z")

</div>

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;  
> scalars-\>SetNumberOfTuples(size);
> 
> //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);  
> }  
> data()-\>GetCellData()-\>SetScalars(scalars);
> 
> \_parent-\>rerender(this);  
> }

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?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 23, 2022, 2:29pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-original-cell-color-when-using-scalars-to-change-the-partial-cell-color/7685/2 "2022-01-23T14:29:07Z")

</div>

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 ([https://vtk.org/doc/nightly/html/classvtkScalarsToColors.html#a55abf196da147f0cb284a6c0a663ae68](https://vtk.org/doc/nightly/html/classvtkScalarsToColors.html#a55abf196da147f0cb284a6c0a663ae68)). So far, I never used such method nor I could find any examples.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![Aron\_Helser](https://discourse.vtk.org/user_avatar/discourse.vtk.org/aron_helser/32/14_2.png) [@Aron\_Helser](https://discourse.vtk.org/u/Aron_Helser)
#### Post date: [January 23, 2022, 4:12pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-original-cell-color-when-using-scalars-to-change-the-partial-cell-color/7685/3 "2022-01-23T16:12:43Z")

</div>

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.

HTH,  
Aron
