I\'m trying to graphically display a graph of N lines and I\'m trying to find a way to dynamically assign distinct colors based on how many lines I have. The values in RGB rang
I wrote this code once to solve exactly the problem you describe (the background was also white). It's the same idea as yours, only generalized. It should be easy to adapt from OCaml to your language.
Usage
You don't need to tell the function how many colors you will need. Call the function with 1, 2, ... to get color number 1, number 2, ... The colors from small numbers are wide apart, the latter colors of course become closer and closer to each other.
Code
lsl
is "logical shift left", lsr
"logical shift right", and !
simply means "access a reference". In OCaml RGB colors are represented in a single integer, with one byte per color.
let number_to_color n = let color = ref 0 in let number = ref n in for i = 0 to 7 do color := (!color lsl 1) + (if !number land 1 <> 0 then 1 else 0) + (if !number land 2 <> 0 then 256 else 0) + (if !number land 4 <> 0 then 65536 else 0); number := !number lsr 3 done; !color