This isnt the most efficient code but it does the job.
Code:
function Num2Hex(n){
if (n==0) return "0";
if (n==1) return "1";
if (n==2) return "2";
if (n==3) return "3";
if (n==4) return "4";
if (n==5) return "5";
if (n==6) return "6";
if (n==7) return "7";
if (n==8) return "8";
if (n==9) return "9";
if (n==10) return "A";
if (n==11) return "B";
if (n==12) return "C";
if (n==13) return "D";
if (n==14) return "E";
if (n==15) return "F";
}
function rgb2Hex(r,g,b) {
var r1,r2,g1,g2,b1,b2
if (r>15)
r1=Num2Hex(Math.floor(r/16));
else
r1=Num2Hex(0);
r2=Num2Hex(r-16*Math.floor(r/16));
if (b>15)
b1=Num2Hex(Math.floor(b/16));
else
b1=Num2Hex(0);
b2=Num2Hex(b-16*Math.floor(b/16));
if (g>15)
g1=Num2Hex(Math.floor(g/16));
else
g1=Num2Hex(0);
g2=Num2Hex(g-16*Math.floor(g/16));
return "#"+r1+r2+g1+g2+b1+b2;
}