Create list of unique colors

I’m trying to create a list of colors that are unique or easily identifiable as being different when shown as the background colour of a cell in a listbox.

Currently I’m dividing the hue by the number of colors required as follows:

    Dim ColDiv As Double = 1 / NumColors
    for i = 1 to NumColors
      Dim HueValue As Double = ColDiv * i
      ColorList.Append HSV(HueValue, SatValue, ColValue)

But when for example I create a list of 8 colors I get two very similar looking green colors. I need to be able to create a range of colors that can be in the 100s, but when there are less than say 10 colors I’d like them to be easily identifiable.

Any ideas?



What about using a predefined color look-up table instead of calculating the colors, or do you need to generate it on the fly?
Tables can be created for any amount of colors and using a color scheme fitting to your application.

Dim ColorList() As Color
  ColorList = Array(&cFF0000, &c00FF00, &c0000FF, &cFFFF00, &cFF00FF, &c00FFFF, &c000000, &c800000, &c008000, &c000080, &c808000, &c800080, &c008080, &c808080, &cC00000, _
  &c00C000, &c0000C0, &cC0C000, &cC000C0, &c00C0C0, &cC0C0C0, &c400000, &c004000, &c000040, &c404000, &c400040, &c004040, &c404040, _
  &c200000, &c002000, &c000020, &c202000, &c200020, &c002020, &c202020, &c600000, &c006000, &c000060, &c606000, &c600060, &c006060, &c606060, _
  &cA00000, &c00A000, &c0000A0, &cA0A000, &cA000A0, &c00A0A0, &cA0A0A0, &cE00000, &c00E000, &c0000E0, &cE0E000, &cE000E0, &c00E0E0, &cE0E0E0)

The above color table looks as follow:

Not the most exciting colors, but should get the job done. Should be trivial to change the values to your liking.

here’s a nice page with several ideas…

Nice article Doofus. Very informative and certainly much more exciting colors than the example above.

An interesting color technology I stumbled upon recently is ChromaDepth. The basic idea is that 3D ChromaDepth glasses gives depth to color. I’m still learning myself how to compose nice 3D scenes with ChromaDepth, but it’s a lot of fun.

ChromaDepth Basic Explanation

Many thanks Alwyn and doofus. Your article doofus is exactly what I was after.


Reading the article above I see the code I need:

[code]offset = Random.NextFloat();

for (int i = 0; i < n; i++)
color[i] = gradient.GetColor(offset + (0.618033988749895f * i) % 1);[/code]

I take it this is coded in c++. I kind of get what it’s doing but not sure on what i++ and % do. Can anyone provide a Xojo version of this?

Many thanks,


i++ is the ‘post increment’ operator essentially it gives the value of i and then increments it by 1. In Xojo this is a for…next loop with a (default) step of 1

% is the modulo operator, it gives the remainder of the division - Xojo has a Mod function.

Thanks Carl

This works pretty well for me (based on above)

[code] Dim r As New Random

Dim d As Double = r.InRange(0, 1000) * .001

Dim hueValue, fudgeValue As Double

fudgeValue = 0.618033988749895

Dim MyColors() As Color

For i As Integer = 0 to 19

hueValue = (d + (fudgeValue * i)) - ((d + (fudgeValue * i)) \\ 1)

MyColors.Append hsv(hueValue, .35, 1.0)


Here is a printout of the colors generated by the algorithm above…

For some reason the image doesn’t show… can view it at

Looks like it is good for around 12 distinct colours then they repeat with slight hue variations, you may squeeze out a few more by using the golden ratio / 2

and / or

Randomising the Saturation & Value parts along the lines of:

colour = HSV(hueValue, (r.InRange(300, 1000) / 1000), (r.InRange(300, 1000) / 1000))

you need to keep the values away from 0.0 unless you want white (saturation = 0.0) or black (value = 0.0)

Thanks or this thread guys - it’s really helping me to solve a tricky problem - Much appreciated, but can’t add much myself.

I came up with the following function, works pretty well:

[code]Protected Function ColorSpectrum(Size As Integer = 1, Saturation as Double = 1.0, Value as Double = 1.0, Alpha as Integer = 0) As Color()
DIM cArray() AS Color

IF Size < 1 OR Saturation < 0 OR Saturation > 1 OR Value < 0 OR Value > 1 OR Alpha < 0 OR Alpha > 255 THEN
Return cArray

FOR i AS Integer = 1 TO Size
cArray.Append HSV((1/size)*i, Saturation, Value, Alpha)

Return cArray

End Function