# Color maps use wrong number of colors to create gradient

**URL:** <https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218>\
**Category:** Support\
**Created:** [November 5, 2025, 2:12pm UTC](https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218 "2025-11-05T14:12:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brockdyer03](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/brockdyer03/32/2015_2.png) [@brockdyer03](https://discuss.avogadro.cc/u/brockdyer03)\
**Post date:** [November 5, 2025, 2:12pm UTC](https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218/1 "2025-11-05T14:12:27Z")

</div>

## Bug

I was using Avogadro to color a graphene sheet by the atomic index with the `turbo` colormap, and I noticed that the endpoint of the color map is not actually the correct endpoint.

## Environment Information

Avogadro version: 1.102.0  
Operating system and version: Windows 11 Pro

## Expected Behavior

Color maps for coloring atoms by some property should be a gradient of _N_ colors where _N_ is the total number of atoms that will be colored.

## Actual Behavior

Color maps are a gradient of _N_+1 colors, and the last color in the map is discarded. It’s just an off-by-one error.

## Steps to Reproduce

Take any collection of _N_ atoms, color by Atomic Index, then compare the color of the last atom to the last color in the color map. Here is a picture of ethylene colored by index with the `turbo` colormap, along with an atom right next to the last atom with the correct final color:

 ![image](https://discuss.avogadro.cc/uploads/default/original/2X/3/3020e56567962aad03695823f45c7c830e68e20a.jpeg)

---

<div class="post-metadata">

**Author:** ![brockdyer03](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/brockdyer03/32/2015_2.png) [@brockdyer03](https://discuss.avogadro.cc/u/brockdyer03)\
**Post date:** [November 5, 2025, 2:57pm UTC](https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218/2 "2025-11-05T14:57:47Z")

</div>

I actually think I might know where the relevant code is for this. I am at work so can’t test it right now, but I think here [in line 230](https://github.com/OpenChemistry/avogadrolibs/blob/4b5eea466536f2765c191dae81ee1d3425697cb5/avogadro/qtplugins/applycolors/applycolors.cpp#L230) of `ApplyColors::applyIndexColors()` the code should be

```cpp
float indexFraction = float(i) / float(numAtoms) - 1;

```

because currently `i` is set by `for (Index i = 0; i < numAtoms; ++i)` (line 225), so the max value of `i` will be `numAtoms - 1`.

---

<div class="post-metadata">

**Author:** ![ghutchis](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/ghutchis/32/456_2.png) [@ghutchis](https://discuss.avogadro.cc/u/ghutchis)\
**Post date:** [November 5, 2025, 3:33pm UTC](https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218/3 "2025-11-05T15:33:55Z")

</div>

Looks good, thanks. Feel free to submit a patch - just make sure that you use:

`float indexFraction = float(i) / (float(numAtoms) - 1);`

Or use `const float maxIndex = float(numAtoms) - 1.0f;`

---

<div class="post-metadata">

**Author:** ![system](https://discuss.avogadro.cc/uploads/default/original/1X/93fe5a56d10b304e678127e54139c28cde211662.png) [@system](https://discuss.avogadro.cc/u/system)\
**Post date:** [November 8, 2025, 3:34pm UTC](https://discuss.avogadro.cc/t/color-maps-use-wrong-number-of-colors-to-create-gradient/7218/4 "2025-11-08T15:34:18Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
