Update RGB to Hex, build README
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user