# Can't color a XML Unstructured Grid (\*.vtu)

**URL:** https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660
**Category:** VR/AR
**Tags:** file-formats
**Created:** [June 4, 2022, 9:31am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660 "2022-06-04T09:31:20Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [June 4, 2022, 9:31am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/1 "2022-06-04T09:31:20Z")

</div>

Hi I have code, which can color \*.vtp, \*.vtk.  
But same code fails to color \*.vtu.

I have list of vtkactors. And I specify which actor to set properties from.

This code which gets all column names from actor index:

```auto
        public static List<string> getColumns(int actorIndex)
        {
            List<string> colums = new List<string>();
            vtkActor actor = actors[actorIndex];
            var output = actor.GetMapper().GetInputAsDataSet();
            var cell = output.GetCellData();
            for (int i = 0; i < cell.GetNumberOfArrays(); i++)
            {
                var colm = cell.GetArrayName(i);
                if (colm != null) colums.Add(colm);

            }
            return colums;
         }

```

**This code colors the model of specific column.**

```auto
        public static void setColors(int actorIndex, string column)
        {
            List<string> colums = new List<string>();
            vtkActor actor = actors[actorIndex];
            var output = actor.GetMapper().GetInputAsDataSet();
            var colors = vtkUnsignedCharArray.New();
            colors.SetNumberOfComponents(3);
            colors.SetName("Colors");

            var cell = output.GetCellData();
            for (int i = 0; i < cell.GetNumberOfArrays(); i++)
            {
                var cells = cell.GetArray(i);
                if (cells == null) continue;
                if (column != cell.GetArrayName(i)) continue;
                for (int j = 0; j < cells.GetNumberOfTuples(); j++)
                {
                    var c = color_range((cells.GetComponent(i, j) / cell.GetArray(i).GetRange()[1]) * 100);
                    var R = c.R;
                    var G = c.G;
                    var B = c.B;
                    colors.InsertNextTuple3((double)R, (double)G, (double)B);
                    colors.InsertNextTuple3((double)R, (double)G, (double)B);
                }

            }
            output.GetPointData().SetScalars(colors);
        }

```

This code gets range of values from specific column.

```auto
        public static double[] getRange(int actorIndex, string column)
        {
            List<string> colums = new List<string>();
            vtkActor actor = actors[actorIndex];
            var output = actor.GetMapper().GetInputAsDataSet();
            var cell = output.GetCellData();
            for (int i = 0; i < cell.GetNumberOfArrays(); i++)
            {
                var colm = cell.GetArrayName(i);
                var arr = cell.GetArray(i);
                if (column == colm)
                {
                    if (arr == null) return null;
                    return arr.GetRange();
                }
            }
            return null;
        }

```

Result looks like this for \*.vtp or \*.vtk (GOOD):  
 ![Screenshot 2022-05-29 213601](https://discourse.vtk.org/uploads/default/original/2X/c/cb2bad5a0e640cf44cf468d1216ecff8d4d22e01.png)

Result for \*.vtu in paraview (GOOD):

 ![Screenshot 2022-06-04 001639](https://discourse.vtk.org/uploads/default/original/2X/7/7ca8c078b6b6bbdf01a082fb3110e592ed9711aa.png)

But result for \*.vtu in my code (BAD):

 ![Screenshot 2022-06-04 001835](https://discourse.vtk.org/uploads/default/original/2X/2/2ec807000efa3b17e035d4d92e841a645ce66159.png)

Please let me know whats I’m doing wrong.

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 4, 2022, 9:52am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/2 "2022-06-04T09:52:47Z")

</div>

> [@graysuit](#):
>
> ```auto
> for (int j = 0; j < cells.GetNumberOfTuples(); j++)
> {
> var c = color_range((cells.GetComponent(i, j) / cell.GetArray(i).GetRange()[1]) * 100);
> var R = c.R;
> var G = c.G;
> var B = c.B;
> colors.InsertNextTuple3((double)R, (double)G, (double)B);
> colors.InsertNextTuple3((double)R, (double)G, (double)B);
> }
> 
> ```

I think the problem is that you are appending gray colors gere in the code.  
And I don’t see where you input your colors array in the setColors function

Hope it helps

Mquro

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [June 4, 2022, 10:01am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/3 "2022-06-04T10:01:50Z")

</div>

Thanks alot for reply.

Sorry I forgot to explain what color\_range is.  
It has following syntax: `Color color_range(double Percentage)`  
I specify percentage as

```nohighlight
(cells.GetComponent(i, j) / cell.GetArray(i).GetRange()[1]) * 100

```

and it returns color range from blue to red.  
Its filled inside `c` variable.  
For interpolation, I use following code: [Color Interpolation Between 3 Colors in .NET - Stack Overflow](https://stackoverflow.com/a/1236826)

Even if I override c and set its value as `c = Color.Blue`, it would show the same gray color.

But strangely, the same code works fine for vtp, vtk.  
Just it fails for vtu files.

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 4, 2022, 10:15am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/4 "2022-06-04T10:15:15Z")

</div>

I guess the imterpolation maybe overflowing and don’t need that multiply by 100.

And I don’t know why put this line twice:

```auto
colors.InsertNextTuple3

```

Hope it helps

Mauro

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [June 4, 2022, 10:30am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/5 "2022-06-04T10:30:09Z")

</div>

![Screenshot 2022-06-04 151902](https://discourse.vtk.org/uploads/default/original/2X/4/4f38bd60b5fe4a1239ec37da2de4b544eba2daee.png)

I have checked colors and are interpolated fine.  
Please see color scale in image. Its my own label made, and uses that same color.  
I verified following way:

```auto
                    var R = c.R;
                    var G = c.G;
                    var B = c.B;
                    Console.WriteLine("rgb(" + R + "," +G + "," + B +")");
/*
rgb(108,89,176) purple like
rgb(132,95,168) purple like
rgb(157,102,160) pink like
rgb(181,108,152) pink like
rgb(206,115,144) pink like
rgb(230,121,136) red like
rgb(255,128,128) red like
rgb(59,76,192) blue like
*/

```

I’m not clear about `colors.InsertNextTuple3`.  
I started calling it twice, as vtp files working with twice and else raises unsafe errors.  
But calling once or twice has no effect on VTU currently.

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 4, 2022, 10:35am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/6 "2022-06-04T10:35:58Z")

</div>

I think you fprgot to set colors in the mapper

```auto
cylinderActor->GetProperty()->SetColor(
      colors->GetColor4d("Tomato").GetData());

```

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [June 4, 2022, 10:46am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/7 "2022-06-04T10:46:04Z")

</div>

Although `Actor.GetProperty().SetColor(color);` works to color the model.  
But it color whole model with same color.  
Whereas I expected to interpolate colors according to percentages.

Please see:

 ![Screenshot 2022-06-04 154527](https://discourse.vtk.org/uploads/default/original/2X/a/a9af1430da307fb7048f5e5cfe09b6732efda6ff.png)

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 4, 2022, 11:33am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/8 "2022-06-04T11:33:20Z")

</div>

You need to set an array for each cell

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [June 5, 2022, 7:29am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/9 "2022-06-05T07:29:43Z")

</div>

Sorry sir, I didn’t understood.  
Please can you elaborate a bit more or specify tiny example ?

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [June 5, 2022, 10:25am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/10 "2022-06-05T10:25:29Z")

</div>

Here is an example that tells you how to do it… But if you need more help, let me know

> <https://stackoverflow.com/questions/68065472/vtk-c-coloring-points-to-scalar-values-on-custom-polydata-to-custom-colors>

Note: I think you should add scalars to the cells instead of the points, but more or less it should be similar.  
Experiment with the lookup table definition, you only need two values “red” and “blue” defined as vectors as you were doing.

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [July 5, 2022, 2:10pm UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/11 "2022-07-05T14:10:12Z")

</div>

Hi sir, Sorry for late reply.

I tried following article and their resources, but vtu color has no effect.  
I tried cell data too instead of point data.  
I also tried lookup table, it do shows with color scalebar but has no effect on model color.

The strange things are that:

- all vtp files work and all vtu files are fails
- vtu file show colorful fine on paraview, but not in my code

I guess we need to handle vtu differently than normally we do.

Thanks for so much.

---

<div class="post-metadata">

### Author: ![graysuit](https://discourse.vtk.org/user_avatar/discourse.vtk.org/graysuit/32/5107_2.png) [@graysuit](https://discourse.vtk.org/u/graysuit)
#### Post date: [August 16, 2022, 8:05am UTC](https://discourse.vtk.org/t/cant-color-a-xml-unstructured-grid-vtu/8660/12 "2022-08-16T08:05:55Z")

</div>

> [@vtkClipClosedSurface with RGBA](https://discourse.vtk.org/t/vtkclipclosedsurface-with-rgba/10140/1):
>
> I am not sure if this is a proper solution, but it might work if you create a new mapper at the end of the `setColors` function.
> 
> ```auto
> output.GetPointData().SetScalars(colors);
> 
> + vtkDataSetMapper mapper = vtkDataSetMapper.New();
> + mapper.SetInput(output);
> + actor.SetMapper(mapper);
> }
> 
> ```

I got that solution from here:  
[https://discourse.paraview.org/t/how-to-color-unstructured-grid-vtu/10140/2](https://discourse.paraview.org/t/how-to-color-unstructured-grid-vtu/10140/2)
