sync
[bootswatch] / AdminLTE-master / js / plugins / colorpicker / bootstrap-colorpicker.js
1 /*!
2  * Bootstrap Colorpicker
3  * http://mjolnic.github.io/bootstrap-colorpicker/
4  *
5  * Originally written by (c) 2012 Stefan Petre
6  * Licensed under the Apache License v2.0
7  * http://www.apache.org/licenses/LICENSE-2.0.txt
8  *
9  * @todo Update DOCS
10  */
11 (function($) {
12     'use strict';
13
14     // Color object
15     var Color = function(val) {
16         this.value = {
17             h: 0,
18             s: 0,
19             b: 0,
20             a: 1
21         };
22         this.origFormat = null; // original string format
23         if (val) {
24             if (val.toLowerCase !== undefined) {
25                 this.setColor(val);
26             } else if (val.h !== undefined) {
27                 this.value = val;
28             }
29         }
30     };
31
32     Color.prototype = {
33         constructor: Color,
34         _sanitizeNumber: function(val) {
35             if (typeof val === 'number') {
36                 return val;
37             }
38             if (isNaN(val) || (val === null) || (val === '') || (val === undefined)) {
39                 return 1;
40             }
41             if (val.toLowerCase !== undefined) {
42                 return parseFloat(val);
43             }
44             return 1;
45         },
46         //parse a string to HSB
47         setColor: function(strVal) {
48             strVal = strVal.toLowerCase();
49             this.value = this.stringToHSB(strVal) ||  {
50                 h: 0,
51                 s: 0,
52                 b: 0,
53                 a: 1
54             };
55         },
56         stringToHSB: function(strVal) {
57             strVal = strVal.toLowerCase();
58             var that = this,
59                 result = false;
60             $.each(this.stringParsers, function(i, parser) {
61                 var match = parser.re.exec(strVal),
62                     values = match && parser.parse.apply(that, [match]),
63                     format = parser.format || 'rgba';
64                 if (values) {
65                     if (format.match(/hsla?/)) {
66                         result = that.RGBtoHSB.apply(that, that.HSLtoRGB.apply(that, values));
67                     } else {
68                         result = that.RGBtoHSB.apply(that, values);
69                     }
70                     that.origFormat = format;
71                     return false;
72                 }
73                 return true;
74             });
75             return result;
76         },
77         setHue: function(h) {
78             this.value.h = 1 - h;
79         },
80         setSaturation: function(s) {
81             this.value.s = s;
82         },
83         setBrightness: function(b) {
84             this.value.b = 1 - b;
85         },
86         setAlpha: function(a) {
87             this.value.a = parseInt((1 - a) * 100, 10) / 100;
88         },
89         toRGB: function(h, s, v, a) {
90             h = h || this.value.h;
91             s = s || this.value.s;
92             v = v || this.value.b;
93             a = a || this.value.a;
94
95             var r, g, b, i, f, p, q, t;
96             if (h && s === undefined && v === undefined) {
97                 s = h.s, v = h.v, h = h.h;
98             }
99             i = Math.floor(h * 6);
100             f = h * 6 - i;
101             p = v * (1 - s);
102             q = v * (1 - f * s);
103             t = v * (1 - (1 - f) * s);
104             switch (i % 6) {
105                 case 0:
106                     r = v, g = t, b = p;
107                     break;
108                 case 1:
109                     r = q, g = v, b = p;
110                     break;
111                 case 2:
112                     r = p, g = v, b = t;
113                     break;
114                 case 3:
115                     r = p, g = q, b = v;
116                     break;
117                 case 4:
118                     r = t, g = p, b = v;
119                     break;
120                 case 5:
121                     r = v, g = p, b = q;
122                     break;
123             }
124             return {
125                 r: Math.floor(r * 255),
126                 g: Math.floor(g * 255),
127                 b: Math.floor(b * 255),
128                 a: a
129             };
130         },
131         toHex: function(h, s, b, a) {
132             var rgb = this.toRGB(h, s, b, a);
133             return '#' + ((1 << 24) | (parseInt(rgb.r) << 16) | (parseInt(rgb.g) << 8) | parseInt(rgb.b)).toString(16).substr(1);
134         },
135         toHSL: function(h, s, b, a) {
136             h = h || this.value.h;
137             s = s || this.value.s;
138             b = b || this.value.b;
139             a = a || this.value.a;
140
141             var H = h,
142                 L = (2 - s) * b,
143                 S = s * b;
144             if (L > 0 && L <= 1) {
145                 S /= L;
146             } else {
147                 S /= 2 - L;
148             }
149             L /= 2;
150             if (S > 1) {
151                 S = 1;
152             }
153             return {
154                 h: H,
155                 s: S,
156                 l: L,
157                 a: a
158             };
159         },
160         RGBtoHSB: function(r, g, b, a) {
161             r /= 255;
162             g /= 255;
163             b /= 255;
164
165             var H, S, V, C;
166             V = Math.max(r, g, b);
167             C = V - Math.min(r, g, b);
168             H = (C === 0 ? null :
169                 V === r ? (g - b) / C :
170                 V === g ? (b - r) / C + 2 :
171                 (r - g) / C + 4
172             );
173             H = ((H + 360) % 6) * 60 / 360;
174             S = C === 0 ? 0 : C / V;
175             return {
176                 h: this._sanitizeNumber(H),
177                 s: S,
178                 b: V,
179                 a: this._sanitizeNumber(a)
180             };
181         },
182         HueToRGB: function(p, q, h) {
183             if (h < 0) {
184                 h += 1;
185             } else if (h > 1) {
186                 h -= 1;
187             }
188             if ((h * 6) < 1) {
189                 return p + (q - p) * h * 6;
190             } else if ((h * 2) < 1) {
191                 return q;
192             } else if ((h * 3) < 2) {
193                 return p + (q - p) * ((2 / 3) - h) * 6;
194             } else {
195                 return p;
196             }
197         },
198         HSLtoRGB: function(h, s, l, a) {
199             if (s < 0) {
200                 s = 0;
201             }
202             var q;
203             if (l <= 0.5) {
204                 q = l * (1 + s);
205             } else {
206                 q = l + s - (l * s);
207             }
208
209             var p = 2 * l - q;
210
211             var tr = h + (1 / 3);
212             var tg = h;
213             var tb = h - (1 / 3);
214
215             var r = Math.round(this.HueToRGB(p, q, tr) * 255);
216             var g = Math.round(this.HueToRGB(p, q, tg) * 255);
217             var b = Math.round(this.HueToRGB(p, q, tb) * 255);
218             return [r, g, b, this._sanitizeNumber(a)];
219         },
220         toString: function(format) {
221             format = format ||  'rgba';
222             switch (format) {
223                 case 'rgb':
224                     {
225                         var rgb = this.toRGB();
226                         return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
227                     }
228                     break;
229                 case 'rgba':
230                     {
231                         var rgb = this.toRGB();
232                         return 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + rgb.a + ')';
233                     }
234                     break;
235                 case 'hsl':
236                     {
237                         var hsl = this.toHSL();
238                         return 'hsl(' + Math.round(hsl.h * 360) + ',' + Math.round(hsl.s * 100) + '%,' + Math.round(hsl.l * 100) + '%)';
239                     }
240                     break;
241                 case 'hsla':
242                     {
243                         var hsl = this.toHSL();
244                         return 'hsla(' + Math.round(hsl.h * 360) + ',' + Math.round(hsl.s * 100) + '%,' + Math.round(hsl.l * 100) + '%,' + hsl.a + ')';
245                     }
246                     break;
247                 case 'hex':
248                     {
249                         return this.toHex();
250                     }
251                     break;
252                 default:
253                     {
254                         return false;
255                     }
256                     break;
257             }
258         },
259         // a set of RE's that can match strings and generate color tuples.
260         // from John Resig color plugin
261         // https://github.com/jquery/jquery-color/
262         stringParsers: [{
263             re: /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
264             format: 'hex',
265             parse: function(execResult) {
266                 return [
267                     parseInt(execResult[1], 16),
268                     parseInt(execResult[2], 16),
269                     parseInt(execResult[3], 16),
270                     1
271                 ];
272             }
273         }, {
274             re: /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,
275             format: 'hex',
276             parse: function(execResult) {
277                 return [
278                     parseInt(execResult[1] + execResult[1], 16),
279                     parseInt(execResult[2] + execResult[2], 16),
280                     parseInt(execResult[3] + execResult[3], 16),
281                     1
282                 ];
283             }
284         }, {
285             re: /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*?\)/,
286             format: 'rgb',
287             parse: function(execResult) {
288                 return [
289                     execResult[1],
290                     execResult[2],
291                     execResult[3],
292                     1
293                 ];
294             }
295         }, {
296             re: /rgb\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
297             format: 'rgb',
298             parse: function(execResult) {
299                 return [
300                     2.55 * execResult[1],
301                     2.55 * execResult[2],
302                     2.55 * execResult[3],
303                     1
304                 ];
305             }
306         }, {
307             re: /rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
308             format: 'rgba',
309             parse: function(execResult) {
310                 return [
311                     execResult[1],
312                     execResult[2],
313                     execResult[3],
314                     execResult[4]
315                 ];
316             }
317         }, {
318             re: /rgba\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
319             format: 'rgba',
320             parse: function(execResult) {
321                 return [
322                     2.55 * execResult[1],
323                     2.55 * execResult[2],
324                     2.55 * execResult[3],
325                     execResult[4]
326                 ];
327             }
328         }, {
329             re: /hsl\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
330             format: 'hsl',
331             parse: function(execResult) {
332                 return [
333                     execResult[1] / 360,
334                     execResult[2] / 100,
335                     execResult[3] / 100,
336                     execResult[4]
337                 ];
338             }
339         }, {
340             re: /hsla\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
341             format: 'hsla',
342             parse: function(execResult) {
343                 return [
344                     execResult[1] / 360,
345                     execResult[2] / 100,
346                     execResult[3] / 100,
347                     execResult[4]
348                 ];
349             }
350         }, {
351             //predefined color name
352             re: /^([a-z]{3,})$/,
353             format: 'alias',
354             parse: function(execResult) {
355                 var hexval = this.colorNameToHex(execResult[0]) ||  '#000000';
356                 var match = this.stringParsers[0].re.exec(hexval),
357                     values = match && this.stringParsers[0].parse.apply(this, [match]);
358                 return values;
359             }
360         }],
361         colorNameToHex: function(name) {
362             // 140 predefined colors from the HTML Colors spec
363             var colors = {
364                 "aliceblue": "#f0f8ff",
365                 "antiquewhite": "#faebd7",
366                 "aqua": "#00ffff",
367                 "aquamarine": "#7fffd4",
368                 "azure": "#f0ffff",
369                 "beige": "#f5f5dc",
370                 "bisque": "#ffe4c4",
371                 "black": "#000000",
372                 "blanchedalmond": "#ffebcd",
373                 "blue": "#0000ff",
374                 "blueviolet": "#8a2be2",
375                 "brown": "#a52a2a",
376                 "burlywood": "#deb887",
377                 "cadetblue": "#5f9ea0",
378                 "chartreuse": "#7fff00",
379                 "chocolate": "#d2691e",
380                 "coral": "#ff7f50",
381                 "cornflowerblue": "#6495ed",
382                 "cornsilk": "#fff8dc",
383                 "crimson": "#dc143c",
384                 "cyan": "#00ffff",
385                 "darkblue": "#00008b",
386                 "darkcyan": "#008b8b",
387                 "darkgoldenrod": "#b8860b",
388                 "darkgray": "#a9a9a9",
389                 "darkgreen": "#006400",
390                 "darkkhaki": "#bdb76b",
391                 "darkmagenta": "#8b008b",
392                 "darkolivegreen": "#556b2f",
393                 "darkorange": "#ff8c00",
394                 "darkorchid": "#9932cc",
395                 "darkred": "#8b0000",
396                 "darksalmon": "#e9967a",
397                 "darkseagreen": "#8fbc8f",
398                 "darkslateblue": "#483d8b",
399                 "darkslategray": "#2f4f4f",
400                 "darkturquoise": "#00ced1",
401                 "darkviolet": "#9400d3",
402                 "deeppink": "#ff1493",
403                 "deepskyblue": "#00bfff",
404                 "dimgray": "#696969",
405                 "dodgerblue": "#1e90ff",
406                 "firebrick": "#b22222",
407                 "floralwhite": "#fffaf0",
408                 "forestgreen": "#228b22",
409                 "fuchsia": "#ff00ff",
410                 "gainsboro": "#dcdcdc",
411                 "ghostwhite": "#f8f8ff",
412                 "gold": "#ffd700",
413                 "goldenrod": "#daa520",
414                 "gray": "#808080",
415                 "green": "#008000",
416                 "greenyellow": "#adff2f",
417                 "honeydew": "#f0fff0",
418                 "hotpink": "#ff69b4",
419                 "indianred ": "#cd5c5c",
420                 "indigo ": "#4b0082",
421                 "ivory": "#fffff0",
422                 "khaki": "#f0e68c",
423                 "lavender": "#e6e6fa",
424                 "lavenderblush": "#fff0f5",
425                 "lawngreen": "#7cfc00",
426                 "lemonchiffon": "#fffacd",
427                 "lightblue": "#add8e6",
428                 "lightcoral": "#f08080",
429                 "lightcyan": "#e0ffff",
430                 "lightgoldenrodyellow": "#fafad2",
431                 "lightgrey": "#d3d3d3",
432                 "lightgreen": "#90ee90",
433                 "lightpink": "#ffb6c1",
434                 "lightsalmon": "#ffa07a",
435                 "lightseagreen": "#20b2aa",
436                 "lightskyblue": "#87cefa",
437                 "lightslategray": "#778899",
438                 "lightsteelblue": "#b0c4de",
439                 "lightyellow": "#ffffe0",
440                 "lime": "#00ff00",
441                 "limegreen": "#32cd32",
442                 "linen": "#faf0e6",
443                 "magenta": "#ff00ff",
444                 "maroon": "#800000",
445                 "mediumaquamarine": "#66cdaa",
446                 "mediumblue": "#0000cd",
447                 "mediumorchid": "#ba55d3",
448                 "mediumpurple": "#9370d8",
449                 "mediumseagreen": "#3cb371",
450                 "mediumslateblue": "#7b68ee",
451                 "mediumspringgreen": "#00fa9a",
452                 "mediumturquoise": "#48d1cc",
453                 "mediumvioletred": "#c71585",
454                 "midnightblue": "#191970",
455                 "mintcream": "#f5fffa",
456                 "mistyrose": "#ffe4e1",
457                 "moccasin": "#ffe4b5",
458                 "navajowhite": "#ffdead",
459                 "navy": "#000080",
460                 "oldlace": "#fdf5e6",
461                 "olive": "#808000",
462                 "olivedrab": "#6b8e23",
463                 "orange": "#ffa500",
464                 "orangered": "#ff4500",
465                 "orchid": "#da70d6",
466                 "palegoldenrod": "#eee8aa",
467                 "palegreen": "#98fb98",
468                 "paleturquoise": "#afeeee",
469                 "palevioletred": "#d87093",
470                 "papayawhip": "#ffefd5",
471                 "peachpuff": "#ffdab9",
472                 "peru": "#cd853f",
473                 "pink": "#ffc0cb",
474                 "plum": "#dda0dd",
475                 "powderblue": "#b0e0e6",
476                 "purple": "#800080",
477                 "red": "#ff0000",
478                 "rosybrown": "#bc8f8f",
479                 "royalblue": "#4169e1",
480                 "saddlebrown": "#8b4513",
481                 "salmon": "#fa8072",
482                 "sandybrown": "#f4a460",
483                 "seagreen": "#2e8b57",
484                 "seashell": "#fff5ee",
485                 "sienna": "#a0522d",
486                 "silver": "#c0c0c0",
487                 "skyblue": "#87ceeb",
488                 "slateblue": "#6a5acd",
489                 "slategray": "#708090",
490                 "snow": "#fffafa",
491                 "springgreen": "#00ff7f",
492                 "steelblue": "#4682b4",
493                 "tan": "#d2b48c",
494                 "teal": "#008080",
495                 "thistle": "#d8bfd8",
496                 "tomato": "#ff6347",
497                 "turquoise": "#40e0d0",
498                 "violet": "#ee82ee",
499                 "wheat": "#f5deb3",
500                 "white": "#ffffff",
501                 "whitesmoke": "#f5f5f5",
502                 "yellow": "#ffff00",
503                 "yellowgreen": "#9acd32"
504             };
505
506             if (typeof colors[name.toLowerCase()] !== 'undefined') {
507                 return colors[name.toLowerCase()];
508             }
509             return false;
510         }
511     };
512
513
514     var defaults = {
515         horizontal: false, // horizontal mode layout ?
516         inline: false, //forces to show the colorpicker as an inline element
517         color: false, //forces a color
518         format: false, //forces a format
519         input: 'input', // children input selector
520         container: false, // container selector
521         component: '.add-on, .input-group-addon', // children component selector
522         sliders: {
523             saturation: {
524                 maxLeft: 100,
525                 maxTop: 100,
526                 callLeft: 'setSaturation',
527                 callTop: 'setBrightness'
528             },
529             hue: {
530                 maxLeft: 0,
531                 maxTop: 100,
532                 callLeft: false,
533                 callTop: 'setHue'
534             },
535             alpha: {
536                 maxLeft: 0,
537                 maxTop: 100,
538                 callLeft: false,
539                 callTop: 'setAlpha'
540             }
541         },
542         slidersHorz: {
543             saturation: {
544                 maxLeft: 100,
545                 maxTop: 100,
546                 callLeft: 'setSaturation',
547                 callTop: 'setBrightness'
548             },
549             hue: {
550                 maxLeft: 100,
551                 maxTop: 0,
552                 callLeft: 'setHue',
553                 callTop: false
554             },
555             alpha: {
556                 maxLeft: 100,
557                 maxTop: 0,
558                 callLeft: 'setAlpha',
559                 callTop: false
560             }
561         },
562         template: '<div class="colorpicker dropdown-menu">' +
563             '<div class="colorpicker-saturation"><i><b></b></i></div>' +
564             '<div class="colorpicker-hue"><i></i></div>' +
565             '<div class="colorpicker-alpha"><i></i></div>' +
566             '<div class="colorpicker-color"><div /></div>' +
567             '</div>'
568     };
569
570     var Colorpicker = function(element, options) {
571         this.element = $(element).addClass('colorpicker-element');
572         this.options = $.extend({}, defaults, this.element.data(), options);
573         this.component = this.options.component;
574         this.component = (this.component !== false) ? this.element.find(this.component) : false;
575         if (this.component && (this.component.length === 0)) {
576             this.component = false;
577         }
578         this.container = (this.options.container === true) ? this.element : this.options.container;
579         this.container = (this.container !== false) ? $(this.container) : false;
580
581         // Is the element an input? Should we search inside for any input?
582         this.input = this.element.is('input') ? this.element : (this.options.input ?
583             this.element.find(this.options.input) : false);
584         if (this.input && (this.input.length === 0)) {
585             this.input = false;
586         }
587         // Set HSB color
588         this.color = new Color(this.options.color !== false ? this.options.color : this.getValue());
589         this.format = this.options.format !== false ? this.options.format : this.color.origFormat;
590
591         // Setup picker
592         this.picker = $(this.options.template);
593         if (this.options.inline) {
594             this.picker.addClass('colorpicker-inline colorpicker-visible');
595         } else {
596             this.picker.addClass('colorpicker-hidden');
597         }
598         if (this.options.horizontal) {
599             this.picker.addClass('colorpicker-horizontal');
600         }
601         if (this.format === 'rgba' || this.format === 'hsla') {
602             this.picker.addClass('colorpicker-with-alpha');
603         }
604         this.picker.on('mousedown.colorpicker', $.proxy(this.mousedown, this));
605         this.picker.appendTo(this.container ? this.container : $('body'));
606
607         // Bind events
608         if (this.input !== false) {
609             this.input.on({
610                 'keyup.colorpicker': $.proxy(this.keyup, this)
611             });
612             if (this.component === false) {
613                 this.element.on({
614                     'focus.colorpicker': $.proxy(this.show, this)
615                 });
616             }
617             if (this.options.inline === false) {
618                 this.element.on({
619                     'focusout.colorpicker': $.proxy(this.hide, this)
620                 });
621             }
622         }
623
624         if (this.component !== false) {
625             this.component.on({
626                 'click.colorpicker': $.proxy(this.show, this)
627             });
628         }
629
630         if ((this.input === false) && (this.component === false)) {
631             this.element.on({
632                 'click.colorpicker': $.proxy(this.show, this)
633             });
634         }
635         this.update();
636
637         $($.proxy(function() {
638             this.element.trigger('create');
639         }, this));
640     };
641
642     Colorpicker.version = '2.0.0-beta';
643
644     Colorpicker.Color = Color;
645
646     Colorpicker.prototype = {
647         constructor: Colorpicker,
648         destroy: function() {
649             this.picker.remove();
650             this.element.removeData('colorpicker').off('.colorpicker');
651             if (this.input !== false) {
652                 this.input.off('.colorpicker');
653             }
654             if (this.component !== false) {
655                 this.component.off('.colorpicker');
656             }
657             this.element.removeClass('colorpicker-element');
658             this.element.trigger({
659                 type: 'destroy'
660             });
661         },
662         reposition: function() {
663             if (this.options.inline !== false) {
664                 return false;
665             }
666             var offset = this.component ? this.component.offset() : this.element.offset();
667             this.picker.css({
668                 top: offset.top + (this.component ? this.component.outerHeight() : this.element.outerHeight()),
669                 left: offset.left
670             });
671         },
672         show: function(e) {
673             if (this.isDisabled()) {
674                 return false;
675             }
676             this.picker.addClass('colorpicker-visible').removeClass('colorpicker-hidden');
677             this.reposition();
678             $(window).on('resize.colorpicker', $.proxy(this.reposition, this));
679             if (!this.hasInput() && e) {
680                 if (e.stopPropagation && e.preventDefault) {
681                     e.stopPropagation();
682                     e.preventDefault();
683                 }
684             }
685             if (this.options.inline === false) {
686                 $(window.document).on({
687                     'mousedown.colorpicker': $.proxy(this.hide, this)
688                 });
689             }
690             this.element.trigger({
691                 type: 'showPicker',
692                 color: this.color
693             });
694         },
695         hide: function() {
696             this.picker.addClass('colorpicker-hidden').removeClass('colorpicker-visible');
697             $(window).off('resize.colorpicker', this.reposition);
698             $(document).off({
699                 'mousedown.colorpicker': this.hide
700             });
701             this.update();
702             this.element.trigger({
703                 type: 'hidePicker',
704                 color: this.color
705             });
706         },
707         updateData: function(val) {
708             val = val ||  this.color.toString(this.format);
709             this.element.data('color', val);
710             return val;
711         },
712         updateInput: function(val) {
713             val = val ||  this.color.toString(this.format);
714             if (this.input !== false) {
715                 this.input.prop('value', val);
716             }
717             return val;
718         },
719         updatePicker: function(val) {
720             if (val !== undefined) {
721                 this.color = new Color(val);
722             }
723             var sl = (this.options.horizontal === false) ? this.options.sliders : this.options.slidersHorz;
724             var icns = this.picker.find('i');
725             if (icns.length === 0) {
726                 return;
727             }
728             if (this.options.horizontal === false) {
729                 sl = this.options.sliders;
730                 icns.eq(1).css('top', sl.hue.maxTop * (1 - this.color.value.h)).end()
731                     .eq(2).css('top', sl.alpha.maxTop * (1 - this.color.value.a));
732             } else {
733                 sl = this.options.slidersHorz;
734                 icns.eq(1).css('left', sl.hue.maxLeft * (1 - this.color.value.h)).end()
735                     .eq(2).css('left', sl.alpha.maxLeft * (1 - this.color.value.a));
736             }
737             icns.eq(0).css({
738                 'top': sl.saturation.maxTop - this.color.value.b * sl.saturation.maxTop,
739                 'left': this.color.value.s * sl.saturation.maxLeft
740             });
741             this.picker.find('.colorpicker-saturation').css('backgroundColor', this.color.toHex(this.color.value.h, 1, 1, 1));
742             this.picker.find('.colorpicker-alpha').css('backgroundColor', this.color.toHex());
743             this.picker.find('.colorpicker-color, .colorpicker-color div').css('backgroundColor', this.color.toString(this.format));
744             return val;
745         },
746         updateComponent: function(val) {
747             val = val ||  this.color.toString(this.format);
748             if (this.component !== false) {
749                 var icn = this.component.find('i').eq(0);
750                 if (icn.length > 0) {
751                     icn.css({
752                         'backgroundColor': val
753                     });
754                 } else {
755                     this.component.css({
756                         'backgroundColor': val
757                     });
758                 }
759             }
760             return val;
761         },
762         update: function(force) {
763             var val = this.updateComponent();
764             if ((this.getValue(false) !== false) || (force === true)) {
765                 // Update input/data only if the current value is not blank
766                 this.updateInput(val);
767                 this.updateData(val);
768             }
769             this.updatePicker();
770             return val;
771
772         },
773         setValue: function(val) { // set color manually
774             this.color = new Color(val);
775             this.update();
776             this.element.trigger({
777                 type: 'changeColor',
778                 color: this.color,
779                 value: val
780             });
781         },
782         getValue: function(defaultValue) {
783             defaultValue = (defaultValue === undefined) ? '#000000' : defaultValue;
784             var val;
785             if (this.hasInput()) {
786                 val = this.input.val();
787             } else {
788                 val = this.element.data('color');
789             }
790             if ((val === undefined) || (val === '') || (val === null)) {
791                 // if not defined or empty, return default
792                 val = defaultValue;
793             }
794             return val;
795         },
796         hasInput: function() {
797             return (this.input !== false);
798         },
799         isDisabled: function() {
800             if (this.hasInput()) {
801                 return (this.input.prop('disabled') === true);
802             }
803             return false;
804         },
805         disable: function() {
806             if (this.hasInput()) {
807                 this.input.prop('disabled', true);
808                 return true;
809             }
810             return false;
811         },
812         enable: function() {
813             if (this.hasInput()) {
814                 this.input.prop('disabled', false);
815                 return true;
816             }
817             return false;
818         },
819         currentSlider: null,
820         mousePointer: {
821             left: 0,
822             top: 0
823         },
824         mousedown: function(e) {
825             e.stopPropagation();
826             e.preventDefault();
827
828             var target = $(e.target);
829
830             //detect the slider and set the limits and callbacks
831             var zone = target.closest('div');
832             var sl = this.options.horizontal ? this.options.slidersHorz : this.options.sliders;
833             if (!zone.is('.colorpicker')) {
834                 if (zone.is('.colorpicker-saturation')) {
835                     this.currentSlider = $.extend({}, sl.saturation);
836                 } else if (zone.is('.colorpicker-hue')) {
837                     this.currentSlider = $.extend({}, sl.hue);
838                 } else if (zone.is('.colorpicker-alpha')) {
839                     this.currentSlider = $.extend({}, sl.alpha);
840                 } else {
841                     return false;
842                 }
843                 var offset = zone.offset();
844                 //reference to guide's style
845                 this.currentSlider.guide = zone.find('i')[0].style;
846                 this.currentSlider.left = e.pageX - offset.left;
847                 this.currentSlider.top = e.pageY - offset.top;
848                 this.mousePointer = {
849                     left: e.pageX,
850                     top: e.pageY
851                 };
852                 //trigger mousemove to move the guide to the current position
853                 $(document).on({
854                     'mousemove.colorpicker': $.proxy(this.mousemove, this),
855                     'mouseup.colorpicker': $.proxy(this.mouseup, this)
856                 }).trigger('mousemove');
857             }
858             return false;
859         },
860         mousemove: function(e) {
861             e.stopPropagation();
862             e.preventDefault();
863             var left = Math.max(
864                 0,
865                 Math.min(
866                     this.currentSlider.maxLeft,
867                     this.currentSlider.left + ((e.pageX || this.mousePointer.left) - this.mousePointer.left)
868                 )
869             );
870             var top = Math.max(
871                 0,
872                 Math.min(
873                     this.currentSlider.maxTop,
874                     this.currentSlider.top + ((e.pageY || this.mousePointer.top) - this.mousePointer.top)
875                 )
876             );
877             this.currentSlider.guide.left = left + 'px';
878             this.currentSlider.guide.top = top + 'px';
879             if (this.currentSlider.callLeft) {
880                 this.color[this.currentSlider.callLeft].call(this.color, left / 100);
881             }
882             if (this.currentSlider.callTop) {
883                 this.color[this.currentSlider.callTop].call(this.color, top / 100);
884             }
885             this.update(true);
886
887             this.element.trigger({
888                 type: 'changeColor',
889                 color: this.color
890             });
891             return false;
892         },
893         mouseup: function(e) {
894             e.stopPropagation();
895             e.preventDefault();
896             $(document).off({
897                 'mousemove.colorpicker': this.mousemove,
898                 'mouseup.colorpicker': this.mouseup
899             });
900             return false;
901         },
902         keyup: function(e) {
903             if ((e.keyCode === 38)) {
904                 if (this.color.value.a < 1) {
905                     this.color.value.a = Math.round((this.color.value.a + 0.01) * 100) / 100;
906                 }
907                 this.update(true);
908             } else if ((e.keyCode === 40)) {
909                 if (this.color.value.a > 0) {
910                     this.color.value.a = Math.round((this.color.value.a - 0.01) * 100) / 100;
911                 }
912                 this.update(true);
913             } else {
914                 var val = this.input.val();
915                 this.color = new Color(val);
916                 if (this.getValue(false) !== false) {
917                     this.updateData();
918                     this.updateComponent();
919                     this.updatePicker();
920                 }
921             }
922             this.element.trigger({
923                 type: 'changeColor',
924                 color: this.color,
925                 value: val
926             });
927         }
928     };
929
930     $.colorpicker = Colorpicker;
931
932     $.fn.colorpicker = function(option) {
933         return this.each(function() {
934             var $this = $(this),
935                 inst = $this.data('colorpicker'),
936                 options = ((typeof option === 'object') ? option : {});
937             if ((!inst) && (typeof option !== 'string')) {
938                 $this.data('colorpicker', new Colorpicker(this, options));
939             } else {
940                 if (typeof option === 'string') {
941                     inst[option].apply(inst, Array.prototype.slice.call(arguments, 1));
942                 }
943             }
944         });
945     };
946
947     $.fn.colorpicker.constructor = Colorpicker;
948
949 })(window.jQuery);