Javascript cleanup.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 11 Dec 2010 21:49:23 +0000 (16:49 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 11 Dec 2010 21:49:23 +0000 (16:49 -0500)
RGB Converter.wdgt/Widget.html
RGB Converter.wdgt/Widget.js

index dc0fb73f8ec41dc883c610af5aca15361ede8b62..94f9fd300cf7581933413c4a9a4897750c124309 100644 (file)
   <canvas id="wheel" width="186" height="186" onmousemove="mousemove(event);" onmouseout="mouseexit(event);"></canvas>
   <!-- front -->
   <div id="front"> 
-    <input type="text" id="hexinput" size="6" onblur="hexwatcher()" value="3C3C3C" />
+    <input type="text" id="hexinput" size="6" onblur="OnHexChanged()" value="3C3C3C" />
                       
-    <input type="text" id="redinput" size="3" onblur="rgbwatcher('red')" value="60" />
-    <input type="text" id="greeninput" size="3" onblur="rgbwatcher('green')" value="60" />
-    <input type="text" id="blueinput" size="3" onblur="rgbwatcher('blue')" value="60" />
+    <input type="text" id="redinput" size="3" onblur="OnRGBChange('red')" value="60" />
+    <input type="text" id="greeninput" size="3" onblur="OnRGBChange('green')" value="60" />
+    <input type="text" id="blueinput" size="3" onblur="OnRGBChange('blue')" value="60" />
 
     <div class="flip" id="fliprollie"></div>
     <div class="flip" id="flip" onclick="show_back(event);" onmouseover="enterflip(event);" onmouseout="exitflip(event)";></div>
index ae334541cf21855854f273b2c172af14eceb925c..aa7c524aa106964cca3a0577f33cccaf6388dc90 100644 (file)
@@ -37,13 +37,12 @@ function Setup()
 
 // ###################################################################
 // watches the three RGB fields to make sure they don't go over the limites
-function rgbwatcher(color)
+function OnRGBChange(color)
 {
-  field = document.getElementById(color + "input");
+  var field = document.getElementById(color + "input");
   
-  // handle RGB triads
-  if (triad = field.value.match(/(rgb)?\(?([0-9]{1,3}),\s?([0-9]{1,3}),\s?([0-9]{1,3})\)?/))
-  {
+  // Handle RGB triads that are pased into a single box.
+  if (triad = field.value.match(/(rgb)?\(?([0-9]{1,3}),\s?([0-9]{1,3}),\s?([0-9]{1,3})\)?/)) {
     document.getElementById("redinput").value = triad[2];
     document.getElementById("greeninput").value = triad[3];
     document.getElementById("blueinput").value = triad[4];
@@ -59,12 +58,9 @@ function rgbwatcher(color)
   newval = Math.floor(newval);
   
   // make sure we don't go over 255
-  if (newval > 255)
-  {
+  if (newval > 255) {
     newval = 255;
-  }
-  else if (newval < 0)
-  {
+  } else if (newval < 0) {
     newval = 0;
   }
   
@@ -75,7 +71,7 @@ function rgbwatcher(color)
   colors_[color] = newval;
   
   // set hex
-  update_hex();
+  UpdateHex();
   
   // redraw the swatch
   Draw();
@@ -201,9 +197,9 @@ function _GetComponentColorString(color)
 
 // ###################################################################
 // watches the hex field for updates
-function hexwatcher()
+function OnHexChanged()
 {
-  field = document.getElementById("hexinput");
+  var field = document.getElementById("hexinput");
   
   // sanitize the hex
   var newval = field.value.replace(/[^0-9a-f]*/gi, "");
@@ -211,19 +207,15 @@ function hexwatcher()
   // get the length
   var length = newval.length;
   
-  // make sure we're always 6
-  if (length > 6)
-  {
+  // Trim the value.
+  if (length > 6) {
     newval = newval.substr(0, 6);
-  }
-  else if (length == 3)
-  {
-    newval = newval.substr(0, 1) + newval.substr(0, 1) + newval.substr(1, 1) + newval.substr(1, 1) + newval.substr(2, 1) + newval.substr(2, 1);
-  }
-  else if (length < 6)
-  {
-    for (var i = length; i <= 6; i++)
-    {
+  } else if (length == 3) {
+    newval = newval.substr(0, 1) + newval.substr(0, 1) +
+             newval.substr(1, 1) + newval.substr(1, 1) +
+             newval.substr(2, 1) + newval.substr(2, 1);
+  } else if (length < 6) {
+    for (var i = length; i <= 6; i++) {
       newval = "" + newval + "0";
     }
   }
@@ -235,7 +227,7 @@ function hexwatcher()
   colors_.hex = newval;
   
   // set RGB
-  update_rgb();
+  UpdateRGB();
   
   // redraw the swatch
   Draw();
@@ -243,23 +235,23 @@ function hexwatcher()
 
 // ###################################################################
 // update the hex value
-function update_hex()
+function UpdateHex()
 {
-  var hexstr = dec2hex(colors_.red) + dec2hex(colors_.green) + dec2hex(colors_.blue);
+  var hexstr = _Dec2Hex(colors_.red) + _Dec2Hex(colors_.green) + _Dec2Hex(colors_.blue);
   colors_.hex = hexstr;
   document.getElementById("hexinput").value = hexstr;
 }
 
 // ###################################################################
 // update the RGB values
-function update_rgb()
+function UpdateRGB()
 {
   // regex match the bits
   var bits = colors_.hex.match(/(..)(..)(..)/);
   
-  colors_.red = hex2dec(bits[1]);
-  colors_.green = hex2dec(bits[2]);
-  colors_.blue = hex2dec(bits[3]);
+  colors_.red = _Hex2dDc(bits[1]);
+  colors_.green = _Hex2dDc(bits[2]);
+  colors_.blue = _Hex2dDc(bits[3]);
   
   // construct the hex values
   document.getElementById("redinput").value = colors_.red;
@@ -269,7 +261,7 @@ function update_rgb()
 
 // ###################################################################
 // convert a decimal to a hexidecimal
-function dec2hex(dec)
+function _Dec2Hex(dec)
 {
   var hexstr = "0123456789ABCDEF";
   var low = dec % 16;
@@ -281,7 +273,7 @@ function dec2hex(dec)
 
 // ###################################################################
 // converts a hexidecimal to a decimal
-function hex2dec(hex)
+function _Hex2dDc(hex)
 {
   return parseInt(hex, 16);
 }