Take the 2-minute tour ×

I need to represent a series of elements through time. For designing purposes each of them should have a color to make it simplier to identify each one at a glance. The point is that color each elemnt would make no sense if the difference in color between them is not appreciable by humans.

The only fixed attribute the elements have is their ID. This ID is supossed to differ by one from one element to another, but this is not certain.

What I look for is a hash function that based on a numeric ID returns an HEX color (web format e.g. #f62035) that differs a lot with the previous ID and the next ID. Also, the HEX color must not be too dark since the background where the elements are displayed is black.


Element ID: 12  
Element ID: 13

hash(12) = #f46600  
hash(13) = #5aa9aa
share|improve this question
You might be able to tweak this solution: stackoverflow.com/questions/4246351/… –  Jon Lin Feb 13 '12 at 9:30
@JonLin that is my last option, since colors can not be saved to a DB and it should be the same color in different executions, but thanks ;) –  eversor Feb 13 '12 at 9:33
@eversor you can avoid problems like that by using a known seed (say 12345L) when you construct the Random. Then if you perform (exactly) the same operations in the same order, you'll get the same results. –  Sean Reilly Feb 13 '12 at 9:37
As a common problem of hashCode() implementations is too less diversity, the HashMap implementation takes extra care to mangle the result so that the hash codes differ a lot. You can look at the source of HashMap#hash(int) and use this idea for your purpose. –  Philipp Wendler Feb 13 '12 at 9:38

1 Answer 1

up vote 2 down vote accepted

IMHO, and depeding how many different colors you are going to need, I think your best solution would be to build a list of colors to use, with say 100 different variations of color, and sort them in the order you want to use them, then you can use these as your colors by assigningn them from this list in order, when you get to the end of the list, you can start again.

This solution would not be scalable, but you have to keep in mind that there are only very few colors that humans can tell they are different and suit your design.

To create this list, maybe you can write a different java application that swhows a color picker, so you can choose colors and the code creates the list for you

share|improve this answer
You can turn this into the exact solution. Build a list of colours (either once manually, or pseudo-randomly as above), and use (traditional hash code) mod (the length of the list) to choose a colour from the list. –  Sean Reilly Feb 13 '12 at 10:34

Your Answer


