How can I convert RGB values into HTML code?
For example, using values like this:
red:0
blue:0
green:0
html color code = #000000
Is there any formula for converting it?
If you don't specifically need to use the hex value (html colour code) you can actually just use the rgb values you already have. Live example: http://jsfiddle.net/DdFg8/1/
color: rgb(0,0,0); /* black */
color: rgb(255,255,255); /* white */
You just need to convert each component value to its corresponding hex representation, and concatenate them into a string, like:
function colorCode(red, green, blue) {
red = normalize(red);
green = normalize(green);
blue = normalize(blue);
return '#' + pad(red.toString(16)) + pad(green.toString(16)) + pad(blue.toString(16));
}
function pad(string) {
return string.length > 1 ? string.toUpperCase() : "0" + string.toUpperCase();
}
function normalize(color) {
return (color < 1.0 && color > 0.0) ? Math.floor(color * 255) : color;
}
Example: http://jsfiddle.net/5TdXJ/2
red,green, andbluevalue range from 0-1 or 0-255?color: rgb(0,0,0);