I am working on a script that converts rgb into hexidecimal color code. However it is only converting the first two characters and then throwing a NaN error for the rest.
Here is what i have:
function convert(r, g, b) {
return toHex(r) + toHex(g) + toHex(b);
}
function toHex(c) {
c = parseInt(c);
return "0123456789abcdef".charAt((c - c % 16) / 16) + "0123456789abcdef".charAt(c % 16);
}
function getStyle(id) {
var elem = document.getElementById(id);
var style = window.getComputedStyle(elem, null).getPropertyValue('background-color');
var clear = style.replace(/[rgb\()]/g, '');
document.write(clear);
document.getElementById(id).innerHTML = '<span>#' + convert(clear) + '</span>';
}
getStyle('box1');
getStyle('box2');
getStyle('box3');
getStyle('box4');
When i type convert(13, 49, 96) it converts it correctly, it doesn't work how i have it setup. Can anyone offer some insight?
EDIT
Here is the css i am using:
#box1 {background-color:#0d3160;}
#box2 {background-color:#ffffff;}
#box3 {background-color:#ecebe3;}
#box4 {background-color:#242e35;}
Here are the RGB values
rgb(13, 49, 96)
rgb(255, 255, 255)
rgb(236, 235, 227)
rgb(36, 46, 53)