Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Last active March 10, 2020 16:04
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 nblackburn/c1164e08851c3fec45a6b19d4c1d7f49 to your computer and use it in GitHub Desktop.
Save nblackburn/c1164e08851c3fec45a6b19d4c1d7f49 to your computer and use it in GitHub Desktop.
module.exports = hex => {
if (hex.charAt(0) === '#') {
hex = hex.substring(1);
}
if (hex.length !== 3 && hex.length !== 6) {
return false;
}
if (hex.length === 3) {
hex = hex.split('').map(c => c.repeat(2)).join('');
}
let red = parseInt(hex.substring(0, 2), 16);
let blue = parseInt(hex.substring(4, 6), 16);
let green = parseInt(hex.substring(2, 4), 16);
return { red, green, blue };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment