Update RGB to Hex, build README

This commit is contained in:
Angelos Chalaris
2017-12-12 20:54:57 +02:00
parent 262a108c43
commit 609bc3cdd4
2 changed files with 5 additions and 21 deletions

View File

@ -395,13 +395,11 @@ const reverseString = str => [...str].reverse().join('');
### RGB to hexadecimal
Convert each value to a hexadecimal string, using `toString(16)`, then `padStart(2,'0')` to get a 2-digit hexadecimal value.
Combine values using `join('')`.
Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `toString(16)`, then `padStart(6,'0')` to get a 6-digit hexadecimal value.
```js
const rgbToHex = (r, g, b) =>
[r,g,b].map( v => v.toString(16).padStart(2,'0')).join('');
// rgbToHex(0, 127, 255) -> '007fff'
const rgbToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
// rgbToHex(255, 165, 1) -> 'ffa501'
```
### Scroll to top