js convert code to hex code example
Example 1: javascript rgb to hex
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hexToRgb(hex) {
var result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex);
if(result){
var r= parseInt(result[1], 16);
var g= parseInt(result[2], 16);
var b= parseInt(result[3], 16);
return r+","+g+","+b;
}
return null;
}
console.log(rgbToHex(10, 54, 120));
console.log(hexToRgb("#0a3678"));
Example 2: js number to hex
number = 255
h = parseInt(number, 10).toString(16)
h = h.padStart(6, "0")