I’m trying to graphically display a graph of N lines and I’m trying to find a way to dynamically assign distinct colors based on how many lines I have. The values in RGB range from 0 to 1. I can’t use white because the background is white. I found it easy for N < 7:
r=(h&0x4)/4;
g=(h&0x2)/2;
b=h&0x1;
This gives me black, blue, green, cyan, red, magenta, yellow. But after that it will use white and then loop. Does anybody know a good way to assign RGB values for an index? I also have an opacity value to play with.
My preferred method for doing this is to find
nevenly-spaced points along the colour wheel.We represent the colour wheel as a range of values between 0 and 360. Thus, the values we will use are
360 / n * 0,360 / n * 1, …,360 / n * (n - 1). In doing this, we’ve defined the hue of each of our colours. We can describe each of these colours as Hue-Saturation-Value (HSV) colours by setting saturation to 1 and lightness to 1.(A higher saturation means the colour is more “rich”; a lower saturation means the colour is closer to gray. A higher lightness means the colour is “brighter”; a lower lightness means the colour is “darker”.)
Now, a simple calculation gives us the RGB values of each of these colours.
http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_HSV_to_RGB
Note that the equations given can be simplified:
Pseudo-code-ish Implementation in Python
Note: This is intentionally a horribly inefficient implementation. The point of giving this example in Python is essentially so I can give executable pseudocode.
Concise Implementation in Python