views:

97

answers:

2

Got a logfile, and it has all kinds of text in it. Currently it is just displayed as one colour, and each entry says something like:

Log from section 1: Some text here 
Log from section 125: Some text here 
Log from section 17: Some text here 
Log from section 1: Some text here 
Log from section 125: Some text here 
Log from section 1: Some text here 
Log from section 17: Some text here

Now the logfile is displayed in real time, and it would be nice to make the rows with the same section number the same colour. However there could be potentially quite a large range of numbers.

What I want to do is create a method that will take a number, and randomly generate a unique colour. The colour must be readable against a black background though, so #000000 is no good, nor is #101010 or anything too dark to read.

Ideally two similar numbers will not produce the same colour because in the above examples, the numbers 1 and 17 might be too similar, and some numbers might be in the 10,000 range.

Any ideas on this?

+1  A: 

Yes. Just generate random numbers for a color in HSV color space and make sure V is above like 50% (so that it isn't too dark). Convert back to RGB as necessary. Pretty simple.

To make sure the colors aren't too similar... I don't know. Find out the range of possible numbers and split the color wheel evenly?

Mark
This sounds great. Regarding having different colours, I was thinking about GUIDs and the fact that abcde and abcdf result in radically different guids, if the same were possible somehow using the random generator.
SLC
Er... I don't know, you could use some kind of hash/GUID and convert it to an integer if you really wanted to, but you're mapping those numbers to such a small range anyway that they very well could be side-by-side anyway. I came up with a stupid complicated algo that mapped English words to different colors awhile back that took into consideration the probabilities that a word would start with a particular letter..... and then never ended up using the damn thing. It did produce a pretty, evenly distributed rainbow for dictionary-like sequences though :D Anyway, don't sweat it. Some lines will
Mark
have similar colors...and people will have to look at the numbers to tell them apart. They're no worse off than they are now ;)
Mark
Thanks for your answers!
SLC
+1  A: 

There are some fairly strict cognitive limits on how many different colors you can use. It would be better to carefully pre-select a range of colors. If you really need to differentiate more than a dozen or so distinct items, you'll need to use something else, like altering the background color as well.

Also, remember that 10% of the male (and therefore developer) population is color-blind, so unless it's just for your own benefit, I wouldn't rely too heavily on color.

A very interesting alternative technique can be found in TortoiseSVN's blame tool, which dynamically highlights lines with matching revisions as the mouse moves over the window.

Marcelo Cantos