yh-hiprint 2.2.10 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1115 @@
1
+ import $ from "../jquery";
2
+ //
3
+ // jQuery MiniColors: A tiny color picker built on jQuery
4
+ //
5
+ // Developed by Cory LaViska for A Beautiful Site, LLC
6
+ //
7
+ // Licensed under the MIT license: http://opensource.org/licenses/MIT
8
+ //
9
+
10
+ // Defaults
11
+ $.minicolors = {
12
+ defaults: {
13
+ animationSpeed: 50,
14
+ animationEasing: 'swing',
15
+ change: null,
16
+ changeDelay: 0,
17
+ control: 'hue',
18
+ defaultValue: '',
19
+ format: 'hex',
20
+ hide: null,
21
+ hideSpeed: 100,
22
+ inline: false,
23
+ keywords: '',
24
+ letterCase: 'lowercase',
25
+ opacity: false,
26
+ position: 'bottom',
27
+ show: null,
28
+ showSpeed: 100,
29
+ theme: 'default',
30
+ swatches: []
31
+ }
32
+ };
33
+
34
+ // Public methods
35
+ $.extend($.fn, {
36
+ minicolors: function (method, data) {
37
+
38
+ switch (method) {
39
+ // Destroy the control
40
+ case 'destroy':
41
+ $(this).each(function () {
42
+ destroy($(this));
43
+ });
44
+ return $(this);
45
+
46
+ // Hide the color picker
47
+ case 'hide':
48
+ hide();
49
+ return $(this);
50
+
51
+ // Get/set opacity
52
+ case 'opacity':
53
+ // Getter
54
+ if (data === undefined) {
55
+ // Getter
56
+ return $(this).attr('data-opacity');
57
+ } else {
58
+ // Setter
59
+ $(this).each(function () {
60
+ updateFromInput($(this).attr('data-opacity', data));
61
+ });
62
+ }
63
+ return $(this);
64
+
65
+ // Get an RGB(A) object based on the current color/opacity
66
+ case 'rgbObject':
67
+ return rgbObject($(this), method === 'rgbaObject');
68
+
69
+ // Get an RGB(A) string based on the current color/opacity
70
+ case 'rgbString':
71
+ case 'rgbaString':
72
+ return rgbString($(this), method === 'rgbaString');
73
+
74
+ // Get/set settings on the fly
75
+ case 'settings':
76
+ if (data === undefined) {
77
+ return $(this).data('minicolors-settings');
78
+ } else {
79
+ // Setter
80
+ $(this).each(function () {
81
+ var settings = $(this).data('minicolors-settings') || {};
82
+ destroy($(this));
83
+ $(this).minicolors($.extend(true, settings, data));
84
+ });
85
+ }
86
+ return $(this);
87
+
88
+ // Show the color picker
89
+ case 'show':
90
+ show($(this).eq(0));
91
+ return $(this);
92
+
93
+ // Get/set the hex color value
94
+ case 'value':
95
+ if (data === undefined) {
96
+ // Getter
97
+ return $(this).val();
98
+ } else {
99
+ // Setter
100
+ $(this).each(function () {
101
+ if (typeof (data) === 'object' && data !== null) {
102
+ if (data.opacity !== undefined) {
103
+ $(this).attr('data-opacity', keepWithin(data.opacity, 0, 1));
104
+ }
105
+ if (data.color) {
106
+ $(this).val(data.color);
107
+ }
108
+ } else {
109
+ $(this).val(data);
110
+ }
111
+ updateFromInput($(this));
112
+ });
113
+ }
114
+ return $(this);
115
+
116
+ // Initializes the control
117
+ default:
118
+ if (method !== 'create') data = method;
119
+ $(this).each(function () {
120
+ init($(this), data);
121
+ });
122
+ return $(this);
123
+
124
+ }
125
+
126
+ }
127
+ });
128
+
129
+ // Initialize input elements
130
+ function init (input, settings) {
131
+ var minicolors = $('<div class="minicolors" />');
132
+ var defaults = $.minicolors.defaults;
133
+ var name;
134
+ var size;
135
+ var swatches;
136
+ var swatch;
137
+ var swatchString;
138
+ var panel;
139
+ var i;
140
+
141
+ // Do nothing if already initialized
142
+ if (input.data('minicolors-initialized')) return;
143
+
144
+ // Handle settings
145
+ settings = $.extend(true, {}, defaults, settings);
146
+
147
+ // The wrapper
148
+ minicolors
149
+ .addClass('minicolors-theme-' + settings.theme)
150
+ .toggleClass('minicolors-with-opacity', settings.opacity);
151
+
152
+ // Custom positioning
153
+ if (settings.position !== undefined) {
154
+ $.each(settings.position.split(' '), function () {
155
+ minicolors.addClass('minicolors-position-' + this);
156
+ });
157
+ }
158
+
159
+ // Input size
160
+ if (settings.format === 'rgb') {
161
+ size = settings.opacity ? '25' : '20';
162
+ } else {
163
+ size = settings.keywords ? '11' : '7';
164
+ }
165
+
166
+ // The input
167
+ input
168
+ .addClass('minicolors-input')
169
+ .data('minicolors-initialized', false)
170
+ .data('minicolors-settings', settings)
171
+ .prop('size', size)
172
+ .wrap(minicolors)
173
+ .after(
174
+ '<div class="minicolors-panel minicolors-slider-' + settings.control + '">' +
175
+ '<div class="minicolors-slider minicolors-sprite">' +
176
+ '<div class="minicolors-picker"></div>' +
177
+ '</div>' +
178
+ '<div class="minicolors-opacity-slider minicolors-sprite">' +
179
+ '<div class="minicolors-picker"></div>' +
180
+ '</div>' +
181
+ '<div class="minicolors-grid minicolors-sprite">' +
182
+ '<div class="minicolors-grid-inner"></div>' +
183
+ '<div class="minicolors-picker"><div></div></div>' +
184
+ '</div>' +
185
+ '</div>'
186
+ );
187
+
188
+ // The swatch
189
+ if (!settings.inline) {
190
+ input.after('<span class="minicolors-swatch minicolors-sprite minicolors-input-swatch"><span class="minicolors-swatch-color"></span></span>');
191
+ input.next('.minicolors-input-swatch').on('click', function (event) {
192
+ event.preventDefault();
193
+ input.trigger('focus');
194
+ });
195
+ }
196
+
197
+ // Prevent text selection in IE
198
+ panel = input.parent().find('.minicolors-panel');
199
+ panel.on('selectstart', function () { return false; }).end();
200
+
201
+ // Swatches
202
+ if (settings.swatches && settings.swatches.length !== 0) {
203
+ panel.addClass('minicolors-with-swatches');
204
+ swatches = $('<ul class="minicolors-swatches"></ul>')
205
+ .appendTo(panel);
206
+ for (i = 0; i < settings.swatches.length; ++i) {
207
+ // allow for custom objects as swatches
208
+ if (typeof settings.swatches[i] === 'object') {
209
+ name = settings.swatches[i].name;
210
+ swatch = settings.swatches[i].color;
211
+ } else {
212
+ name = '';
213
+ swatch = settings.swatches[i];
214
+ }
215
+ swatchString = swatch;
216
+ swatch = isRgb(swatch) ? parseRgb(swatch, true) : hex2rgb(parseHex(swatch, true));
217
+ $('<li class="minicolors-swatch minicolors-sprite"><span class="minicolors-swatch-color"></span></li>')
218
+ .attr("title", name)
219
+ .appendTo(swatches)
220
+ .data('swatch-color', swatchString)
221
+ .find('.minicolors-swatch-color')
222
+ .css({
223
+ backgroundColor: ((swatchString !== 'transparent') ? rgb2hex(swatch) : 'transparent'),
224
+ opacity: String(swatch.a)
225
+ });
226
+ settings.swatches[i] = swatch;
227
+ }
228
+ }
229
+
230
+ // Inline controls
231
+ if (settings.inline) input.parent().addClass('minicolors-inline');
232
+
233
+ updateFromInput(input, false);
234
+
235
+ input.data('minicolors-initialized', true);
236
+ }
237
+
238
+ // Returns the input back to its original state
239
+ function destroy (input) {
240
+ var minicolors = input.parent();
241
+
242
+ // Revert the input element
243
+ input
244
+ .removeData('minicolors-initialized')
245
+ .removeData('minicolors-settings')
246
+ .removeProp('size')
247
+ .removeClass('minicolors-input');
248
+
249
+ // Remove the wrap and destroy whatever remains
250
+ minicolors.before(input).remove();
251
+ }
252
+
253
+ // Shows the specified dropdown panel
254
+ function show (input) {
255
+ var minicolors = input.parent();
256
+ var panel = minicolors.find('.minicolors-panel');
257
+ var settings = input.data('minicolors-settings');
258
+
259
+ // Do nothing if uninitialized, disabled, inline, or already open
260
+ if (
261
+ !input.data('minicolors-initialized') ||
262
+ input.prop('disabled') ||
263
+ minicolors.hasClass('minicolors-inline') ||
264
+ minicolors.hasClass('minicolors-focus')
265
+ ) return;
266
+
267
+ hide();
268
+
269
+ minicolors.addClass('minicolors-focus');
270
+ if (panel.animate) {
271
+ panel
272
+ .stop(true, true)
273
+ .fadeIn(settings.showSpeed, function () {
274
+ if (settings.show) settings.show.call(input.get(0));
275
+ });
276
+ } else {
277
+ panel.show();
278
+ if (settings.show) settings.show.call(input.get(0));
279
+ }
280
+ }
281
+
282
+ // Hides all dropdown panels
283
+ function hide () {
284
+ $('.minicolors-focus').each(function () {
285
+ var minicolors = $(this);
286
+ var input = minicolors.find('.minicolors-input');
287
+ var panel = minicolors.find('.minicolors-panel');
288
+ var settings = input.data('minicolors-settings');
289
+
290
+ if (panel.animate) {
291
+ panel.fadeOut(settings.hideSpeed, function () {
292
+ if (settings.hide) settings.hide.call(input.get(0));
293
+ minicolors.removeClass('minicolors-focus');
294
+ });
295
+ } else {
296
+ panel.hide();
297
+ if (settings.hide) settings.hide.call(input.get(0));
298
+ minicolors.removeClass('minicolors-focus');
299
+ }
300
+ });
301
+ }
302
+
303
+ // Moves the selected picker
304
+ function move (target, event, animate) {
305
+ var input = target.parents('.minicolors').find('.minicolors-input');
306
+ var settings = input.data('minicolors-settings');
307
+ var picker = target.find('[class$=-picker]');
308
+ var offsetX = target.offset().left;
309
+ var offsetY = target.offset().top;
310
+ var x = Math.round(event.pageX - offsetX);
311
+ var y = Math.round(event.pageY - offsetY);
312
+ var duration = animate ? settings.animationSpeed : 0;
313
+ var wx, wy, r, phi, styles;
314
+
315
+ // Touch support
316
+ if (event.originalEvent.changedTouches) {
317
+ x = event.originalEvent.changedTouches[0].pageX - offsetX;
318
+ y = event.originalEvent.changedTouches[0].pageY - offsetY;
319
+ }
320
+
321
+ // Constrain picker to its container
322
+ if (x < 0) x = 0;
323
+ if (y < 0) y = 0;
324
+ if (x > target.width()) x = target.width();
325
+ if (y > target.height()) y = target.height();
326
+
327
+ // Constrain color wheel values to the wheel
328
+ if (target.parent().is('.minicolors-slider-wheel') && picker.parent().is('.minicolors-grid')) {
329
+ wx = 75 - x;
330
+ wy = 75 - y;
331
+ r = Math.sqrt(wx * wx + wy * wy);
332
+ phi = Math.atan2(wy, wx);
333
+ if (phi < 0) phi += Math.PI * 2;
334
+ if (r > 75) {
335
+ r = 75;
336
+ x = 75 - (75 * Math.cos(phi));
337
+ y = 75 - (75 * Math.sin(phi));
338
+ }
339
+ x = Math.round(x);
340
+ y = Math.round(y);
341
+ }
342
+
343
+ // Move the picker
344
+ styles = {
345
+ top: y + 'px'
346
+ };
347
+ if (target.is('.minicolors-grid')) {
348
+ styles.left = x + 'px';
349
+ }
350
+ if (picker.animate) {
351
+ picker
352
+ .stop(true)
353
+ .animate(styles, duration, settings.animationEasing, function () {
354
+ updateFromControl(input, target);
355
+ });
356
+ } else {
357
+ picker
358
+ .css(styles);
359
+ updateFromControl(input, target);
360
+ }
361
+ }
362
+
363
+ // Sets the input based on the color picker values
364
+ function updateFromControl (input, target) {
365
+
366
+ function getCoords (picker, container) {
367
+ var left, top;
368
+ if (!picker.length || !container) return null;
369
+ left = picker.offset().left;
370
+ top = picker.offset().top;
371
+
372
+ return {
373
+ x: left - container.offset().left + (picker.outerWidth() / 2),
374
+ y: top - container.offset().top + (picker.outerHeight() / 2)
375
+ };
376
+ }
377
+
378
+ var hue, saturation, brightness, x, y, r, phi;
379
+ var hex = input.val();
380
+ var opacity = input.attr('data-opacity');
381
+
382
+ // Helpful references
383
+ var minicolors = input.parent();
384
+ var settings = input.data('minicolors-settings');
385
+ var swatch = minicolors.find('.minicolors-input-swatch');
386
+
387
+ // Panel objects
388
+ var grid = minicolors.find('.minicolors-grid');
389
+ var slider = minicolors.find('.minicolors-slider');
390
+ var opacitySlider = minicolors.find('.minicolors-opacity-slider');
391
+
392
+ // Picker objects
393
+ var gridPicker = grid.find('[class$=-picker]');
394
+ var sliderPicker = slider.find('[class$=-picker]');
395
+ var opacityPicker = opacitySlider.find('[class$=-picker]');
396
+
397
+ // Picker positions
398
+ var gridPos = getCoords(gridPicker, grid);
399
+ var sliderPos = getCoords(sliderPicker, slider);
400
+ var opacityPos = getCoords(opacityPicker, opacitySlider);
401
+
402
+ // Handle colors
403
+ if (target.is('.minicolors-grid, .minicolors-slider, .minicolors-opacity-slider')) {
404
+
405
+ // Determine HSB values
406
+ switch (settings.control) {
407
+ case 'wheel':
408
+ // Calculate hue, saturation, and brightness
409
+ x = (grid.width() / 2) - gridPos.x;
410
+ y = (grid.height() / 2) - gridPos.y;
411
+ r = Math.sqrt(x * x + y * y);
412
+ phi = Math.atan2(y, x);
413
+ if (phi < 0) phi += Math.PI * 2;
414
+ if (r > 75) {
415
+ r = 75;
416
+ gridPos.x = 69 - (75 * Math.cos(phi));
417
+ gridPos.y = 69 - (75 * Math.sin(phi));
418
+ }
419
+ saturation = keepWithin(r / 0.75, 0, 100);
420
+ hue = keepWithin(phi * 180 / Math.PI, 0, 360);
421
+ brightness = keepWithin(100 - Math.floor(sliderPos.y * (100 / slider.height())), 0, 100);
422
+ hex = hsb2hex({
423
+ h: hue,
424
+ s: saturation,
425
+ b: brightness
426
+ });
427
+
428
+ // Update UI
429
+ slider.css('backgroundColor', hsb2hex({ h: hue, s: saturation, b: 100 }));
430
+ break;
431
+
432
+ case 'saturation':
433
+ // Calculate hue, saturation, and brightness
434
+ hue = keepWithin(parseInt(gridPos.x * (360 / grid.width()), 10), 0, 360);
435
+ saturation = keepWithin(100 - Math.floor(sliderPos.y * (100 / slider.height())), 0, 100);
436
+ brightness = keepWithin(100 - Math.floor(gridPos.y * (100 / grid.height())), 0, 100);
437
+ hex = hsb2hex({
438
+ h: hue,
439
+ s: saturation,
440
+ b: brightness
441
+ });
442
+
443
+ // Update UI
444
+ slider.css('backgroundColor', hsb2hex({ h: hue, s: 100, b: brightness }));
445
+ minicolors.find('.minicolors-grid-inner').css('opacity', saturation / 100);
446
+ break;
447
+
448
+ case 'brightness':
449
+ // Calculate hue, saturation, and brightness
450
+ hue = keepWithin(parseInt(gridPos.x * (360 / grid.width()), 10), 0, 360);
451
+ saturation = keepWithin(100 - Math.floor(gridPos.y * (100 / grid.height())), 0, 100);
452
+ brightness = keepWithin(100 - Math.floor(sliderPos.y * (100 / slider.height())), 0, 100);
453
+ hex = hsb2hex({
454
+ h: hue,
455
+ s: saturation,
456
+ b: brightness
457
+ });
458
+
459
+ // Update UI
460
+ slider.css('backgroundColor', hsb2hex({ h: hue, s: saturation, b: 100 }));
461
+ minicolors.find('.minicolors-grid-inner').css('opacity', 1 - (brightness / 100));
462
+ break;
463
+
464
+ default:
465
+ // Calculate hue, saturation, and brightness
466
+ hue = keepWithin(360 - parseInt(sliderPos.y * (360 / slider.height()), 10), 0, 360);
467
+ saturation = keepWithin(Math.floor(gridPos.x * (100 / grid.width())), 0, 100);
468
+ brightness = keepWithin(100 - Math.floor(gridPos.y * (100 / grid.height())), 0, 100);
469
+ hex = hsb2hex({
470
+ h: hue,
471
+ s: saturation,
472
+ b: brightness
473
+ });
474
+
475
+ // Update UI
476
+ grid.css('backgroundColor', hsb2hex({ h: hue, s: 100, b: 100 }));
477
+ break;
478
+ }
479
+
480
+ // Handle opacity
481
+ if (settings.opacity) {
482
+ opacity = parseFloat(1 - (opacityPos.y / opacitySlider.height())).toFixed(2);
483
+ } else {
484
+ opacity = 1;
485
+ }
486
+
487
+ updateInput(input, hex, opacity);
488
+ }
489
+ else {
490
+ // Set swatch color
491
+ swatch.find('span').css({
492
+ backgroundColor: hex,
493
+ opacity: String(opacity)
494
+ });
495
+
496
+ // Handle change event
497
+ doChange(input, hex, opacity);
498
+ }
499
+ }
500
+
501
+ // Sets the value of the input and does the appropriate conversions
502
+ // to respect settings, also updates the swatch
503
+ function updateInput (input, value, opacity) {
504
+ var rgb;
505
+
506
+ // Helpful references
507
+ var minicolors = input.parent();
508
+ var settings = input.data('minicolors-settings');
509
+ var swatch = minicolors.find('.minicolors-input-swatch');
510
+
511
+ if (settings.opacity) input.attr('data-opacity', opacity);
512
+
513
+ // Set color string
514
+ if (settings.format === 'rgb') {
515
+ // Returns RGB(A) string
516
+
517
+ // Checks for input format and does the conversion
518
+ if (isRgb(value)) {
519
+ rgb = parseRgb(value, true);
520
+ }
521
+ else {
522
+ rgb = hex2rgb(parseHex(value, true));
523
+ }
524
+
525
+ opacity = input.attr('data-opacity') === '' ? 1 : keepWithin(parseFloat(input.attr('data-opacity')).toFixed(2), 0, 1);
526
+ if (isNaN(opacity) || !settings.opacity) opacity = 1;
527
+
528
+ if (input.minicolors('rgbObject').a <= 1 && rgb && settings.opacity) {
529
+ // Set RGBA string if alpha
530
+ value = 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', ' + parseFloat(opacity) + ')';
531
+ } else {
532
+ // Set RGB string (alpha = 1)
533
+ value = 'rgb(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')';
534
+ }
535
+ } else {
536
+ // Returns hex color
537
+
538
+ // Checks for input format and does the conversion
539
+ if (isRgb(value)) {
540
+ value = rgbString2hex(value);
541
+ }
542
+
543
+ value = convertCase(value, settings.letterCase);
544
+ }
545
+
546
+ // Update value from picker
547
+ input.val(value);
548
+
549
+ // Set swatch color
550
+ swatch.find('span').css({
551
+ backgroundColor: value,
552
+ opacity: String(opacity)
553
+ });
554
+
555
+ // Handle change event
556
+ doChange(input, value, opacity);
557
+ }
558
+
559
+ // Sets the color picker values from the input
560
+ function updateFromInput (input, preserveInputValue) {
561
+ var hex, hsb, opacity, keywords, alpha, value, x, y, r, phi;
562
+
563
+ // Helpful references
564
+ var minicolors = input.parent();
565
+ var settings = input.data('minicolors-settings');
566
+ var swatch = minicolors.find('.minicolors-input-swatch');
567
+
568
+ // Panel objects
569
+ var grid = minicolors.find('.minicolors-grid');
570
+ var slider = minicolors.find('.minicolors-slider');
571
+ var opacitySlider = minicolors.find('.minicolors-opacity-slider');
572
+
573
+ // Picker objects
574
+ var gridPicker = grid.find('[class$=-picker]');
575
+ var sliderPicker = slider.find('[class$=-picker]');
576
+ var opacityPicker = opacitySlider.find('[class$=-picker]');
577
+
578
+ // Determine hex/HSB values
579
+ if (isRgb(input.val())) {
580
+ // If input value is a rgb(a) string, convert it to hex color and update opacity
581
+ hex = rgbString2hex(input.val());
582
+ alpha = keepWithin(parseFloat(getAlpha(input.val())).toFixed(2), 0, 1);
583
+ if (alpha) {
584
+ input.attr('data-opacity', alpha);
585
+ }
586
+ } else {
587
+ hex = convertCase(parseHex(input.val(), true), settings.letterCase);
588
+ }
589
+
590
+ if (!hex) {
591
+ hex = convertCase(parseInput(settings.defaultValue, true), settings.letterCase);
592
+ }
593
+ hsb = hex2hsb(hex);
594
+
595
+ // Get array of lowercase keywords
596
+ keywords = !settings.keywords ? [] : $.map(settings.keywords.split(','), function (a) {
597
+ return a.toLowerCase().trim();
598
+ });
599
+
600
+ // Set color string
601
+ if (input.val() !== '' && $.inArray(input.val().toLowerCase(), keywords) > -1) {
602
+ value = convertCase(input.val());
603
+ } else {
604
+ value = isRgb(input.val()) ? parseRgb(input.val()) : hex;
605
+ }
606
+
607
+ // Update input value
608
+ if (!preserveInputValue) input.val(value);
609
+
610
+ // Determine opacity value
611
+ if (settings.opacity) {
612
+ // Get from data-opacity attribute and keep within 0-1 range
613
+ opacity = input.attr('data-opacity') === '' ? 1 : keepWithin(parseFloat(input.attr('data-opacity')).toFixed(2), 0, 1);
614
+ if (isNaN(opacity)) opacity = 1;
615
+ input.attr('data-opacity', opacity);
616
+ swatch.find('span').css('opacity', String(opacity));
617
+
618
+ // Set opacity picker position
619
+ y = keepWithin(opacitySlider.height() - (opacitySlider.height() * opacity), 0, opacitySlider.height());
620
+ opacityPicker.css('top', y + 'px');
621
+ }
622
+
623
+ // Set opacity to zero if input value is transparent
624
+ if (input.val().toLowerCase() === 'transparent') {
625
+ swatch.find('span').css('opacity', String(0));
626
+ }
627
+
628
+ // Update swatch
629
+ swatch.find('span').css('backgroundColor', hex);
630
+
631
+ // Determine picker locations
632
+ switch (settings.control) {
633
+ case 'wheel':
634
+ // Set grid position
635
+ r = keepWithin(Math.ceil(hsb.s * 0.75), 0, grid.height() / 2);
636
+ phi = hsb.h * Math.PI / 180;
637
+ x = keepWithin(75 - Math.cos(phi) * r, 0, grid.width());
638
+ y = keepWithin(75 - Math.sin(phi) * r, 0, grid.height());
639
+ gridPicker.css({
640
+ top: y + 'px',
641
+ left: x + 'px'
642
+ });
643
+
644
+ // Set slider position
645
+ y = 150 - (hsb.b / (100 / grid.height()));
646
+ if (hex === '') y = 0;
647
+ sliderPicker.css('top', y + 'px');
648
+
649
+ // Update panel color
650
+ slider.css('backgroundColor', hsb2hex({ h: hsb.h, s: hsb.s, b: 100 }));
651
+ break;
652
+
653
+ case 'saturation':
654
+ // Set grid position
655
+ x = keepWithin((5 * hsb.h) / 12, 0, 150);
656
+ y = keepWithin(grid.height() - Math.ceil(hsb.b / (100 / grid.height())), 0, grid.height());
657
+ gridPicker.css({
658
+ top: y + 'px',
659
+ left: x + 'px'
660
+ });
661
+
662
+ // Set slider position
663
+ y = keepWithin(slider.height() - (hsb.s * (slider.height() / 100)), 0, slider.height());
664
+ sliderPicker.css('top', y + 'px');
665
+
666
+ // Update UI
667
+ slider.css('backgroundColor', hsb2hex({ h: hsb.h, s: 100, b: hsb.b }));
668
+ minicolors.find('.minicolors-grid-inner').css('opacity', hsb.s / 100);
669
+ break;
670
+
671
+ case 'brightness':
672
+ // Set grid position
673
+ x = keepWithin((5 * hsb.h) / 12, 0, 150);
674
+ y = keepWithin(grid.height() - Math.ceil(hsb.s / (100 / grid.height())), 0, grid.height());
675
+ gridPicker.css({
676
+ top: y + 'px',
677
+ left: x + 'px'
678
+ });
679
+
680
+ // Set slider position
681
+ y = keepWithin(slider.height() - (hsb.b * (slider.height() / 100)), 0, slider.height());
682
+ sliderPicker.css('top', y + 'px');
683
+
684
+ // Update UI
685
+ slider.css('backgroundColor', hsb2hex({ h: hsb.h, s: hsb.s, b: 100 }));
686
+ minicolors.find('.minicolors-grid-inner').css('opacity', 1 - (hsb.b / 100));
687
+ break;
688
+
689
+ default:
690
+ // Set grid position
691
+ x = keepWithin(Math.ceil(hsb.s / (100 / grid.width())), 0, grid.width());
692
+ y = keepWithin(grid.height() - Math.ceil(hsb.b / (100 / grid.height())), 0, grid.height());
693
+ gridPicker.css({
694
+ top: y + 'px',
695
+ left: x + 'px'
696
+ });
697
+
698
+ // Set slider position
699
+ y = keepWithin(slider.height() - (hsb.h / (360 / slider.height())), 0, slider.height());
700
+ sliderPicker.css('top', y + 'px');
701
+
702
+ // Update panel color
703
+ grid.css('backgroundColor', hsb2hex({ h: hsb.h, s: 100, b: 100 }));
704
+ break;
705
+ }
706
+
707
+ // Fire change event, but only if minicolors is fully initialized
708
+ if (input.data('minicolors-initialized')) {
709
+ doChange(input, value, opacity);
710
+ }
711
+ }
712
+
713
+ // Runs the change and changeDelay callbacks
714
+ function doChange (input, value, opacity) {
715
+ var settings = input.data('minicolors-settings');
716
+ var lastChange = input.data('minicolors-lastChange');
717
+ var obj, sel, i;
718
+
719
+ // Only run if it actually changed
720
+ if (!lastChange || lastChange.value !== value || lastChange.opacity !== opacity) {
721
+
722
+ // Remember last-changed value
723
+ input.data('minicolors-lastChange', {
724
+ value: value,
725
+ opacity: opacity
726
+ });
727
+
728
+ // Check and select applicable swatch
729
+ if (settings.swatches && settings.swatches.length !== 0) {
730
+ if (!isRgb(value)) {
731
+ obj = hex2rgb(value);
732
+ }
733
+ else {
734
+ obj = parseRgb(value, true);
735
+ }
736
+ sel = -1;
737
+ for (i = 0; i < settings.swatches.length; ++i) {
738
+ if (obj.r === settings.swatches[i].r && obj.g === settings.swatches[i].g && obj.b === settings.swatches[i].b && obj.a === settings.swatches[i].a) {
739
+ sel = i;
740
+ break;
741
+ }
742
+ }
743
+
744
+ input.parent().find('.minicolors-swatches .minicolors-swatch').removeClass('selected');
745
+ if (sel !== -1) {
746
+ input.parent().find('.minicolors-swatches .minicolors-swatch').eq(i).addClass('selected');
747
+ }
748
+ }
749
+
750
+ // Fire change event
751
+ if (settings.change) {
752
+ if (settings.changeDelay) {
753
+ // Call after a delay
754
+ clearTimeout(input.data('minicolors-changeTimeout'));
755
+ input.data('minicolors-changeTimeout', setTimeout(function () {
756
+ settings.change.call(input.get(0), value, opacity);
757
+ }, settings.changeDelay));
758
+ } else {
759
+ // Call immediately
760
+ settings.change.call(input.get(0), value, opacity);
761
+ }
762
+ }
763
+ input.trigger('change').trigger('input');
764
+ }
765
+ }
766
+
767
+ // Generates an RGB(A) object based on the input's value
768
+ function rgbObject (input) {
769
+ var rgb,
770
+ opacity = $(input).attr('data-opacity');
771
+ if (isRgb($(input).val())) {
772
+ rgb = parseRgb($(input).val(), true);
773
+ } else {
774
+ var hex = parseHex($(input).val(), true);
775
+ rgb = hex2rgb(hex);
776
+ }
777
+ if (!rgb) return null;
778
+ if (opacity !== undefined) $.extend(rgb, { a: parseFloat(opacity) });
779
+ return rgb;
780
+ }
781
+
782
+ // Generates an RGB(A) string based on the input's value
783
+ function rgbString (input, alpha) {
784
+ var rgb,
785
+ opacity = $(input).attr('data-opacity');
786
+ if (isRgb($(input).val())) {
787
+ rgb = parseRgb($(input).val(), true);
788
+ } else {
789
+ var hex = parseHex($(input).val(), true);
790
+ rgb = hex2rgb(hex);
791
+ }
792
+ if (!rgb) return null;
793
+ if (opacity === undefined) opacity = 1;
794
+ if (alpha) {
795
+ return 'rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ', ' + parseFloat(opacity) + ')';
796
+ } else {
797
+ return 'rgb(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')';
798
+ }
799
+ }
800
+
801
+ // Converts to the letter case specified in settings
802
+ function convertCase (string, letterCase) {
803
+ return letterCase === 'uppercase' ? string.toUpperCase() : string.toLowerCase();
804
+ }
805
+
806
+ // Parses a string and returns a valid hex string when possible
807
+ function parseHex (string, expand) {
808
+ string = string.replace(/^#/g, '');
809
+ if (!string.match(/^[A-F0-9]{3,6}/ig)) return '';
810
+ if (string.length !== 3 && string.length !== 6) return '';
811
+ if (string.length === 3 && expand) {
812
+ string = string[0] + string[0] + string[1] + string[1] + string[2] + string[2];
813
+ }
814
+ return '#' + string;
815
+ }
816
+
817
+ // Parses a string and returns a valid RGB(A) string when possible
818
+ function parseRgb (string, obj) {
819
+ var values = string.replace(/[^\d,.]/g, '');
820
+ var rgba = values.split(',');
821
+
822
+ rgba[0] = keepWithin(parseInt(rgba[0], 10), 0, 255);
823
+ rgba[1] = keepWithin(parseInt(rgba[1], 10), 0, 255);
824
+ rgba[2] = keepWithin(parseInt(rgba[2], 10), 0, 255);
825
+ if (rgba[3] !== undefined) {
826
+ rgba[3] = keepWithin(parseFloat(rgba[3], 10), 0, 1);
827
+ }
828
+
829
+ // Return RGBA object
830
+ if (obj) {
831
+ if (rgba[3] !== undefined) {
832
+ return {
833
+ r: rgba[0],
834
+ g: rgba[1],
835
+ b: rgba[2],
836
+ a: rgba[3]
837
+ };
838
+ } else {
839
+ return {
840
+ r: rgba[0],
841
+ g: rgba[1],
842
+ b: rgba[2]
843
+ };
844
+ }
845
+ }
846
+
847
+ // Return RGBA string
848
+ if (typeof (rgba[3]) !== 'undefined' && rgba[3] <= 1) {
849
+ return 'rgba(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ', ' + rgba[3] + ')';
850
+ } else {
851
+ return 'rgb(' + rgba[0] + ', ' + rgba[1] + ', ' + rgba[2] + ')';
852
+ }
853
+
854
+ }
855
+
856
+ // Parses a string and returns a valid color string when possible
857
+ function parseInput (string, expand) {
858
+ if (isRgb(string)) {
859
+ // Returns a valid rgb(a) string
860
+ return parseRgb(string);
861
+ } else {
862
+ return parseHex(string, expand);
863
+ }
864
+ }
865
+
866
+ // Keeps value within min and max
867
+ function keepWithin (value, min, max) {
868
+ if (value < min) value = min;
869
+ if (value > max) value = max;
870
+ return value;
871
+ }
872
+
873
+ // Checks if a string is a valid RGB(A) string
874
+ function isRgb (string) {
875
+ var rgb = string.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
876
+ return (rgb && rgb.length === 4) ? true : false;
877
+ }
878
+
879
+ // Function to get alpha from a RGB(A) string
880
+ function getAlpha (rgba) {
881
+ rgba = rgba.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+(\.\d{1,2})?|\.\d{1,2})[\s+]?/i);
882
+ return (rgba && rgba.length === 6) ? rgba[4] : '1';
883
+ }
884
+
885
+ // Converts an HSB object to an RGB object
886
+ function hsb2rgb (hsb) {
887
+ var rgb = {};
888
+ var h = Math.round(hsb.h);
889
+ var s = Math.round(hsb.s * 255 / 100);
890
+ var v = Math.round(hsb.b * 255 / 100);
891
+ if (s === 0) {
892
+ rgb.r = rgb.g = rgb.b = v;
893
+ } else {
894
+ var t1 = v;
895
+ var t2 = (255 - s) * v / 255;
896
+ var t3 = (t1 - t2) * (h % 60) / 60;
897
+ if (h === 360) h = 0;
898
+ if (h < 60) { rgb.r = t1; rgb.b = t2; rgb.g = t2 + t3; }
899
+ else if (h < 120) { rgb.g = t1; rgb.b = t2; rgb.r = t1 - t3; }
900
+ else if (h < 180) { rgb.g = t1; rgb.r = t2; rgb.b = t2 + t3; }
901
+ else if (h < 240) { rgb.b = t1; rgb.r = t2; rgb.g = t1 - t3; }
902
+ else if (h < 300) { rgb.b = t1; rgb.g = t2; rgb.r = t2 + t3; }
903
+ else if (h < 360) { rgb.r = t1; rgb.g = t2; rgb.b = t1 - t3; }
904
+ else { rgb.r = 0; rgb.g = 0; rgb.b = 0; }
905
+ }
906
+ return {
907
+ r: Math.round(rgb.r),
908
+ g: Math.round(rgb.g),
909
+ b: Math.round(rgb.b)
910
+ };
911
+ }
912
+
913
+ // Converts an RGB string to a hex string
914
+ function rgbString2hex (rgb) {
915
+ rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
916
+ return (rgb && rgb.length === 4) ? '#' +
917
+ ('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) +
918
+ ('0' + parseInt(rgb[2], 10).toString(16)).slice(-2) +
919
+ ('0' + parseInt(rgb[3], 10).toString(16)).slice(-2) : '';
920
+ }
921
+
922
+ // Converts an RGB object to a hex string
923
+ function rgb2hex (rgb) {
924
+ var hex = [
925
+ rgb.r.toString(16),
926
+ rgb.g.toString(16),
927
+ rgb.b.toString(16)
928
+ ];
929
+ $.each(hex, function (nr, val) {
930
+ if (val.length === 1) hex[nr] = '0' + val;
931
+ });
932
+ return '#' + hex.join('');
933
+ }
934
+
935
+ // Converts an HSB object to a hex string
936
+ function hsb2hex (hsb) {
937
+ return rgb2hex(hsb2rgb(hsb));
938
+ }
939
+
940
+ // Converts a hex string to an HSB object
941
+ function hex2hsb (hex) {
942
+ var hsb = rgb2hsb(hex2rgb(hex));
943
+ if (hsb.s === 0) hsb.h = 360;
944
+ return hsb;
945
+ }
946
+
947
+ // Converts an RGB object to an HSB object
948
+ function rgb2hsb (rgb) {
949
+ var hsb = { h: 0, s: 0, b: 0 };
950
+ var min = Math.min(rgb.r, rgb.g, rgb.b);
951
+ var max = Math.max(rgb.r, rgb.g, rgb.b);
952
+ var delta = max - min;
953
+ hsb.b = max;
954
+ hsb.s = max !== 0 ? 255 * delta / max : 0;
955
+ if (hsb.s !== 0) {
956
+ if (rgb.r === max) {
957
+ hsb.h = (rgb.g - rgb.b) / delta;
958
+ } else if (rgb.g === max) {
959
+ hsb.h = 2 + (rgb.b - rgb.r) / delta;
960
+ } else {
961
+ hsb.h = 4 + (rgb.r - rgb.g) / delta;
962
+ }
963
+ } else {
964
+ hsb.h = -1;
965
+ }
966
+ hsb.h *= 60;
967
+ if (hsb.h < 0) {
968
+ hsb.h += 360;
969
+ }
970
+ hsb.s *= 100 / 255;
971
+ hsb.b *= 100 / 255;
972
+ return hsb;
973
+ }
974
+
975
+ // Converts a hex string to an RGB object
976
+ function hex2rgb (hex) {
977
+ hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
978
+ return {
979
+ r: hex >> 16,
980
+ g: (hex & 0x00FF00) >> 8,
981
+ b: (hex & 0x0000FF)
982
+ };
983
+ }
984
+
985
+ // Handle events
986
+ $([document])
987
+ // Hide on clicks outside of the control
988
+ .on('mousedown.minicolors touchstart.minicolors', function (event) {
989
+ if (!$(event.target).parents().add(event.target).hasClass('minicolors')) {
990
+ hide();
991
+ }
992
+ })
993
+ // Start moving
994
+ .on('mousedown.minicolors touchstart.minicolors', '.minicolors-grid, .minicolors-slider, .minicolors-opacity-slider', function (event) {
995
+ var target = $(this);
996
+ event.preventDefault();
997
+ $(event.delegateTarget).data('minicolors-target', target);
998
+ move(target, event, true);
999
+ })
1000
+ // Move pickers
1001
+ .on('mousemove.minicolors touchmove.minicolors', function (event) {
1002
+ var target = $(event.delegateTarget).data('minicolors-target');
1003
+ if (target) move(target, event);
1004
+ })
1005
+ // Stop moving
1006
+ .on('mouseup.minicolors touchend.minicolors', function () {
1007
+ $(this).removeData('minicolors-target');
1008
+ })
1009
+ // Selected a swatch
1010
+ .on('click.minicolors', '.minicolors-swatches li', function (event) {
1011
+ event.preventDefault();
1012
+ var target = $(this), input = target.parents('.minicolors').find('.minicolors-input'), color = target.data('swatch-color');
1013
+ updateInput(input, color, getAlpha(color));
1014
+ updateFromInput(input);
1015
+ })
1016
+ // Show panel when swatch is clicked
1017
+ .on('mousedown.minicolors touchstart.minicolors', '.minicolors-input-swatch', function (event) {
1018
+ var input = $(this).parent().find('.minicolors-input');
1019
+ event.preventDefault();
1020
+ show(input);
1021
+ })
1022
+ // Show on focus
1023
+ .on('focus.minicolors', '.minicolors-input', function () {
1024
+ var input = $(this);
1025
+ if (!input.data('minicolors-initialized')) return;
1026
+ show(input);
1027
+ })
1028
+ // Update value on blur
1029
+ .on('blur.minicolors', '.minicolors-input', function () {
1030
+ var input = $(this);
1031
+ var settings = input.data('minicolors-settings');
1032
+ var keywords;
1033
+ var hex;
1034
+ var rgba;
1035
+ var swatchOpacity;
1036
+ var value;
1037
+
1038
+ if (!input.data('minicolors-initialized')) return;
1039
+
1040
+ // Get array of lowercase keywords
1041
+ keywords = !settings.keywords ? [] : $.map(settings.keywords.split(','), function (a) {
1042
+ return a.toLowerCase().trim();
1043
+ });
1044
+
1045
+ // Set color string
1046
+ if (input.val() !== '' && $.inArray(input.val().toLowerCase(), keywords) > -1) {
1047
+ value = input.val();
1048
+ } else {
1049
+ // Get RGBA values for easy conversion
1050
+ if (isRgb(input.val())) {
1051
+ rgba = parseRgb(input.val(), true);
1052
+ } else {
1053
+ hex = parseHex(input.val(), true);
1054
+ rgba = hex ? hex2rgb(hex) : null;
1055
+ }
1056
+
1057
+ // Convert to format
1058
+ if (rgba === null) {
1059
+ value = settings.defaultValue;
1060
+ } else if (settings.format === 'rgb') {
1061
+ value = settings.opacity ?
1062
+ parseRgb('rgba(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ',' + input.attr('data-opacity') + ')') :
1063
+ parseRgb('rgb(' + rgba.r + ',' + rgba.g + ',' + rgba.b + ')');
1064
+ } else {
1065
+ value = rgb2hex(rgba);
1066
+ }
1067
+ }
1068
+
1069
+ // Update swatch opacity
1070
+ swatchOpacity = settings.opacity ? input.attr('data-opacity') : 1;
1071
+ if (value.toLowerCase() === 'transparent') swatchOpacity = 0;
1072
+ input
1073
+ .closest('.minicolors')
1074
+ .find('.minicolors-input-swatch > span')
1075
+ .css('opacity', String(swatchOpacity));
1076
+
1077
+ // Set input value
1078
+ input.val(value);
1079
+
1080
+ // Is it blank?
1081
+ if (input.val() === '') input.val(parseInput(settings.defaultValue, true));
1082
+
1083
+ // Adjust case
1084
+ input.val(convertCase(input.val(), settings.letterCase));
1085
+
1086
+ })
1087
+ // Handle keypresses
1088
+ .on('keydown.minicolors', '.minicolors-input', function (event) {
1089
+ var input = $(this);
1090
+ if (!input.data('minicolors-initialized')) return;
1091
+ switch (event.which) {
1092
+ case 9: // tab
1093
+ hide();
1094
+ break;
1095
+ case 13: // enter
1096
+ case 27: // esc
1097
+ hide();
1098
+ input.blur();
1099
+ break;
1100
+ }
1101
+ })
1102
+ // Update on keyup
1103
+ .on('keyup.minicolors', '.minicolors-input', function () {
1104
+ var input = $(this);
1105
+ if (!input.data('minicolors-initialized')) return;
1106
+ updateFromInput(input, true);
1107
+ })
1108
+ // Update on paste
1109
+ .on('paste.minicolors', '.minicolors-input', function () {
1110
+ var input = $(this);
1111
+ if (!input.data('minicolors-initialized')) return;
1112
+ setTimeout(function () {
1113
+ updateFromInput(input, true);
1114
+ }, 1);
1115
+ });