1

All,

Are there any nice algorithms out there to generate a unique colour based on index in an array?

This is of course going to be used in a UI, to set the background colour of a number of dynamic buttons.

Now with .Net (and Java off top of my head), the following methods are supported:

Color.FromArgb
Color.FromName

FromArgb can take an 32-bit integer containing the argb color.

However, the algorithmic approach might cause some colours to be too similar in order, depending upon how many items were in the array. And also, where the foreground colour is similar to the background.

The only way I can think of is to create some kind of Color array, with a set of predefined colours in. Off course, this is manual code effort, but this way you can get a different set of colours in a small range that can be visually different from each other, before repeating sequence towards the end.

The other way could be to use the following to generate the array of colours:

Enum.GetValues(typeof(KnownColor)

Any suggestions?

Cheers

Andez
  • 5,588
  • 20
  • 75
  • 116
  • [This post](http://stackoverflow.com/questions/3632843/how-to-generate-a-set-of-random-colors-where-no-two-colors-are-almost-similar) could give you ideas. – assylias Aug 01 '12 at 15:09
  • Also see [this post](http://stackoverflow.com/questions/7656179/generate-visually-different-colors-with-an-unknown-color-collection-size/7731511) – Lior Kogan Aug 01 '12 at 18:28

2 Answers2

1

Hash the index, and take the lower 32 bits of the hash for your color. This will appear random but should produce a uniform distribution of colors. Will not guarantee that the chosen colors will be visually different from each other or the background, but may serve.

You could also take the whole color spectrum, cut it into n evenly intervaled colors, and assign them to each element of the array, assuming that you know the size of the array.

https://stackoverflow.com/a/43235/684934 might also give good ideas.

Community
  • 1
  • 1
  • I've used the hashing idea in several projects I've worked on. It's great when you have a lot of things so you know there will be many visually similar colors no matter what you do. – Running Wild Aug 01 '12 at 19:41
0

RGB-colors form a 3D-cube of color-space. Begin by selecting the corners of this cube (0 or 255 values). Then subdivide the cube into a grid of 8 cubes, and take the newly formed vertices. Subdivide again, into 64 cubes, and take the newly formed vertices. This will give you progressively closer and closer colors for higher indices.

IEnumerable<Color> GeneratePalette()
{
    for (int scale = 1; scale < 256; scale *= 2)
    {
        for (int r = 0; r <= scale; r++)
        for (int g = 0; g <= scale; g++)
        for (int b = 0; b <= scale; b++)
        {
            if (scale == 1 || (r & 1) == 1 || (g & 1) == 1 || (b & 1) == 1)
            {
                yield return new Color
                {
                    A = 255,
                    R = (byte) (255 * r / scale),
                    G = (byte) (255 * g / scale),
                    B = (byte) (255 * b / scale),
                };
            }
        }
    }
}

The first few colors:

#FF000000 
#FF0000FF 
#FF00FF00 
#FF00FFFF 
#FFFF0000 
#FFFF00FF 
#FFFFFF00 
#FFFFFFFF 
#FF00007F 
#FF007F00 
#FF007F7F 
#FF007FFF 
...
#FFFF7FFF 
#FFFFFF7F 
#FF00003F 
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138