📄 colorpicker-debug.js
字号:
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 */ proto.TXT = { 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 */ proto.IMAGE = { 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 */ proto.DEFAULT = { PICKER_SIZE: 180 }; /** * Constants for the control's configuration attributes * @property OPT * @final */ proto.OPT = { 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" }; /** * Moves the hue slider into the position dictated by the current state * of the control * @method _updateHueSlider * @private */ var _updateHueSlider = function() { var size = this.get(this.OPT.PICKER_SIZE), h = this.get(this.OPT.HUE); h = size - Math.round(h / 360 * size); // 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; } this.logger.log("Hue slider is being set to " + h); this.hueSlider.setValue(h); }; /** * Moves the picker slider into the position dictated by the current state * of the control * @method _updatePickerSlider * @private */ var _updatePickerSlider = function() { var size = this.get(this.OPT.PICKER_SIZE), s = this.get(this.OPT.SATURATION), v = this.get(this.OPT.VALUE); s = Math.round(s * size / 100); v = Math.round(size - (v * size / 100)); this.logger.log("Setting picker slider to " + [s, v]); this.pickerSlider.setRegionValue(s, v); }; /** * 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); }; /** * 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 */ proto.setValue = function(rgb, silent) { silent = (silent) || false; this.set(this.OPT.RGB, rgb, silent); _updateSliders.call(this); }; /** * The hue slider * @property hueSlider * @type YAHOO.widget.Slider */ proto.hueSlider = null; /** * 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() { var size = this.get(this.OPT.PICKER_SIZE), h = (size - this.hueSlider.getValue()) / size; 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() { return this.pickerSlider.getXValue() / this.get(this.OPT.PICKER_SIZE); }; /** * 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() { var size = this.get(this.OPT.PICKER_SIZE); return (size - this.pickerSlider.getYValue()) / size; }; /** * 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() { var rgb = this.get(this.OPT.RGB), websafe = this.get(this.OPT.WEBSAFE), el = this.getElement(this.ID.SWATCH), color = rgb.join(","), txt = this.get(this.OPT.TXT); Dom.setStyle(el, "background-color", "rgb(" + color + ")"); el.title = lang.substitute(txt.CURRENT_COLOR, { "rgb": "#" + this.get(this.OPT.HEX) }); el = this.getElement(this.ID.WEBSAFE_SWATCH); color = websafe.join(","); Dom.setStyle(el, "background-color", "rgb(" + color + ")"); el.title = lang.substitute(txt.CLOSEST_WEBSAFE, { "rgb": "#" + Color.rgb2hex(websafe) }); }; /** * 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() { var h=_getH.call(this), s=_getS.call(this), v=_getV.call(this); YAHOO.log("hsv " + [h, s, v]); var rgb = Color.hsv2rgb(h, s, v); //var websafe = Color.websafe(rgb); //var hex = Color.rgb2hex(rgb[0], rgb[1], rgb[2]); this.set(this.OPT.RGB, rgb); }; /** * Updates the form field controls with the state data contained * in the control. * @method _updateFormFields * @private */ var _updateFormFields = function() { this.getElement(this.ID.H).value = this.get(this.OPT.HUE); this.getElement(this.ID.S).value = this.get(this.OPT.SATURATION); this.getElement(this.ID.V).value = this.get(this.OPT.VALUE); this.getElement(this.ID.R).value = this.get(this.OPT.RED); this.getElement(this.ID.R_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.RED)); this.getElement(this.ID.G).value = this.get(this.OPT.GREEN); this.getElement(this.ID.G_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.GREEN)); this.getElement(this.ID.B).value = this.get(this.OPT.BLUE); this.getElement(this.ID.B_HEX).innerHTML = Color.dec2hex(this.get(this.OPT.BLUE)); this.getElement(this.ID.HEX).value = this.get(this.OPT.HEX); }; /** * Event handler for the hue slider. * @method _onHueSliderChange * @param newOffset {int} pixels from the start position * @private */ var _onHueSliderChange = function(newOffset) { this.logger.log("hue update: " + newOffset , "warn"); var h = _getH.call(this); this.set(this.OPT.HUE, h, true); // set picker background to the hue var rgb = Color.hsv2rgb(h, 1, 1); var styleDef = "rgb(" + rgb.join(",") + ")"; Dom.setStyle(this.getElement(this.ID.PICKER_BG), "background-color", styleDef); if (this.hueSlider.valueChangeSource === this.hueSlider.SOURCE_UI_EVENT) { _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 */ var _onPickerSliderChange = function(newOffset) { this.logger.log(sub("picker update [{x}, {y}]", newOffset)); var s=_getS.call(this), v=_getV.call(this); this.set(this.OPT.SATURATION, Math.round(s*100), true); this.set(this.OPT.VALUE, Math.round(v*100), true); if (this.pickerSlider.valueChangeSource === this.pickerSlider.SOURCE_UI_EVENT) { _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 } else if ("8, 9, 13, 27, 37, 39".indexOf(c) > -1) { // special chars return 5; } else { // something we probably don't want return 0; } }; /** * 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 */ var _useFieldValue = function(e, el, prop) { var val = el.value; if (prop !== this.OPT.HEX) { val = parseInt(val, 10); } if (val !== this.get(prop)) { this.set(prop, val); } }; /** * 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 _rgbFieldKeypress = function(e, el, prop) { var command = _getCommand(e); var inc = (e.shiftKey) ? 10 : 1; switch (command) { case 6: // return, update the value _useFieldValue.apply(this, arguments); break; case 3: // up arrow, increment this.set(prop, Math.min(this.get(prop)+inc, 255)); _updateFormFields.call(this); //Event.stopEvent(e); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -