Algorithm For Generating Unique Colors

You can multiply the id by the golden ratio (phi) to get a number 0 <= n < 1. All of the numbers you generate will tend to be evenly distributed.

PHI = (1 + sqrt(5))/2
n = id * PHI - floor(id * PHI) 

Then it's a matter of converting that number to a color, e.g.

hue = floor(n * 256)

I have written an article about this same topic:

How to Generate Random Colors Programmatically

Basically you can use the HSV color space, set a fixed Saturation and Value, and modify the Hue. When you know the number of colors you need, just split the Hue's range [0,1[ by this. If you do not know the number of required colors, you can still use the golden ratio to select colors from this space.


I've checked all the ways to do it, solving more than a few ways. And finally brute force with CIEde2000 and really, your best bet is just a static list. You can't get more than about 30 colors with good distinctness no matter what you do. Factor in color blindness and the lines of confusion, and you're really making something that is a hindrance. Use a lookup table and modify the data points with some other characteristics than just color.

max difference color palette

#000000
#00FF00
#0000FF
#FF0000
#01FFFE
#FFA6FE
#FFDB66
#006401
#010067
#95003A
#007DB5
#FF00F6
#FFEEE8
#774D00
#90FB92
#0076FF
#D5FF00
#FF937E
#6A826C
#FF029D
#FE8900
#7A4782
#7E2DD2
#85A900
#FF0056
#A42400
#00AE7E
#683D3B
#BDC6FF
#263400
#BDD393
#00B917
#9E008E
#001544
#C28C9F
#FF74A3
#01D0FF
#004754
#E56FFE
#788231
#0E4CA1
#91D0CB
#BE9970
#968AE8
#BB8800
#43002C
#DEFF74
#00FFC6
#FFE502
#620E00
#008F9C
#98FF52
#7544B1
#B500FF
#00FF78
#FF6E41
#005F39
#6B6882
#5FAD4E
#A75740
#A5FFD2
#FFB167
#009BFF
#E85EBE