javascripthistogram

Create unique colors using javascript


What is the best way to pick random colors for a bar chart / histogram such that each color is different from the other.. and possibly in contrast

The most talked about way is

'#'+(Math.random()*0xFFFFFF<<0).toString(16);

but this can generate similar colors.. and sometimes distinguishing them might be a problem.. Example enter image description here


Solution

  • The best way is to convert from HSV values. You can divide the maximum value of "Hue" by the amount of colors you need and then increment by this result.

    For improved contrast, you can also alternate between high and low values of lightness.