2

I'm setting up a simple web chart using chart.js. Given a hexadecimal color value like C8C8C8, I want to get rgba(200, 200, 200, x). The x is passed as a second variable. My code looks like this:

function colorconvert(color, transparency) {
        var r = parseInt(color.substring(0,2),16);
        var g = parseInt(color.substring(2,4),16);
        var b = parseInt(color.substring(4,6),16);
        var a = parseInt(transparency);
        return ('rgba(r, g, b, a)');
}

But Chrome dev console logs the error:

Uncaught Error: Unable to parse color from string "rgba(r, g, b, a)"

What am I doing wrong? Any help is appreciated!

LukeLR
  • 1,129
  • 1
  • 14
  • 27

1 Answers1

3

return ('rgba(r, g, b, a)'); will return text

'rgba(r, g, b, a)'

You should return something like

return ('rgba(' + r ', ' + g + ', ' + b + ', ' + a + ')';
Daniel
  • 611
  • 5
  • 20
  • 1
    There were some parenthese and punctuation mistakes, but this worked: return ('rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')'); Thanks a lot! – LukeLR Jan 18 '17 at 00:33