How can I get a list of visually distinct colors?
Asked Answered
P

2

8

I need to assign colours to distinguish between different items. Simple case: order rows of different color according to customer.

I don't want to have the user choose a colour for every customer, I want to do this at runtime.

If there is a single customer I will use "red", if there are two "red" and "white", ...

Of course I can create my GimmeRandomColour(i: index) own function that uses i and the RGB function to create good random colours (but in this case creating nice colours can be tricky). Or I can say if i is 0 give me clRed, ... (in this way creating many colours can be a problem).

How can I get a "good list of colours" with a acceptable levels of contrast?

Pipit answered 24/3, 2011 at 10:52 Comment(2)
Not out of the box. That's way too specific to supply a built-in function for.External
Create your own palette and use random sampling without replacement.Trinidadtrinitarian
D
7

No in-built function in Delphi. See this question for ways to generate pleasing colour schemes.

Algorithm to randomly generate an aesthetically-pleasing color palette

Digit answered 24/3, 2011 at 11:23 Comment(3)
Nice link but as I wrote at the end of my question "random" is not correct. I want always the same colour for a given value of i. SOmething like case i of 0 Result := clRed; 1 Result := clBlue; 2 ... but i want something that makes sense also for i = 100, even if realistically I will use it for i < 50. Of course I can harcode 50 colours so I am sure it will work ok.Pipit
@user193655 To always get the same value for a given I, for every different I use the random suggested algorithm to generate a new color and bind that color with that particular I, so you can get the same color with subsequent getColorFor(I) calls. If you want to get the same color for different program runs, store the I=ColorValue binding in a database, the registry, a Ini or config file.Dunston
@user193655 - the (currently) 6th answer on that question gives a link to an article on generating colors using the 'golden angle'. martin.ankerl.com/2009/12/09/…Digit
L
2

A common approach is to use the values $00, $33, $66, $99, $CC and $FF (Random(6) * $33) for each RGB color component.

That will result in 216 different colors that are safe to use.

See examples on wikipedia.

Levon answered 24/3, 2011 at 11:13 Comment(2)
You mean "for each RGB component".Ultraconservative
@Andreas, I originally meant "for each red, green and blue color", but shortened it without seen the new meaning :) Component is nonetheless a more precise term.Columelliform

© 2022 - 2024 — McMap. All rights reserved.