My JavaScript solution, which would look a lot nicer if the TC39 finally added a pipeline operator.
const checkRange = num => Math.min(255, Math.max(num, 0)) const rgb = (...args) => args.slice(0, 3) .map(x => checkRange(x) .toString(16) .toUpperCase() .padStart(2, '0')) .join('')
We have padStart now in strings to add characters to the beginning up to a certain length.
padStart
String.prototype.padStart would handle the addZeroes trick for you. ;)
addZeroes
I got really confused when the link opened in Portuguese. I had to check my VPN 😆
Whoops! Sorry about that. :P Should be fixed now.
I've modified it to use padStart instead of my custom function.
Thanks @lffg and @avalander !
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My JavaScript solution, which would look a lot nicer if the TC39 finally added a pipeline operator.
We have
padStart
now in strings to add characters to the beginning up to a certain length.String.prototype.padStart would handle the
addZeroes
trick for you. ;)I got really confused when the link opened in Portuguese. I had to check my VPN 😆
Whoops! Sorry about that. :P Should be fixed now.
I've modified it to use
padStart
instead of my custom function.Thanks @lffg and @avalander !