Create hexidecimal-to-RGB.md

This commit is contained in:
David Wu
2017-12-15 00:48:15 +01:00
committed by GitHub
parent c286d55182
commit 11db72f354

View File

@ -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]'
```