-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
html5kellycolorpicker.js
2692 lines (2078 loc) · 84.3 KB
/
html5kellycolorpicker.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
/**
* @category html5 widgets
* @package Kelly
* @author Rubchuk Vladimir <[email protected]>
* @copyright 2015-2022 Rubchuk Vladimir
* @license GPLv3
* @version 1.22
*
* Usage example :
*
* new KellyColorPicker({place : 'color-picker'});
*
* ToDo :
*
* Add switch color in colorsavers button (analog of X button in Photoshop)
* updateConfig method
*
**/
/**
* Create color picker
* @param {Array} cfg
* @returns {KellyColorPicker}
*/
function KellyColorPicker(cfg) {
var PI = Math.PI;
var svFig; // current method SV figure object
var changeCursor = true;
var svCursor = new Object;
svCursor.radius = 4;
var canvas = false;
var ctx = false;
var method = 'quad';
var alpha = false; // is alpha slider enabled
var drag = false;
var cursorAnimReady = true; // sets by requestAnimationFrame to limit FPS on events like mousemove etc. when draging
var events = new Array();
var userEvents = new Array();
var canvasHelper = document.createElement("canvas");
var canvasHelperCtx = false; // used if needed to copy image data throw ctx.drawImage for save alpha channel
var rendered = false; // is colorpicker rendered (without side alpha bar and cursors, rendered image stores in canvasHelperData
var canvasHelperData = null; // rendered interface without cursors and without alpha slider [wheelBlockSize x wheelBlockSize]
var input = false;
// used by updateInput() function if not overloaded by user event
var inputColor = true; // update input color according to picker
var inputFormat = 'mixed'; // text format of colorpicker color displayed in input element | values : mixed | hex | rgba
var popup = new Object; // popup block for input
popup.tag = false; // Dom element if popup is enabled
popup.margin = 6; // margin from input in pixels
// container, or canvas element
var place = false;
var handler = this;
var basePadding = 2;
var padding;
var wheelBlockSize = 200;
var center;
// current color
var hsv;
var rgb;
var hex = '#000000';
var a = 1;
var resizeWith = false;
var resizeSide = false;
var colorSavers = new Array();
var styleSwitch = false; // change method from square to triangle
var svFigsPool = new Array(); // if we have button for switch method, better store already created figure object to buffer
// style switch from triange to quad and backwards
function initStyleSwitch() {
styleSwitch = new Object;
styleSwitch.size;
styleSwitch.sizePercentage = 10;
styleSwitch.position;
styleSwitch.paddingY = 4;
styleSwitch.paddingX = 4;
styleSwitch.imageData = new Array();
styleSwitch.lineWidth = 2;
styleSwitch.color = '#c1ebf5';
styleSwitch.updateSize = function () {
this.size = parseInt(wheelBlockSize - (wheelBlockSize / 100) * (100 - this.sizePercentage));
if (this.size < 16)
this.size = 16;
this.position = {x: this.paddingX, y: this.paddingY};
}
styleSwitch.draw = function () {
if (this.imageData[method]) {
ctx.putImageData(this.imageData[method], this.position.x, this.position.y);
return;
}
var rgb = hexToRgb(this.color);
canvasHelper.width = this.size;
canvasHelper.height = this.size;
canvasHelperCtx.clearRect(0, 0, this.size, this.size);
canvasHelperCtx.beginPath();
var switchFig = 'triangle';
if (method == 'triangle')
switchFig = 'quad';
canvasHelperCtx.beginPath();
if (this.size < 35) {
var circleRadiusMain = canvasHelper.width / 2;
var circleRadius = circleRadiusMain;
} else {
var circleRadiusMain = (canvasHelper.width / 2) - this.lineWidth;
canvasHelperCtx.arc(this.size / 2, this.size / 2, circleRadiusMain, 0, PI * 2);
canvasHelperCtx.strokeStyle = 'rgba(0, 0, 0, 0.4)';
canvasHelperCtx.lineWidth = this.lineWidth;
canvasHelperCtx.stroke();
var circleRadius = circleRadiusMain - 6;
canvasHelperCtx.closePath();
canvasHelperCtx.beginPath();
canvasHelperCtx.arc(this.size / 2, this.size / 2, circleRadius, 0, PI * 2);
canvasHelperCtx.strokeStyle = 'rgba(0, 0, 0, 0.4)';
canvasHelperCtx.lineWidth = this.lineWidth;
canvasHelperCtx.stroke();
canvasHelperCtx.closePath();
}
canvasHelperCtx.beginPath();
var svmSize;
if (switchFig == 'quad') {
var workDiametr = (circleRadius * 2) - 4; // may be some paddings here
svmSize = Math.floor(workDiametr / Math.sqrt(2));
var padding = (this.size - svmSize) / 2;
var svmPos = {x: padding + svmSize, y: padding + svmSize / 2}; // start middle point
svmPos.y = svmPos.y - (svmSize / 2);
canvasHelperCtx.moveTo(svmPos.x, svmPos.y); // right top
canvasHelperCtx.lineTo(svmPos.x - svmSize, svmPos.y); // left tp
canvasHelperCtx.lineTo(svmPos.x - svmSize, svmPos.y + svmSize); // left bottom
canvasHelperCtx.lineTo(svmPos.x, svmPos.y + svmSize); // right bottom
} else {
svmSize = Math.floor((2 * circleRadius - 4) * Math.sin(toRadians(60))); // side size
var svmPos = {x: circleRadius * 2 + (circleRadiusMain - circleRadius), y: this.size / 2}; // start middle point
var h = ((Math.sqrt(3) / 2) * svmSize);
canvasHelperCtx.moveTo(svmPos.x, svmPos.y);
canvasHelperCtx.lineTo(svmPos.x - h, svmPos.y - (svmSize / 2)); // top
canvasHelperCtx.lineTo(svmPos.x - h, svmPos.y + (svmSize / 2)); // bottom
canvasHelperCtx.lineTo(svmPos.x, svmPos.y);
}
canvasHelperCtx.lineTo(svmPos.x, svmPos.y);
canvasHelperCtx.fillStyle = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ', 1)';
canvasHelperCtx.fill();
canvasHelperCtx.lineWidth = this.lineWidth;
canvasHelperCtx.strokeStyle = 'rgba(0, 0, 0, 0.6)';
canvasHelperCtx.stroke();
canvasHelperCtx.closePath();
this.imageData[method] = canvasHelperCtx.getImageData(0, 0, canvasHelper.width, canvasHelper.width);
ctx.drawImage(canvasHelper, this.position.x, this.position.y);
}
styleSwitch.isDotIn = function (dot) {
if (
dot.x >= this.position.x && dot.x <= this.position.x + this.size &&
dot.y >= this.position.y && dot.y <= this.position.y + this.size
) {
return true;
}
//if (Math.pow(this.position.x - dot.x, 2) + Math.pow(this.position.y - dot.y, 2) < Math.pow(this.outerRadius, 2)) {
// return true;
//}
return false;
}
}
// triangle colorsavers for left and right side
function initColorSaver(align, selected, color) {
if (!selected)
selected = false;
else
selected = true;
var colorSaver = new Object;
colorSaver.width; // size of side of triangle
colorSaver.widthPercentage = 22;
colorSaver.imageData = null; // last rendered colorsaver image
colorSaver.align = align;
colorSaver.selected = selected; // current color
colorSaver.color = '#ffffff'; // hex color
colorSaver.position; // top point of triangle
colorSaver.paddingY = -4;
colorSaver.paddingX = 4;
colorSaver.lineWidth = 1;
colorSaver.selectSize = 4;
if (align == 'right') {
colorSaver.paddingX = colorSaver.paddingX * -1;
}
if (colorSaver.selected) {
colorSaver.color = hex;
}
if (color) {
colorSaver.color = color;
}
colorSaver.updateSize = function () {
this.width = parseInt(wheelBlockSize - (wheelBlockSize / 100) * (100 - this.widthPercentage));
// start render point in global canvas coords
if (this.align == 'left') {
this.position = {x: 0, y: wheelBlockSize - this.width};
} else if (this.align == 'right') {
this.position = {x: wheelBlockSize - this.width, y: wheelBlockSize - this.width};
}
}
// calc triangle area (same method as for triangle sv figure)
colorSaver.calcS = function (p) {
return Math.abs((p[1].x - p[0].x) * (p[2].y - p[0].y) - (p[2].x - p[0].x) * (p[1].y - p[0].y)) / 2;
}
colorSaver.isDotIn = function (dot) {
var path = new Array();
if (this.align == 'left') {
path[0] = {x: this.position.x, y: this.position.y}; // top
path[1] = {x: this.position.x, y: this.position.y + this.width}; // bottom left
path[2] = {x: this.position.x + this.width, y: this.position.y + this.width}; // bottom right
} else {
path[0] = {x: this.position.x + this.width, y: this.position.y}; // top
path[1] = {x: path[0].x, y: path[0].y + this.width}; // bottom right
path[2] = {x: path[0].x - this.width, y: this.position.y + this.width}; // bottom left
}
for (var i = 0; i <= path.length - 1; ++i)
{
path[i].x += this.paddingX;
path[i].y += this.paddingY;
}
var selfS = this.calcS(path);
var t = [
{x: path[0].x, y: path[0].y},
{x: path[1].x, y: path[1].y},
{x: dot.x, y: dot.y}
];
var s = this.calcS(t);
t[1] = {x: path[2].x, y: path[2].y};
s += this.calcS(t);
t[0] = {x: path[1].x, y: path[1].y};
s += this.calcS(t);
if (Math.ceil(s) == Math.ceil(selfS))
return true;
else
return false;
}
colorSaver.draw = function () {
canvasHelper.width = this.width;
canvasHelper.height = this.width;
canvasHelperCtx.clearRect(0, 0, this.width, this.width);
canvasHelperCtx.beginPath();
if (this.align == 'left') {
canvasHelperCtx.moveTo(this.lineWidth / 2, this.width - this.lineWidth);
canvasHelperCtx.lineTo(this.width, this.width - this.lineWidth);
canvasHelperCtx.lineTo(this.lineWidth, this.lineWidth);
canvasHelperCtx.lineTo(this.lineWidth, this.width - this.lineWidth);
}
if (this.align == 'right') {
canvasHelperCtx.moveTo(this.lineWidth / 2, this.width - this.lineWidth);
canvasHelperCtx.lineTo(this.width - this.lineWidth, this.width - this.lineWidth);
canvasHelperCtx.lineTo(this.width - this.lineWidth, this.lineWidth);
canvasHelperCtx.lineTo(this.lineWidth, this.width - this.lineWidth);
}
if (this.selected) {
// start draw addition inner figure
canvasHelperCtx.fillStyle = 'rgba(255,255,255, 1)';
canvasHelperCtx.fill();
canvasHelperCtx.strokeStyle = 'rgba(0, 0, 0, 1)';
canvasHelperCtx.stroke();
canvasHelperCtx.closePath();
canvasHelperCtx.beginPath();
canvasHelperCtx.lineWidth = this.lineWidth;
if (this.align == 'left') {
canvasHelperCtx.moveTo(this.selectSize, this.width - this.selectSize);
canvasHelperCtx.lineTo(this.width - this.selectSize * 2, this.width - this.selectSize);
canvasHelperCtx.lineTo(this.selectSize, this.selectSize * 2);
canvasHelperCtx.lineTo(this.selectSize, this.width - this.selectSize);
}
if (this.align == 'right') {
canvasHelperCtx.moveTo(this.selectSize * 2, this.width - this.selectSize);
canvasHelperCtx.lineTo(this.width - this.selectSize, this.width - this.selectSize);
canvasHelperCtx.lineTo(this.width - this.selectSize, this.selectSize * 2);
canvasHelperCtx.lineTo(this.selectSize * 2, this.width - this.selectSize);
}
}
var rgb = hexToRgb(this.color);
canvasHelperCtx.fillStyle = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ', 1)';
canvasHelperCtx.fill();
canvasHelperCtx.strokeStyle = 'rgba(0, 0, 0, 1)';
canvasHelperCtx.stroke();
this.imageData = canvasHelperCtx.getImageData(0, 0, this.width, this.width);
ctx.drawImage(canvasHelper, this.position.x + this.paddingX, this.position.y + this.paddingY);
}
var colorSaverKey = colorSavers.length;
colorSavers[colorSaverKey] = colorSaver;
}
var wheel = new Object;
wheel.width = 18;
wheel.imageData = null; // rendered wheel image data
wheel.innerRadius;
wheel.startAngle = 0; // 150
wheel.outerRadius;
wheel.outerStrokeStyle = 'rgba(0,0,0,0.2)';
wheel.innerStrokeStyle = 'rgba(0,0,0,0.2)';
wheel.pos; // updates in updateSize() | center point; wheel cursor \ hsv quad \ hsv triangle positioned relative that point
wheel.draw = function () {
// put rendered data
if (this.imageData) {
canvasHelper.width = wheelBlockSize;
canvasHelper.height = wheelBlockSize;
canvasHelperCtx.putImageData(this.imageData, 0, 0); // use helper only, put image data directly to ctx cause render bug if combine with draw image ontop after (ex. in triangle.draw) in modern chrome versions
ctx.drawImage(canvasHelper, 0, 0); // draw with save overlaps transparent things , not direct putImageData that rewrite all pixels
} else {
var hAngle = this.startAngle;
for (var angle = 0; angle <= 360; angle++) {
var startAngle = toRadians(angle - 2);
var endAngle = toRadians(angle);
ctx.beginPath();
ctx.moveTo(center, center);
ctx.arc(center, center, this.outerRadius, startAngle, endAngle, false);
ctx.closePath();
var targetRgb = hsvToRgb(hAngle / 360, 1, 1);
ctx.fillStyle = 'rgb(' + targetRgb.r + ', ' + targetRgb.g + ', ' + targetRgb.b + ')';
//ctx.fillStyle = 'hsl('+hAngle+', 100%, 50%)';
ctx.fill();
hAngle++;
if (hAngle >= 360)
hAngle = 0;
}
ctx.globalCompositeOperation = "destination-out"; // cut out color wheel inside by circle next
ctx.beginPath();
ctx.arc(center, center, this.innerRadius, 0, PI * 2);
ctx.fill();
ctx.globalCompositeOperation = "source-over";
ctx.strokeStyle = this.innerStrokeStyle; // 'rgba(0,0,0,0.2)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
// wheel border
ctx.beginPath();
ctx.arc(center, center, this.outerRadius, 0, PI * 2);
ctx.strokeStyle = this.outerStrokeStyle;
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
this.imageData = ctx.getImageData(0, 0, wheelBlockSize, wheelBlockSize);
}
};
wheel.isDotIn = function (dot) {
// is dot in circle
if (Math.pow(this.pos.x - dot.x, 2) + Math.pow(this.pos.y - dot.y, 2) < Math.pow(this.outerRadius, 2)) {
if (Math.pow(this.pos.x - dot.x, 2) + Math.pow(this.pos.y - dot.y, 2) > Math.pow(this.innerRadius, 2)) {
return true;
}
}
return false;
};
var wheelCursor = new Object;
wheelCursor.lineWeight = 2;
wheelCursor.height = 4;
wheelCursor.paddingX = 2; // padding from sides of wheel
wheelCursor.path; // rotatePath2 --- поворот по старой функции, в фигуре не приплюсован центр
var alphaSlider = new Object;
alphaSlider.width = 18;
alphaSlider.padding = 4;
alphaSlider.outerStrokeStyle = 'rgba(0,0,0,0.2)';
alphaSlider.innerStrokeStyle = 'rgba(0,0,0,0.2)';
alphaSlider.height;
alphaSlider.pos; // left top corner position
alphaSlider.updateSize = function () {
this.pos = {x: wheelBlockSize + alphaSlider.padding, y: alphaSlider.padding};
this.height = wheelBlockSize - alphaSlider.padding * 2;
};
alphaSlider.draw = function () {
var alphaGrd = ctx.createLinearGradient(0, 0, 0, this.height);
var aRgb = hsvToRgb(hsv.h, 1, 1);
alphaGrd.addColorStop(0, 'rgba(' + aRgb.r + ',' + aRgb.g + ',' + aRgb.b + ',1)');
alphaGrd.addColorStop(1, 'rgba(' + aRgb.r + ',' + aRgb.g + ',' + aRgb.b + ',0)');
ctx.beginPath();
ctx.rect(this.pos.x, this.pos.y, this.width, this.height);
ctx.fillStyle = "white";
ctx.fill();
ctx.fillStyle = alphaGrd;
ctx.fill();
ctx.strokeStyle = 'rgba(0,0,0, 0.2)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
};
alphaSlider.dotToAlpha = function (dot) {
return 1 - Math.abs(this.pos.y - dot.y) / this.height;
};
alphaSlider.alphaToDot = function (alpha) {
return {
x: 0,
y: this.height - (this.height * alpha)
};
};
alphaSlider.limitDotPosition = function (dot) {
var y = dot.y;
if (y < this.pos.y) {
y = this.pos.y;
}
if (y > this.pos.y + this.height) {
y = this.pos.y + this.height;
}
return {x: this.pos.x, y: y};
};
alphaSlider.isDotIn = function (dot) {
if (dot.x < this.pos.x ||
dot.x > this.pos.x + alphaSlider.width ||
dot.y < this.pos.y ||
dot.y > this.pos.y + this.height) {
return false;
}
return true;
};
// svCursorMouse - для устройств с мышкой, генератор указателя в зависимости от активной области
// todo on very very small sv when set by hex, cursor may be go out of bounds
var svCursorMouse = new Object;
svCursorMouse.svCursorData = null;
svCursorMouse.stCursor = null; // cursor before replace
svCursorMouse.curType = 0; // if > 0 cursor switched by KellyColorPicker to custom
svCursorMouse.size = 16;
svCursorMouse.cEl = document.body;
svCursorMouse.initSvCursor = function () {
if (!canvas)
return false;
this.curType = 1;
if (!this.stCursor) {
this.stCursor = window.getComputedStyle(this.cEl).cursor;
if (!this.stCursor) {
this.stCursor = 'auto';
}
}
if (this.svCursorData) {
this.cEl.style.cursor = this.svCursorData;
return true;
}
if (!canvasHelper)
return false;
// create canvas on 2 pixels bigger for Opera that cut image
var canvasSize = this.size + 2;
canvasHelper.width = canvasSize;
canvasHelper.height = canvasSize;
canvasHelperCtx.clearRect(0, 0, this.size, this.size);
canvasHelperCtx.strokeStyle = 'rgba(255, 255, 255, 1)';
canvasHelperCtx.beginPath();
canvasHelperCtx.lineWidth = 2;
canvasHelperCtx.arc(canvasSize / 2, canvasSize / 2, this.size / 2, 0, PI * 2);
canvasHelperCtx.stroke();
canvasHelperCtx.closePath();
var offset = canvasSize; //if (input.value.indexOf(curImageData) !== -1)
var curImageData = canvasHelper.toDataURL();
this.svCursorData = 'url(' + curImageData + ') ' + offset / 2 + ' ' + offset / 2 + ', auto';
if (!this.svCursorData)
return false;
this.cEl.style.cursor = this.svCursorData;
if (this.cEl.style.cursor.indexOf(curImageData) === -1) { // for autist IE (Edge also), that not support data-uri for cursor -_-
this.svCursorData = 'crosshair';
this.cEl.style.cursor = 'crosshair';
}
return true;
};
svCursorMouse.initStandartCursor = function () {
if (!this.stCursor)
return;
svCursorMouse.curType = 0;
this.cEl.style.cursor = this.stCursor;
};
svCursorMouse.updateCursor = function (newDot) {
if (!changeCursor)
return;
if (KellyColorPicker.cursorLock)
return;
if (svFig.isDotIn(newDot)) {
svCursorMouse.initSvCursor();
} else {
svCursorMouse.initStandartCursor();
}
};
// updateinput
function constructor(cfg) {
var criticalError = '', placeName = '';
// save non-camelased old style options compatibility
if (cfg.alpha_slider !== undefined) {
cfg.alphaSlider = cfg.alpha_slider;
}
if (cfg.input_color !== undefined) {
cfg.inputColor = cfg.input_color;
}
if (cfg.input_format !== undefined) {
cfg.inputFormat = cfg.input_format;
}
// config apply
if (cfg.input && typeof cfg.input !== 'object') {
cfg.input = document.getElementById(cfg.input);
input = cfg.input;
// if (!cfg.input) log += '| "input" (' + inputName + ') not not found';
} else if (typeof cfg.input === 'object') {
input = cfg.input;
}
if (cfg.changeCursor !== undefined) {
changeCursor = cfg.changeCursor;
}
if (cfg.alpha !== undefined) {
a = cfg.alpha;
}
if (cfg.alphaSlider !== undefined) {
alpha = cfg.alphaSlider;
}
if (cfg.inputColor !== undefined) {
inputColor = cfg.inputColor;
}
if (cfg.inputFormat !== undefined) {
inputFormat = cfg.inputFormat;
}
if (cfg.userEvents)
userEvents = cfg.userEvents;
if (cfg.place && typeof cfg.place !== 'object') {
placeName = cfg.place;
cfg.place = document.getElementById(cfg.place);
}
if (cfg.place) {
place = cfg.place;
} else if (input) {
popup.tag = document.createElement('div');
popup.tag.className = "popup-kelly-color";
if (!cfg.popupClass) {
popup.tag.className = "popup-kelly-color";
popup.tag.style.position = 'absolute';
popup.tag.style.bottom = '0px';
popup.tag.style.left = '0px';
popup.tag.style.display = 'none';
popup.tag.style.backgroundColor = '#e1e1e1';
popup.tag.style.border = "1px solid #bfbfbf";
popup.tag.style.boxShadow = "7px 7px 14px -3px rgba(0,0,0,0.24)";
popup.tag.style.borderTopLeftRadius = '4px';
popup.tag.style.borderTopRightRadius = '4px';
popup.tag.style.borderBottomLeftRadius = '4px';
popup.tag.style.borderBottomRightRadius = '4px';
popup.tag.style.padding = "12px";
popup.tag.style.boxSizing = "content-box";
} else {
popup.tag.className = cfg.popupClass;
}
place = popup.tag;
var body = document.getElementsByTagName('body')[0];
body.appendChild(popup.tag);
addEventListner(input, "click", function (e) {
return handler.popUpShow(e);
}, 'popup_');
} // attach directly to input by popup
else
criticalError += '| "place" (' + placeName + ') not not found';
// hex default #000000
var colorData = false;
if (cfg.color) {
colorData = readColorData(cfg.color);
} else if (input && input.value) {
colorData = readColorData(input.value);
}
if (colorData) {
hex = colorData.h;
if (alpha)
a = colorData.a;
}
//if (hex.charAt(0) == '#') hex = hex.slice(1);
//if (hex.length == 3) hex = hex + hex;
//if (hex.length !== 6) hex = '#000000';
if (cfg.method && (cfg.method == 'triangle' || cfg.method == 'quad'))
method = cfg.method;
if (!initCanvas()) {
criticalError += ' | cant init canvas context';
}
// size of elments init
if (cfg.resizeWith) {
if (typeof cfg.resizeWith !== 'object' && typeof cfg.resizeWith !== 'boolean')
cfg.resizeWith = document.getElementById(cfg.resizeWith);
if (cfg.resizeWith === true) {
resizeWith = canvas;
} else {
resizeWith = cfg.resizeWith;
}
if (cfg.resizeSide)
resizeSide = cfg.resizeSide;
if (resizeWith) {
var newSize = getSizeByElement(resizeWith);
if (newSize)
cfg.size = getSizeByElement(resizeWith);
addEventListner(window, "resize", function (e) {
return handler.syncSize(e);
}, 'canvas_');
}
}
if (cfg.size && cfg.size > 0) {
wheelBlockSize = cfg.size;
}
// size init end
if (criticalError) {
if (typeof console !== 'undefined')
console.log('KellyColorPicker : ' + criticalError);
return;
}
if (method == 'quad')
svFig = getSvFigureQuad();
if (method == 'triangle')
svFig = getSvFigureTriangle();
if (input) {
var inputEdit = function (e) {
var e = e || window.event;
if (!e.target) {
e.target = e.srcElement;
}
handler.setColorByHex(e.target.value, true);
};
addEventListner(input, "click", inputEdit, 'input_edit_');
addEventListner(input, "change", inputEdit, 'input_edit_');
addEventListner(input, "keyup", inputEdit, 'input_edit_');
addEventListner(input, "keypress", inputEdit, 'input_edit_');
}
if (cfg.colorSaver) {
initColorSaver('left', true);
initColorSaver('right');
}
if (cfg.methodSwitch) {
initStyleSwitch();
}
enableEvents();
updateSize();
handler.setColorByHex(false); // update color info and first draw
}
// may be zero in some cases / check before applay
function getSizeByElement(el) {
var sizeInfo = el.getBoundingClientRect();
var size = 0;
var sizeReduse = 0;
if (alpha) {
sizeReduse = alphaSlider.width + alphaSlider.padding * 2;
}
if (el === canvas) {
if (sizeInfo.width <= sizeInfo.height)
size = sizeInfo.height;
else if (sizeInfo.height < sizeInfo.width)
size = sizeInfo.width;
} else {
if (resizeSide) {
if (resizeSide == 'height')
size = sizeInfo.height;
else if (resizeSide == 'width')
size = sizeInfo.width;
} else {
if (sizeInfo.width > sizeInfo.height)
size = sizeInfo.height;
else if (sizeInfo.height >= sizeInfo.width)
size = sizeInfo.width;
}
}
size = parseInt(size);
if (alpha) {
size -= sizeReduse;
}
if (size <= 0) {
return false;
}
return size;
}
// Read color value from string cString in rgb \ rgba \ hex \ hsl \ hsla format or from object ex. rgb {r : 255, g : 255, b : 255, a : 1}, hsv {h : 1, s: 1, l : 1, a : 1}
// return array {h : color in rgb hex format (string #000000), a : alpha (float value from 0 to 1) }
// falseOnFail = false - return default array on fail {h : '#000000', a : 1} or return false on fail if true
function readColorData(cString, falseOnFail) {
var alpha = 1;
var h = false;
if (typeof cString == 'string') {
cString = cString.trim(cString);
if (cString.indexOf("(") == -1) { // hex color
if (cString.charAt(0) == '#')
cString = cString.slice(1);
cString = cString.substr(0, 8);
if (cString.length >= 3) {
if (cString.length > 6 && cString.length < 8) {
cString = cString.substr(0, 6); // bad alpha data
}
if (cString.length > 3 && cString.length < 6) {
cString = cString.substr(0, 3); // bad full format
}
h = cString;
// complite full format, by replicating the R, G, and B values
if (cString.length >= 3 && cString.length <= 4) {
h = "";
for (let i = 0; i < cString.length; i++) {
h += cString[i] + cString[i];
}
}
if (h.length == 8)
alpha = (parseInt(h, 16) & 255) / 255;
}
} else {
vals = cString.split(",");
if (vals.length >= 3) {
switch (cString.substring(0, 3)) {
case 'rgb':
vals[0] = vals[0].replace("rgba(", "");
vals[0] = vals[0].replace("rgb(", "");
var rgb = {r: parseInt(vals[0]), g: parseInt(vals[1]), b: parseInt(vals[2])};
if (rgb.r <= 255 && rgb.g <= 255 && rgb.b <= 255) {
h = rgbToHex(rgb);
}
break;
case 'hsl':
vals[0] = vals[0].replace("hsl(", "");
vals[0] = vals[0].replace("hsla(", "");
var hue = parseFloat(vals[0]) / 360.0;
var s = parseFloat(vals[1]) / 100.0; //js will ignore % in the end
var l = parseFloat(vals[2]) / 100.0;
if (hue >= 0 && s <= 1 && l <= 1) {
h = rgbToHex(hsvToRgb(hue, s, l));
}
break;
}
if (vals.length == 4) {
alpha = parseFloat(vals[3]);
if (!alpha || alpha < 0)
alpha = 0;
if (alpha > 1)
alpha = 1;
}
}
}
} else if (typeof cString == 'object') {
if (typeof cString.r != 'undefined' &&
typeof cString.g != 'undefined' &&
typeof cString.b != 'undefined') { // rgb input
h = rgbToHex(cString);
} else if (
typeof cString.h != 'undefined' &&
typeof cString.s != 'undefined' &&
typeof cString.l != 'undefined') {
cString.h = parseFloat(cString.h) / 360.0;
cString.s = parseFloat(cString.s) / 100.0;
cString.l = parseFloat(cString.l) / 100.0;
if (cString.h >= 0 && cString.s <= 1 && cString.l <= 1) {
h = rgbToHex(hsvToRgb(cString.h, cString.s, cString.l));
}
}
if (typeof cString.a != 'undefined') {
alpha = cString.a;
}
}
if (h === false && falseOnFail) {
return false;
}
if (h === false) {
h = '000000';
}
if (h.charAt(0) != '#') {
h = h.substr(0, 6);
h = '#' + h;
} else {
h = h.substr(0, 7); // for private purposes must contain only rgb part
}
return {h: h, a: alpha};
}
function getSvFigureQuad() {
if (svFigsPool['quad'])
return svFigsPool['quad'];