From 11db72f3549466272c7b1fa972bb854a3a4a135c Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 00:48:15 +0100 Subject: [PATCH 1/4] Create hexidecimal-to-RGB.md --- snippets/hexidecimal-to-RGB.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/hexidecimal-to-RGB.md 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]' +``` From 79bfdba046feb5ec2dcb18ca806245672037b088 Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 01:00:44 +0100 Subject: [PATCH 2/4] Rename hexidecimal-to-RGB.md to hexadecimal-to-RGB.md --- snippets/{hexidecimal-to-RGB.md => hexadecimal-to-RGB.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{hexidecimal-to-RGB.md => hexadecimal-to-RGB.md} (100%) diff --git a/snippets/hexidecimal-to-RGB.md b/snippets/hexadecimal-to-RGB.md similarity index 100% rename from snippets/hexidecimal-to-RGB.md rename to snippets/hexadecimal-to-RGB.md From f7b8d5e78d64f746b892ca3bd0c9b6241681bba8 Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 13:27:43 +0100 Subject: [PATCH 3/4] 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]' +``` From 2d7d45fe05f1293c0c87b3249c3a2a789c9fec09 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 14:36:23 +0200 Subject: [PATCH 4/4] Update hexcode-to-RGB.md --- snippets/hexcode-to-RGB.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/hexcode-to-RGB.md b/snippets/hexcode-to-RGB.md index 9ca5c7447..852a25ff8 100644 --- a/snippets/hexcode-to-RGB.md +++ b/snippets/hexcode-to-RGB.md @@ -1,10 +1,8 @@ ### 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)`. +Use `Array.slice()`, `Array.map()` and `match()` to convert a hexadecimal colorcode (prefixed with `#`) to a string with the RGB values. ```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]' ```