I attempted to change the rgba
color representation to hexadecimal, but I encountered difficulty in converting the opacity value while successfully converting the remaining colors.
Here is the snippet of my code:
var colorcode = "rgba(0, 0, 0, 0.74)";
var finalCode = rgba2hex(colorcode)
function rgba2hex(orig) {
var a, isPercent,
rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i),
alpha = (rgb && rgb[4] || "").trim(),
hex = rgb ?
(rgb[1] | 1 << 8).toString(16).slice(1) +
(rgb[2] | 1 << 8).toString(16).slice(1) +
(rgb[3] | 1 << 8).toString(16).slice(1) : orig;
if (alpha !== "") { a = alpha; }
else { a = 01; }
hex = hex + a;
return hex;
}
console.log(finalCode)
My objective is to also convert the alpha value to hex code. If you have any suggestions on how to achieve this, please let me know.
Desired Output
Expected: 000000bd