There are three main types of color notation, rgb or rgba, hex and a single number where the r value is multiplied by 256×256, the g by 256 and the three numbers summed. Sometimes it is necessary to convert from one to another, which can be tedious. This app converts a value inserted in one format to the other two. I found it invaluable when I had to convert a table of colors to an array of a different format.
Here is how it looks:
To try Click Here
Here is the code:
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head profile=”http://gmpg.org/xfn/11″>
<title>HTML Editor</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal 700 20px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
input{text-align: center;font:normal normal 700 24px Arial}
</style>
</head>
<body>
<center><input type=”text” id=”t1″ name=”t1″ value=”rgb(,,)” ondblclick=’convclr(“1″);’ /><br /><br /> <input type=”text” id=”t2″ name=”t2″ value=”#” ondblclick=’convclr(“2″);’ /><br /><br /> <input type=”text” id=”t3″ name=”t3″ value=”16777215″ ondblclick=’convclr(“3″);’ /></center>
<script type=”text/javascript”>
var a = 0; var b = 4; var hx = “#”; var tot = 0;
function convclr(f) {
a = 0; b = 4; tot = 0; hx = “#”;
if (f == “1”) {
var v1 = document.getElementById(“t1”).value;
a = v1.indexOf(“,”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += 256 * 256 * parseInt(v1.substr(b,a-b));
b = a + 1;
a = v1.indexOf(“,”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += 256 * parseInt(v1.substr(b,a-b));
b = a + 1;
a = v1.indexOf(“)”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += parseInt(v1.substr(b,a-b));
document.getElementById(“t2”).value = hx.toUpperCase();
document.getElementById(“t3”).value = tot;
} else if (f == “2”) {
var v2 = document.getElementById(“t2”).value;
document.getElementById(“t1”).value = “rgb(” + parseInt(v2.substr(1,2),16) + “,” + parseInt(v2.substr(3,2),16) + “,” + parseInt(v2.substr(5,2),16) + “)”;
document.getElementById(“t3”).value = 256 * 256 * parseInt(v2.substr(1,2),16) + 256 * parseInt(v2.substr(3,2),16) + parseInt(v2.substr(5,2),16);
} else if (f == “3”) {
tot = parseInt(document.getElementById(“t3”).value);
var r = Math.floor(tot / (256 * 256));
tot -= (r * 256 * 256);
var g = Math.floor(tot/256);
tot -= g * 256;
b = tot;
document.getElementById(“t1”).value = “rgb(” + r + “,” + g + “,” + b + “)”;
r = r.toString(16).toUpperCase();
if (r.length == 1) r = “0” + r;
g = g.toString(16).toUpperCase();
if (g.length == 1) g = “0” + g;
b = b.toString(16).toUpperCase();
if (b.length == 1) b = “0” + b;
document.getElementById(“t2”).value = “#” + r + g + b;
}
}
</script>
</body></html>
Converting from rgb is the following:
if (f == “1”) {
var v1 = document.getElementById(“t1”).value;
a = v1.indexOf(“,”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += 256 * 256 * parseInt(v1.substr(b,a-b));
b = a + 1;
a = v1.indexOf(“,”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += 256 * parseInt(v1.substr(b,a-b));
b = a + 1;
a = v1.indexOf(“)”,b);
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
hx += parseInt(v1.substr(b,a-b)).toString(16);
tot += parseInt(v1.substr(b,a-b));
document.getElementById(“t2”).value = hx.toUpperCase();
document.getElementById(“t3”).value = tot;
}
The rgb value is parsed for r between the opening parenthesis and the first comma, g between the commas and b between the second comma and the close parenthesis. :
toString(16) does not include a preceding 0 if the hex value is a single digit. This can lead to confusion, as it is not always clear which hex color has the single digit. I therefore precede the single digit with a 0 before listing it:
if ( parseInt(v1.substr(b,a-b)).toString(16).length == 1 ) hx += “0”;
The variable hx is then appended:
hx += parseInt(v1.substr(b,a-b)).toString(16);
Finally, the full hex value is posted in the text input:
document.getElementById(“t2”).value = hx.toUpperCase();
It is not necessary but aesthetically, I prefer the letters to be upper case.
The third format is made by appending a variable tot, first by multiplying by 256 twice, then by 256 and then just the number:
tot += 256 * 256 * parseInt(v1.substr(b,a-b));
tot += 256 * parseInt(v1.substr(b,a-b));
tot += parseInt(v1.substr(b,a-b));
Converting from hex is easier.
Since red green and blue values are made two digit, sequential two digit reading can be used:
document.getElementById(“t1”).value = “rgb(” + parseInt(v2.substr(1,2),16) + “,” + parseInt(v2.substr(3,2),16) + “,” + parseInt(v2.substr(5,2),16) + “)”;
The same values can be used for the third format:
document.getElementById(“t3”).value = 256 * 256 * parseInt(v2.substr(1,2),16) + 256 * parseInt(v2.substr(3,2),16) + parseInt(v2.substr(5,2),16);
In converting from the third format, r,g, and b values are created by the reverse of conversion from rgb:
var r = Math.floor(tot / (256 * 256));
tot -= (r * 256 * 256);
var g = Math.floor(tot/256);
tot -= g * 256;
b = tot;
These values are used for rgb:
document.getElementById(“t1”).value = “rgb(” + r + “,” + g + “,” + b + “)”;
They are also used for hex:
r = r.toString(16).toUpperCase();
if (r.length == 1) r = “0” + r;
g = g.toString(16).toUpperCase();
if (g.length == 1) g = “0” + g;
b = b.toString(16).toUpperCase();
if (b.length == 1) b = “0” + b;
document.getElementById(“t2”).value = “#” + r + g + b;