# VBForums CodeBank > CodeBank - JavaScript >  JavaScript RGB --> HTML Hex and back

## vbNeo

I wrote this for my own usage, maybe someone here can benefit from it(sorry about the fact that there are no comments).



```
function hextorgb(n1,n2,n3,n4,n5,n6) {

n1 = replacevals(n1);
n2 = replacevals(n2);
n3 = replacevals(n3);
n4 = replacevals(n4);
n5 = replacevals(n5);
n6 = replacevals(n6);

var returnval = ((16 * n1) + (1 * n2));
var returnval1 = 16 * n3 + n4;
var returnval2 = 16 * n5 + n6;

return ((16 * n1) + (1 * n2))+","+((16 * n3) + (1 * n4))+","+((16 * n5) + (1 * n6));

}

function replacevals(n) {

if (n == "a") { n = "10"; }
if (n == "b") { n = "11"; }
if (n == "c") { n = "12"; }
if (n == "d") { n = "13"; }
if (n == "e") { n = "14"; }
if (n == "f") { n = "15"; }

return n

}

function replacevalsreverse(thenum) {
 if (thenum == 10) { thenum = "a"; }
 if (thenum == 11) { thenum = "b"; }
 if (thenum == 12) { thenum = "c"; }
 if (thenum == 13) { thenum = "d"; }
 if (thenum == 14) { thenum = "e"; }
 if (thenum == 15) { thenum = "f"; }

 return thenum;
}

function rgbtohex(num1, num2, num3) {
return calc10to16(num1) + calc10to16(num2) + calc10to16(num3);

}

function calc10to16(num) {
if (num < 16) {
 var thenum = replacevalsreverse(num);
 return thenum;
 }
else {
 var themod=num % 16;
 var thenum=((num-themod) / 16);
 thenum = replacevalsreverse(thenum)
 themod = replacevalsreverse(themod)

 return ""+thenum+themod
}
}


alert(hextorgb("1","0","1","0","1","0"));
alert(rgbtohex("40", "73", "212"));
```

Tested in Firefox 0.9 and Internet Explorer 6.0

----------

