// Convert RGB to Hex
function getColorFun(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
getColorFun(178, 232, 55);
// The output here is #b2e837
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function hexToRgb(hex, result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex)) {
return result ? result.map(i => parseInt(i, 16)).slice(1) : null
//returns [23, 14, 45] -> reformat if needed
}
console.log(rgbToHex(10, 54, 120)); // #0a3678
console.log(hexToRgb("#0a3678")); // [10, 54, 120]
function hexToRgb(hex){
var result = /^#?([a-fd]{2}])([a-fd]{2})([a-fd]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16);
g: parseInt(result[2], 16);
b: parseInt(result[3], 16);
} : null;
}
var hex = "#0a3678";
console.log(hexToRgb(hex).r+","+hexToRgb(hex).g+","+hexToRgb(hex).b);//10,54,120