aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Benjamin Jones <bjones@galois.com>2013-06-11 17:44:46 -0700
committerGravatar Benjamin Jones <bjones@galois.com>2013-06-11 17:44:46 -0700
commitb57041814a28c0915aa140130ee43e7ea71013a9 (patch)
tree0a7c5cb7f959776d6ea7ab1da40a9155d48de761 /src
parente1ba386f3174571e8664b53cd48b679374165184 (diff)
added more color utility functions; tests are failing now
Diffstat (limited to 'src')
-rw-r--r--src/js/fiveui/injected/prelude.js41
1 files changed, 31 insertions, 10 deletions
diff --git a/src/js/fiveui/injected/prelude.js b/src/js/fiveui/injected/prelude.js
index a77a1de..705956a 100644
--- a/src/js/fiveui/injected/prelude.js
+++ b/src/js/fiveui/injected/prelude.js
@@ -257,6 +257,33 @@ fiveui.color.colorCheck = function (selector, colorSet) {
};
/**
+ * Convert a base-10 byte to a zero padded hex byte.
+ */
+fiveui.color.componentToHex = function (c) {
+ var hex = c.toString(16).toUpperCase();
+ return hex.length == 1 ? "0" + hex : hex;
+}
+
+/**
+ * Convert RGB values to Hex.
+ */
+fiveui.color.rgbToHex = function (r, g, b) {
+ return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
+}
+
+/**
+ * Convert a 3-byte hex value to base-10 RGB
+ */
+fiveui.color.hexToRgb = function (hex) {
+ var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
+ return result ? {
+ r: parseInt(result[1], 16),
+ g: parseInt(result[2], 16),
+ b: parseInt(result[3], 16)
+ } : null;
+}
+
+/**
* Covert rgb colors to hex and abreviated hex colors to their full 3 byte
* form.
*
@@ -290,17 +317,11 @@ fiveui.color.colorToHex = function(color) {
// return the input unchanged
}
- var red = parseInt(digits[1]);
- var green = parseInt(digits[2]);
- var blue = parseInt(digits[3]);
+ var r = parseInt(digits[1]);
+ var g = parseInt(digits[2]);
+ var b = parseInt(digits[3]);
- var rgb = blue | (green << 8) | (red << 16);
- if (rgb === 0) {
- return '#000000'; // normalized form
- }
- else {
- return '#' + rgb.toString(16).toUpperCase();
- }
+ return fiveui.color.rgbToHex(r, g, b);
};