📄 colorpicker.js
字号:
/*Copyright (c) 2008, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.6.0*//** * Provides color conversion and validation utils * @class YAHOO.util.Color * @namespace YAHOO.util */YAHOO.util.Color = function() { var HCHARS="0123456789ABCDEF", lang=YAHOO.lang; 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) { return Math.min(255, Math.round(n*256)); }, /** * 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) { if (lang.isArray(h)) { return this.hsv2rgb.call(this, h[0], h[1], h[2]); } var r, g, b, i, f, p, q, t; i = Math.floor((h/60)%6); 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; } var fn=this.real2dec; return [fn(r), fn(g), fn(b)]; }, /** * 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) { if (lang.isArray(r)) { return this.rgb2hsv.call(this, r[0], r[1], r[2]); } r=r/255; g=g/255; b=b/255; var min,max,delta,h,s,v; min = Math.min(Math.min(r,g),b); max = Math.max(Math.max(r,g),b); delta = max-min; switch (max) { case min: h=0; break; case r: h=60*(g-b)/delta; if (g<b) { h+=360; } break; case g: h=(60*(b-r)/delta)+120; break; case b: h=(60*(r-g)/delta)+240; break; } s = (max === 0) ? 0 : 1-(min/max); var hsv = [Math.round(h), s, max]; 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) { if (lang.isArray(r)) { return this.rgb2hex.call(this, r[0], r[1], r[2]); } 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 = (lang.isNumber(n)) ? n : 0; n = (n > 255 || n < 0) ? 0 : n; return HCHARS.charAt((n - n % 16) / 16) + HCHARS.charAt(n % 16); }, /** * 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 */ hex2dec: function(str) { var f = function(c) { return HCHARS.indexOf(c.toUpperCase()); }; var s=str.split(''); 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; return [f(s.substr(0, 2)), f(s.substr(2, 2)), f(s.substr(4, 2))]; }, /** * 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) { if (lang.isArray(r)) { return this.websafe.call(this, r[0], r[1], r[2]); } // returns the closest match [0, 51, 102, 153, 204, 255] var f = function(v) { if (lang.isNumber(v)) { v = Math.min(Math.max(0, v), 255); 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 */ /** * Creates the host element if it doesn't exist * @method _createHostElement * @private */ var _createHostElement = function() { var el = document.createElement('div'); if (this.CSS.BASE) { el.className = this.CSS.BASE; } return el; }; /** * 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. */ YAHOO.widget.ColorPicker = function(el, attr) { _pickercount = _pickercount + 1; attr = attr || {}; if (arguments.length === 1 && !YAHOO.lang.isString(el) && !el.nodeName) { attr = el; // treat first arg as attr object el = attr.element || null; } if (!el && !attr.element) { // create if we dont have one el = _createHostElement.call(this, attr); } YAHOO.widget.ColorPicker.superclass.constructor.call(this, el, attr); }; YAHOO.extend(YAHOO.widget.ColorPicker, YAHOO.util.Element); var proto = YAHOO.widget.ColorPicker.prototype, Slider=YAHOO.widget.Slider, Color=YAHOO.util.Color, Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, lang = YAHOO.lang, sub = lang.substitute; var b = "yui-picker"; /** * The element ids used by this control * @property ID * @final */ proto.ID = { /** * 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -