Last active
January 2, 2016 03:29
-
-
Save rlemon/8244125 to your computer and use it in GitHub Desktop.
colorConverter function to convert hex to rgb
This file contains hidden or 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
/* colorConverter | |
* takes hex input (3 or 6 char) | |
* and return the RGB value as an | |
* object. | |
* Does not account for shitty inputs | |
* #f000 will result in some funky RGB | |
* | |
* Note: '#' in the hex string is optional | |
*/ | |
var colorConverter = function (h) { | |
h = h.replace(/#/, ''); | |
if (h.length === 3) h = h.replace(/./g, '$&$&'); | |
h = parseInt(h, 16); | |
return { | |
r: h >> 16, | |
g: h >> 8 & 0xff, | |
b: h & 0xff | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment