Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm interested in what algorithms or rules I can programmatically implement to generate RGB or HSV colors for plotlines to keep them visually distinct from the neighbors.

I know that in professional mapmaking, there are algorithms or rules that make sure that no two adjacent countries on a map are the same color. I can also think of Microsoft office Excel as picking good tints/shades for plot lines (red, then blue, then purple/orange).

Here's an example of what I'm talking about - I need to generate colors for 12 lines on a black background. The colors here I've hardcoded by hand using web-safe RGB color codes. The problem arises when these lines overlap - it's hard to tell if one is looking at purple, slighly darker purple or violet. I'm looking for a better algorithm to produce colors for plotlines like these.

Multiple plot lines color example

Here's an example using Flot plotting library for jQuery, it has nice succession of colors for graphs: enter image description here

share|improve this question
It seems they just chose the base colors. And you have only five lines in this chart while your other example has twelve. If you would let Flot have more graphs you would most likely run into similar problems. There are only that many different colors and from there you have to repeat shades and can only try to have some "distance" between them. –  thorsten müller May 14 '13 at 14:35
While there is a very interesting (and good) problem here, from a practical implementation side, I would strognly suggest looking at identifying the color set for a theme (greyscale, high contrast, pastels, color blind, etc...) with a reasonable maximum number (before it becomes unreadable) - 32 or 128 or so colors (depending on the application) and use that array index rather than trying to calculate a next color. –  MichaelT May 14 '13 at 15:50
add comment

1 Answer

I recommend using the HSV or HSL color spaces, not the RGB color space, because HSV and HSL are better structured for generating colors that look different to humans. You'll have more work in RGB (though conversions back and forth exist, should you need them).

This is what HSV / HSL look like: enter image description here

When using the HSV or HSL color space you can assume (very roughly) that the difference between the H (hue) components of two colors are a good approximation of the perceptual distance between the colors - i.e. the bigger the change in hue, the more different the colors will look to humans. You can try playing with S (saturation) and L/V (lightness/value) as well to eke out a few more very different colors, but they will not look as different for the same value change as varying the hue.

Depending on the number of distinct colors that you need, you can divide the hue-space into that number of different colors. If for example you have a hue-range of 256 values and you require 16 distinct colors, then your first color might be (0, 128, 128), your second (16, 128, 128) and so on. I somewhat arbitrarily picked S/L values smack in the middle here as that will usually be light and saturated enough to clearly see the color differences. This system is simple, and assumes you need to know nothing about adjacency of colors in your graph/map.

If you do not know in advance how many distinct colors you need but you know the upper limit, and dividing the hue range into colors with that upper limit in mind as above still gives you good perceptually different colors then you can use the same system with the upper limit.

If you (could) need very many distinct colors, you could still get away with using very similar or even the same colors, as long as they do not appear near the other elements of the graph that have the similar color. This requires knowing your adjacency situation in the graph you're rendering and may not always be straightforward, and even then it may not be a good idea as Dukeling points out in the comments: it may be confusing to viewers that the same color is used twice in the graph for two different concepts.

So finally in the most complex situation your graph is complex enough that you do not have enough color space to ensure you do not end up with distinct elements with colors that are too similar using the above system. In this case you need to build up an adjacency graph of elements of your visualization graph. Adjacency here is a fuzzy concept - you will have to define it correctly for your actual situation. For example in your second example the data on July 12 has a choke point where every color is adjacent to every other one. One approach that can help you if you can build up the adjacency graph is the graph coloring problem - there are libraries that may help you - for example boost::graph in C++.

share|improve this answer
+1 for HSL or HSV –  jk. May 14 '13 at 16:21
Regardless of whether or not there is intersection, you do not want the same colours representing different things on a single graph (if this is what you imply), there's too much room for confusion and you may not be able to figure out which is which, so graph colouring is unrelated. In case you didn't notice, each line only has a single colour and consists of a sequence of connected points. –  Dukeling May 14 '13 at 16:54
@Dukeling - you misunderstand my point. At some point, no matter how carefully you choose, if you need many different colors you will start having colors that look similar (e.g. the purples in the first image in the question). At that point a naïve but simple implementation like I suggested may put those colors close together, making it hard to see what's going on. That's when you can use graph coloring to ensure that you are using distant colors for "close" lines on the graph. –  Joris Timmermans May 15 '13 at 5:53
@Dukeling - I've edited my answer to include your remark about why reusing the same color may be bad. –  Joris Timmermans May 15 '13 at 9:59
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.