Created
January 4, 2017 19:11
-
-
Save slamkajs/e617cabdc43fc687ffb3d439bfc0e06e to your computer and use it in GitHub Desktop.
String function that converts a hex value (#FFFFFF) into an rgba value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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