Skip to content

Instantly share code, notes, and snippets.

@darkmavis1980
Last active April 4, 2018 08:48
Show Gist options
  • Save darkmavis1980/d705aa2886d6c692e36639e4ab177c96 to your computer and use it in GitHub Desktop.
Save darkmavis1980/d705aa2886d6c692e36639e4ab177c96 to your computer and use it in GitHub Desktop.
HexToRGB in Javascript
// Converts any given HEX color to the relative RGB value
const HexToRGB = hex => hex.match(hex.length === 6 ? /.{2}/g : /.{1}/g).map(color => {
color = hex.length === 3 ? color + color : color;
return parseInt(color, 16);
});
console.log(HexToRGB('FFA955')); //[ 255, 169, 85 ]
console.log(HexToRGB('000000')); //[ 0, 0, 0 ]
console.log(HexToRGB('F00')); //[ 255, 0, 0 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment