3 var gShowTransparency = false;
6 var satSlider = new objColour();
7 var hexChars = "0123456789ABCDEF";
8 var selectedColour = 0;
10 var eventInitiator = null;
11 var mouseX, mouseY, offsetLeft, offsetTop;
14 var LastPickedColor = "";
15 var ColorType = "Text";
17 var HighlightType = false;
18 var TableOrCell = false;
19 var LastPickedIsDefault = false;
20 var NoDefault = false;
22 var namedColorsArray = [
23 { name:"aqua", value:"#00ffff" },
24 { name:"black", value:"#000000" },
25 { name:"blue", value:"#0000ff" },
26 { name:"fuchsia", value:"#ff00ff" },
27 { name:"gray", value:"#808080" },
28 { name:"green", value:"#008000" },
29 { name:"lime", value:"#00ff00" },
30 { name:"maroon", value:"#800000" },
31 { name:"navy", value:"#000080" },
32 { name:"olive", value:"#808000" },
33 { name:"orange", value:"#FFA500" },
34 { name:"purple", value:"#800080" },
35 { name:"red", value:"#ff0000" },
36 { name:"silver", value:"#c0c0c0" },
37 { name:"teal", value:"#008080" },
38 { name:"white", value:"#ffffff" },
39 { name:"yellow", value:"#ffff00" }
48 if (!window.arguments[0])
50 dump("colourPicker: Missing color object param\n");
54 // window.arguments[0] is object to get initial values and return color data
55 gColorObj = window.arguments[0];
56 gWindowTitle = window.arguments[1];
57 gShowTransparency = window.arguments[2];
59 gColorObj.cancelled = false;
61 colours = new objColour();
63 makeDraggable(gDialog.hueAndSaturationImg);
64 makeDraggable(gDialog.hueAndSaturationCrosshair);
65 makeDraggable(gDialog.brightnessImg);
67 gDialog.hexColour.value = "";
69 var haveTableRadio = false;
70 var showTransparencyCheckbox = false;
72 if (!gShowTransparency)
73 gDialog.transparencyCheckbox.setAttribute("hidden", true);
75 gColorObj.currentColor = ConvertRGBColorIntoHEXColor(gColorObj.currentColor);
76 gColorObj.lastPickedColor = ConvertRGBColorIntoHEXColor(gColorObj.lastPickedColor);
78 gDialog.LastPickedColor.setAttribute("style", "background-color: " +
79 gColorObj.lastPickedColor);
81 // Set initial color in input field and in the colorpicker
82 SetCurrentColor(gColorObj.currentColor);
83 if (!showTransparencyCheckbox)
84 gDialog.colorpicker.initColor(gColorObj.currentColor);
86 // Caller can prevent user from submitting an empty, i.e., default color
87 NoDefault = gColorObj.NoDefault;
90 // Hide the "Default button -- user must pick a color
91 var defaultColorButton = document.getElementById("DefaultColorButton");
92 if (defaultColorButton)
93 defaultColorButton.collapsed = true;
96 // Set focus to colorpicker if not set to table radio buttons above
98 gDialog.colorpicker.focus();
100 // SetWindowLocation();
103 // * utility function to convert predefined HTML4 color names
104 // into their #rrggbb equivalent and back
105 function getHexColorFromColorName(color)
107 color = color.toLowerCase();
108 for (var i=0; i< namedColorsArray.length; i++) {
109 if (color == namedColorsArray[i].name) {
110 return namedColorsArray[i].value;
116 function getColorNameFromHexColor(color)
118 color = color.toLowerCase();
119 for (var i=0; i< namedColorsArray.length; i++) {
120 if (color == namedColorsArray[i].value) {
121 return namedColorsArray[i].name;
127 function makeDraggable(obj)
129 obj.onmousedown = startDrag;
130 obj.onmousemove = moveDrag;
131 obj.onmouseup = endDrag;
134 function computeOffsets(t)
138 while (t && !(t instanceof XULElement))
140 offsetLeft += t.offsetLeft;
141 offsetTop += t.offsetTop;
146 function startDrag(e)
150 var target = e.target;
151 if (target.id == "hueAndSaturationCrosshair")
152 target = target.parentNode;
154 eventInitiator = target;
155 computeOffsets(target);
156 mouseX = e.clientX - offsetLeft + 1;
157 mouseY = e.clientY - offsetTop + 1;
159 handleValueChange(target);
166 var target = e.target;
167 if (target.id == "hueAndSaturationCrosshair")
168 target = target.parentNode;
170 if (mouseDown && target == eventInitiator)
172 computeOffsets(target);
173 mouseX = e.clientX - offsetLeft + 1;
174 mouseY = e.clientY - offsetTop + 1;
176 mouseX = Math.max(0, Math.min(mouseX, 199));
177 mouseY = Math.max(0, Math.min(mouseY, 199));
179 handleValueChange(target);
187 eventInitiator = null;
188 handleValueChange(e.target);
192 function handleValueChange(obj)
196 if (obj.id == "brightnessImg")
198 var bVal = mouseX * 255 / sWidth;
199 var h = colours.getHue();
200 var s = colours.getSaturation();
201 colours.setHSB(h, s, bVal);
204 else if (obj.id == "hueAndSaturationImg")
206 var hVal = mouseX * 360 / sWidth;
207 var sVal = (200 - mouseY) * 100 / sWidth;
208 var b = colours.getBrightness();
211 colours.setHSB(hVal, sVal/100, b);
216 function checkRange(value, min, max)
218 return Math.max(min, Math.min(value, max));
221 // the user has changed the RGB textboxes
225 var red = gDialog.red;
226 var green = gDialog.green;
227 var blue = gDialog.blue;
229 // XXX Check for numbers
230 red.value = checkRange(red.value, 0, 255);
231 green.value = checkRange(green.value, 0, 255);
232 blue.value = checkRange(blue.value, 0, 255);
234 colours.setRGB(red.value, green.value, blue.value);
240 var hue = gDialog.hue;
241 var saturation = gDialog.saturation;
242 var brightness = gDialog.brightness;
244 // XXX Check for letters
245 brightness.value = checkRange(brightness.value, 0, 255);
246 saturation.value = checkRange(saturation.value, 0, 100);
248 var sat = saturation.value / 100;
250 // Hue is a degree from 0-360
251 // XXX Maybe rotate it back until it's 0-360
252 hue.value = checkRange(hue.value, 0, 359);
254 colours.setHSB(hue.value, sat, brightness.value);
258 function SetCurrentColor(color)
261 color = "transparent";
263 if (color == "transparent")
264 gDialog.transparencyCheckbox.checked = true;
267 var hexCol = getHexColorFromColorName(color);
270 gDialog.hexColour.value = color;
273 ToggleTransparency(gDialog.transparencyCheckbox);
278 var hex = gDialog.hexColour.value;
280 // XXX Check to see if they are hex digits
283 alert("Color is not made of a hash ('#') followed by six hex digits");
287 colours.setHex(hex.toUpperCase().substr(1, hex.length-1));
292 function redrawEverything()
294 gDialog.transparencyCheckbox.checked = false;
295 ToggleTransparency(gDialog.transparencyCheckbox);
297 LastPickedIsDefault = false;
301 redisplayRGBValues();
302 redisplayHSBValues();
304 redisplayColorName();
305 redisplayBrightness();
308 function redisplayBrightness()
310 var sat = gDialog.brightnessImg;
311 var h = colours.getHue();
312 var s = colours.getSaturation();
313 satSlider.setHSB(h, s, 255);
314 sat.setAttribute("style",
315 sat.getAttribute("style") + ";background-color: #" + satSlider.getHex());
318 function redisplaySaturation()
320 var sat = gDialog.saturationImg;
321 var h = colours.getHue();
322 var b = colours.getBrightness();
323 satSlider.setHSB(h, 1, b);
324 sat.setAttribute("style",
325 sat.getAttribute("style") + ";background-color: #" + satSlider.getHex());
328 function redisplaySwatches()
330 gDialog.swatch.style.backgroundColor = "#" + colours.getHex();
333 function redisplayHexValue()
335 gDialog.hexColour.value = "#" + colours.getHex();
338 function redisplayColorName()
340 var color = getColorNameFromHexColor("#" + colours.getHex());
342 gDialog.nameColour.value = color;
344 gDialog.nameColour.value = "";
347 function redisplayRGBValues()
349 gDialog.red.value = Math.round(colours.getRed());
350 gDialog.green.value = Math.round(colours.getGreen());
351 gDialog.blue.value = Math.round(colours.getBlue());
354 function redisplayHSBValues()
356 var h = Math.round(colours.getHue());
357 var s = Math.round(colours.getSaturation() * 100);
358 var b = Math.round(colours.getBrightness());
360 gDialog.hue.value = h;
361 gDialog.saturation.value = s;
362 gDialog.brightness.value = b;
364 computeOffsets(gDialog.hueAndSaturationCrosshair.parentNode);
366 var arrow = gDialog.brightnessArrow;
367 arrow.setAttribute("style",
368 arrow.getAttribute("style") + ";left: " + (b/255*200 + 2) + "px");
370 var crosshair = gDialog.hueAndSaturationCrosshair;
371 crosshair.setAttribute("style",
372 crosshair.getAttribute("style") + ";left: " + (h/360*200 + 4) + "px"
373 + ";top: " + ((100-s)/100*200 +4) + "px");
378 // The object that stores the colours in ram
383 this.g = (128 + (Math.random() * 100));
386 // Returns the hex value
387 this.getHex = function()
389 var v = Math.floor(this.r) * 256 * 256 +
390 Math.floor(this.g) *256 +
392 var c = Number(v).toString(16);
397 this.getRed = function()
401 this.getGreen = function()
405 this.getBlue = function()
409 this.getBrightness = function()
411 // find the min and max rgb values
412 var max1 = Math.max(this.r, this.g);
413 var max2 = Math.max(max1, this.b);
416 this.getSaturation = function()
418 // find the min and max rgb values
419 var min1 = Math.min(this.r, this.g);
420 var min2 = Math.min(min1, this.b);
421 var max1 = Math.max(this.r, this.g);
422 var max2 = Math.max(max1, this.b); // v
424 var delta = max2 - min2;
433 this.getHue = function()
436 // If all the values are the same, there is no hue, just brightness
437 if (this.r == this.g && this.g == this.b)
443 // find the min and max rgb values
444 var min1 = Math.min(this.r, this.g);
445 var min2 = Math.min(min1, this.b);
446 var max1 = Math.max(this.r, this.g);
447 var max2 = Math.max(max1, this.b); // v
449 var delta = max2 - min2;
454 return hue; // Saturation is undefined, so there is no hue
459 hue = (this.g - this.b) / delta; // It's between yellow and magenta
461 else if (this.g == max2)
463 hue = 2 + (this.b - this.r) / delta; // It's between cyan and yellow
467 hue = 4 + (this.r - this.g) / delta; // It's between magenta and cyan
470 hue *= 60; // Get it in degrees
482 this.setRGB = function(r, g, b)
489 this.setHSB = function(h, s, b)
494 // Set it to a grey based on the brightness
501 h /= 60; // Get it out of degrees
503 var i = Math.floor(h);
504 var f = h - i; // Grab the decimal part
506 var q = b * (1 - s * f);
507 var t = b * (1 - s * (1 - f));
544 this.setHex = function(hex)
546 var c = hex.split("");
547 var red = hex2dec(c[0]) * 16 + hex2dec(c[1]);
548 var green = hex2dec(c[2]) * 16 + hex2dec(c[3]);
549 var blue = hex2dec(c[4]) * 16 + hex2dec(c[5]);
557 // Returns the decimal value of a hex character
558 function hex2dec(hex)
560 return hexChars.indexOf(hex);
563 // return the hexidecimal value of a decimal digit from 1-16
564 function dec2hex(dec)
566 return hexChars.charAt(dec);
569 function SelectColor()
571 var color = gDialog.colorpicker.color;
574 colours.setHex(color.toUpperCase().substr(1, color.length-1));
581 if (gDialog.transparencyCheckbox.checked)
582 gColorObj.currentColor = "transparent";
584 gColorObj.currentColor = gDialog.hexColour.value;
585 gColorObj.lastPickedColor = gColorObj.currentColor;
589 function ValidateData()
593 function onCancelColor()
595 // Tells caller that user canceled
596 gColorObj.cancelled = true;
597 //SaveWindowLocation();
601 function IncreaseTextboxValue(id, maxValue)
603 var e = document.getElementById(id);
607 var newValue = Math.min(maxValue, Number(v) + 1);
610 redrawEverythingAfterTexboxValueChanged(id);
614 function DecreaseTextboxValue(id, minValue)
616 var e = document.getElementById(id);
620 var newValue = Math.max(minValue, Number(v) - 1);
623 redrawEverythingAfterTexboxValueChanged(id);
627 function redrawEverythingAfterTexboxValueChanged(id)
630 id == "saturation" ||
633 var h = gDialog.hue.value;
634 var s = gDialog.saturation.value;
635 var b = gDialog.brightness.value;
636 colours.setHSB(h, s/100, b);
640 var r = gDialog.red.value;
641 var g = gDialog.green.value;
642 var b = gDialog.blue.value;
643 colours.setRGB(r, g, b);
648 function onTextboxValueChanged(e, id)
655 v = checkRange(v, 0, 359);
661 v = checkRange(v, 0, 255);
664 v = checkRange(v, 0, 100);
668 redrawEverythingAfterTexboxValueChanged(id);
671 function ToggleTransparency(elt)
675 gDialog.red.setAttribute("disabled", true);
676 gDialog.blue.setAttribute("disabled", true);
677 gDialog.green.setAttribute("disabled", true);
678 gDialog.hue.setAttribute("disabled", true);
679 gDialog.saturation.setAttribute("disabled", true);
680 gDialog.brightness.setAttribute("disabled", true);
681 gDialog.hexColour.setAttribute("disabled", true);
682 gDialog.nameColour.setAttribute("disabled", true);
683 gDialog.redLabel.setAttribute("disabled", true);
684 gDialog.blueLabel.setAttribute("disabled", true);
685 gDialog.greenLabel.setAttribute("disabled", true);
686 gDialog.hueLabel.setAttribute("disabled", true);
687 gDialog.saturationLabel.setAttribute("disabled", true);
688 gDialog.brightnessLabel.setAttribute("disabled", true);
689 gDialog.hexColourLabel.setAttribute("disabled", true);
690 gDialog.nameColourLabel.setAttribute("disabled", true);
692 gDialog.swatch.style.backgroundColor = "#ffffff";
696 gDialog.red.removeAttribute("disabled");
697 gDialog.blue.removeAttribute("disabled");
698 gDialog.green.removeAttribute("disabled");
699 gDialog.hue.removeAttribute("disabled");
700 gDialog.saturation.removeAttribute("disabled");
701 gDialog.brightness.removeAttribute("disabled");
702 gDialog.hexColour.removeAttribute("disabled");
703 gDialog.nameColour.removeAttribute("disabled");
704 gDialog.redLabel.removeAttribute("disabled");
705 gDialog.blueLabel.removeAttribute("disabled");
706 gDialog.greenLabel.removeAttribute("disabled");
707 gDialog.hueLabel.removeAttribute("disabled");
708 gDialog.saturationLabel.removeAttribute("disabled");
709 gDialog.brightnessLabel.removeAttribute("disabled");
710 gDialog.hexColourLabel.removeAttribute("disabled");
711 gDialog.nameColourLabel.removeAttribute("disabled");
713 gDialog.swatch.style.backgroundColor = "#" + colours.getHex();
717 function onNamedColourChanged(elt)
719 var namedColour = elt.value;
720 var i, l = namedColorsArray.length;
723 if (namedColorsArray[i].name == namedColour)
725 gDialog.hexColour.value = namedColorsArray[i].value;
732 function SelectLastPickedColor()
734 SetCurrentColor(gColorObj.lastPickedColor);
735 LastPickedIsDefault = true;
744 function forceInteger(elementID)
746 var editField = document.getElementById( elementID );
750 var stringIn = editField.value;
751 if (stringIn && stringIn.length > 0)
753 // Strip out all nonnumeric characters
754 stringIn = stringIn.replace(/\D+/g,"");
755 if (!stringIn) stringIn = "";
757 // Write back only if changed
758 if (stringIn != editField.value)
759 editField.value = stringIn;
764 function ConvertRGBColorIntoHEXColor(color)
766 if ( /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.test(color) ) {
767 var r = Number(RegExp.$1).toString(16);
768 if (r.length == 1) r = "0"+r;
769 var g = Number(RegExp.$2).toString(16);
770 if (g.length == 1) g = "0"+g;
771 var b = Number(RegExp.$3).toString(16);
772 if (b.length == 1) b = "0"+b;