Si assegnano 6 cifre esadecimali e si ottiene il codice, #RRGGBB, da utilizzare nelle pagine HTML
- Le cifre in base 16 sono: 0 1 2 3 4 5 6 7 8 9 A B C D E F
- In ogni coppia di cifre la prima cifra pesa più della seconda…
- Il codice per ogni componente del colore va da 00 a FF (da 0 a 255)
- Le combinazioni totali sono 16*16*16*16*16*16 = 224 (più di 16 milioni).
<script language="JavaScript">
var rgb16=Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F");
function opzioni16()
{
for(i=0; i < 15; i++)
document.writeln("<option value='" + rgb16[i] + "'>"+rgb16[i]+"</option>");
document.writeln("<option value=" + rgb16[15] + " selected>"+rgb16[15]+"</option>");
}
function cambiaRgb()
{
var r1=document.colorergb.selectR1.value;
var r2=document.colorergb.selectR2.value;
document.colorergb.coloreR.style.background="#"+r1+r2+"0000";
var g1=document.colorergb.selectG1.value;
var g2=document.colorergb.selectG2.value;
document.colorergb.coloreG.style.background="#00"+g1+g2+"00";
var b1=document.colorergb.selectB1.value;
var b2=document.colorergb.selectB2.value;
document.colorergb.coloreB.style.background="#0000"+b1+b2;
var colore="#"+r1+r2+g1+g2+b1+b2;
document.colorergb.colore.style.background=colore;
document.colorergb.txtColore.value=colore;
}
</script>
<form name="colorergb">
<table cellspacing="0" cellpadding="3">
<tr height=50>
<td>RGB</td>
<td><input type="text" value=" " name="colore" size="30" readonly /></td>
<td><input type="text" value="#FFFFFF" name="txtColore" size="10" readonly /></td>
</tr>
<tr>
<td>Red</td>
<td><input type="text" value=" " name="coloreR" size="30" readonly /></td>
<td>
<select name="selectR1" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
<select name="selectR2" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
</td>
</tr>
<tr>
<td>Green</td>
<td><input type="text" value=" " name="coloreG" size="30" readonly /></td>
<td>
<select name="selectG1" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
<select name="selectG2" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
</td>
</tr>
<tr>
<td>Blue</td>
<td><input type="text" value=" " name="coloreB" size="30" readonly /></td>
<td>
<select name="selectB1" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
<select name="selectB2" onChange="cambiaRgb();" ><script>opzioni16();</script></select>
</td>
</tr>
</table>
</form>
<script language="JavaScript">
cambiaRgb();
</script>