roblema de mudança de JavaScript (rgb e rgba para he
Encontrei um conversor de RGB para hexadecimal e estou tentando fazer um conversor de RGBA para hexadecimal. O originalrgb2hex
funciona, mas o novorgba2hex
função @ não. O que estou fazendo de errado? A função rgba está retornando gba, sem r.
// convert RGB color data to hex
function rgb2hex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}
// convert RGBA color data to hex
function rgba2hex(r, g, b, a) {
if (r > 255 || g > 255 || b > 255 || a > 255)
throw "Invalid color component";
return ((r << 32) | (g << 16) | (b << 8) | a).toString(16);
}
Exemplo
alert(rgb2hex(255, 155, 055));
alert(rgba2hex(255, 155, 055, 255));
Saída atual:ff9b2d
e9b2dff
Saída esperada:ff9b2d
eff9b2dff