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