diff --git a/snippets/hexidecimal-to-RGB.md b/snippets/hexidecimal-to-RGB.md new file mode 100644 index 000000000..73ee1d18d --- /dev/null +++ b/snippets/hexidecimal-to-RGB.md @@ -0,0 +1,14 @@ +### Hexadecimal to RGB + +Converts hexadecimal colorcodes to RGB colorcodes by matching the hexidecimal (two-digit) red, green and blue values and +mapping their decimal equivalents to an array [r, g, b]. Works also with shorthand hex triplets such as "#05F". + +```js +const hexToRgb = hex => + hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i + ,(m, r, g, b) => '#' + r + r + g + g + b + b) + .substring(1).match(/.{2}/g) + .map(x => parseInt(x, 16)) +// hexToRgb('#27ae60') -> '[39, 174, 96]' +// hexToRgb('#aaa') -> '[170, 170, 170]' +```