Two Hoots Banner



Hex to RGB Converter

Select the required values in the boxes below and the pages background colour will change to match the selected colour. This script does not currently work in Opera 6.0.
Color/Code Red Green Blue
0-255
HEX
The above table uses calls to two functions NumToHex to convert a decimal to a Hexadecimal number and HexToNum to convert from Hexadecimal back to a decimal. The conversion routines also set the background colour of the page.

function NumToHex(num1,num2) {
// Convert number from decimal to hexadecimal
// num1 refers to the source element
// num 2 refers to the destination element
// first retrieve the source element value
strNum=document.forms[0].elements[num1].value;
for(i = 0; i < strNum.length; i++) {
// loop through each digit
chr=strNum.substring(i, i + 1);
// isNan - check for not a number
// see if not numeric or space 
if((isNaN(chr))||(chr == ' ')) {
// show alert message
alert('You must enter a digit between 0 and 9!');
// set cursor on source element
document.forms[0].elements[num1].select();
// clear destination element
document.forms[0].elements[num2].value='';
// return
return false;
   }
}
// valid digit so check value
if(strNum > 255) {
// invalid number
alert('You must enter a number between 0 and 255!');
// set cursor on source element
document.forms[0].elements[num1].select();
// clear destination element
document.forms[0].elements[num2].value='';
return false;
}
else {
// now convert to hexadecimal
base = strNum / 16;
rem = strNum % 16;
base = base - (rem / 16);
// convert base to hexadecimal
baseS = MakeHex(base);
// convert remainder to hexadecimal
remS = MakeHex(rem);
// write destination value
document.forms[0].elements[num2].value=baseS + '' + remS;
// Change background to values in elements 3,4,5
ChangeBackground(3, 4, 5);
return true;
   }
}


function MakeHex(x) {
// convert passed decimal into hex
if((x >= 0) && (x <= 9))
// between 0 and 9 so return as is
return x;
else {
switch(x) {
// return remaining numbers
case 10: return "A"; 
case 11: return "B";  
case 12: return "C";  
case 13: return "D";  
case 14: return "E";  
case 15: return "F";  
      }
   }
}


function HexToNum(num1,num2) {
// Convert number from hexadecimal to decimal
// num1 refers to the source element
// num 2 refers to the destination element
// first retrieve the source element value
numberS = document.forms[0].elements[num1].value;
// check first digit
tens = MakeNum(numberS.substring(0,1));
if(tens == 'X') {
// if invalid number
document.forms[0].elements[num1].select();
document.forms[0].elements[num2].value='';
return false;
}
ones = 0;
if(numberS.length > 1) 
// means two characters entered
// get the numbers and check them
ones=MakeNum(numberS.substring(1,2));
if(ones == 'X') {
// invalid digit entered
document.forms[0].elements[num1].select();
document.forms[0].elements[num2].value='';
return false;
}
// write decimal number
document.forms[0].elements[num2].value = (tens * 16) + (ones * 1);
// convert hex to upper case
document.forms[0].elements[num1].value = 
		document.forms[0].elements[num1].value.toUpperCase();
// change background colour
ChangeBackground(3, 4, 5);
return true;
}



function MakeNum(str) {
// validate the passed number
if((str >= 0) && (str <= 9))
return str;
switch(str.toUpperCase()) {
// return decimal equivalent
case "A": return 10;
case "B": return 11;
case "C": return 12;
case "D": return 13;
case "E": return 14;
case "F": return 15;
// invalid number entered
default:  alert('Choose a number between 0 and 9 or a letter between A and F!');
return 'X';
   }
}



function ChangeBackground(num1, num2, num3) {
// change the background colour
// use the passed elements to build the colour
document.bgColor = '#'+document.forms[0].elements[num1].value + 
                       document.forms[0].elements[num2].value + 
                       document.forms[0].elements[num3].value;
}
Free JavaScripts provided
by The JavaScript Source