0

I'm using the Chroma.js library to generate a color scale.

So far I'm using this to generate 25 'rainbow' colors:

var scale = chroma.scale(['red','orange','yellow','green','blue','purple']).colors(25);

But when I log each color:

for (var i = 0; i < scale.length; i++) {
   console.log(scale[i]);
}

The color is a HEX color like #ff0000, how do I get it to be RGB?

NewToJS
  • 2,011
  • 4
  • 35
  • 62

2 Answers2

1

Just recall chroma on the returned value:

for (var i = 0; i < scale.length; i++) {
   console.log(chroma(scale[i]).rgb());
}
meo
  • 30,872
  • 17
  • 87
  • 123
-2

Here is a function for converting a hex color to a rgb color:

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

alert( hexToRgb("#0033ff").g ); // "51";

This is taken from Tim Down's answer here, where you can also find a lot more about hex to rgb color conversion:

RGB to Hex and Hex to RGB

Nick Lewis
  • 11
  • 2