<div id="front" onmousemove="mousemove(event);" onmouseout="mouseexit(event);">
<img src="Default.png" alt="" />
+ <div id="hexinput">#<input type="text" name="hexinput" id="hexinputf" size="6" maxlength="6" onblur="hexwatcher()" value="0" /></div>
+
<div id="swatch"></div>
<div id="redinput"><input type="text" name="redinput" id="redinputf" style="color: red" size="3" maxlength="3" onblur="rgbwatcher('red')" value="0" /></div>
hex : ''
};
-var lastinput = 'rgb';
-
// ###################################################################
// watches the three RGB fields to make sure they don't go over the limites
function rgbwatcher(colour)
update_hex();
// redraw the swatch
- lastinput = 'rgb';
+ update_swatch();
+}
+
+// ###################################################################
+// watches the hex field for updates
+function hexwatcher()
+{
+ field = document.getElementById("hexinputf");
+
+ // sanitize the hex
+ var newval = field.value.replace(/[^0-9a-f]*/gi, "");
+
+ // get the length
+ var length = newval.length;
+
+ // make sure we're always 6
+ if (length > 6)
+ {
+ newval = newval.substr(0, 6);
+ }
+ else if (length < 6)
+ {
+ for (var i = length; i <= 6; i++)
+ {
+ newval = "" + newval + "0";
+ }
+ }
+
+ // update the field
+ field.value = newval;
+
+ // update fields[]
+ fields['hex'] = newval;
+
+ // set RGB
+ update_rgb();
+
+ // redraw the swatch
update_swatch();
}
{
var hexstr = dec2hex(fields['red']) + dec2hex(fields['green']) + dec2hex(fields['blue']);
fields['hex'] = hexstr;
+ document.getElementById("hexinputf").value = hexstr;
+}
+
+// ###################################################################
+// update the RGB values
+function update_rgb()
+{
+ // regex match the bits
+ var bits = fields['hex'].match(/(..)(..)(..)/);
+
+ fields['red'] = hex2dec(bits[1]);
+ fields['green'] = hex2dec(bits[2]);
+ fields['blue'] = hex2dec(bits[3]);
+
+ // construct the hex values
+ document.getElementById("redinputf").value = fields['red'];
+ document.getElementById("greeninputf").value = fields['green'];
+ document.getElementById("blueinputf").value = fields['blue'];
}
// ###################################################################