rgb to hex conversion in JavaScript

A handy function to convert rgb colors to hex in JavaScript
parameters: red, blue, green values.
returns a hex string which is 7 characters long including the hash

function toHex(r, b, g) {
return "#" + [r, b, g].map(param => {
var hex = Number(param).toString(16)
return (hex.length < 2) ? '0' + hex : hex

For instance  toHex(0, 0, 0)
// returns #000000


Popular posts from this blog

How to create zip archives in nodejs

Open-graph meta data tags introdution

What is 'this.' keyword in JavaScript

How to get user's ip address in JavaScript

How to build a twitter retweet bot using JavaScript