2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 * Provides color conversion and validation utils
9 * @class YAHOO.util.Color
10 * @namespace YAHOO.util
12 YAHOO.util.Color = function() {
15 isArray = YAHOO.lang.isArray,
16 isNumber = YAHOO.lang.isNumber;
21 * Converts 0-1 to 0-255
23 * @param n {float} the number to convert
24 * @return {int} a number 0-255
26 real2dec: function(n) {
27 return Math.min(255, Math.round(n*256));
31 * Converts HSV (h[0-360], s[0-1]), v[0-1] to RGB [255,255,255]
33 * @param h {int|[int, float, float]} the hue, or an
34 * array containing all three parameters
35 * @param s {float} the saturation
36 * @param v {float} the value/brightness
37 * @return {[int, int, int]} the red, green, blue values in
40 hsv2rgb: function(h, s, v) {
43 return this.hsv2rgb.call(this, h[0], h[1], h[2]);
47 i = Math.floor((h/60)%6),
55 case 0: r=v; g=t; b=p; break;
56 case 1: r=q; g=v; b=p; break;
57 case 2: r=p; g=v; b=t; break;
58 case 3: r=p; g=q; b=v; break;
59 case 4: r=t; g=p; b=v; break;
60 case 5: r=v; g=p; b=q; break;
65 return [fn(r), fn(g), fn(b)];
69 * Converts to RGB [255,255,255] to HSV (h[0-360], s[0-1]), v[0-1]
71 * @param r {int|[int, int, int]} the red value, or an
72 * array containing all three parameters
73 * @param g {int} the green value
74 * @param b {int} the blue value
75 * @return {[int, float, float]} the value converted to hsv
77 rgb2hsv: function(r, g, b) {
80 return this.rgb2hsv.apply(this, r);
88 min = Math.min(Math.min(r,g),b),
89 max = Math.max(Math.max(r,g),b),
95 case r: h=60*(g-b)/delta;
100 case g: h=(60*(b-r)/delta)+120; break;
101 case b: h=(60*(r-g)/delta)+240; break;
104 s = (max === 0) ? 0 : 1-(min/max);
106 hsv = [Math.round(h), s, max];
112 * Converts decimal rgb values into a hex string
113 * 255,255,255 -> FFFFFF
115 * @param r {int|[int, int, int]} the red value, or an
116 * array containing all three parameters
117 * @param g {int} the green value
118 * @param b {int} the blue value
119 * @return {string} the hex string
121 rgb2hex: function(r, g, b) {
123 return this.rgb2hex.apply(this, r);
127 return f(r) + f(g) + f(b);
131 * Converts an int 0...255 to hex pair 00...FF
133 * @param n {int} the number to convert
134 * @return {string} the hex equivalent
136 dec2hex: function(n) {
137 n = parseInt(n,10)|0;
138 n = (n > 255 || n < 0) ? 0 : n;
140 return (ZERO+n.toString(16)).slice(-2).toUpperCase();
144 * Converts a hex pair 00...FF to an int 0...255
146 * @param str {string} the hex pair to convert
147 * @return {int} the decimal
149 hex2dec: function(str) {
150 return parseInt(str,16);
154 * Converts a hex string to rgb
156 * @param str {string} the hex string
157 * @return {[int, int, int]} an array containing the rgb values
159 hex2rgb: function(s) {
160 var f = this.hex2dec;
161 return [f(s.slice(0, 2)), f(s.slice(2, 4)), f(s.slice(4, 6))];
165 * Returns the closest websafe color to the supplied rgb value.
167 * @param r {int|[int, int, int]} the red value, or an
168 * array containing all three parameters
169 * @param g {int} the green value
170 * @param b {int} the blue value
171 * @return {[int, int, int]} an array containing the closes
172 * websafe rgb colors.
174 websafe: function(r, g, b) {
177 return this.websafe.apply(this, r);
180 // returns the closest match [0, 51, 102, 153, 204, 255]
181 var f = function(v) {
183 v = Math.min(Math.max(0, v), 255);
185 for (i=0; i<256; i=i+51) {
187 if (v >= i && v <= next) {
188 return (v-i > 25) ? next : i;
196 return [f(r), f(g), f(b)];
203 * The colorpicker module provides a widget for selecting colors
204 * @module colorpicker
205 * @requires yahoo, dom, event, element, slider
209 var _pickercount = 0,
212 Slider = YAHOO.widget.Slider,
216 sub = lang.substitute,
222 * A widget to select colors
223 * @namespace YAHOO.widget
224 * @class YAHOO.widget.ColorPicker
225 * @extends YAHOO.util.Element
227 * @param {HTMLElement | String | Object} el(optional) The html
228 * element that represents the colorpicker, or the attribute object to use.
229 * An element will be created if none provided.
230 * @param {Object} attr (optional) A key map of the colorpicker's
231 * initial attributes. Ignored if first arg is attributes object.
233 function ColorPicker(el, attr) {
234 _pickercount = _pickercount + 1;
236 if (arguments.length === 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
237 attr = el; // treat first arg as attr object
238 el = attr.element || null;
241 if (!el && !attr.element) { // create if we dont have one
242 el = this._createHostElement(attr);
245 ColorPicker.superclass.constructor.call(this, el, attr);
250 YAHOO.extend(ColorPicker, YAHOO.util.Element, {
253 * The element ids used by this control
260 * The id for the "red" form field
264 * @default yui-picker-r
269 * The id for the "red" hex pair output
273 * @default yui-picker-rhex
278 * The id for the "green" form field
282 * @default yui-picker-g
287 * The id for the "green" hex pair output
291 * @default yui-picker-ghex
297 * The id for the "blue" form field
301 * @default yui-picker-b
306 * The id for the "blue" hex pair output
310 * @default yui-picker-bhex
315 * The id for the "hue" form field
319 * @default yui-picker-h
324 * The id for the "saturation" form field
328 * @default yui-picker-s
333 * The id for the "value" form field
337 * @default yui-picker-v
342 * The id for the picker region slider
343 * @property ID.PICKER_BG
346 * @default yui-picker-bg
348 PICKER_BG: b + "-bg",
351 * The id for the picker region thumb
352 * @property ID.PICKER_THUMB
355 * @default yui-picker-thumb
357 PICKER_THUMB: b + "-thumb",
360 * The id for the hue slider
361 * @property ID.HUE_BG
364 * @default yui-picker-hue-bg
366 HUE_BG: b + "-hue-bg",
369 * The id for the hue thumb
370 * @property ID.HUE_THUMB
373 * @default yui-picker-hue-thumb
375 HUE_THUMB: b + "-hue-thumb",
378 * The id for the hex value form field
382 * @default yui-picker-hex
387 * The id for the color swatch
388 * @property ID.SWATCH
391 * @default yui-picker-swatch
393 SWATCH: b + "-swatch",
396 * The id for the websafe color swatch
397 * @property ID.WEBSAFE_SWATCH
400 * @default yui-picker-websafe-swatch
402 WEBSAFE_SWATCH: b + "-websafe-swatch",
405 * The id for the control details
406 * @property ID.CONTROLS
408 * @default yui-picker-controls
410 CONTROLS: b + "-controls",
413 * The id for the rgb controls
414 * @property ID.RGB_CONTROLS
416 * @default yui-picker-rgb-controls
418 RGB_CONTROLS: b + "-rgb-controls",
421 * The id for the hsv controls
422 * @property ID.HSV_CONTROLS
424 * @default yui-picker-hsv-controls
426 HSV_CONTROLS: b + "-hsv-controls",
429 * The id for the hsv controls
430 * @property ID.HEX_CONTROLS
432 * @default yui-picker-hex-controls
434 HEX_CONTROLS: b + "-hex-controls",
437 * The id for the hex summary
438 * @property ID.HEX_SUMMARY
440 * @default yui-picker-hex-summary
442 HEX_SUMMARY: b + "-hex-summary",
445 * The id for the controls section header
446 * @property ID.CONTROLS_LABEL
448 * @default yui-picker-controls-label
450 CONTROLS_LABEL: b + "-controls-label"
454 * Constants for any script-generated messages. The values here
455 * are the default messages. They can be updated by providing
456 * the complete list to the constructor for the "txt" attribute.
457 * Note: the strings are added to the DOM as HTML.
462 ILLEGAL_HEX: "Illegal hex value entered",
463 SHOW_CONTROLS: "Show color details",
464 HIDE_CONTROLS: "Hide color details",
465 CURRENT_COLOR: "Currently selected color: {rgb}",
466 CLOSEST_WEBSAFE: "Closest websafe color: {rgb}. Click to select.",
479 * Constants for the default image locations for img tags that are
480 * generated by the control. They can be modified by passing the
481 * complete list to the contructor for the "images" attribute
486 PICKER_THUMB: "../../build/colorpicker/assets/picker_thumb.png",
487 HUE_THUMB: "../../build/colorpicker/assets/hue_thumb.png"
491 * Constants for the control's default default values
500 * Constants for the control's configuration attributes
506 SATURATION : "saturation",
515 PICKER_SIZE : "pickersize",
516 SHOW_CONTROLS : "showcontrols",
517 SHOW_RGB_CONTROLS : "showrgbcontrols",
518 SHOW_HSV_CONTROLS : "showhsvcontrols",
519 SHOW_HEX_CONTROLS : "showhexcontrols",
520 SHOW_HEX_SUMMARY : "showhexsummary",
521 SHOW_WEBSAFE : "showwebsafe",
522 CONTAINER : "container",
524 ELEMENTS : "elements",
531 * Flag to allow individual UI updates to forego animation if available.
532 * True during construction for initial thumb placement. Set to false
542 * Creates the host element if it doesn't exist
543 * @method _createHostElement
546 _createHostElement : function () {
547 var el = document.createElement('div');
550 el.className = this.CSS.BASE;
557 * Moves the hue slider into the position dictated by the current state
559 * @method _updateHueSlider
562 _updateHueSlider : function() {
563 var size = this.get(this.OPT.PICKER_SIZE),
564 h = this.get(this.OPT.HUE);
566 h = size - Math.round(h / 360 * size);
568 // 0 is at the top and bottom of the hue slider. Always go to
569 // the top so we don't end up sending the thumb to the bottom
570 // when the value didn't actually change (e.g., a conversion
571 // produced 360 instead of 0 and the value was already 0).
576 this.hueSlider.setValue(h, this.skipAnim);
580 * Moves the picker slider into the position dictated by the current state
582 * @method _updatePickerSlider
585 _updatePickerSlider : function() {
586 var size = this.get(this.OPT.PICKER_SIZE),
587 s = this.get(this.OPT.SATURATION),
588 v = this.get(this.OPT.VALUE);
590 s = Math.round(s * size / 100);
591 v = Math.round(size - (v * size / 100));
594 this.pickerSlider.setRegionValue(s, v, this.skipAnim);
598 * Moves the sliders into the position dictated by the current state
600 * @method _updateSliders
603 _updateSliders : function() {
604 this._updateHueSlider();
605 this._updatePickerSlider();
609 * Sets the control to the specified rgb value and
610 * moves the sliders to the proper positions
612 * @param rgb {[int, int, int]} the rgb value
613 * @param silent {boolean} whether or not to fire the change event
615 setValue : function(rgb, silent) {
616 silent = (silent) || false;
617 this.set(this.OPT.RGB, rgb, silent);
618 this._updateSliders();
623 * @property hueSlider
624 * @type YAHOO.widget.Slider
630 * @property pickerSlider
631 * @type YAHOO.widget.Slider
636 * Translates the slider value into hue, int[0,359]
639 * @return {int} the hue from 0 to 359
642 var size = this.get(this.OPT.PICKER_SIZE),
643 h = (size - this.hueSlider.getValue()) / size;
644 h = Math.round(h*360);
645 return (h === 360) ? 0 : h;
649 * Translates the slider value into saturation, int[0,1], left to right
652 * @return {int} the saturation from 0 to 1
655 return this.pickerSlider.getXValue() / this.get(this.OPT.PICKER_SIZE);
659 * Translates the slider value into value/brightness, int[0,1], top
663 * @return {int} the value from 0 to 1
666 var size = this.get(this.OPT.PICKER_SIZE);
667 return (size - this.pickerSlider.getYValue()) / size;
671 * Updates the background of the swatch with the current rbg value.
672 * Also updates the websafe swatch to the closest websafe color
673 * @method _updateSwatch
676 _updateSwatch : function() {
677 var rgb = this.get(this.OPT.RGB),
678 websafe = this.get(this.OPT.WEBSAFE),
679 el = this.getElement(this.ID.SWATCH),
680 color = rgb.join(","),
681 txt = this.get(this.OPT.TXT);
683 Dom.setStyle(el, "background-color", "rgb(" + color + ")");
684 el.title = sub(txt.CURRENT_COLOR, {
685 "rgb": "#" + this.get(this.OPT.HEX)
689 el = this.getElement(this.ID.WEBSAFE_SWATCH);
690 color = websafe.join(",");
692 Dom.setStyle(el, "background-color", "rgb(" + color + ")");
693 el.title = sub(txt.CLOSEST_WEBSAFE, {
694 "rgb": "#" + Color.rgb2hex(websafe)
700 * Reads the sliders and converts the values to RGB, updating the
701 * internal state for all the individual form fields
702 * @method _getValuesFromSliders
705 _getValuesFromSliders : function() {
706 this.set(this.OPT.RGB, Color.hsv2rgb(this._getH(), this._getS(), this._getV()));
710 * Updates the form field controls with the state data contained
712 * @method _updateFormFields
715 _updateFormFields : function() {
716 this.getElement(this.ID.H).value = this.get(this.OPT.HUE);
717 this.getElement(this.ID.S).value = this.get(this.OPT.SATURATION);
718 this.getElement(this.ID.V).value = this.get(this.OPT.VALUE);
719 this.getElement(this.ID.R).value = this.get(this.OPT.RED);
720 this.getElement(this.ID.R_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.RED));
721 this.getElement(this.ID.G).value = this.get(this.OPT.GREEN);
722 this.getElement(this.ID.G_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.GREEN));
723 this.getElement(this.ID.B).value = this.get(this.OPT.BLUE);
724 this.getElement(this.ID.B_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.BLUE));
725 this.getElement(this.ID.HEX).value = this.get(this.OPT.HEX);
729 * Event handler for the hue slider.
730 * @method _onHueSliderChange
731 * @param newOffset {int} pixels from the start position
734 _onHueSliderChange : function(newOffset) {
736 var h = this._getH(),
737 rgb = Color.hsv2rgb(h, 1, 1),
738 styleDef = "rgb(" + rgb.join(",") + ")";
740 this.set(this.OPT.HUE, h, true);
742 // set picker background to the hue
743 Dom.setStyle(this.getElement(this.ID.PICKER_BG), "background-color", styleDef);
745 if (this.hueSlider.valueChangeSource !== Slider.SOURCE_SET_VALUE) {
746 this._getValuesFromSliders();
749 this._updateFormFields();
750 this._updateSwatch();
754 * Event handler for the picker slider, which controls the
755 * saturation and value/brightness.
756 * @method _onPickerSliderChange
757 * @param newOffset {{x: int, y: int}} x/y pixels from the start position
760 _onPickerSliderChange : function(newOffset) {
762 var s=this._getS(), v=this._getV();
763 this.set(this.OPT.SATURATION, Math.round(s*100), true);
764 this.set(this.OPT.VALUE, Math.round(v*100), true);
766 if (this.pickerSlider.valueChangeSource !== Slider.SOURCE_SET_VALUE) {
767 this._getValuesFromSliders();
770 this._updateFormFields();
771 this._updateSwatch();
776 * Key map to well-known commands for txt field input
777 * @method _getCommand
778 * @param e {Event} the keypress or keydown event
779 * @return {int} a command code
781 * <li>0 = not a number, letter in range, or special key</li>
782 * <li>1 = number</li>
783 * <li>2 = a-fA-F</li>
784 * <li>3 = increment (up arrow)</li>
785 * <li>4 = decrement (down arrow)</li>
786 * <li>5 = special key (tab, delete, return, escape, left, right)</li>
787 * <li>6 = return</li>
791 _getCommand : function(e) {
792 var c = Event.getCharCode(e);
794 //alert(Event.getCharCode(e) + ", " + e.keyCode + ", " + e.charCode);
797 if (c === 38) { // up arrow
799 } else if (c === 13) { // return
801 } else if (c === 40) { // down array
803 } else if (c >= 48 && c<=57) { // 0-9
805 } else if (c >= 97 && c<=102) { // a-f
807 } else if (c >= 65 && c<=70) { // A-F
809 //} else if ("8, 9, 13, 27, 37, 39".indexOf(c) > -1 ||
810 // (c >= 112 && c <=123)) { // including F-keys
811 // tab, delete, return, escape, left, right or ctrl/meta sequences
812 } else if ("8, 9, 13, 27, 37, 39".indexOf(c) > -1 ||
813 e.ctrlKey || e.metaKey) { // special chars
815 } else { // something we probably don't want
821 * Use the value of the text field to update the control
822 * @method _useFieldValue
823 * @param e {Event} an event
824 * @param el {HTMLElement} the field
825 * @param prop {string} the key to the linked property
828 _useFieldValue : function(e, el, prop) {
831 if (prop !== this.OPT.HEX) {
832 val = parseInt(val, 10);
835 if (val !== this.get(prop)) {
841 * Handle keypress on one of the rgb or hsv fields.
842 * @method _rgbFieldKeypress
843 * @param e {Event} the keypress event
844 * @param el {HTMLElement} the field
845 * @param prop {string} the key to the linked property
848 _rgbFieldKeypress : function(e, el, prop) {
849 var command = this._getCommand(e),
850 inc = (e.shiftKey) ? 10 : 1;
852 case 6: // return, update the value
853 this._useFieldValue.apply(this, arguments);
856 case 3: // up arrow, increment
857 this.set(prop, Math.min(this.get(prop)+inc, 255));
858 this._updateFormFields();
859 //Event.stopEvent(e);
861 case 4: // down arrow, decrement
862 this.set(prop, Math.max(this.get(prop)-inc, 0));
863 this._updateFormFields();
864 //Event.stopEvent(e);
873 * Handle keydown on the hex field
874 * @method _hexFieldKeypress
875 * @param e {Event} the keypress event
876 * @param el {HTMLElement} the field
877 * @param prop {string} the key to the linked property
880 _hexFieldKeypress : function(e, el, prop) {
881 var command = this._getCommand(e);
882 if (command === 6) { // return, update the value
883 this._useFieldValue.apply(this, arguments);
888 * Allows numbers and special chars, and by default allows a-f.
889 * Used for the hex field keypress handler.
891 * @param e {Event} the event
892 * @param numbersOnly omits a-f if set to true
894 * @return {boolean} false if we are canceling the event
896 _hexOnly : function(e, numbersOnly) {
897 var command = this._getCommand(e);
900 case 5: // special char
903 case 2: // hex char (a-f)
904 if (numbersOnly !== true) {
908 // fallthrough is intentional
910 default: // prevent alpha and punctuation
917 * Allows numbers and special chars only. Used for the
918 * rgb and hsv fields keypress handler.
919 * @method _numbersOnly
920 * @param e {Event} the event
922 * @return {boolean} false if we are canceling the event
924 _numbersOnly : function(e) {
925 return this._hexOnly(e, true);
929 * Returns the element reference that is saved. The id can be either
930 * the element id, or the key for this id in the "id" config attribute.
931 * For instance, the host element id can be obtained by passing its
932 * id (default: "yui_picker") or by its key "YUI_PICKER".
933 * @param id {string} the element id, or key
934 * @return {HTMLElement} a reference to the element
936 getElement : function(id) {
937 return this.get(this.OPT.ELEMENTS)[this.get(this.OPT.IDS)[id]];
940 _createElements : function() {
941 var el, child, img, fld, p,
942 ids = this.get(this.OPT.IDS),
943 txt = this.get(this.OPT.TXT),
944 images = this.get(this.OPT.IMAGES),
945 Elem = function(type, o) {
946 var n = document.createElement(type);
948 lang.augmentObject(n, o, true);
952 RGBElem = function(type, obj) {
962 return new Elem(type, o);
965 p = this.get("element");
967 // Picker slider (S and V) ---------------------------------------------
969 el = new Elem("div", {
970 id: ids[this.ID.PICKER_BG],
971 className: "yui-picker-bg",
976 child = new Elem("div", {
977 id: ids[this.ID.PICKER_THUMB],
978 className: "yui-picker-thumb"
981 img = new Elem("img", {
982 src: images.PICKER_THUMB
985 child.appendChild(img);
986 el.appendChild(child);
989 // Hue slider ---------------------------------------------
990 el = new Elem("div", {
991 id: ids[this.ID.HUE_BG],
992 className: "yui-picker-hue-bg",
997 child = new Elem("div", {
998 id: ids[this.ID.HUE_THUMB],
999 className: "yui-picker-hue-thumb"
1002 img = new Elem("img", {
1003 src: images.HUE_THUMB
1006 child.appendChild(img);
1007 el.appendChild(child);
1011 // controls ---------------------------------------------
1013 el = new Elem("div", {
1014 id: ids[this.ID.CONTROLS],
1015 className: "yui-picker-controls"
1022 el = new Elem("div", {
1026 child = new Elem("a", {
1027 id: ids[this.ID.CONTROLS_LABEL],
1028 //className: "yui-picker-controls-label",
1031 el.appendChild(child);
1035 el = new Elem("div", {
1043 el = new Elem("ul", {
1044 id: ids[this.ID.RGB_CONTROLS],
1045 className: "yui-picker-rgb-controls"
1048 child = new Elem("li");
1049 child.appendChild(document.createTextNode(txt.R + " "));
1051 fld = new RGBElem("input", {
1053 className: "yui-picker-r"
1056 child.appendChild(fld);
1057 el.appendChild(child);
1059 child = new Elem("li");
1060 child.appendChild(document.createTextNode(txt.G + " "));
1062 fld = new RGBElem("input", {
1064 className: "yui-picker-g"
1067 child.appendChild(fld);
1068 el.appendChild(child);
1070 child = new Elem("li");
1071 child.appendChild(document.createTextNode(txt.B + " "));
1073 fld = new RGBElem("input", {
1075 className: "yui-picker-b"
1078 child.appendChild(fld);
1079 el.appendChild(child);
1084 el = new Elem("ul", {
1085 id: ids[this.ID.HSV_CONTROLS],
1086 className: "yui-picker-hsv-controls"
1089 child = new Elem("li");
1090 child.appendChild(document.createTextNode(txt.H + " "));
1092 fld = new RGBElem("input", {
1094 className: "yui-picker-h"
1097 child.appendChild(fld);
1098 child.appendChild(document.createTextNode(" " + txt.DEG));
1100 el.appendChild(child);
1102 child = new Elem("li");
1103 child.appendChild(document.createTextNode(txt.S + " "));
1105 fld = new RGBElem("input", {
1107 className: "yui-picker-s"
1110 child.appendChild(fld);
1111 child.appendChild(document.createTextNode(" " + txt.PERCENT));
1113 el.appendChild(child);
1115 child = new Elem("li");
1116 child.appendChild(document.createTextNode(txt.V + " "));
1118 fld = new RGBElem("input", {
1120 className: "yui-picker-v"
1123 child.appendChild(fld);
1124 child.appendChild(document.createTextNode(" " + txt.PERCENT));
1126 el.appendChild(child);
1132 el = new Elem("ul", {
1133 id: ids[this.ID.HEX_SUMMARY],
1134 className: "yui-picker-hex_summary"
1137 child = new Elem("li", {
1138 id: ids[this.ID.R_HEX]
1140 el.appendChild(child);
1142 child = new Elem("li", {
1143 id: ids[this.ID.G_HEX]
1145 el.appendChild(child);
1147 child = new Elem("li", {
1148 id: ids[this.ID.B_HEX]
1150 el.appendChild(child);
1154 el = new Elem("div", {
1155 id: ids[this.ID.HEX_CONTROLS],
1156 className: "yui-picker-hex-controls"
1158 el.appendChild(document.createTextNode(txt.HEX + " "));
1160 child = new RGBElem("input", {
1161 id: ids[this.ID.HEX],
1162 className: "yui-picker-hex",
1167 el.appendChild(child);
1170 p = this.get("element");
1173 el = new Elem("div", {
1174 id: ids[this.ID.SWATCH],
1175 className: "yui-picker-swatch"
1181 el = new Elem("div", {
1182 id: ids[this.ID.WEBSAFE_SWATCH],
1183 className: "yui-picker-websafe-swatch"
1190 _attachRGBHSV : function(id, config) {
1191 Event.on(this.getElement(id), "keydown", function(e, me) {
1192 me._rgbFieldKeypress(e, this, config);
1194 Event.on(this.getElement(id), "keypress", this._numbersOnly, this, true);
1195 Event.on(this.getElement(id), "blur", function(e, me) {
1196 me._useFieldValue(e, this, config);
1202 * Updates the rgb attribute with the current state of the r,g,b
1203 * fields. This is invoked from change listeners on these
1204 * attributes to facilitate updating these values from the
1205 * individual form fields
1206 * @method _updateRGB
1209 _updateRGB : function() {
1210 var rgb = [this.get(this.OPT.RED),
1211 this.get(this.OPT.GREEN),
1212 this.get(this.OPT.BLUE)];
1214 this.set(this.OPT.RGB, rgb);
1216 this._updateSliders();
1220 * Creates any missing DOM structure.
1222 * @method _initElements
1225 _initElements : function () {
1226 // bind all of our elements
1228 ids = this.get(o.IDS),
1229 els = this.get(o.ELEMENTS),
1232 // Add the default value as a key for each element for easier lookup
1233 for (i in this.ID) {
1234 if (lang.hasOwnProperty(this.ID, i)) {
1235 ids[this.ID[i]] = ids[i];
1239 // Check for picker element, if not there, create all of them
1240 el = Dom.get(ids[this.ID.PICKER_BG]);
1242 this._createElements();
1247 if (lang.hasOwnProperty(ids, i)) {
1249 el = Dom.get(ids[i]);
1251 // generate an id if the implementer passed in an element reference,
1252 // and the element did not have an id already
1253 id = Dom.generateId(el);
1255 // update the id in case we generated the id
1256 ids[i] = id; // key is WEBSAFE_SWATCH
1257 ids[ids[i]] = id; // key is websafe_swatch
1259 // store the dom ref
1267 * Sets the initial state of the sliders
1268 * @method initPicker
1270 initPicker : function () {
1271 this._initSliders();
1277 * Creates the Hue and Value/Saturation Sliders.
1279 * @method _initSliders
1282 _initSliders : function () {
1284 size = this.get(this.OPT.PICKER_SIZE);
1287 this.hueSlider = Slider.getVertSlider(
1288 this.getElement(ID.HUE_BG),
1289 this.getElement(ID.HUE_THUMB), 0, size);
1291 this.pickerSlider = Slider.getSliderRegion(
1292 this.getElement(ID.PICKER_BG),
1293 this.getElement(ID.PICKER_THUMB), 0, size, 0, size);
1295 // Apply animate attribute configuration
1296 this.set(this.OPT.ANIMATE, this.get(this.OPT.ANIMATE));
1300 * Adds event listeners to Sliders and UI elements. Wires everything
1306 _bindUI : function () {
1310 this.hueSlider.subscribe("change",
1311 this._onHueSliderChange, this, true);
1312 this.pickerSlider.subscribe("change",
1313 this._onPickerSliderChange, this, true);
1315 Event.on(this.getElement(ID.WEBSAFE_SWATCH), "click", function(e) {
1316 this.setValue(this.get(O.WEBSAFE));
1319 Event.on(this.getElement(ID.CONTROLS_LABEL), "click", function(e) {
1320 this.set(O.SHOW_CONTROLS, !this.get(O.SHOW_CONTROLS));
1321 Event.preventDefault(e);
1324 this._attachRGBHSV(ID.R, O.RED);
1325 this._attachRGBHSV(ID.G, O.GREEN);
1326 this._attachRGBHSV(ID.B, O.BLUE);
1327 this._attachRGBHSV(ID.H, O.HUE);
1328 this._attachRGBHSV(ID.S, O.SATURATION);
1329 this._attachRGBHSV(ID.V, O.VALUE);
1331 Event.on(this.getElement(ID.HEX), "keydown", function(e, me) {
1332 me._hexFieldKeypress(e, this, O.HEX);
1335 Event.on(this.getElement(this.ID.HEX), "keypress",
1336 this._hexOnly, this,true);
1337 Event.on(this.getElement(this.ID.HEX), "blur", function(e, me) {
1338 me._useFieldValue(e, this, O.HEX);
1343 * Wrapper for _updateRGB, but allows non-animated update
1346 * @param skipAnim {Boolean} Omit Slider animation for this action
1348 syncUI : function (skipAnim) {
1349 this.skipAnim = skipAnim;
1351 this.skipAnim = false;
1356 * Updates the RGB values from the current state of the HSV
1357 * values. Executed when the one of the HSV form fields are
1362 _updateRGBFromHSV : function() {
1363 var hsv = [this.get(this.OPT.HUE),
1364 this.get(this.OPT.SATURATION)/100,
1365 this.get(this.OPT.VALUE)/100],
1366 rgb = Color.hsv2rgb(hsv);
1368 this.set(this.OPT.RGB, rgb);
1370 this._updateSliders();
1374 * Parses the hex string to normalize shorthand values, converts
1375 * the hex value to rgb and updates the rgb attribute (which
1376 * updates the state for all of the other values)
1380 _updateHex : function() {
1382 var hex = this.get(this.OPT.HEX),
1386 // support #369 -> #336699 shorthand
1389 for (i=0; i<l; i=i+1) {
1396 if (hex.length !== 6) {
1400 rgb = Color.hex2rgb(hex);
1408 * Returns the cached element reference. If the id is not a string, it
1409 * is assumed that it is an element and this is returned.
1410 * @param id {string|HTMLElement} the element key, id, or ref
1411 * @param on {boolean} hide or show. If true, show
1414 _hideShowEl : function(id, on) {
1415 var el = (lang.isString(id) ? this.getElement(id) : id);
1416 Dom.setStyle(el, "display", (on) ? "" : "none");
1421 * Sets up the config attributes and the change listeners for this
1423 * @method initAttributes
1424 * @param attr An object containing default attribute values
1426 initAttributes : function(attr) {
1429 ColorPicker.superclass.initAttributes.call(this, attr);
1432 * The size of the picker. Trying to change this is not recommended.
1433 * @attribute pickersize
1437 this.setAttributeConfig(this.OPT.PICKER_SIZE, {
1438 value: attr.size || this.DEFAULT.PICKER_SIZE
1442 * The current hue value 0-360
1446 this.setAttributeConfig(this.OPT.HUE, {
1447 value: attr.hue || 0,
1448 validator: lang.isNumber
1452 * The current saturation value 0-100
1453 * @attribute saturation
1456 this.setAttributeConfig(this.OPT.SATURATION, {
1457 value: attr.saturation || 0,
1458 validator: lang.isNumber
1462 * The current value/brightness value 0-100
1466 this.setAttributeConfig(this.OPT.VALUE, {
1467 value: lang.isNumber(attr.value) ? attr.value : 100,
1468 validator: lang.isNumber
1472 * The current red value 0-255
1476 this.setAttributeConfig(this.OPT.RED, {
1477 value: lang.isNumber(attr.red) ? attr.red : 255,
1478 validator: lang.isNumber
1482 * The current green value 0-255
1486 this.setAttributeConfig(this.OPT.GREEN, {
1487 value: lang.isNumber(attr.green) ? attr.green : 255,
1488 validator: lang.isNumber
1492 * The current blue value 0-255
1496 this.setAttributeConfig(this.OPT.BLUE, {
1497 value: lang.isNumber(attr.blue) ? attr.blue : 255,
1498 validator: lang.isNumber
1502 * The current hex value #000000-#FFFFFF, without the #
1506 this.setAttributeConfig(this.OPT.HEX, {
1507 value: attr.hex || "FFFFFF",
1508 validator: lang.isString
1512 * The current rgb value. Updates the state of all of the
1513 * other value fields. Read-only: use setValue to set the
1514 * controls rgb value.
1516 * @type [int, int, int]
1519 this.setAttributeConfig(this.OPT.RGB, {
1520 value: attr.rgb || [255,255,255],
1521 method: function(rgb) {
1523 this.set(this.OPT.RED, rgb[0], true);
1524 this.set(this.OPT.GREEN, rgb[1], true);
1525 this.set(this.OPT.BLUE, rgb[2], true);
1527 var websafe = Color.websafe(rgb),
1528 hex = Color.rgb2hex(rgb),
1529 hsv = Color.rgb2hsv(rgb);
1531 this.set(this.OPT.WEBSAFE, websafe, true);
1532 this.set(this.OPT.HEX, hex, true);
1536 // fix bug #1754338 - when saturation is 0, hue is
1537 // silently always set to 0, but input field not updated
1539 this.set(this.OPT.HUE, hsv[0], true);
1541 this.set(this.OPT.SATURATION, Math.round(hsv[1]*100), true);
1542 this.set(this.OPT.VALUE, Math.round(hsv[2]*100), true);
1548 * If the color picker will live inside of a container object,
1549 * set, provide a reference to it so the control can use the
1550 * container's events.
1551 * @attribute container
1552 * @type YAHOO.widget.Panel
1554 this.setAttributeConfig(this.OPT.CONTAINER, {
1556 method: function(container) {
1558 // Position can get out of sync when the
1559 // control is manipulated while display is
1560 // none. Resetting the slider constraints
1561 // when it is visible gets the state back in
1563 container.showEvent.subscribe(function() {
1564 // this.pickerSlider.thumb.resetConstraints();
1565 // this.hueSlider.thumb.resetConstraints();
1566 this.pickerSlider.focus();
1572 * The closest current websafe value
1573 * @attribute websafe
1576 this.setAttributeConfig(this.OPT.WEBSAFE, {
1577 value: attr.websafe || [255,255,255]
1581 var ids = attr.ids || lang.merge({}, this.ID), i;
1583 if (!attr.ids && _pickercount > 1) {
1585 if (lang.hasOwnProperty(ids, i)) {
1586 ids[i] = ids[i] + _pickercount;
1593 * A list of element ids and/or element references used by the
1594 * control. The default is the this.ID list, and can be customized
1595 * by passing a list in the contructor
1597 * @type {referenceid: realid}
1600 this.setAttributeConfig(this.OPT.IDS, {
1606 * A list of txt strings for internationalization. Default
1612 this.setAttributeConfig(this.OPT.TXT, {
1613 value: attr.txt || this.TXT,
1618 * The img src default list
1621 * @type {key: image}
1624 this.setAttributeConfig(this.OPT.IMAGES, {
1625 value: attr.images || this.IMAGE,
1629 * The element refs used by this control. Set at initialization
1630 * @attribute elements
1631 * @type {id: HTMLElement}
1634 this.setAttributeConfig(this.OPT.ELEMENTS, {
1640 * Hide/show the entire set of controls
1641 * @attribute showcontrols
1645 this.setAttributeConfig(this.OPT.SHOW_CONTROLS, {
1646 value: lang.isBoolean(attr.showcontrols) ? attr.showcontrols : true,
1647 method: function(on) {
1649 var el = Dom.getElementsByClassName("bd", "div",
1650 this.getElement(this.ID.CONTROLS))[0];
1652 this._hideShowEl(el, on);
1654 this.getElement(this.ID.CONTROLS_LABEL).innerHTML =
1655 (on) ? this.get(this.OPT.TXT).HIDE_CONTROLS :
1656 this.get(this.OPT.TXT).SHOW_CONTROLS;
1662 * Hide/show the rgb controls
1663 * @attribute showrgbcontrols
1667 this.setAttributeConfig(this.OPT.SHOW_RGB_CONTROLS, {
1668 value: lang.isBoolean(attr.showrgbcontrols) ? attr.showrgbcontrols : true,
1669 method: function(on) {
1670 this._hideShowEl(this.ID.RGB_CONTROLS, on);
1675 * Hide/show the hsv controls
1676 * @attribute showhsvcontrols
1680 this.setAttributeConfig(this.OPT.SHOW_HSV_CONTROLS, {
1681 value: lang.isBoolean(attr.showhsvcontrols) ?
1682 attr.showhsvcontrols : false,
1683 method: function(on) {
1684 //Dom.setStyle(this.getElement(this.ID.HSV_CONTROLS), "visibility", (on) ? "" : "hidden");
1685 this._hideShowEl(this.ID.HSV_CONTROLS, on);
1687 // can't show both the hsv controls and the rbg hex summary
1688 if (on && this.get(this.OPT.SHOW_HEX_SUMMARY)) {
1689 this.set(this.OPT.SHOW_HEX_SUMMARY, false);
1695 * Hide/show the hex controls
1696 * @attribute showhexcontrols
1700 this.setAttributeConfig(this.OPT.SHOW_HEX_CONTROLS, {
1701 value: lang.isBoolean(attr.showhexcontrols) ?
1702 attr.showhexcontrols : false,
1703 method: function(on) {
1704 this._hideShowEl(this.ID.HEX_CONTROLS, on);
1709 * Hide/show the websafe swatch
1710 * @attribute showwebsafe
1714 this.setAttributeConfig(this.OPT.SHOW_WEBSAFE, {
1715 value: lang.isBoolean(attr.showwebsafe) ? attr.showwebsafe : true,
1716 method: function(on) {
1717 this._hideShowEl(this.ID.WEBSAFE_SWATCH, on);
1722 * Hide/show the hex summary
1723 * @attribute showhexsummary
1727 this.setAttributeConfig(this.OPT.SHOW_HEX_SUMMARY, {
1728 value: lang.isBoolean(attr.showhexsummary) ? attr.showhexsummary : true,
1729 method: function(on) {
1730 this._hideShowEl(this.ID.HEX_SUMMARY, on);
1732 // can't show both the hsv controls and the rbg hex summary
1733 if (on && this.get(this.OPT.SHOW_HSV_CONTROLS)) {
1734 this.set(this.OPT.SHOW_HSV_CONTROLS, false);
1738 this.setAttributeConfig(this.OPT.ANIMATE, {
1739 value: lang.isBoolean(attr.animate) ? attr.animate : true,
1740 method: function(on) {
1741 if (this.pickerSlider) {
1742 this.pickerSlider.animate = on;
1743 this.hueSlider.animate = on;
1748 this.on(this.OPT.HUE + "Change", this._updateRGBFromHSV, this, true);
1749 this.on(this.OPT.SATURATION + "Change", this._updateRGBFromHSV, this, true);
1750 this.on(this.OPT.VALUE + "Change", this._updateRGBFromHSV, this, true);
1752 this.on(this.OPT.RED + "Change", this._updateRGB, this, true);
1753 this.on(this.OPT.GREEN + "Change", this._updateRGB, this, true);
1754 this.on(this.OPT.BLUE + "Change", this._updateRGB, this, true);
1756 this.on(this.OPT.HEX + "Change", this._updateHex, this, true);
1758 this._initElements();
1762 YAHOO.widget.ColorPicker = ColorPicker;
1764 YAHOO.register("colorpicker", YAHOO.widget.ColorPicker, {version: "2.9.0", build: "2800"});