Michael Schwartz Michael Schwartz - 5 months ago 9x
Javascript Question

Convert Hex to RGBA

My fiddle - http://jsbin.com/pitu/1/edit

I wanted to try an easy hex to rgba conversion. Ever browser I've used renders colors using rgb as default so when using the farbtastic color picker I'm converting the hex value to rgb by grabbing the background color the hex value generates (as rgb by default = simple conversion)

I tried replacing the

symbol to
, 1)
, but that didn't work so I went to just see how converting rgb to rgba would work, and I'm still having trouble.

The jquery


The goal

enter image description here

//If you write your own code, remember hex color shortcuts (eg., #fff, #000)

function hexToRgbA(hex){
    var c;
        c= hex.substring(1).split('');
        if(c.length== 3){
            c= [c[0], c[0], c[1], c[1], c[2], c[2]];
        c= '0x'+c.join('');
        return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
    throw new Error('Bad Hex');


/*  returned value: (String)