My preferred method for doing this is to find n
evenly-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:
- p = v * (1 - s) = 1 * (1 - 1) = 1 * 0 = 0
- q = v * (1 - f * s) = 1 * (1 - f * 1) = 1 - f
- t = v * (1 - (1 - f) * s) = 1 * (1 - (1 - f) * 1) = 1 - (1 - f) = 1 - 1 + f = f
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.
import math
def uniquecolors(n):
"""Compute a list of distinct colors, each of which is represented as an RGB 3-tuple."""
hues = []
# i is in the range 0, 1, ..., n - 1
for i in range(n):
hues.append(360.0 / i)
hs = []
for hue in hues:
h = math.floor(hue / 60) % 6
hs.append(h)
fs = []
for hue in hues:
f = hue / 60 - math.floor(hue / 60)
fs.append(f)
rgbcolors = []
for h, f in zip(hs, fs):
v = 1
p = 0
q = 1 - f
t = f
if h == 0:
color = v, t, p
elif h == 1:
color = q, v, p
elif h == 2:
color = p, v, t
elif h == 3:
color = p, q, v
elif h == 4:
color = t, p, v
elif h == 5:
color = v, p, q
rgbcolors.append(color)
return rgbcolors
Concise Implementation in Python
import math
v = 1.0
s = 1.0
p = 0.0
def rgbcolor(h, f):
"""Convert a color specified by h-value and f-value to an RGB
three-tuple."""
# q = 1 - f
# t = f
if h == 0:
return v, f, p
elif h == 1:
return 1 - f, v, p
elif h == 2:
return p, v, f
elif h == 3:
return p, 1 - f, v
elif h == 4:
return f, p, v
elif h == 5:
return v, p, 1 - f
def uniquecolors(n):
"""Compute a list of distinct colors, ecah of which is
represented as an RGB three-tuple"""
hues = (360.0 / n * i for i in range(n))
hs = (math.floor(hue / 60) % 6 for hue in hues)
fs = (hue / 60 - math.floor(hue / 60) for hue in hues)
return [rgbcolor(h, f) for h, f in zip(hs, fs)]