# How do I reorder gradient colors for highest contrast?

If I color the world's 41 time zones using the qgis' "spectral" gradient, I get the following, as expected:

I like the colors (though it'd be nice to get a little purple in there), but want to re-arrange them so that nearby values have very different values. In other words, I want to do the opposite of a gradient, and have colors change as much as possible between the 41 discrete values. Notes:

• I realize something like this requires discrete values, since creating an continuous "antigradient" wouldn't work.

• I'm asking for a general procedure to do this for any gradient, not just this specific gradient. In particular, I plan to create a pure "rainbow" gradient where every color has saturation 1 and value 1.

• I'm looking for an "antigradient" that has 41 different colors. I realize I could use the 4-color theorem, and the data I'm using even has color_6 and color_8 fields suggesting which zones should have different colors. However, I want to create a legend which maps each color to a value.

• I realize the term "have colors change as much as possible" isn't well-defined, but hopefully I've gotten the idea across. As a counterexample, using alternating colors wouldn't work because each value would then have the same color as the values that are 2 above and 2 below it. What I want: the closer two values are, the more different the colors are.

• Using "random colors" works surprisingly well, but it's hit and miss, since I can't guarantee nearby values will have different colors:

• I'd personally refer to these as "divergent" gradients, but that term apparently already has a different meaning.

• The cpt-city christmas-candy gradient is one of the few that seems to do what I want, provided you use it with only 3 discrete values. The runners up are the Set1, Set2, and Set3 gradients, which make a deliberate effort to put different colors next to each other, but, again, they won't work for 41 values.

• Other cpt-city gradients (such as Spectral_11) ARE discrete, but still map nearby values to nearby colors.

• I'm familiar with the Kelly colors, but they're not quite what I'm looking for.

• I asked a mathematical version of this question at: https://mathematica.stackexchange.com/questions/198888/find-permutation-with-highest-organization-number-oeis-a047838 but I now believe that asks the wrong question

• This isn't strictly relevant, but when I save images like the above using qgis, they should be paletted with a small number of colors, but actually end up 24-bit with thousands of colors.

• Check out the four color theorem Commented May 23, 2019 at 17:48
• Related, if not dupe: How do I select a high-contrast color set for a map? Commented May 23, 2019 at 17:56
• @ZachSaucier, I was thinking about that as well. But it doesn't fit the condition about " the closer two values are, the more different the colors are". It sounds a little impossible... Commented May 23, 2019 at 17:56
• I'm looking forward to see if this question can indeed be answered. I think it's impossible, but I can't be sure. Try to do it with a list of numbers from 1-10. Anyway, two comments: 1. The "rainbow" gradient you talk about, with saturation=0 and value=1 will just be white for any hue. 2. Answer to your last bullet: Your image is anti-aliased so there are many intermediate colors to create smooth contours. Commented May 23, 2019 at 18:11
• @ZachSaucier I've edited the question to reflect I'm looking for 41 colors in this case. I am familiar with things like Kelly colors, but that's not the same as using a gradient.
– user90460
Commented May 23, 2019 at 18:22

It's an interesting question, but I don't think it's possible to arrange the colors of a gradient in a way which satisfies the condition "the closer two values are, the more different the colors are".

(I'm a graphic designer - not a mathematician, so I can't give you any mathematical proof. I can only try to visualize what my logic tells me.)

You have a "linear" gradient which is "clamped" at the edges. (I'll use your image with 25 colors as example - I know you want 41 colors) It goes from red (color 0) over orange (color 12) to blue (color 24):

Let's try to start arranging the colors. We have to have the red color (0) first at the left, since it has no neighbors to its left. The color after red must be the color which is most different from it, which is the blue color (24):

So far so good. After that we need to have the color which is second most different from red, color 23:

Already we have a problem! The condition is true for the red color, but the two blues can't be next to each other. Since the logic is already broken it makes no sense to continue.

So what if you had a gradient which "wrapped" around in a circular manner? It could look like this (there's your purple btw):

Now the red color (like all the other colors) is "in the middle" of the range so that's not a problem anymore. We can start out with the two most different colors from the other side of the circle (I accidentally made an even number of colors - the example would be more clear with an uneven number - sorry):

If we continue satisfying the condition we end up with an even gradient on each side with colors which are becoming less and less different from red:

The same problem as before! We can't satisfy the condition for two colors at the same time.

The closest I can get to answering your question would be the following (though I suspect that this is what you mean by "alternating colors").

Cut the gradient in two and "weave" the colors together:

I hope this can be a help somehow and that I don't misunderstand your question.

• I now think you're correct and I'm wrong. The condition `the closer two values are, the more different the colors are` is too strict. I did consider the splitting in two and shifting (which is sort of like alternating color) or even three or four. It may be that my problem is simply poorly formulated. I'm looking into giving a better definition (maybe requiring the highest possible minimum difference between two elements)
– user90460
Commented May 23, 2019 at 23:45
• There isn't necessarily a solution to any set of conditions you can imagine. And if there is, it isn't given that it's aesthetically pleasing. I'm thinking my weaving suggestion actually might fit your new proposed rule. Can't really see how to get a higher minimum difference than this (12 in this example with 25 colors). But it doesn't look like you wanted. Maybe if you could post an image of a color range which you think has that quality you are looking for and then we could analyze it to see if a certain pattern applied? Commented May 24, 2019 at 20:42
• I believe your alternating colors solution is mathematically the optimal one. Nice illustrations. Commented May 26, 2019 at 21:22