-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColorPicker.js
2221 lines (1985 loc) · 66 KB
/
ColorPicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Color picker used by <input type='color' />
*/
function initializeColorPicker() {
if (global.params.selectedColor === undefined) {
global.params.selectedColor = DefaultColor;
}
const colorPicker = new ColorPicker(new Color(global.params.selectedColor));
main.append(colorPicker);
const width = colorPicker.offsetWidth;
const height = colorPicker.offsetHeight;
resizeWindow(width, height);
}
/**
* @param {!Object} args
* @return {?string} An error message, or null if the argument has no errors.
*/
function validateColorPickerArguments(args) {
if (args.shouldShowColorSuggestionPicker)
return 'Should be showing the color suggestion picker.';
if (!args.selectedColor)
return 'No selectedColor.';
return null;
}
/**
* Supported movement directions.
* @enum {number}
*/
const Direction = {
UNDEFINED: 0,
LEFT: 1,
RIGHT: 2,
UP: 3,
DOWN: 4,
}
/**
* Supported color channels.
* @enum {number}
*/
const ColorChannel = {
HEX: 0,
R: 1,
G: 2,
B: 3,
H: 4,
S: 5,
L: 6,
};
/**
* Supported color formats.
* @enum {number}
*/
const ColorFormat = {
HEX: 0,
RGB: 1,
HSL: 2,
};
/**
* Color: Helper class to get color values in different color formats.
*/
class Color {
/**
* @param {string|!ColorFormat} colorStringOrFormat
* @param {...number} colorValues ignored if colorStringOrFormat is a string
*/
constructor(colorStringOrFormat, ...colorValues) {
if (typeof colorStringOrFormat === 'string') {
colorStringOrFormat = colorStringOrFormat.toLowerCase();
if (colorStringOrFormat.startsWith('#')) {
this.hexValue_ = colorStringOrFormat.substr(1);
} else if (colorStringOrFormat.startsWith('rgb')) {
// Ex. 'rgb(255, 255, 255)' => [255,255,255]
colorStringOrFormat = colorStringOrFormat.replace(/\s+/g, '');
[this.rValue_, this.gValue_, this.bValue_] =
colorStringOrFormat.substring(4, colorStringOrFormat.length - 1)
.split(',')
.map(Number);
} else if (colorStringOrFormat.startsWith('hsl')) {
colorStringOrFormat = colorStringOrFormat.replace(/%|\s+/g, '');
[this.hValue_, this.sValue_, this.lValue_] =
colorStringOrFormat.substring(4, colorStringOrFormat.length - 1)
.split(',')
.map(Number);
}
} else {
switch (colorStringOrFormat) {
case ColorFormat.HEX:
this.hexValue_ = colorValues[0].toLowerCase();
break;
case ColorFormat.RGB:
[this.rValue_, this.gValue_, this.bValue_] = colorValues.map(Number);
break;
case ColorFormat.HSL:
[this.hValue_, this.sValue_, this.lValue_] = colorValues.map(Number);
break;
}
}
}
/**
* @param {!Color} other
*/
equals(other) {
return (this.hexValue === other.hexValue);
}
/**
* @returns {string}
*/
get hexValue() {
this.computeHexValue_();
return this.hexValue_;
}
computeHexValue_() {
if (this.hexValue_ !== undefined) {
// Already computed.
} else if (this.rValue_ !== undefined) {
this.hexValue_ = Color.rgbToHex(this.rValue_, this.gValue_, this.bValue_);
} else if (this.hValue_ !== undefined) {
this.hexValue_ = Color.hslToHex(this.hValue_, this.sValue_, this.lValue_);
}
}
asHex() {
return '#' + this.hexValue;
}
/**
* @returns {number} between 0 and 255
*/
get rValue() {
this.computeRGBValues_();
return this.rValue_;
}
/**
* @returns {number} between 0 and 255
*/
get gValue() {
this.computeRGBValues_();
return this.gValue_;
}
/**
* @returns {number} between 0 and 255
*/
get bValue() {
this.computeRGBValues_();
return this.bValue_;
}
computeRGBValues_() {
if (this.rValue_ !== undefined) {
// Already computed.
} else if (this.hexValue_ !== undefined) {
[this.rValue_, this.gValue_, this.bValue_] =
Color.hexToRGB(this.hexValue_);
} else if (this.hValue_ !== undefined) {
[this.rValue_, this.gValue_, this.bValue_] =
Color.hslToRGB(this.hValue_, this.sValue_, this.lValue_);
}
}
rgbValues() {
return [this.rValue, this.gValue, this.bValue];
}
asRGB() {
return 'rgb(' + this.rgbValues().join() + ')';
}
/**
* @returns {number} between 0 and 359
*/
get hValue() {
this.computeHSLValues_();
return this.hValue_;
}
/**
* @returns {number} between 0 and 100
*/
get sValue() {
this.computeHSLValues_();
return this.sValue_;
}
/**
* @returns {number} between 0 and 100
*/
get lValue() {
this.computeHSLValues_();
return this.lValue_;
}
computeHSLValues_() {
if (this.hValue_ !== undefined) {
// Already computed.
} else if (this.rValue_ !== undefined) {
[this.hValue_, this.sValue_, this.lValue_] =
Color.rgbToHSL(this.rValue_, this.gValue_, this.bValue_);
} else if (this.hexValue_ !== undefined) {
[this.hValue_, this.sValue_, this.lValue_] =
Color.hexToHSL(this.hexValue_);
}
}
hslValues() {
return [this.hValue, this.sValue, this.lValue];
}
asHSL() {
return 'hsl(' + this.hValue + ',' + this.sValue + '%,' + this.lValue + '%)';
}
/**
* @param {string} hexValue
* @returns {number[]}
*/
static hexToRGB(hexValue) {
// Ex. 'ffffff' => '[255,255,255]'
const colorValue = parseInt(hexValue, 16);
return [
(colorValue >> 16) & 255, (colorValue >> 8) & 255, colorValue & 255
];
}
/**
* @param {...number} rgbValues
* @returns {string}
*/
static rgbToHex(...rgbValues) {
// Ex. '[255,255,255]' => 'ffffff'
return rgbValues.reduce((cumulativeHexValue, rgbValue) => {
let hexValue = Number(rgbValue).toString(16);
if (hexValue.length == 1) {
hexValue = '0' + hexValue;
}
return (cumulativeHexValue + hexValue);
}, '');
}
/**
* The algorithm has been written based on the mathematical formula found at:
* https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB.
* @param {...number} hslValues
* @returns {number[]}
*/
static hslToRGB(...hslValues) {
let [hValue, sValue, lValue] = hslValues;
hValue /= 60;
sValue /= 100;
lValue /= 100;
let rValue = lValue;
let gValue = lValue;
let bValue = lValue;
let match = 0;
if (sValue !== 0) {
const chroma = (1 - Math.abs(2 * lValue - 1)) * sValue;
const x = chroma * (1 - Math.abs(hValue % 2 - 1));
match = lValue - chroma / 2;
if ((0 <= hValue) && (hValue <= 1)) {
rValue = chroma;
gValue = x;
bValue = 0;
} else if ((1 < hValue) && (hValue <= 2)) {
rValue = x;
gValue = chroma;
bValue = 0;
} else if ((2 < hValue) && (hValue <= 3)) {
rValue = 0;
gValue = chroma;
bValue = x;
} else if ((3 < hValue) && (hValue <= 4)) {
rValue = 0;
gValue = x;
bValue = chroma;
} else if ((4 < hValue) && (hValue <= 5)) {
rValue = x;
gValue = 0;
bValue = chroma;
} else {
// (5 < hValue) && (hValue < 6)
rValue = chroma;
gValue = 0;
bValue = x;
}
}
rValue = Math.round((rValue + match) * 255);
gValue = Math.round((gValue + match) * 255);
bValue = Math.round((bValue + match) * 255);
return [rValue, gValue, bValue];
}
/**
* The algorithm has been written based on the mathematical formula found at:
* https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
* @param {...number} rgbValues
* @returns {number[]}
*/
static rgbToHSL(...rgbValues) {
const [rValue, gValue, bValue] = rgbValues.map((value) => value / 255);
const max = Math.max(rValue, gValue, bValue);
const min = Math.min(rValue, gValue, bValue);
let hValue = 0;
let sValue = 0;
let lValue = (max + min) / 2;
if (max !== min) {
const diff = max - min;
if (max === rValue) {
hValue = ((gValue - bValue) / diff);
} else if (max === gValue) {
hValue = ((bValue - rValue) / diff) + 2;
} else {
// max === bValue
hValue = ((rValue - gValue) / diff) + 4;
}
hValue = Math.round(hValue * 60);
if (hValue < 0) {
hValue += 360;
}
sValue = Math.round((diff / (1 - Math.abs(2 * lValue - 1))) * 100);
}
lValue = Math.round(lValue * 100);
return [hValue, sValue, lValue];
}
/**
* @param {...number} rgbValues
* @returns {string}
*/
static hslToHex(...hslValues) {
return Color.rgbToHex(...Color.hslToRGB(...hslValues));
}
/**
* @param {string} hexValue
* @returns {...number}
*/
static hexToHSL(hexValue) {
return Color.rgbToHSL(...Color.hexToRGB(hexValue));
}
/**
* @param {number[]} colorTripleA RGB or HSL values
* @param {number[]} colorTripleB RGB or HSL values
* Both color triples must be of the same color format.
*/
static distance(colorTripleA, colorTripleB) {
return Math.sqrt(
Math.pow(colorTripleA[0] - colorTripleB[0], 2) +
Math.pow(colorTripleA[1] - colorTripleB[1], 2) +
Math.pow(colorTripleA[2] - colorTripleB[2], 2));
}
}
/**
* Point: Helper class to get/set coordinates for two dimensional points.
*/
class Point {
/**
* @param {number} x
* @param {number} y
*/
constructor(x, y) {
this.set(x, y);
}
/**
* @param {number} x
* @param {number} y
*/
set(x, y) {
this.x = x;
this.y = y;
}
get x() {
return this.x_;
}
/**
* @param {number} x
*/
set x(x) {
this.x_ = x;
}
get y() {
return this.y_;
}
/**
* @param {number} y
*/
set y(y) {
this.y_ = y;
}
}
/**
* ColorPicker: Custom element providing a color picker implementation.
* A color picker is comprised of three main parts: a visual color
* picker to allow visual selection of colors, a manual color
* picker to allow numeric selection of colors, and submission
* controls to save/discard new color selections.
*/
class ColorPicker extends HTMLElement {
/**
* @param {!Color} initialColor
*/
constructor(initialColor) {
super();
if (global.params.isBorderTransparent) {
this.style.borderColor = 'transparent';
}
this.selectedColor_ = initialColor;
this.colorWhenOpened_ = initialColor;
this.visualColorPicker_ = new VisualColorPicker(initialColor);
this.manualColorPicker_ = new ManualColorPicker(initialColor);
this.colorValueAXAnnouncer_ = new ColorValueAXAnnouncer();
this.append(
this.visualColorPicker_, this.manualColorPicker_,
this.colorValueAXAnnouncer_);
this.visualColorPicker_.addEventListener(
'visual-color-picker-initialized', this.initializeListeners_);
window.addEventListener('resize', this.onWindowResize_, {once: true});
}
initializeListeners_ = () => {
this.manualColorPicker_
.addEventListener('manual-color-change', this.onManualColorChange_);
this.addEventListener('visual-color-change', this.onVisualColorChange_);
this.addEventListener('format-change', this.onFormatChange_);
this.addEventListener('focusin', this.onFocusin_);
window.addEventListener('message', this.onMessageReceived_);
document.documentElement.addEventListener('keydown', this.onKeyDown_);
}
get selectedColor() {
return this.selectedColor_;
}
/**
* @param {!Color} newColor
*/
set selectedColor(newColor) {
this.selectedColor_ = newColor;
}
/**
* @param {!Event} event
*/
onManualColorChange_ = (event) => {
const newColor = event.detail.color;
if (!this.selectedColor.equals(newColor)) {
this.selectedColor = newColor;
// There may not be an exact match for newColor in the HueSlider or
// ColorWell, in which case we will display the closest match. When this
// happens though, we want the manually chosen values to remain the
// selected values (as they were explicitly specified by the user).
// Therefore, we need to prevent them from getting overwritten when
// onVisualColorChange_ runs. We do this by setting the
// processingManualColorChange_ flag here and checking for it inside
// onVisualColorChange_. If the flag is set, the manual color values
// will not be updated with the color shown in the visual color picker.
this.processingManualColorChange_ = true;
this.visualColorPicker_.color = newColor;
this.processingManualColorChange_ = false;
const selectedValue = newColor.asHex();
window.pagePopupController.setValue(selectedValue);
}
};
/**
* @param {!Event} event
*/
onVisualColorChange_ = (event) => {
const newColor = event.detail.color;
if (!this.selectedColor.equals(newColor)) {
if (!this.processingManualColorChange_) {
this.selectedColor = newColor;
this.manualColorPicker_.color = newColor;
this.colorValueAXAnnouncer_.announceColor(newColor);
const selectedValue = newColor.asHex();
window.pagePopupController.setValue(selectedValue);
} else {
// We are making a visual color change in response to a manual color
// change. So we do not overwrite the manually specified values and do
// not change the selected color.
}
}
};
/**
* @param {!Event} event
*/
onKeyDown_ = (event) => {
switch (event.key) {
case 'Enter':
window.pagePopupController.closePopup();
break;
case 'Escape':
if (this.selectedColor.equals(this.colorWhenOpened_)) {
window.pagePopupController.closePopup();
} else {
this.manualColorPicker_.dispatchEvent(new CustomEvent(
'manual-color-change',
{bubbles: true, detail: {color: this.colorWhenOpened_}}));
}
break;
case 'Tab':
event.preventDefault();
if (this.focusableElements_ === undefined) {
this.updateFocusableElements_();
}
const length = this.focusableElements_.length;
if (length > 0) {
const currentFocusIndex =
this.focusableElements_.indexOf(document.activeElement);
let nextFocusIndex;
if (event.shiftKey) {
nextFocusIndex =
(currentFocusIndex > 0) ? currentFocusIndex - 1 : length - 1;
} else {
nextFocusIndex = (currentFocusIndex + 1) % length;
}
this.focusableElements_[nextFocusIndex].focus({preventScroll: true});
}
break;
}
};
onFormatChange_ = (event) => {
this.updateFocusableElements_();
this.colorValueAXAnnouncer_.updateColorFormat(event.detail.colorFormat);
};
onFocusin_ = (event) => {
if (event.target instanceof ColorSelectionRing) {
// Announce the current color when the user focuses the ColorWell or the
// HueSlider.
this.colorValueAXAnnouncer_.announceColor(this.selectedColor);
} else if (event.target instanceof FormatToggler) {
// Announce the current color format when the user focuses the
// FormatToggler.
this.colorValueAXAnnouncer_.announceColorFormat();
}
};
updateFocusableElements_ = () => {
this.focusableElements_ = Array.from(this.querySelectorAll(
'color-value-container:not(.hidden-color-value-container) > input,' +
'[tabindex]:not([tabindex=\'-1\'])'));
};
onWindowResize_ = () => {
// Set focus on the first focusable element.
if (this.focusableElements_ === undefined) {
this.updateFocusableElements_();
}
this.focusableElements_[0].focus({preventScroll: true});
};
onMessageReceived_ = (event) => {
eval(event.data);
if (window.updateData && window.updateData.success) {
// Update the popup with the color selected using the eye dropper.
const selectedValue = new Color(window.updateData.color);
this.selectedColor = selectedValue;
this.manualColorPicker_.color = selectedValue;
this.visualColorPicker_.color = selectedValue;
const hexValue = selectedValue.asHex();
window.pagePopupController.setValue(hexValue);
}
this.visualColorPicker_.eyeDropper.finished();
delete window.updateData;
}
}
window.customElements.define('color-picker', ColorPicker);
/**
* VisualColorPicker: Provides functionality to see the selected color and
* select a different color visually.
*/
class VisualColorPicker extends HTMLElement {
/**
* @param {!Color} initialColor
*/
constructor(initialColor) {
super();
let visualColorPickerStrip = document.createElement('span');
visualColorPickerStrip.setAttribute('id', 'visual-color-picker-strip');
this.eyeDropper_ = new EyeDropper();
this.colorViewer_ = new ColorViewer(initialColor);
this.hueSlider_ = new HueSlider(initialColor);
visualColorPickerStrip.append(
this.eyeDropper_, this.colorViewer_, this.hueSlider_);
this.append(visualColorPickerStrip);
this.colorWell_ = new ColorWell(initialColor);
this.prepend(this.colorWell_);
this.colorWell_.addEventListener('color-well-initialized', () => {
this.initializeListeners_();
});
this.hueSlider_.addEventListener('hue-slider-initialized', () => {
this.initializeListeners_();
});
}
initializeListeners_ = () => {
if (this.colorWell_.initialized && this.hueSlider_.initialized) {
this.addEventListener('hue-slider-update', this.onHueSliderUpdate_);
this.addEventListener('visual-color-change', this.onVisualColorChange_);
this.colorWell_
.addEventListener('mousedown', this.onColorWellMouseDown_);
this.hueSlider_
.addEventListener('mousedown', this.onHueSliderMouseDown_);
document.documentElement
.addEventListener('mousedown', this.onMouseDown_);
document.documentElement
.addEventListener('mousemove', this.onMouseMove_);
document.documentElement.addEventListener('mouseup', this.onMouseUp_);
this.colorWell_
.addEventListener('touchstart', this.onColorWellTouchStart_);
this.hueSlider_
.addEventListener('touchstart', this.onHueSliderTouchStart_);
document.documentElement
.addEventListener('touchstart', this.onTouchStart_);
document.documentElement
.addEventListener('touchmove', this.onTouchMove_);
document.documentElement.addEventListener('touchend', this.onTouchEnd_);
document.documentElement.addEventListener('keydown', this.onKeyDown_);
this.dispatchEvent(new CustomEvent('visual-color-picker-initialized'));
}
}
onHueSliderUpdate_ = () => {
this.colorWell_.fillColor = this.hueSlider_.color;
}
/**
* @param {!Event} event
*/
onVisualColorChange_ = (event) => {
this.colorViewer_.color = event.detail.color;
}
/**
* @param {!Event} event
*/
onColorWellMouseDown_ = (event) => {
event.preventDefault();
event.stopPropagation();
this.hueSlider_.focused = false;
this.colorWell_.pointerDown(new Point(event.clientX, event.clientY));
}
/**
* @param {!Event} event
*/
onHueSliderMouseDown_ = (event) => {
event.preventDefault();
event.stopPropagation();
this.colorWell_.focused = false;
this.hueSlider_.pointerDown(new Point(event.clientX, event.clientY));
}
onMouseDown_ = () => {
this.colorWell_.focused = false;
this.hueSlider_.focused = false;
}
/**
* @param {!Event} event
*/
onMouseMove_ = (event) => {
var point = new Point(event.clientX, event.clientY);
this.colorWell_.pointerMove(point);
this.hueSlider_.pointerMove(point);
}
onMouseUp_ = () => {
this.colorWell_.pointerUp();
this.hueSlider_.pointerUp();
}
/**
* @param {!Event} event
*/
onColorWellTouchStart_ = (event) => {
event.preventDefault();
event.stopPropagation();
this.hueSlider_.focused = false;
this.colorWell_.pointerDown(new Point(Math.round(event.touches[0].clientX), Math.round(event.touches[0].clientY)));
}
/**
* @param {!Event} event
*/
onHueSliderTouchStart_ = (event) => {
event.preventDefault();
event.stopPropagation();
this.colorWell_.focused = false;
this.hueSlider_.pointerDown(new Point(Math.round(event.touches[0].clientX), Math.round(event.touches[0].clientY)));
}
onTouchStart_ = () => {
this.colorWell_.focused = false;
this.hueSlider_.focused = false;
}
/**
* @param {!Event} event
*/
onTouchMove_ = (event) => {
var point = new Point(Math.round(event.touches[0].clientX), Math.round(event.touches[0].clientY));
this.colorWell_.pointerMove(point);
this.hueSlider_.pointerMove(point);
}
onTouchEnd_ = () => {
this.colorWell_.pointerUp();
this.hueSlider_.pointerUp();
}
/**
* @param {!Event} event
*/
onKeyDown_ = (event) => {
let moveDirection = Direction.UNDEFINED;
switch(event.key) {
case 'ArrowUp':
moveDirection = Direction.UP;
break;
case 'ArrowDown':
moveDirection = Direction.DOWN;
break;
case 'ArrowLeft':
moveDirection = Direction.LEFT;
break;
case 'ArrowRight':
moveDirection = Direction.RIGHT;
break;
}
if (moveDirection !== Direction.UNDEFINED) {
const acceleratedMove = event.ctrlKey;
this.hueSlider_.move(moveDirection, acceleratedMove);
this.colorWell_.move(moveDirection, acceleratedMove);
}
}
/**
* @param {!Color} newColor
*/
set color(newColor) {
this.hueSlider_.color = newColor;
this.colorWell_.selectedColor = newColor;
}
get eyeDropper() {
return this.eyeDropper_;
}
}
window.customElements.define('visual-color-picker', VisualColorPicker);
/**
* EyeDropper: Allows color selection from content outside the color picker.
* (This is currently just a placeholder for a future
* implementation.)
* TODO(http://crbug.com/992297): Implement eye dropper
*/
class EyeDropper extends HTMLElement {
constructor() {
super();
if (!global.params.isEyeDropperEnabled) {
this.classList.add('hidden');
return;
}
this.setAttribute('tabIndex', 0);
this.innerHTML =
'<svg width="16" height="16" viewBox="0 0 16 16" fill="none" ' +
'xmlns="http://www.w3.org/2000/svg"><path d="M13.7344 0C14.0469 0 ' +
'14.3411 0.0598958 14.6172 0.179688C14.8932 0.299479 15.1328 ' +
'0.460938 15.3359 0.664062C15.5391 0.867188 15.7005 1.10677 15.8203 ' +
'1.38281C15.9401 1.65885 16 1.95312 16 2.26562C16 2.56771 15.9427 ' +
'2.85938 15.8281 3.14062C15.7135 3.41667 15.5495 3.66146 15.3359 ' +
'3.875L13.4609 5.75C13.6328 5.91667 13.7656 6.10677 13.8594 ' +
'6.32031C13.9531 6.52865 14 6.75521 14 7C14 7.23958 13.9531 7.46354 ' +
'13.8594 7.67188C13.7708 7.88021 13.6432 8.06771 13.4766 ' +
'8.23438L12.25 9.46094L11 8.20312L4.71094 14.4922L4.50781 ' +
'14.5C4.24219 14.5104 4.01302 14.5547 3.82031 14.6328C3.63281 ' +
'14.7109 3.46615 14.8073 3.32031 14.9219C3.17969 15.0312 3.04948 ' +
'15.1484 2.92969 15.2734C2.8151 15.3984 2.69271 15.5156 2.5625 ' +
'15.625C2.43229 15.7344 2.28906 15.8255 2.13281 15.8984C1.97656 ' +
'15.9661 1.78646 16 1.5625 16C1.34896 16 1.14583 15.9583 0.953125 ' +
'15.875C0.765625 15.7917 0.601562 15.6797 0.460938 15.5391C0.320312 ' +
'15.3984 0.208333 15.2344 0.125 15.0469C0.0416667 14.8542 0 14.651 0 ' +
'14.4375C0 14.2135 0.0338542 14.0234 0.101562 13.8672C0.174479 ' +
'13.7057 0.265625 13.5625 0.375 13.4375C0.484375 13.3073 0.601562 ' +
'13.1849 0.726562 13.0703C0.851562 12.9505 0.96875 12.8203 1.07812 ' +
'12.6797C1.19271 12.5339 1.28906 12.3672 1.36719 12.1797C1.44531 ' +
'11.9922 1.48958 11.763 1.5 11.4922L1.50781 11.2891L7.79688 ' +
'5L6.53906 3.75L7.76562 2.52344C7.93229 2.35677 8.11979 2.22917 ' +
'8.32812 2.14062C8.53646 2.04688 8.76042 2 9 2C9.24479 2 9.47135 ' +
'2.04688 9.67969 2.14062C9.89323 2.23438 10.0833 2.36719 10.25 ' +
'2.53906L12.125 0.664062C12.3385 0.450521 12.5833 0.286458 12.8594 ' +
'0.171875C13.1406 0.0572917 13.4323 0 13.7344 0ZM10.2891 7.5L8.5 ' +
'5.71094L2.49219 11.7188C2.46615 11.9844 2.41667 12.2214 2.34375 ' +
'12.4297C2.27083 12.638 2.17708 12.8333 2.0625 13.0156C1.94792 ' +
'13.1927 1.8125 13.3646 1.65625 13.5312C1.50521 13.6927 1.34115 ' +
'13.8646 1.16406 14.0469C1.05469 14.1562 1 14.2891 1 14.4453C1 ' +
'14.5964 1.05469 14.7266 1.16406 14.8359C1.27344 14.9453 1.40365 15 ' +
'1.55469 15C1.71094 15 1.84375 14.9453 1.95312 14.8359C2.13542 ' +
'14.6589 2.3099 14.4948 2.47656 14.3438C2.64323 14.1875 2.8151 ' +
'14.0521 2.99219 13.9375C3.16927 13.8229 3.36198 13.7292 3.57031 ' +
'13.6562C3.77865 13.5833 4.01562 13.5339 4.28125 13.5078L10.2891 ' +
'7.5ZM14.625 3.16406C14.875 2.91406 15 2.61719 15 2.27344C15 2.10156 ' +
'14.9661 1.9375 14.8984 1.78125C14.8307 1.625 14.7396 1.48958 14.625 ' +
'1.375C14.5104 1.26042 14.375 1.16927 14.2188 1.10156C14.0625 ' +
'1.03385 13.8984 1 13.7266 1C13.3828 1 13.0859 1.125 12.8359 ' +
'1.375L10.25 3.95312L9.51562 3.21875C9.36979 3.07292 9.19792 3 9 ' +
'3C8.89062 3 8.78646 3.02604 8.6875 3.07812C8.59375 3.13021 8.5026 ' +
'3.19531 8.41406 3.27344C8.33073 3.35156 8.25 3.4349 8.17188 ' +
'3.52344C8.09375 3.60677 8.02083 3.68229 7.95312 3.75L12.25 ' +
'8.04688L12.7812 7.51562C12.9271 7.36979 13 7.19792 13 7C13 6.89583 ' +
'12.9792 6.80208 12.9375 6.71875C12.901 6.63021 12.8464 6.54948 ' +
'12.7734 6.47656L12.0469 5.75L14.625 3.16406Z" fill="WindowText"/> ' +
'</svg>';
this.addEventListener('click', this.onClick_);
this.addEventListener('keydown', this.onKeyDown_);
}
onClick_ = () => {
event.preventDefault();
event.stopPropagation();
this.classList.add('selected');
window.pagePopupController.openEyeDropper();
};
/**
* @param {!Event} event
*/
onKeyDown_ = (event) => {
switch (event.key) {
case 'Enter':
this.onClick_();
break;
}
};
finished = () => {
this.classList.remove('selected');
}
}
window.customElements.define('eye-dropper', EyeDropper);
/**
* ColorViewer: Provides a view of the selected color.
*/
class ColorViewer extends HTMLElement {
/**
* @param {!Color} initialColor
*/
constructor(initialColor) {
super();
this.color = initialColor;
// Leave the ColorViewer out of the accessibility tree; it's redundant
// with the updates from ColorValueAXAnnouncer.
this.setAttribute('aria-hidden', 'true');
}
get color() {
return this.color_;
}
/**
* @param {!Color} color
*/
set color(color) {
if (this.color_ === undefined || !this.color_.equals(color)) {
this.color_ = color;
this.style.backgroundColor = color.asRGB();
}
}
}
window.customElements.define('color-viewer', ColorViewer);
/**
* ColorSelectionArea: Base class for ColorWell and HueSlider that encapsulates
* a ColorPalette and a ColorSelectionRing.
*/
class ColorSelectionArea extends HTMLElement {
constructor() {
super();
this.colorPalette_ = new ColorPalette();
this.colorSelectionRing_ = new ColorSelectionRing(this.colorPalette_);
this.append(this.colorPalette_, this.colorSelectionRing_);
this.initialized_ = false;
this.colorSelectionRing_.addEventListener(
'focus', this.onColorSelectionRingFocus_);
this.colorSelectionRing_.addEventListener(
'blur', this.onColorSelectionRingBlur_);
}
get initialized() {
return this.initialized_;
}
onColorSelectionRingFocus_ = () => {
this.focused_ = true;
};
onColorSelectionRingBlur_ = () => {
this.focused_ = false;
};
/**
* @param {!Point} point
*/
pointerDown(point) {
this.colorSelectionRing_.focus({preventScroll: true});
this.colorSelectionRing_.drag = true;
this.moveColorSelectionRingTo_(point);
}
/**
* @param {!Point} point
*/
pointerMove(point) {
if (this.colorSelectionRing_.drag) {
this.moveColorSelectionRingTo_(point);
}
}
pointerUp() {
this.colorSelectionRing_.drag = false;
}
/**
* @param {!Direction} direction
* @param {bool} accelerated
*/
move(direction, accelerated) {
if (this.focused) {
this.colorSelectionRing_.move(direction, accelerated);
}
}
get focused() {
return this.focused_;
}
/**
* @param {bool} focused
*/
set focused(focused) {
this.focused_ = focused;
}
}
window.customElements.define('color-selection-area', ColorSelectionArea);