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
}).join('')
}

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

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript