Update and rename hexadecimal-to-RGB.md to hexcode-to-RGB.md

This commit is contained in:
David Wu
2017-12-15 13:27:43 +01:00
committed by GitHub
parent d3e070a906
commit 7f9a090537
2 changed files with 10 additions and 14 deletions

View File

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

View File

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