From f7b8d5e78d64f746b892ca3bd0c9b6241681bba8 Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 13:27:43 +0100 Subject: [PATCH] Update and rename hexadecimal-to-RGB.md to hexcode-to-RGB.md --- snippets/hexadecimal-to-RGB.md | 14 -------------- snippets/hexcode-to-RGB.md | 10 ++++++++++ 2 files changed, 10 insertions(+), 14 deletions(-) delete mode 100644 snippets/hexadecimal-to-RGB.md create mode 100644 snippets/hexcode-to-RGB.md diff --git a/snippets/hexadecimal-to-RGB.md b/snippets/hexadecimal-to-RGB.md deleted file mode 100644 index 73ee1d18d..000000000 --- a/snippets/hexadecimal-to-RGB.md +++ /dev/null @@ -1,14 +0,0 @@ -### 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]' -``` diff --git a/snippets/hexcode-to-RGB.md b/snippets/hexcode-to-RGB.md new file mode 100644 index 000000000..9ca5c7447 --- /dev/null +++ b/snippets/hexcode-to-RGB.md @@ -0,0 +1,10 @@ +### Hexcode 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]. Returns a string `rgb(r,g,b)`. + +```js +const hexToRgb = hex => `rgb(${hex.slice(1).match(/.{2}/g).map(x => parseInt(x, 16)).join()})` +// hexToRgb('#27ae60') -> 'rgb(39,174,96)' +// hexToRgb('#aaa') -> '[170, 170, 170]' +```