Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active January 2, 2016 03:29
Show Gist options
  • Save rlemon/8244125 to your computer and use it in GitHub Desktop.
Save rlemon/8244125 to your computer and use it in GitHub Desktop.
colorConverter function to convert hex to rgb
/* 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