Convert Hexidecimal numbers to Decimal numbers.
Or
Convert Decimal numbers to Hexidecimal numbers.
<script type="text/javascript">
<!--
function Convert(){
Hex_value = document.con.RGB.value; // get initial values
Red_Dec = document.con.Dec_R.value;
Green_Dec = document.con.Dec_G.value;
Blue_Dec = document.con.Dec_B.value;
if (Hex_value!=""){
hex=1
chars_1_2 =Hex2Dec(Hex_value.substring(0,2)) // convert first and second digit to dec
chars_3_4 =Hex2Dec(Hex_value.substring(2,4)) // convert third and fourth digit to dec
chars_5_6 =Hex2Dec(Hex_value.substring(4,6)) // convert fifth and sixth digit to dec
document.con.Dec_R.value=chars_1_2;
document.con.Dec_G.value=chars_3_4;
document.con.Dec_B.value=chars_5_6;
}
else{
hex=0
document.con.RGB.value= Dec2Hex(Red_Dec) + Dec2Hex(Green_Dec) + Dec2Hex(Blue_Dec)
}
if(hex==1){
colourme.bgColor=Hex_value
}
else{
colourme.bgColor=Dec2Hex(Red_Dec) + Dec2Hex(Green_Dec) + Dec2Hex(Blue_Dec)
}
}
var hexChars = "0123456789ABCDEF";
function Hex2Dec(HexVal){
HexVal = HexVal.toUpperCase();
var DecVal = 0;
var temp = HexVal.substring(0,1); // first digit
DecVal = (hexChars.indexOf(temp) * 16); // hexChars index number * 16 ..... (0 = 0, 5 = 5, A = 10, F = 15 )
temp = HexVal.substring(1); // second digit
DecVal += hexChars.indexOf(temp);
return DecVal;
}
function Dec2Hex(Dec) {
var a = Dec % 16;
var b = (Dec - a)/16;
hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
return hex;
}
function clear_rgb(num){
if(num!=""){
document.con.RGB.value=""
}
}
function clear_hex(num){
if(num!=""){
document.con.Dec_R.value="";
document.con.Dec_G.value="";
document.con.Dec_B.value="";
}
}
//-->
</script>
<FORM name="con">
<DIV style="position:absolute; left:200px; top:95px; height:150px; width:266px; background: #CB9E83; border: 2px groove #CFCAB7">
<DIV style="position:relative; left:0px; top:0px; height:15px; background:#4D4731; color:#E8DCC2; width:262px;font-size:12px"> RGB & HEX Converter</DIV>
<table cellspacing=0 cellpadding=0 border=0 style="position:relative; left:4px; top:4px; width:254px; height:122;border:2px groove #CFCAB7;font-size:12px;color:#322F24">
<tr>
<td align="right" width="40px">Red:</td>
<td align="center" width="30px"><INPUT value="0" size=3 name="Dec_R" maxlength="3" onblur="clear_rgb(this.value)" onclick="this.select()"></td>
<td align="right" width="100px">Hex: #</td>
<td> <INPUT value="000000" size=6 name="RGB" maxlength="6" onblur="clear_hex(this.value)" onclick="this.select()"></td>
</tr>
<tr>
<td align="right">Green:</td>
<td align="center"><INPUT value="0" size=3 name="Dec_G" maxlength="3" onblur="clear_rgb(this.value)" onclick="this.select()"></td>
<td align="right" bgcolor="#000000" id="colourme"> </td>
<td> <INPUT type=button value="Convert" onclick="Convert();this.blur()"></td>
</tr>
<tr>
<td align="right">Blue:</td>
<td align="center"><INPUT onblur="clear_rgb(this.value)" value="0" size=3 name="Dec_B" maxlength="3" onclick="this.select()"></td>
<td colspan="2" align="center">Enter RGB or Hex value<BR>and click 'Convert'.</td>
</tr>
</table>
</DIV>
</DIV>
</FORM>