mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Create Conversions/RGBToHex.js
This commit is contained in:
16
Conversions/RGBToHex.js
Normal file
16
Conversions/RGBToHex.js
Normal file
@ -0,0 +1,16 @@
|
||||
function RGBToHex (r, g, b) {
|
||||
if (
|
||||
typeof r !== 'number' ||
|
||||
typeof g !== 'number' ||
|
||||
typeof b !== 'number'
|
||||
) {
|
||||
throw new TypeError('argument is not a Number')
|
||||
}
|
||||
|
||||
const toHex = n => (n || '0').toString(16).padStart(2, '0')
|
||||
|
||||
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
|
||||
}
|
||||
|
||||
console.log(RGBToHex(255, 255, 255) === '#ffffff')
|
||||
console.log(RGBToHex(255, 99, 71) === '#ff6347')
|
Reference in New Issue
Block a user