Color maps use wrong number of colors to create gradient

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:

1 Like

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 of ApplyColors::applyIndexColors() the code should be

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.

1 Like

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;

1 Like

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