Merge pull request #204 from kutsan/hex-to-rgb

Update Hexcode to RGB function with bitwise version
This commit is contained in:
Angelos Chalaris
2017-12-17 12:09:07 +02:00
committed by GitHub

View File

@ -1,8 +1,11 @@
### Hexcode to RGB ### Hexcode to RGB
Use `Array.slice()`, `Array.map()` and `match()` to convert a hexadecimal colorcode (prefixed with `#`) to a string with the RGB values. Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (prefixed with `#`) to a string with the RGB values.
```js ```js
const hexToRgb = hex => `rgb(${hex.slice(1).match(/.{2}/g).map(x => parseInt(x, 16)).join()})` const hexToRgb = hex => {
// hexToRgb('#27ae60') -> 'rgb(39,174,96)' const h = parseInt(hex.slice(1), 16);
return `rgb(${h >> 16}, ${(h & 0x00ff00) >> 8}, ${h & 0x0000ff})`;
}
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
``` ```