views:

659

answers:

5

I want to draw some data into a texture: many items in a row. They aren't created in order, and they may all be different sizes (think of a memory heap). Each data item is a small rectangle and I want to be able to distinguish them apart, so I'd like each of them to have a unique colour.

Now I could just use rand() to generate RGB values and hope they are all different, but I suspect I won't get good distribution in RGB space. Is there a better way than this? E.g. what is a good way of cycling through different colours before they (nearly) repeat?

The colours don't have to match with any data in the items. I just want to be able to look at many values and see that they are different, as they are adjacent.

I could figure something out but I think this is an interesting question. :)

+9  A: 

Using the RGB color model is not a good way to get a good color mix. It's better to use another color model to generate your color, and then convert from that color model to RGB.

I suggest you the HSV or HSL color model instead, in particular you want to vary the Hue.

If you want X different color values, vary them from 0 to 360 with a step size of 360 divided by X.

Brian R. Bondy
A: 

Whats your sample space... how many items are we talking.

You could build up an array of RGB Triples from

for(int r = 0; r < 255; r = r+16)
   for(int g = 0; g < 255; g = g+16)
      for(int b = 0; b < 255; b = b+16)
           // take r, g, b and add it to a list

Then randomise your list and iterate through it. that'd give you 16^3 (4096) different colors before a repeated color.

Eoin Campbell
+2  A: 

In general RGB isn't a great color space for doing these sorts of things because it's perceptually nonlinear, for starters. This means that equal distances moved between RGB triplets do not look equally different to our eyes.

I'd probably work in the L*c*h* space (see also) space, or HSL space, and just generate a uniform spacing in hue. These spaces have been designed to be approximately perceptually linear.

simon
A: 

This is very similar to the four-colour problem relating to colouring maps, this might yield some interesting solutions for you:

Four colour theorem

Lazarus
It really isn't at all that similar to the 4 colour problem. Nick is using as many colors as needed, whereas the coloring problems are trying to minimize how many you need....
simon
Actually it has nothing to do with the 4 color problem, in where only 4 colors are needed and they are only used as "tags". Here he is concerned with the optical qualities (sameness) of the selected colors.
foljs
I disagree, I think there is merit in the 4 colour problem here. The problem is distinguishing items arranged in a 2D plane through the use of colour. The 4 colour problem proves that you *can* solve the problem using just 4 contrasting colours.
Mark Pim
Nick's problem revolves around having a palette of colours sufficient to represent the elements he is displaying without having close contrast colours adjacent to one another. I wasn't proposing that he limit himself to 4 colours just that the theory around representing 'tiles' and how many colours are need to ensure clear contrast was relevant and could lead to some interesting solutions. Each to their own.
Lazarus
I did think about the 4 colour problem, but I don't create the rectangles in a known order. I suppose I could reassign colours and just alternate them as they are in a line: 2 colour problem! But I think reassigning colours is confusing to the user. It makes you think the existing data has changed, when it hasn't.
Nick
So is this a representation of a heap-like structure, just 1 dimensional... a line? I think I see where you are coming from on that basis and Simon/foljs are probably correct as I was thinking that this was a 2d problem. I guess on that basis the question is what is the relationship between the blocks, what's it representing? If there are instances of a fixed set of structures on this 'heap' then a colour for each and a grey (perhaps) border to allow separation. Effectively... "Tell us more!" :)
Lazarus
Yes it is 1d, and it is to visualise a heap. I may, however, draw the heap in 2d, "folding into rows" so you can see more detail. It's useful to view the data in different ways as every way gives you a different perspective.
Nick
Lazarus; the point above I was trying to make was that the 4-color problem is hard because you have to reuse colors. Nick probably has effectively as many as he wants, just needs a way to generate distiguishable colors. Very different problem (not completely unrelated, but very different).
simon
Simon, yes, agree completely.
Lazarus
A: 

Google "delta e cie 2000"; the colour-difference formula is useful for determining apparent (visual) distance between 2 colours. (On a monitor; there's a different formula for pigments.) It operates on colours in Lab space (props to simon), but applies a perceptual calculation of difference.

We found that a number around 1.5 was sufficient to ensure visually distinct colours (i.e. you can tell the difference if they are near one another), but if you want identifiable colours (you can find any colour in a legend) you'll need to bump that up.

As to creating a set of colours... I'd probably start at some corner of Lab space, and walk around it using a step size that gives large enough visual differences (note: it's not linear, so step size will probably have to be adaptive) and then randomize the list.