# How do I use vtkjs to color cells individually

**URL:** https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797
**Category:** Web
**Created:** [November 11, 2022, 2:29am UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797 "2022-11-11T02:29:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![755\_simulation](https://discourse.vtk.org/user_avatar/discourse.vtk.org/755_simulation/32/6010_2.png) [@755\_simulation](https://discourse.vtk.org/u/755_simulation)
#### Post date: [November 11, 2022, 2:29am UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/1 "2022-11-11T02:29:52Z")

</div>

![image](https://discourse.vtk.org/uploads/default/original/2X/9/91523f92bb4d499d5bbde57599e0146a652b3a85.png)

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [November 11, 2022, 5:17pm UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/2 "2022-11-11T17:17:26Z")

</div>

Assign a field to your cells and color by that field.

---

<div class="post-metadata">

### Author: ![755\_simulation](https://discourse.vtk.org/user_avatar/discourse.vtk.org/755_simulation/32/6010_2.png) [@755\_simulation](https://discourse.vtk.org/u/755_simulation)
#### Post date: [November 14, 2022, 8:05am UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/3 "2022-11-14T08:05:22Z")

</div>

I can understand what you mean, but I’m sorry that I don’t quite understand how to write this code. Could you please give me a similar example?

---

<div class="post-metadata">

### Author: ![755\_simulation](https://discourse.vtk.org/user_avatar/discourse.vtk.org/755_simulation/32/6010_2.png) [@755\_simulation](https://discourse.vtk.org/u/755_simulation)
#### Post date: [November 14, 2022, 8:09am UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/4 "2022-11-14T08:09:25Z")

</div>

I tried to do this using the filter Calculator, but if I did this, the other cells wouldn’t stay white and would turn red, as shown in the image below.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/b/b92a1ce3daebecc7c9f849b76e500cdf9b55a8c4.png)

---

<div class="post-metadata">

### Author: ![Ebrahim\_Ebrahim](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ebrahim_ebrahim/32/5752_2.png) [@Ebrahim\_Ebrahim](https://discourse.vtk.org/u/Ebrahim_Ebrahim)
#### Post date: [November 14, 2022, 12:35pm UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/5 "2022-11-14T12:35:24Z")

</div>

I had to color cells individually once before, and here is the approach I took:

```nohighlight
  addScalarsForColor = vtkCalculator.newInstance();
  addScalarsForColor.setInputConnection(<the previous filter whose output is polydata>.getOutputPort());
  addScalarsForColor.setFormula({
    getArrays: inputDataSets => ({
      input: [],
      output: [
        { location: FieldDataTypes.CELL, name: 'MyColorsArray', dataType: 'Int8Array', attribute: AttributeTypes.SCALARS, numberOfComponents: 3 },
      ],
    }),
    evaluate: (arraysIn, arraysOut) => {
      let dataArray = arraysOut[0]; // a vtkDataArray
      for (let i = 0; i < dataArray.getNumberOfTuples(); i++) {
        dataArray.setTuple(i, <set the color of the i^th cell here (an RGB vector like [255,255,182])> );
      }
    },
  });

  mapper = vtkMapper.newInstance();
  mapper.setInputConnection(addScalarsForColor.getOutputPort());
  mapper.setScalarVisibility(true); // it's true by default but, but just emphasizing here that we need it to be.
  mapper.setScalarModeToUseCellFieldData(); // use cell field data to set colors; this necessitates the next line
  mapper.setColorByArrayName('MyColorsArray'); // since we use cell field data for colors, we need to specify which data array will be used

  // this makes it so that the cell field data array that we use for colors is interpreted directly as RGB values, rather than as scalars to be passed through some lookup table.
  mapper.setColorModeToDirectScalars();

```

---

<div class="post-metadata">

### Author: ![755\_simulation](https://discourse.vtk.org/user_avatar/discourse.vtk.org/755_simulation/32/6010_2.png) [@755\_simulation](https://discourse.vtk.org/u/755_simulation)
#### Post date: [November 15, 2022, 7:11am UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/6 "2022-11-15T07:11:57Z")

</div>

Excellent. That’s very useful. Thank you very much for your answer.

---

<div class="post-metadata">

### Author: ![j-fu](https://discourse.vtk.org/user_avatar/discourse.vtk.org/j-fu/32/3977_2.png) [@j-fu](https://discourse.vtk.org/u/j-fu)
#### Post date: [July 20, 2026, 11:55pm UTC](https://discourse.vtk.org/t/how-do-i-use-vtkjs-to-color-cells-individually/9797/7 "2026-07-20T23:55:54Z")

</div>

This made my day! [Fix trimesh bug & update stuff by j-fu · Pull Request #23 · j-fu/PlutoVista.jl · GitHub](https://github.com/j-fu/PlutoVista.jl/pull/23/changes/a04d24622967b5c927b2a3e4f74cde32de3d07c4)
