From bf2aa4827479b0687896a86e3f1828780a130c72 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Dec 2010 16:49:23 -0500 Subject: [PATCH] Javascript cleanup. --- RGB Converter.wdgt/Widget.html | 8 ++--- RGB Converter.wdgt/Widget.js | 60 +++++++++++++++------------------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/RGB Converter.wdgt/Widget.html b/RGB Converter.wdgt/Widget.html index dc0fb73..94f9fd3 100644 --- a/RGB Converter.wdgt/Widget.html +++ b/RGB Converter.wdgt/Widget.html @@ -37,11 +37,11 @@
- + - - - + + +
diff --git a/RGB Converter.wdgt/Widget.js b/RGB Converter.wdgt/Widget.js index ae33454..aa7c524 100644 --- a/RGB Converter.wdgt/Widget.js +++ b/RGB Converter.wdgt/Widget.js @@ -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); } -- 2.43.5