/*
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
version: 2.3.0
*/
/**
* Provides color conversion and validation utils
* @class YAHOO.util.Color
* @namespace YAHOO.util
*/
return {
/**
* Converts 0-1 to 0-255
* @method real2dec
* @param n {float} the number to convert
* @return {int} a number 0-255
*/
real2dec: function(n) {
},
/**
* Converts HSV (h[0-360], s[0-1]), v[0-1] to RGB [255,255,255]
* @method hsv2rgb
* @param h {int|[int, float, float]} the hue, or an
* array containing all three parameters
* @param s {float} the saturation
* @param v {float} the value/brightness
* @return {[int, int, int]} the red, green, blue values in
* decimal.
*/
hsv2rgb: function(h, s, v) {
}
var r, g, b, i, f, p, q, t;
f = (h/60)-i;
p = v*(1-s);
q = v*(1-f*s);
t = v*(1-(1-f)*s);
switch(i) {
case 0: r=v; g=t; b=p; break;
case 1: r=q; g=v; b=p; break;
case 2: r=p; g=v; b=t; break;
case 3: r=p; g=q; b=v; break;
case 4: r=t; g=p; b=v; break;
case 5: r=v; g=p; b=q; break;
}
},
/**
* Converts to RGB [255,255,255] to HSV (h[0-360], s[0-1]), v[0-1]
* @method rgb2hsv
* @param r {int|[int, int, int]} the red value, or an
* array containing all three parameters
* @param g {int} the green value
* @param b {int} the blue value
* @return {[int, float, float]} the value converted to hsv
*/
rgb2hsv: function(r, g, b) {
}
r=r/255;
g=g/255;
b=b/255;
switch (max) {
case min: h=0; break;
case r: h=60*(g-b)/delta;
if (g<b) {
h+=360;
}
break;
}
return hsv;
},
/**
* Converts decimal rgb values into a hex string
* 255,255,255 -> FFFFFF
* @method rgb2hex
* @param r {int|[int, int, int]} the red value, or an
* array containing all three parameters
* @param g {int} the green value
* @param b {int} the blue value
* @return {string} the hex string
*/
rgb2hex: function(r, g, b) {
}
var f=this.dec2hex;
return f(r) + f(g) + f(b);
},
/**
* Converts an int 0...255 to hex pair 00...FF
* @method dec2hex
* @param n {int} the number to convert
* @return {string} the hex equivalent
*/
dec2hex: function(n) {
n = parseInt(n, 10);
n = (n > 255 || n < 0) ? 0 : n;
},
/**
* Converts a hex pair 00...FF to an int 0...255
* @method hex2dec
* @param str {string} the hex pair to convert
* @return {int} the decimal
*/
var f = function(c) {
};
return ((f(s[0]) * 16) + f(s[1]));
},
/**
* Converts a hex string to rgb
* @method hex2rgb
* @param str {string} the hex string
* @return {[int, int, int]} an array containing the rgb values
*/
hex2rgb: function(s) {
var f = this.hex2dec;
},
/**
* Returns the closest websafe color to the supplied rgb value.
* @method websafe
* @param r {int|[int, int, int]} the red value, or an
* array containing all three parameters
* @param g {int} the green value
* @param b {int} the blue value
* @return {[int, int, int]} an array containing the closes
* websafe rgb colors.
*/
websafe: function(r, g, b) {
}
// returns the closest match [0, 51, 102, 153, 204, 255]
var f = function(v) {
var i, next;
for (i=0; i<256; i=i+51) {
next = i+51;
if (v >= i && v <= next) {
return (v-i > 25) ? next : i;
}
}
}
return v;
};
return [f(r), f(g), f(b)];
}
};
}();
(function() {
var pickercount = 0;
/**
* The colorpicker module provides a widget for selecting colors
* @module colorpicker
* @requires yahoo, dom, event, element, slider
*/
/**
* A widget to select colors
* @namespace YAHOO.widget
* @class YAHOO.widget.ColorPicker
* @extends YAHOO.util.Element
* @constructor
* @param {HTMLElement | String | Object} el(optional) The html
* element that represents the colorpicker, or the attribute object to use.
* An element will be created if none provided.
* @param {Object} attr (optional) A key map of the colorpicker's
* initial attributes. Ignored if first arg is attributes object.
*/
}
}
};
var b = "yui-picker";
/**
* The element ids used by this control
* @property ID
* @final
*/
/**
* The id for the "red" form field
* @property ID.R
* @type String
* @final
* @default yui-picker-r
*/
R: b + "-r",
/**
* The id for the "red" hex pair output
* @property ID.R_HEX
* @type String
* @final
* @default yui-picker-rhex
*/
R_HEX: b + "-rhex",
/**
* The id for the "green" form field
* @property ID.G
* @type String
* @final
* @default yui-picker-g
*/
G: b + "-g",
/**
* The id for the "green" hex pair output
* @property ID.G_HEX
* @type String
* @final
* @default yui-picker-ghex
*/
G_HEX: b + "-ghex",
/**
* The id for the "blue" form field
* @property ID.B
* @type String
* @final
* @default yui-picker-b
*/
B: b + "-b",
/**
* The id for the "blue" hex pair output
* @property ID.B_HEX
* @type String
* @final
* @default yui-picker-bhex
*/
B_HEX: b + "-bhex",
/**
* The id for the "hue" form field
* @property ID.H
* @type String
* @final
* @default yui-picker-h
*/
H: b + "-h",
/**
* The id for the "saturation" form field
* @property ID.S
* @type String
* @final
* @default yui-picker-s
*/
S: b + "-s",
/**
* The id for the "value" form field
* @property ID.V
* @type String
* @final
* @default yui-picker-v
*/
V: b + "-v",
/**
* The id for the picker region slider
* @property ID.PICKER_BG
* @type String
* @final
* @default yui-picker-bg
*/
PICKER_BG: b + "-bg",
/**
* The id for the picker region thumb
* @property ID.PICKER_THUMB
* @type String
* @final
* @default yui-picker-thumb
*/
PICKER_THUMB: b + "-thumb",
/**
* The id for the hue slider
* @property ID.HUE_BG
* @type String
* @final
* @default yui-picker-hue-bg
*/
HUE_BG: b + "-hue-bg",
/**
* The id for the hue thumb
* @property ID.HUE_THUMB
* @type String
* @final
* @default yui-picker-hue-thumb
*/
HUE_THUMB: b + "-hue-thumb",
/**
* The id for the hex value form field
* @property ID.HEX
* @type String
* @final
* @default yui-picker-hex
*/
HEX: b + "-hex",
/**
* The id for the color swatch
* @property ID.SWATCH
* @type String
* @final
* @default yui-picker-swatch
*/
SWATCH: b + "-swatch",
/**
* The id for the websafe color swatch
* @property ID.WEBSAFE_SWATCH
* @type String
* @final
* @default yui-picker-websafe-swatch
*/
WEBSAFE_SWATCH: b + "-websafe-swatch",
/**
* The id for the control details
* @property ID.CONTROLS
* @final
* @default yui-picker-controls
*/
CONTROLS: b + "-controls",
/**
* The id for the rgb controls
* @property ID.RGB_CONTROLS
* @final
* @default yui-picker-rgb-controls
*/
RGB_CONTROLS: b + "-rgb-controls",
/**
* The id for the hsv controls
* @property ID.HSV_CONTROLS
* @final
* @default yui-picker-hsv-controls
*/
HSV_CONTROLS: b + "-hsv-controls",
/**
* The id for the hsv controls
* @property ID.HEX_CONTROLS
* @final
* @default yui-picker-hex-controls
*/
HEX_CONTROLS: b + "-hex-controls",
/**
* The id for the hex summary
* @property ID.HEX_SUMMARY
* @final
* @default yui-picker-hex-summary
*/
HEX_SUMMARY: b + "-hex-summary",
/**
* The id for the controls section header
* @property ID.CONTROLS_LABEL
* @final
* @default yui-picker-controls-label
*/
CONTROLS_LABEL: b + "-controls-label"
};
/**
* Constants for any script-generated messages. The values here
* are the default messages. They can be updated by providing
* the complete list to the constructor for the "txt" attribute.
* @property TXT
* @final
*/
ILLEGAL_HEX: "Illegal hex value entered",
SHOW_CONTROLS: "Show color details",
HIDE_CONTROLS: "Hide color details",
CURRENT_COLOR: "Currently selected color: {rgb}",
CLOSEST_WEBSAFE: "Closest websafe color: {rgb}. Click to select.",
R: "R",
G: "G",
B: "B",
H: "H",
S: "S",
V: "V",
HEX: "#",
DEG: "\u00B0",
PERCENT: "%"
};
/**
* Constants for the default image locations for img tags that are
* generated by the control. They can be modified by passing the
* complete list to the contructor for the "images" attribute
* @property IMAGE
* @final
*/
PICKER_THUMB: "../../build/colorpicker/assets/picker_thumb.png",
HUE_THUMB: "../../build/colorpicker/assets/hue_thumb.png"
};
/*
* Constants for the control's custom event names. subscribe
* to the rgbChange event instead.
* @property EVENT
* @final
*/
//proto.EVENT = {
//CHANGE: "change"
//};
//proto.CSS = { };
/**
* Constants for the control's default default values
* @property DEFAULT
* @final
*/
PICKER_SIZE: 180
};
/**
* Constants for the control's configuration attributes
* @property OPT
* @final
*/
HUE: "hue",
SATURATION: "saturation",
VALUE: "value",
RED: "red",
GREEN: "green",
BLUE: "blue",
HSV: "hsv",
RGB: "rgb",
WEBSAFE: "websafe",
HEX: "hex",
PICKER_SIZE: "pickersize",
SHOW_CONTROLS: "showcontrols",
SHOW_RGB_CONTROLS: "showrgbcontrols",
SHOW_HSV_CONTROLS: "showhsvcontrols",
SHOW_HEX_CONTROLS: "showhexcontrols",
SHOW_HEX_SUMMARY: "showhexsummary",
SHOW_WEBSAFE: "showwebsafe",
//SHOW_SUBMIT: "showsubmit",
CONTAINER: "container",
IDS: "ids",
ELEMENTS: "elements",
TXT: "txt",
IMAGES: "images",
ANIMATE: "animate"
};
/**
* Sets the control to the specified rgb value and
* moves the sliders to the proper positions
* @method setValue
* @param rgb {[int, int, int]} the rgb value
* @param silent {boolean} whether or not to fire the change event
*/
_updateSliders.call(this);
};
/**
* The hue slider
* @property hueSlider
* @type YAHOO.widget.Slider
*/
/**
* The picker region
* @property pickerSlider
* @type YAHOO.widget.Slider
*/
proto.pickerSlider = null;
/**
* Translates the slider value into hue, int[0,359]
* @method _getH
* @private
* @return {int} the hue from 0 to 359
*/
var _getH = function() {
h = Math.round(h*360);
return (h === 360) ? 0 : h;
};
/**
* Translates the slider value into saturation, int[0,1], left to right
* @method _getS
* @private
* @return {int} the saturation from 0 to 1
*/
var _getS = function() {
};
/**
* Translates the slider value into value/brightness, int[0,1], top
* to bottom
* @method _getV
* @private
* @return {int} the value from 0 to 1
*/
var _getV = function() {
};
/**
* Updates the background of the swatch with the current rbg value.
* Also updates the websafe swatch to the closest websafe color
* @method _updateSwatch
* @private
*/
var _updateSwatch = function() {
});
});
};
/**
* Reads the sliders and converts the values to RGB, updating the
* internal state for all the individual form fields
* @method _getValuesFromSliders
* @private
*/
var _getValuesFromSliders = function() {
};
/**
* Updates the form field controls with the state data contained
* in the control.
* @method _updateFormFields
* @private
*/
var _updateFormFields = function() {
};
/**
* Event handler for the hue slider.
* @method _onHueSliderChange
* @param newOffset {int} pixels from the start position
* @private
*/
// set picker background to the hue
_getValuesFromSliders.call(this);
}
_updateFormFields.call(this);
_updateSwatch.call(this);
};
/**
* Event handler for the picker slider, which controls the
* saturation and value/brightness.
* @method _onPickerSliderChange
* @param newOffset {{x: int, y: int}} x/y pixels from the start position
* @private
*/
_getValuesFromSliders.call(this);
}
_updateFormFields.call(this);
_updateSwatch.call(this);
};
/**
* Key map to well-known commands for txt field input
* @method _getCommand
* @param e {Event} the keypress or keydown event
* @return {int} a command code
* <ul>
* <li>0 = not a number, letter in range, or special key</li>
* <li>1 = number</li>
* <li>2 = a-fA-F</li>
* <li>3 = increment (up arrow)</li>
* <li>4 = decrement (down arrow)</li>
* <li>5 = special key (tab, delete, return, escape, left, right)</li>
* <li>6 = return</li>
* </ul>
* @private
*/
var _getCommand = function(e) {
var c = Event.getCharCode(e);
//alert(Event.getCharCode(e) + ", " + e.keyCode + ", " + e.charCode);
// special keys
if (c === 38) { // up arrow
return 3;
} else if (c === 13) { // return
return 6;
} else if (c === 40) { // down array
return 4;
} else if (c >= 48 && c<=57) { // 0-9
return 1;
} else if (c >= 97 && c<=102) { // a-f
return 2;
} else if (c >= 65 && c<=70) { // A-F
return 2;
//} else if ("8, 9, 13, 27, 37, 39".indexOf(c) > -1 ||
// (c >= 112 && c <=123)) { // including F-keys
// tab, delete, return, escape, left, right
return 5;
} else { // something we probably don't want
return 0;
}
};
/**
* Handle keypress on one of the rgb or hsv fields.
* @method _rgbFieldKeypress
* @param e {Event} the keypress event
* @param el {HTMLElement} the field
* @param prop {string} the key to the linked property
* @private
*/
var command = _getCommand(e);
switch (command) {
case 6: // return, update the value
break;
case 3: // up arrow, increment
_updateFormFields.call(this);
//Event.stopEvent(e);
break;
case 4: // down arrow, decrement
_updateFormFields.call(this);
//Event.stopEvent(e);
break;
default:
}
};
/**
* Handle keydown on the hex field
* @method _hexFieldKeypress
* @param e {Event} the keypress event
* @param el {HTMLElement} the field
* @param prop {string} the key to the linked property
* @private
*/
var command = _getCommand(e);
}
};
/**
* Use the value of the text field to update the control
* @method _hexFieldKeypress
* @param e {Event} an event
* @param el {HTMLElement} the field
* @param prop {string} the key to the linked property
* @private
*/
}
}
};
/**
* Allows numbers and special chars only. Used for the
* rgb and hsv fields keypress handler.
* @method _numbersOnly
* @param e {Event} the event
* @private
* @return {boolean} false if we are canceling the event
*/
var _numbersOnly = function(e) {
return _hexOnly(e, true);
};
/**
* Allows numbers and special chars, and by default allows a-f.
* Used for the hex field keypress handler.
* @method _hexOnly
* @param e {Event} the event
* @param numbersOnly omits a-f if set to true
* @private
* @return {boolean} false if we are canceling the event
*/
var command = _getCommand(e);
switch (command) {
case 6: // return
case 5: // special char
case 1: // number
break;
case 2: // hex char (a-f)
if (numbersOnly !== true) {
break;
}
// fallthrough is intentional
default: // prevent alpha and punctuation
return false;
}
};
/**
* Returns the element reference that is saved. The id can be either
* the element id, or the key for this id in the "id" config attribute.
* For instance, the host element id can be obtained by passing its
* id (default: "yui_picker") or by its key "YUI_PICKER".
* @param id {string} the element id, or key
* @return {HTMLElement} a reference to the element
*/
};
_createElements = function() {
if (o) {
lang.augmentObject(n, o, true);
}
return n;
},
//type: "txt",
autocomplete: "off",
value: "0",
size: 3,
maxlength: 3
}, obj);
};
var p = this.get("element");
// Picker slider (S and V) ---------------------------------------------
className: "yui-picker-bg",
tabIndex: -1,
hideFocus: true
});
className: "yui-picker-thumb"
});
});
p.appendChild(el);
// Hue slider ---------------------------------------------
className: "yui-picker-hue-bg",
tabIndex: -1,
hideFocus: true
});
className: "yui-picker-hue-thumb"
});
});
p.appendChild(el);
// controls ---------------------------------------------
className: "yui-picker-controls"
});
p.appendChild(el);
p = el;
// controls header
className: "hd"
});
//className: "yui-picker-controls-label",
href: "#"
});
p.appendChild(el);
// bd
className: "bd"
});
p.appendChild(el);
p = el;
// rgb
className: "yui-picker-rgb-controls"
});
className: "yui-picker-r"
});
className: "yui-picker-g"
});
className: "yui-picker-b"
});
p.appendChild(el);
// hsv
className: "yui-picker-hsv-controls"
});
className: "yui-picker-h"
});
className: "yui-picker-s"
});
className: "yui-picker-v"
});
p.appendChild(el);
// hex summary
className: "yui-picker-hex_summary"
});
});
});
});
p.appendChild(el);
// hex field
className: "yui-picker-hex-controls"
});
className: "yui-picker-hex",
size: 6,
maxlength: 6
});
p.appendChild(el);
p = this.get("element");
// swatch
className: "yui-picker-swatch"
});
p.appendChild(el);
// websafe swatch
className: "yui-picker-websafe-swatch"
});
p.appendChild(el);
};
/**
* Sets the initial state of the sliders
* @method initPicker
*/
proto.initPicker = function () {
// bind all of our elements
var o=this.OPT,
// Add the default value as a key for each element for easier lookup
for (i in this.ID) {
}
}
// Check for picker element, if not there, create all of them
if (!el) {
_createElements.call(this);
} else {
}
for (i in ids) {
// look for element
// generate an id if the implementer passed in an element reference,
// and the element did not have an id already
// update the id in case we generated the id
// store the dom ref
}
}
// set the initial visibility state of our controls
els = [o.SHOW_CONTROLS,
];
}
var s = this.get(o.PICKER_SIZE);
//_onHueSliderChange.call(this, 0);
//_updateSliders
}, this, true);
Event.preventDefault(e);
}, this, true);
}, this);
}, this);
};
}, this);
}, this);
};
/**
* Sets up the config attributes and the change listeners for this
* properties
* @method initAttributes
* @param attr An object containing default attribute values
*/
/**
* The size of the picker. Trying to change this is not recommended.
* @config pickersize
* @default 180
* @type int
*/
});
/**
* The current hue value 0-360
* @config hue
* @type int
*/
});
/**
* The current saturation value 0-100
* @config saturation
* @type int
*/
});
/**
* The current value/brightness value 0-100
* @config value
* @type int
*/
});
/**
* The current red value 0-255
* @config red
* @type int
*/
});
/**
* The current green value 0-255
* @config green
* @type int
*/
});
/**
* The current blue value 0-255
* @config blue
* @type int
*/
});
/**
* The current hex value #000000-#FFFFFF, without the #
* @config hex
* @type string
*/
});
/**
* The current rgb value. Updates the state of all of the
* other value fields. Read-only: use setValue to set the
* controls rgb value.
* @config hex
* @type [int, int, int]
* @readonly
*/
}));
},
readonly: true
});
/**
* If the color picker will live inside of a container object,
* set, provide a reference to it so the control can use the
* container's events.
* @config container
* @type YAHOO.widget.Panel
*/
value: null,
if (container) {
// Position can get out of sync when the
// control is manipulated while display is
// none. Resetting the slider constraints
// when it is visible gets the state back in
// order.
// this.pickerSlider.thumb.resetConstraints();
// this.hueSlider.thumb.resetConstraints();
this.pickerSlider.focus();
}, this, true);
}
}
});
/**
* The closest current websafe value
* @config websafe
* @type int
*/
});
for (var i in ids) {
}
}
}
/**
* control. The default is the this.ID list, and can be customized
* by passing a list in the contructor
* @config ids
* @type {referenceid: realid}
* @writeonce
*/
writeonce: true
});
/**
* A list of txt strings for internationalization. Default
* is this.TXT
* @config txt
* @type {key: txt}
* @writeonce
*/
writeonce: true
});
/**
* The img src default list
* is this.IMAGES
* @config images
* @type {key: image}
* @writeonce
*/
writeonce: true
});
/**
* The element refs used by this control. Set at initialization
* @config elements
* @type {id: HTMLElement}
* @readonly
*/
value: {},
readonly: true
});
/**
* Returns the cached element reference. If the id is not a string, it
* is assumed that it is an element and this is returned.
* @param id {string|HTMLElement} the element key, id, or ref
* @param on {boolean} hide or show. If true, show
* @private */
//Dom.setStyle(id, "visibility", (on) ? "" : "hidden");
};
/**
* @config showcontrols
* @type boolean
* @default true
*/
}
});
/**
* @config showrgbcontrols
* @type boolean
* @default true
*/
//Dom.setStyle(this.getElement(this.ID.RBG_CONTROLS), "visibility", (on) ? "" : "hidden");
}
});
/**
* @config showhsvcontrols
* @type boolean
* @default false
*/
//Dom.setStyle(this.getElement(this.ID.HSV_CONTROLS), "visibility", (on) ? "" : "hidden");
// can't show both the hsv controls and the rbg hex summary
}
}
});
/**
* @config showhexcontrols
* @type boolean
* @default true
*/
}
});
/**
* @config showwebsafe
* @type boolean
* @default true
*/
}
});
/**
* @config showhexsummary
* @type boolean
* @default true
*/
// can't show both the hsv controls and the rbg hex summary
}
}
});
}
});
this.initPicker();
};
/**
* Updates the rgb attribute with the current state of the r,g,b
* fields. This is invoked from change listeners on these
* attributes to facilitate updating these values from the
* individual form fields
* @method _updateRGB
* @private
*/
var _updateRGB = function() {
_updateSliders.call(this);
};
/**
* Updates the RGB values from the current state of the HSV
* values. Executed when the one of the HSV form fields are
* updated
* _updateRGBFromHSV
* @private
*/
var _updateRGBFromHSV = function() {
_updateSliders.call(this);
};
/**
* Parses the hex string to normalize shorthand values, converts
* the hex value to rgb and updates the rgb attribute (which
* updates the state for all of the other values)
* method _updateHex
* @private
*/
var _updateHex = function() {
// support #369 -> #336699 shorthand
if (l === 3) {
for (i=0; i<l; i=i+1) {
c[i] = c[i] + c[i];
}
}
return false;
}
}));
//_updateSliders.call(this);
};
/**
* Moves the sliders into the position dictated by the current state
* of the control
* @method _updateSliders
* @private
*/
var _updateSliders = function() {
_updateHueSlider.call(this);
_updatePickerSlider.call(this);
};
/**
* Moves the hue slider into the position dictated by the current state
* of the control
* @method _updateHueSlider
* @private
*/
var _updateHueSlider = function() {
// 0 is at the top and bottom of the hue slider. Always go to
// the top so we don't end up sending the thumb to the bottom
// when the value didn't actually change (e.g., a conversion
// produced 360 instead of 0 and the value was already 0).
if (h === size) {
h = 0;
}
};
/**
* Moves the picker slider into the position dictated by the current state
* of the control
* @method _updatePickerSlider
* @private
*/
var _updatePickerSlider = function() {
this.pickerSlider.setRegionValue(s, v);
};
/**
* Creates the host element if it doesn't exist
* @method _createHostElement
* @private
*/
var _createHostElement = function() {
}
return el;
};
})();