Skip to content

Instantly share code, notes, and snippets.

@bendc
Created May 11, 2015 20:16
Show Gist options
  • Save bendc/ecd2a7e684677c308549 to your computer and use it in GitHub Desktop.
Save bendc/ecd2a7e684677c308549 to your computer and use it in GitHub Desktop.
Hexadecimal to RGB converter
const toRGB = (() => {
const expand = hex =>
hex.length < 7 ? hex.split("").reduce((a, b) => a + b + b) : hex;
const convert = hex =>
hex.match(/[\d\w]{2}/g).map(val => parseInt(val, 16));
return hex => {
const [r, g, b] = convert(expand(hex));
return `rgb(${r}, ${g}, ${b})`;
};
})();
@bendc
Copy link
Author

bendc commented May 14, 2015

Example:

toRGB("#0a5"); // => "rgb(0, 170, 85)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment