GENERATE N DISTINCT COLORS
While working on a project I realized I needed to generate a specific number of colors that are visually distinct from each other so that when two colors are side-by-side, the viewer can tell they are different. In my case, the players (of a game I am working on) need to pick a piece color so they can't be confused by another player's pieces.explanation and source code) There are three channels to RGB, which unsurprisingly are Red, Green, and Blue. An RGB number can be represented in Hexadecimal like so: #3CA6FF. What my algorithm does is vary each channel value in large jumps. Start with each channel supporting two values: 00 and FF. If we vary each channel with these values we get 8 different colors (2 values per channel for 3 channels: 2*2*2 or 2^3):One could argue about what "visually distinct color" means since the human eye/brain perceives a linear hue gradient (aka rainbow) in bands. Even a linear change in a single color is seen in bands (sudden changes of shade or saturation). There is also the issue with color blindness... I ignored all those "what-abouts" and "what ifs" out since I wanted something quick and dirty... something finished and useful. So, I decided to slice up the RGB color space. Sure, I could have messed with HSL or HSV, but using RGB does the trick just fine... ...continued (
# this is pseudo code total = 0 for red in range(4): for green in range(4): for blue in range(4): if total <= N: print color(red, green, blue) total += 1Not only that, but your list will include more of the less-distinct colors. For example, there isn't much difference between #000033 and #000000 or #66CC99 and #99CC99. It would be better to include #FF0000 before including values that are "close" to each other. So, the python code included is an attempt to output colors that are more distinct first. Take a look at these samples (generated with colortohtml.py). You'll notice that the lower the N, the more distinct the color set is.