This is something I've pseudo-solved many times and have never quite found a solution for.

The problem is to come up with a way to generate `N`

colors, that are as distinguishable as possible where `N`

is a parameter.

Translate

This is something I've pseudo-solved many times and have never quite found a solution for.

The problem is to come up with a way to generate `N`

colors, that are as distinguishable as possible where `N`

is a parameter.

所有的回答

My first thought on this is "how to generate N vectors in a space that maximize distance from each other."

You can see that the RGB (or any other scale you use that forms a basis in color space) are just vectors. Take a look at Random Point Picking. Once you have a set of vectors that are maximized apart, you can save them in a hash table or something for later, and just perform random rotations on them to get all the colors you desire that are maximally apart from each other!

Thinking about this problem more, it would be better to map the colors in a linear manner, possibly (0,0,0) → (255,255,255) lexicographically, and then distribute them evenly.

I really don't know how well this will work, but it should since, let us say:

we know we have 16777216 colors (256^3).

We can use Buckles Algorithm 515 to find the lexicographically indexed color.. You'll probably have to edit the algorithm to avoid overflow and probably add some minor speed improvements.

It would be best to find colors maximally distant in a "perceptually uniform" colorspace, e.g. CIELAB (using Euclidean distance between L*, a*, b* coordinates as your distance metric) and then converting to the colorspace of your choice. Perceptual uniformity is achieved by tweaking the colorspace to approximate the non-linearities in the human visual system.

Some related resources:

ColorBrewer - Sets of colours designed to be maximally distinguishable for use on maps.

Escaping RGBland: Selecting Colors for Statistical Graphics - A technical report describing a set of algorithms for generating good (i.e. maximally distinguishable) colour sets in the hcl colour space.

Here is some code to allocate RGB colors evenly around a HSL color wheel of specified luminosity.

Isn't it also a factor which order you set up the colors?

Like if you use Dillie-Os idea you need to mix the colors as much as possible. 0 64 128 256 is from one to the next. but 0 256 64 128 in a wheel would be more "apart"

Does this make sense?

I've read somewhere the human eye can't distinguish between less than 4 values apart. so This is something to keep in mind. The following algorithm does not compensate for this.

I'm not sure this is exactly what you want, but this is one way to randomly generate non-repeating color values:

(beware, inconsistent pseudo-code ahead)

One way you could optimize this for better visibility would be to compare the distance between each new color and all the colors in the array:

But this approach would significantly slow down your algorithm.

Another way would be to scrap the randomness and systematically go through every 4 values and add a color to an array in the above example.

I know this an old post but I found it while looking for a PHP solution to the topic and finally came with a simple solution:

So just call the random_color() function where $i identifies the color, $n the number of possible colors, $sat the saturation and $br the brightness.

To achieve "most distinguishable" we need to use a perceptual color space like Lab (or any other perceptually linear color space) and not RGB. Also, we can quantize this space to reduce the size of the space.

Generate the full 3D space with all possible quantized entries and run the K-means algorithm with

`k=N`

. The resulting centers/ "means" should be approximately most distinguishabl from each other.