-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1463 lines (1324 loc) · 53.3 KB
/
index.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
import colorString from 'color-string';
import { mean, std, sqrt } from 'mathjs';
/**
* An abstract data type representating mutable colour entities for RGB, HSV, HSL, CMYK, and HWB colour models.
* @class Colour
*/
export default class Colour {
attributes = {
red: 0,
green: 0,
blue: 0,
hue: 0,
saturationv: 0,
value: 0,
cyan: 0,
magenta: 0,
yellow: 0,
saturationl: 0,
light: 0,
white: 0,
black: 0,
alpha: 0
}
/**
* @constant {number}
* @default 255
*/
static get redMax() { return 255; }
/**
* @constant {number}
* @default 0
*/
static get redMin() { return 0; }
/**
* @constant {number}
* @default 255
*/
static get greenMax() { return 255; }
/**
* @constant {number}
* @default 0
*/
static get greenMin() { return 0; }
/**
* @constant {number}
* @default 255
*/
static get blueMax() { return 255; }
/**
* @constant {number}
* @default 0
*/
static get blueMin() { return 0; }
/**
* @constant {number}
* @default 360
*/
static get hueMax() { return 360; }
/**
* @constant {number}
* @default 0
*/
static get hueMin() { return 0; }
/**
* @constant {number}
* @default 1.00
*/
static get saturationvMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get saturationvMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get valueMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get valueMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get cyanMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get cyanMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get magentaMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get magentaMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get yellowMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get yellowMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get saturationlMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get saturationlMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get lightMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get lightMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get whiteMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get whiteMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get blackMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get blackMin() { return 0.00; }
/**
* @constant {number}
* @default 1.00
*/
static get alphaMax() { return 1.00; }
/**
* @constant {number}
* @default 0.00
*/
static get alphaMin() { return 0.00; }
/**
* Initialize a new Colour instance given a valid CSS colour string or HEX code
* @param {string} colourString - A valid CSS colour string or HEX code to parse.
* @returns {Colour} A new Colour instance generated from the CSS colour string.
* @see [color-string](https://github.com/Qix-/color-string) handles all parsing of CSS colour strings.
*/
constructor(colourString) {
if (colourString != undefined) {
try {
const { model, value } = colorString.get(colourString);
switch (model) {
case "rgb":
return Colour.RGB(value[0], value[1], value[2], value[3]);
case "hsl":
return Colour.HSL(value[0], value[1] / 100, value[2] / 100, value[3]);
case "hwb":
return Colour.HWB(value[0], value[1] / 100, value[2] / 100, value[3]);
}
} catch (error) {
throw new SyntaxError("The given colourString value was not recognized as a valid CSS colour string or HEX code.");
}
}
}
/**
* Initialize a new Colour instance from RGB colour attributes
* @param {number} red - The value of the R channel [0, 255]
* @param {number} green - The value of the G channel [0, 255]
* @param {number} blue - The value of the B channel [0, 255]
* @param {number} [alpha=1.00] - The percentage of transparency or opacity channel [0, 1]
* @returns {Colour} A new Colour instance generated from the RGB colour attributes.
*/
static RGB(red, green, blue, alpha = Colour.alphaMax) {
if (red > Colour.redMax || green > Colour.greenMax || blue > Colour.blueMax || alpha > Colour.alphaMax) {
throw new RangeError("RGB attributes must be in the following acceptable ranges...red:[0, 255], green:[0, 255], blue:[0, 255], alpha:[0, 1].");
} else if (red < Colour.redMin || green < Colour.greenMin || blue < Colour.blueMin || alpha < Colour.alphaMin) {
throw new RangeError("RGB attributes must be in the following acceptable ranges...red:[0, 255], green:[0, 255], blue:[0, 255], alpha:[0, 1].");
}
let colour = new Colour();
colour.attributes.red = roundInt(red);
colour.attributes.green = roundInt(green);
colour.attributes.blue = roundInt(blue);
colour.attributes.alpha = roundFloat(alpha);
colour.#updateHSV();
colour.#updateHSL();
colour.#updateCMYK();
colour.#updateHWB();
return colour;
}
/**
* Initialize a new Colour instance from HSV colour attributes
* @param {number} hue - The value of the H channel [0, 360]
* @param {number} saturationv - The percentage of the S channel [0, 1]
* @param {number} value - The percentage of the V channel [0, 1]
* @param {number} [alpha=1.00] - The percentage of transparency or opacity channel [0, 1]
* @returns {Colour} A new Colour instance generated from the HSV colour attributes.
*/
static HSV(hue, saturationv, value, alpha = Colour.alphaMax) {
if (hue > Colour.hueMax || saturationv > Colour.saturationvMax || value > Colour.valueMax || alpha > Colour.alphaMax) {
throw new RangeError("HSV attributes must be in the following acceptable ranges...hue:[0, 360], saturationv:[0, 1], value:[0, 1], alpha:[0, 1].");
} else if (hue < Colour.hueMin || saturationv < Colour.saturationvMin || value < Colour.valueMin || alpha < Colour.alphaMin) {
throw new RangeError("HSV attributes must be in the following acceptable ranges...hue:[0, 360], saturationv:[0, 1], value:[0, 1], alpha:[0, 1].");
}
let colour = new Colour();
colour.attributes.hue = roundInt(hue);
colour.attributes.saturationv = roundFloat(saturationv);
colour.attributes.value = roundFloat(value);
colour.attributes.alpha = roundFloat(alpha);
colour.#updateRGB_HSV();
colour.#updateCMYK();
colour.#updateHSL();
colour.#updateHWB();
return colour;
}
/**
* Initialize a new Colour instance from CMYK colour attributes
* @param {number} cyan - The value of the C channel [0, 1]
* @param {number} magenta - The value of the M channel [0, 1]
* @param {number} yellow - The value of the Y channel [0, 1]
* @param {number} black - The value of the K channel [0, 1]
* @param {number} [alpha=1.00] - The percentage of transparency or opacity channel [0, 1]
* @returns {Colour} A new Colour instance generated from the CMYK colour attributes.
*/
static CMYK(cyan, magenta, yellow, black, alpha = Colour.alphaMax) {
if (cyan > Colour.cyanMax || magenta > Colour.magentaMax || yellow > Colour.yellowMax || black > Colour.blackMax || alpha > Colour.alphaMax) {
throw new RangeError("CMYK attributes must be in the following acceptable range...cyan:[0, 1], magenta:[0, 1], yellow:[0, 1], black:[0, 1], alpha:[0, 1].");
}
else if (cyan < Colour.cyanMin || magenta < Colour.magentaMin || yellow < Colour.yellowMin || black < Colour.blackMin || alpha < Colour.alphaMin) {
throw new RangeError("CMYK attributes must be in the following acceptable range...cyan:[0, 1], magenta:[0, 1], yellow:[0, 1], black:[0, 1], alpha:[0, 1].");
}
let colour = new Colour();
colour.attributes.cyan = roundFloat(cyan);
colour.attributes.magenta = roundFloat(magenta);
colour.attributes.yellow = roundFloat(yellow);
colour.attributes.black = roundFloat(black);
colour.attributes.alpha = roundFloat(alpha);
colour.#updateRGB_CMYK();
colour.#updateHSV();
colour.#updateHSL();
colour.#updateHWB();
return colour;
}
/**
* Initialize a new Colour instance from HSL colour attributes
* @param {number} hue - The value of the H channel [0, 360]
* @param {number} saturationl - The value of the S channel [0, 1]
* @param {number} light - The value of the L channel [0, 1]
* @param {number} [alpha=1.00] - The percentage of transparency or opacity channel [0, 1]
* @returns {Colour} A new Colour instance generated from the HSL colour attributes.
*/
static HSL(hue, saturationl, light, alpha = Colour.alphaMax) {
if (hue > Colour.hueMax || saturationl > Colour.saturationlMax || light > Colour.lightMax || alpha > Colour.alphaMax) {
throw new RangeError("HSL attributes must be in the following acceptable ranges...hue:[0, 360], saturationl:[0, 1], light:[0, 1], alpha:[0, 1].");
}
else if (hue < Colour.hueMin || saturationl < Colour.saturationlMin || light < Colour.lightMin || alpha < Colour.alphaMin) {
throw new RangeError("HSL attributes must be in the following acceptable ranges...hue:[0, 360], saturationl:[0, 1], light:[0, 1], alpha:[0, 1].");
}
let colour = new Colour();
colour.attributes.hue = roundInt(hue);
colour.attributes.saturationl = roundFloat(saturationl);
colour.attributes.light = roundFloat(light);
colour.attributes.alpha = roundFloat(alpha);
colour.#updateRGB_HSL();
colour.#updateCMYK();
colour.#updateHSV();
colour.#updateHWB();
return colour;
}
/**
*
* @param {number} hue - The value of the H channel [0, 360]
* @param {number} white - The value of the W channel [0, 1]
* @param {number} black - The value of the B channel [0, 360]
* @param {number} [alpha=1.00] - The percentage of transparency or opacity channel [0, 1]
* @returns {Colour} A new Colour instance generated from the HWB colour attributes.
*/
static HWB(hue, white, black, alpha = Colour.alphaMax) {
if (hue > Colour.hueMax || white > Colour.whiteMax || black > Colour.blackMax || alpha > Colour.alphaMax) {
throw new RangeError("HWB attributes must be in the following acceptable ranges...hue:[0, 360], white:[0, 1], black:[0, 1], alpha:[0, 1].");
}
else if (hue < Colour.hueMin || white < Colour.whiteMin || black < Colour.blackMin || alpha < Colour.alphaMin) {
throw new RangeError("HWB attributes must be in the following acceptable ranges...hue:[0, 360], white:[0, 1], black:[0, 1], alpha:[0, 1].");
}
let colour = new Colour();
colour.attributes.hue = roundInt(hue);
colour.attributes.white = roundFloat(white);
colour.attributes.black = roundFloat(black);
colour.attributes.alpha = roundFloat(alpha);
colour.#updateHSV_HWB();
colour.#updateRGB_HSV();
colour.#updateCMYK();
colour.#updateHSL();
return colour;
}
/**
* @type {number}
*/
get red() { return this.attributes.red; }
set red(newRed) {
if (newRed < Colour.redMin || newRed > Colour.redMax) {
throw new RangeError("The red attribute must be in the following acceptable range: [0, 255].");
}
this.attributes.red = roundInt(newRed);
this.#updateHSV();
this.#updateHSL();
this.#updateCMYK();
this.#updateHWB();
}
/**
* @type {number}
*/
get green() { return this.attributes.green; }
set green(newGreen) {
if (newGreen < Colour.greenMin || newGreen > Colour.greenMax) {
throw new RangeError("The green attribute must be in the following acceptable range: [0, 255].");
}
this.attributes.green = roundInt(newGreen);
this.#updateHSV();
this.#updateHSL();
this.#updateCMYK();
this.#updateHWB();
}
/**
* @type {number}
*/
get blue() { return this.attributes.blue; }
set blue(newBlue) {
if (newBlue < Colour.blueMin || newBlue > Colour.blueMax) {
throw new RangeError("The blue attribute must be in the following acceptable range: [0, 255].");
}
this.attributes.blue = roundInt(newBlue);
this.#updateHSV();
this.#updateHSL();
this.#updateCMYK();
this.#updateHWB();
}
/**
* @type {number}
*/
get hue() { return this.attributes.hue; }
set hue(newHue) {
if (newHue < Colour.hueMin || newHue >= Colour.hueMax) {
throw new RangeError("The hue attribute must be in the following acceptable range: [0, 360].");
}
this.attributes.hue = roundInt(newHue);
this.#updateRGB_HSV();
this.#updateCMYK();
this.#updateHSL();
this.#updateHWB();
}
/**
* @type {number}
*/
get saturationv() { return this.attributes.saturationv; }
set saturationv(newSaturationv) {
if (newSaturationv < Colour.saturationvMin || newSaturationv > Colour.saturationvMax) {
throw new RangeError("The saturationv attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.saturationv = roundFloat(newSaturationv);
this.#updateRGB_HSV();
this.#updateCMYK();
this.#updateHSL();
this.#updateHWB();
}
/**
* @type {number}
*/
get value() { return this.attributes.value; }
set value(newValue) {
if (newValue < Colour.valueMin || newValue > Colour.valueMax) {
throw new RangeError("The value attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.value = roundFloat(newValue);
this.#updateRGB_HSV();
this.#updateCMYK();
this.#updateHSL();
this.#updateHWB();
}
/**
* @type {number}
*/
get cyan() { return this.attributes.cyan; }
set cyan(newCyan) {
if (newCyan < Colour.cyanMin || newCyan > Colour.cyanMax) {
throw new RangeError("The cyan attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.cyan = roundFloat(newCyan);
this.#updateRGB_CMYK();
this.#updateHSL();
this.#updateHSV();
this.#updateHWB();
}
/**
* @type {number}
*/
get magenta() { return this.attributes.magenta; }
set magenta(newMagenta) {
if (newMagenta < Colour.magentaMin || newMagenta > Colour.magentaMax) {
throw new RangeError("The magenta attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.magenta = roundFloat(newMagenta);
this.#updateRGB_CMYK();
this.#updateHSL();
this.#updateHSV();
this.#updateHWB();
}
/**
* @type {number}
*/
get yellow() { return this.attributes.yellow; }
set yellow(newYellow) {
if (newYellow < Colour.yellowMin || newYellow > Colour.yellowMax) {
throw new RangeError("The yellow attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.yellow = roundFloat(newYellow);
this.#updateRGB_CMYK();
this.#updateHSL();
this.#updateHSV();
this.#updateHWB();
}
/**
* @type {number}
*/
get saturationl() { return this.attributes.saturationl; }
set saturationl(newSaturationl) {
if (newSaturationl < Colour.saturationlMin || newSaturationl > Colour.saturationlMax) {
throw new RangeError("The saturationl attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.saturationl = roundFloat(newSaturationl);
this.#updateRGB_HSL();
this.#updateHSV();
this.#updateCMYK();
this.#updateHWB();
}
/**
* @type {number}
*/
get light() { return this.attributes.light; }
set light(newLight) {
if (newLight < Colour.lightMin || newLight > Colour.lightMax) {
throw new RangeError("The light attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.light = roundFloat(newLight);
this.#updateRGB_HSL();
this.#updateHSV();
this.#updateCMYK();
this.#updateHWB();
}
/**
* @type {number}
*/
get white() { return this.attributes.white; }
set white(newWhite) {
if (newWhite < Colour.whiteMin || newWhite > Colour.whiteMax) {
throw new RangeError("The white attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.white = roundFloat(newWhite);
this.#updateHSV_HWB();
this.#updateRGB_HSV();
this.#updateCMYK();
this.#updateHSL();
}
/**
* @type {number}
*/
get black() { return this.attributes.black; }
set black(newBlack) {
if (newBlack < Colour.blackMin || newBlack > Colour.blackMax) {
throw new RangeError("The black attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.black = roundFloat(newBlack);
this.#updateHSV_HWB();
this.#updateRGB_HSV();
this.#updateCMYK();
this.#updateHSL();
}
/**
* @type {number}
*/
get alpha() { return this.attributes.alpha; }
set alpha(newAlpha) {
if (newAlpha < Colour.alphaMin || newAlpha > Colour.alphaMax) {
throw new RangeError("The alpha attribute must be in the following acceptable range: [0, 1].");
}
this.attributes.alpha = roundFloat(newAlpha);
}
/**
* Check if another Colour instance is equivalent
* @param {Colour} colour - Another Colour instance to compare equality with.
* @returns True if both colours have the same red, green, blue, and alpha values.
*/
equals(colour) {
return (
this.red == colour.red &&
this.green == colour.green &&
this.blue == colour.blue &&
this.alpha == colour.alpha
);
}
/**
* Returns a copy of the Colour instance.
* @returns {Colour} An identical Colour object
*/
copy() {
return Colour.RGB(this.red, this.green, this.blue, this.alpha);
}
/**
* Return a valid hexadecimal colour code that represents the colour.
* @returns {Colour} A HEX code representing the colour.
*/
toHEX() {
return colorString.to.hex(this.red, this.green, this.blue, this.alpha);
}
#updateHSV() {
//https://www.rapidtables.com/convert/color/rgb-to-hsv.html
let cMax = Math.max(this.attributes.red, this.attributes.green, this.attributes.blue) / 255;
let cMin = Math.min(this.attributes.red, this.attributes.green, this.attributes.blue) / 255;
let delta = cMax - cMin;
//Updating hue
if (delta == 0) {
this.attributes.hue = 0;
} else if (cMax == this.attributes.red / 255) {
this.attributes.hue = roundInt(60 * (((this.attributes.green / 255 - this.attributes.blue / 255) / delta) % 6));
} else if (cMax == this.attributes.green / 255) {
this.attributes.hue = roundInt(60 * (((this.attributes.blue / 255 - this.attributes.red / 255) / delta) + 2));
} else if (cMax == this.attributes.blue / 255) {
this.attributes.hue = roundInt(60 * (((this.attributes.red / 255 - this.attributes.green / 255) / delta) + 4));
}
// hue loop bug fix
if (this.attributes.hue < 0) { this.attributes.hue += 360; }
//Updating saturation
if (cMax == 0) {
this.attributes.saturationv = 0;
} else {
this.attributes.saturationv = roundFloat(delta / cMax);
}
//Updating Value
this.attributes.value = roundFloat(cMax);
}
#updateRGB_HSV() {
//https://www.rapidtables.com/convert/color/hsv-to-rgb.html
let c = this.attributes.value * this.attributes.saturationv;
let x = c * (1 - Math.abs((this.attributes.hue / 60) % 2 - 1));
let m = this.attributes.value - c;
if (this.attributes.hue >= 0 && this.attributes.hue < 60) {
this.attributes.red = roundInt((c + m) * 255);
this.attributes.green = roundInt((x + m) * 255);
this.attributes.blue = roundInt((0 + m) * 255);
} else if (this.attributes.hue >= 60 && this.attributes.hue < 120) {
this.attributes.red = roundInt((x + m) * 255);
this.attributes.green = roundInt((c + m) * 255);
this.attributes.blue = roundInt((0 + m) * 255);
} else if (this.attributes.hue >= 120 && this.attributes.hue < 180) {
this.attributes.red = roundInt((0 + m) * 255);
this.attributes.green = roundInt((c + m) * 255);
this.attributes.blue = roundInt((x + m) * 255);
} else if (this.attributes.hue >= 180 && this.attributes.hue < 240) {
this.attributes.red = roundInt((0 + m) * 255);
this.attributes.green = roundInt((x + m) * 255);
this.attributes.blue = roundInt((c + m) * 255);
} else if (this.attributes.hue >= 240 && this.attributes.hue < 300) {
this.attributes.red = roundInt((x + m) * 255);
this.attributes.green = roundInt((0 + m) * 255);
this.attributes.blue = roundInt((c + m) * 255);
} else if (this.attributes.hue >= 300 && this.attributes.hue < 360) {
this.attributes.red = roundInt((c + m) * 255);
this.attributes.green = roundInt((0 + m) * 255);
this.attributes.blue = roundInt((x + m) * 255);
}
}
#updateRGB_CMYK() {
this.attributes.red = roundInt(255 * (1 - this.attributes.cyan) * (1 - this.attributes.black));
this.attributes.green = roundInt(255 * (1 - this.attributes.magenta) * (1 - this.attributes.black));
this.attributes.blue = roundInt(255 * (1 - this.attributes.yellow) * (1 - this.attributes.black));
}
#updateRGB_HSL() {
//https://www.rapidtables.com/convert/color/hsl-to-rgb.html
let c = (1 - Math.abs(2 * this.attributes.light - 1)) * this.attributes.saturationl;
let x = c * (1 - Math.abs((this.attributes.hue / 60) % 2 - 1));
let m = this.attributes.light - c / 2;
if (this.attributes.hue >= 0 && this.attributes.hue < 60) {
this.attributes.red = roundInt((c + m) * 255);
this.attributes.green = roundInt((x + m) * 255);
this.attributes.blue = roundInt((0 + m) * 255);
} else if (this.attributes.hue >= 60 && this.attributes.hue < 120) {
this.attributes.red = roundInt((x + m) * 255);
this.attributes.green = roundInt((c + m) * 255);
this.attributes.blue = roundInt((0 + m) * 255);
} else if (this.attributes.hue >= 120 && this.attributes.hue < 180) {
this.attributes.red = roundInt((0 + m) * 255);
this.attributes.green = roundInt((c + m) * 255);
this.attributes.blue = roundInt((x + m) * 255);
} else if (this.attributes.hue >= 180 && this.attributes.hue < 240) {
this.attributes.red = roundInt((0 + m) * 255);
this.attributes.green = roundInt((x + m) * 255);
this.attributes.blue = roundInt((c + m) * 255);
} else if (this.attributes.hue >= 240 && this.attributes.hue < 300) {
this.attributes.red = roundInt((x + m) * 255);
this.attributes.green = roundInt((0 + m) * 255);
this.attributes.blue = roundInt((c + m) * 255);
} else if (this.attributes.hue >= 300 && this.attributes.hue < 360) {
this.attributes.red = roundInt((c + m) * 255);
this.attributes.green = roundInt((0 + m) * 255);
this.attributes.blue = roundInt((x + m) * 255);
}
}
#updateHSV_HWB() {
this.attributes.saturationv = roundFloat(1 - this.attributes.white / (1 - this.attributes.black));
this.attributes.value = roundFloat(1 - this.attributes.black);
}
#updateHWB() {
this.attributes.white = roundFloat(Math.min(this.attributes.red, this.attributes.green, this.attributes.blue) / 255);
this.attributes.black = roundFloat(1 - Math.max(this.attributes.red, this.attributes.green, this.attributes.blue) / 255);
}
#updateCMYK() {
let black = 1 - Math.max(this.attributes.red, this.attributes.green, this.attributes.blue) / 255;
this.attributes.black = roundFloat(black);
this.attributes.cyan = roundFloat((1 - this.attributes.red / 255 - black) / (1 - black));
this.attributes.magenta = roundFloat((1 - this.attributes.green / 255 - black) / (1 - black));
this.attributes.yellow = roundFloat((1 - this.attributes.blue / 255 - black) / (1 - black));
}
#updateHSL() {
let r = this.attributes.red / 255;
let g = this.attributes.green / 255;
let b = this.attributes.blue / 255;
let cMax = Math.max(r, g, b);
let cMin = Math.min(r, g, b);
let delta = cMax - cMin;
this.attributes.light = roundFloat((cMax + cMin) / 2);
if (delta == 0) {
this.attributes.saturationl = 0.00;
}
else {
this.attributes.saturationl = roundFloat(delta / (1 - Math.abs(cMax + cMin - 1)));
}
}
}
function roundInt(num) {
return Math.round(num);
}
function roundFloat(num) {
return Number(num.toFixed(2).replace('-0', '0'));
}
/**
* Determine the inverse colour or the colour on the opposite side of the colour wheel.
* @param {Colour} colour - The colour to invert
* @returns {Colour} The colour resulting from the negation
*/
export function negate(colour) {
let red = 255 - colour.red;
let green = 255 - colour.green;
let blue = 255 - colour.blue;
let alpha = colour.alpha;
return Colour.RGB(red, green, blue, alpha);
}
/**
* Rotate a given colour a certain number of degrees in 3-dimensional space.
* @param {Colour} colour - The colour to rotate
* @param {number} degrees - The number of degrees to rotate the colour
* @returns {Colour} - The colour resulting from the rotation
*/
export function rotate(colour, degrees) {
let hue = colour.hue;
hue = (hue + degrees) % 360;
hue = hue < 0 ? 360 + hue : hue;
return Colour.HSL(hue, colour.saturationl, colour.light, colour.alpha);
}
/**
* Determine the equivalent [grayscale colour of a given colour](https://www.tutorialspoint.com/dip/grayscale_to_rgb_conversion.htm).
* @param {Colour} colour - The colour to grayscale
* @returns {Colour} The resulting colour from the grayscale transformation
*/
export function grayscale(colour) {
let weightedTotal = roundInt(0.3 * colour.red + 0.59 * colour.green + 0.11 * colour.blue);
return Colour.RGB(weightedTotal, weightedTotal, weightedTotal, colour.alpha);
}
/**
* Calculate the [WCAG contrast ratio](http://www.w3.org/TR/WCAG20/#contrast-ratiodef)
* between two colours. (Note: The order of the colours does not matter!)
* @param {Colour} colour1 - The first colour to be compared
* @param {Colour} colour2 - The second colour to be compared
* @returns {number} The WCAG contrast ratio of the two colours (values ranging between 1 and 21)
*/
export function contrast(colour1, colour2) {
const lum1 = luminosity(colour1);
const lum2 = luminosity(colour2);
if (lum1 > lum2) {
return roundInt(((lum1 + 0.05) / (lum2 + 0.05)) * 100) / 100;
}
return roundInt(((lum2 + 0.05) / (lum1 + 0.05)) * 100) / 100;
}
/**
* Calculate the [colourfulness index](https://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf) of a given colour as defined by Hasler and Süsstrunk (2003).
* @param {Colour} colour - The colour to calculate colourfulness of.
* @returns {number} The resulting colourfulness grading
*/
export function colourfulness(colour) {
let rg = Math.abs(colour.red - colour.green);
let yb = Math.abs(0.5 * (colour.red + colour.green) - colour.blue);
// compute the mean and standard deviation of both `rg` and `yb`.
let rg_mean = mean(rg);
let rg_std = std(rg);
let yb_mean = mean(yb);
let yb_std = std(yb);
// combine the mean and standard deviations.
let std_root = sqrt((rg_std ** 2) + (yb_std ** 2));
let mean_root = sqrt((rg_mean ** 2) + (yb_mean ** 2));
return std_root + (0.3 * mean_root);
}
/**
* Calculate the [temperature](https://ams.com/documents/20143/80162/TCS34xx_AN000517_1-00.pdf) of a given colour.
* @param {Colour} colour - The colour to calculate temperature of.
* @returns {number} The resulting temperature grading in degrees Kelvin (K)
*/
export function temperature(colour) {
// get XYZ values (CIE tristimulus values)
let X = -0.14282 * colour.red + 1.54924 * colour.green + -0.95641 * colour.blue;
let Y = -0.32466 * colour.red + 1.57837 * colour.green + -0.73191 * colour.blue;
let Z = -0.68202 * colour.red + 0.77073 * colour.green + 0.56332 * colour.blue;
// normalize values
let x = X / (X + Y + Z);
let y = Y / (X + Y + Z);
// calculate correlated colour temperature
let n = (x - 0.3320) / (0.1858 - y);
let CCT = 449 * (n ** 3) + 3525 * (n ** 2) + 6823.3 * n + 5520.33;
return CCT;
}
/**
* Calculate the [relative luminance](https://www.w3.org/WAI/GL/wiki/Relative_luminance) of a given colour as defined by the WCAG.
* @param {Colour} colour - The colour to calculate luminence of.
* @returns {number} The resulting luminence grading
*/
export function luminosity(colour) {
const lum = [];
const rgb = [colour.red, colour.green, colour.blue];
for (let i = 0; i < rgb.length; i++) {
const chan = rgb[i] / 255;
lum[i] = (chan <= 0.03928) ? (chan / 12.92) : ((chan + 0.055) / 1.055) ** 2.4;
}
return 0.2126 * lum[0] + 0.7152 * lum[1] + 0.0722 * lum[2];
}
/**
* Evenly interpolate two colours and produce the resulting midpoint colour.
* @param {Colour} colour1 - The first colour to include in the mix
* @param {Colour} colour2 - The second colour to include in the mix
* @param {number} percent - The percentage of the blend colour to mix
* @returns {Colour} The colour resulting from the mix
*/
export function mix(colour1, colour2, percent) {
const weight = 2 * percent - 1;
const alpha = colour2.alpha - colour1.alpha;
const weight1 = (((weight * alpha === -1) ? weight : (weight + alpha) / (1 + weight * alpha)) + 1) / 2;
const weight2 = 1 - weight1;
return Colour.RGB(
weight1 * colour2.red + weight2 * colour1.red,
weight1 * colour2.green + weight2 * colour1.green,
weight1 * colour2.blue + weight2 * colour1.blue,
colour2.alpha * percent + colour1.alpha * (1 - percent)
);
}
/**
* Interpolate a given colour with white to create a tint.
* @param {Colour} colour - A colour to tint
* @param {number} percent - The percentage of white to mix; setting 100% results in #FFFFFF
* @returns {Colour} The colour resulting from tinting the original colour
*/
export function tint(colour, percent) {
return mix(
colour,
new Colour("#FFFFFF"),
percent
);
}
/**
* Interpolate a given colour with black to create a shade.
* @param {Colour} colour - A colour to shade
* @param {number} percent - The percentage of black to mix; setting 100% results in #000000
* @returns {Colour} The colour resulting from shading the original colour
*/
export function shade(colour, percent) {
return mix(
colour,
new Colour("#000000"),
percent
);
}
/**
* Interpolate a given colour with gray to create a tone.
* @param {Colour} colour - A colour to tone
* @param {number} percent - The percentage of gray to mix; setting 100% results in #808080
* @returns {Colour} The colour resulting from toning the original colour
*/
export function tone(colour, percent) {
return mix(
colour,
new Colour("#808080"),
percent
);
}
/**
* This is the blend mode which specifies no blending. The blending formula simply selects the blend color.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function normal(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,
_separableBlend,
_normal
);
}
/**
* Looks at the colour information in each channel and multiplies the base colour by the blend colour. The result colour is always a darker colour. Multiplying any colour with black produces black. Multiplying any colour with white leaves the colour unchanged.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function multiply(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,
_separableBlend,
_multiply
);
}
/**
* Looks at each channel’s colour information and multiplies the inverse of the blend and base colours. The result colour is always a lighter colour. Screening with black leaves the colour unchanged. Screening with white produces white. The effect is similar to projecting multiple photographic slides on top of each other.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function screen(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,
_separableBlend,
_screen
);
}
/**
* Multiplies or screens the colors, depending on the base color. The base color is not replaced, but mixed with the blend color to reflect the lightness or darkness of the original color.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function overlay(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,
_separableBlend,
_overlay
);
}
/**
* Looks at the colour information in each channel and selects the base or blend colour—whichever is darker—as the result colour.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function darken(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,
_separableBlend,
_darken
);
}
/**
* Looks at the colour information in each channel and selects the base or blend colour—whichever is lighter—as the result colour.
* @param {Colour} baseColour - The base colour being blended
* @param {Colour} blendColour - The colour being applied with the designated blend mode
* @returns {Colour} The colour resulting from the blend
*/
export function lighten(baseColour, blendColour) {
return _blend(
baseColour,
blendColour,