Skip to content

Instantly share code, notes, and snippets.

@slamkajs
Created January 4, 2017 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slamkajs/e617cabdc43fc687ffb3d439bfc0e06e to your computer and use it in GitHub Desktop.
Save slamkajs/e617cabdc43fc687ffb3d439bfc0e06e to your computer and use it in GitHub Desktop.
String function that converts a hex value (#FFFFFF) into an rgba value.
/**
* HEX TO RGBA
* Convert a hex string for colors into an rbga value
*
* @param alpha (float) THe opacity value of the color
**/
String.prototype.hexToRgbA = function(alpha) {
var c;
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(this)){
c= this.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(',')+','+(alpha ? alpha : 1)+')';
}
throw new Error('Bad Hex');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment