-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.umd.js
2177 lines (2172 loc) · 78.2 KB
/
index.umd.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.chroma = {}));
}(this, (function (exports) { 'use strict';
/**
* @license
*
* js - JavaScript library for color conversions
*
* Copyright (c) 2011-2017, Gregor Aisch
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name Gregor Aisch may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GREGOR AISCH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// tslint:disable:no-unnecessary-qualifier
const { abs, atan2, cos, floor, log, min, max, round, sign, sin, sqrt, cbrt, PI, hypot } = Math;
function lerp(a, b, f) {
return a + (b - a) * f;
}
function lerpInv(a, b, f) {
return (f - a) / (b - a);
}
function clamp(x, min = 0, max = 1) {
return x < min ? min : x > max ? max : x;
}
function newtonIterate1d(f, xStart, max_steps, eps = 1e-8) {
let x = xStart, fx;
while (max_steps-- && abs((fx = f(x))) > eps) {
const dfdx = (f(x + eps) - fx) / eps;
console.log("fx / dfdx", fx / dfdx, "fx", fx, "x", x);
x = x - fx / dfdx;
}
return x;
}
function bisect(f, a, b, steps) {
//assert(a < b)
let fA = f(a);
// let fB = f(b)
//assert(fA * fB < 0)
while (steps--) {
const c = (a + b) / 2;
const fC = f(c);
// console.log("fC", fC, "c", c)
if (sign(fA) == sign(fC)) {
a = c;
fA = fC;
}
else {
b = c;
// fB = fC
}
}
//assert(a <= (b + a) / 2)
//assert(b >= (b + a) / 2)
return (a + b) / 2;
}
const TWOPI = 2 * PI;
const DEG2RAD = PI / 180;
const RAD2DEG = 180 / PI;
function color(...args) {
if (args[0] instanceof Color) {
return args[0];
}
if (args.length > 1 && "string" == typeof args[args.length - 1]) {
return guess(args.slice(0, args.length - 1), args[args.length - 1]);
}
else if (Array.isArray(args[0])) {
return guess(args[0]);
}
else {
return guess(args);
}
}
class Color {
/** @internal */
constructor(r, g, b, a = 1) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
// public shade() {
// const shades: [string, string, number][] = [
// ["ff0000", "red"],
// ["ffa500", "orange"],
// ["ffff00", "yellow"],
// ["008000", "green"],
// ["0000ff", "blue"],
// ["ee82ee", "violet"],
// ["a52a2a", "brown"],
// ["000000", "black"],
// ["808080", "grey"],
// ["ffffff", "white"],
// ] as any
// function angleDiff(a: number, b: number) {
// const d = (a - b) % 360
// if (d > 180) return d - 360
// if (d < -180) return d + 360
// return d
// }
// shades.forEach(arr => arr.push(color(arr[0]).hsl()[0]))
// const [h, s, l] = this.hsl()
// if (l > 0.9) return "white"
// if (l > 0.8 && s < 0.2) return "white"
// if (s < 0.1) return "grey"
// if (s < 0.4 && h > 0 && h < 48) return "brown"
// const distanceInXYZ: { [hue: number]: number } = { 0: 0 }
// for (let i = 60; i <= 360; i += 60) {
// distanceInXYZ[i] =
// distanceInXYZ[i - 60] + distance(hsl(i - 60, 1, 0.5), hsl(i, 1, 0.5), "xyz")
// }
// // console.log(distanceInXYZ)
// const shadeEnds: { [hue: number]: number } = {
// 0: 9,
// 38: 48,
// 60: 65,
// 120: 165,
// 240: 245,
// 300: 338,
// 360: 369,
// }
// const getColorDistanceAlongXYZHue = (hueDegrees: number) => {
// const base = hueDegrees - (hueDegrees % 60)
// return (
// distanceInXYZ[base] + distance(hsl(base, 1, 0.5), hsl(hueDegrees, 1, 0.5), "xyz")
// )
// }
// const colorXYZD = getColorDistanceAlongXYZHue(this.hsl()[0])
// const md = distanceInXYZ[360]
// const shadeHue =
// (Object.keys(shadeEnds) as any[]).find(shadeHue => shadeEnds[shadeHue | 0] >= this.hsl()[0])! % 360
// return shades.find(([_hex, _name, _hue]) => (_hue | 0) === shadeHue)![1]
// // process.exit()
// return withMax(shades, ([_hex, _name, _hue]) => {
// return -abs(angleDiff(this.hsl()[0], _hue))
// })[1]
// return withMax(shades, ([_hex, _name, _hue]) => {
// const [thisL, thisA, thisB] = this.lab()
// const [L, A, B] = color(_hex).lab()
// return -hypot(thisL - L, thisA - A, thisB - B)
// })[1]
// }
/**
* @see [[mix]]
*/
mix(col2, f, m = "rgb") {
return mix(this, col2, f, m);
}
rgb(doRound = true, clamp_ = true) {
const f = (t) => {
if (doRound)
t = round(t);
if (clamp_)
t = clamp(t, 0, 255);
return t;
};
const { r, g, b } = this;
return [f(r), f(g), f(b)];
}
rgba(doRound = true, clamp_ = true) {
const f = (t) => {
if (doRound)
t = round(t);
if (clamp_)
t = clamp(t, 0, 255);
return t;
};
const { r, g, b, a } = this;
return [f(r), f(g), f(b), a];
}
/**
* Return a hex-string representation of this color.
*
* @param mode
* @see #num for a hex-number representation.
* @example chroma.color('yellow').alpha(0.7).hex()
* @example chroma.color('yellow').alpha(0.7).hex('rgba')
* @example chroma.color('yellow').alpha(0.7).hex('argb')
*/
hex(mode = "rgb") {
const { r, g, b, a } = this;
return rgb2hex(r, g, b, a, mode);
}
/**
* Returns the [HSL] representation of this color. hue will always be in [0;360). Values are never NaN.
*
* @example chroma.color('purple').hsl()
*/
hsl() {
const { r, g, b } = this;
return rgb2hsl(r, g, b);
}
/**
* Returns the [HSL] representation of this color. hue will always be in [0;360). Values are never NaN.
*
* @example chroma.color('purple').hsv()
*/
hsv() {
const { r, g, b } = this;
return rgb2hsv(r, g, b);
}
/**
* Returns the [HSL] representation of this color. hue will always be in [0;360). Values are never NaN.
*
* @example chroma.color('purple').hcg()
*/
hcg() {
const { r, g, b } = this;
return rgb2hcg(r, g, b);
}
/**
* Returns a CSS `rgb(...)` or `hsl(...)` string representation that can be used as CSS-color definition. The alpha
* value is not output if it 1.
* @example chroma.color('teal').css() // == "rgb(0,128,128)"
* @example chroma.color('teal').alpha(0.5).css() // == "rgba(0,128,128,0.5)"
* @example chroma.color('teal').css('hsl') // == "hsl(180,100%,25.1%)"
*/
css(mode = "rgb") {
if ("rgb" == mode) {
const { r, g, b, a } = this;
return rgb2css(r, g, b, a);
}
else if ("hsl" == mode) {
return hsl2css(this.hsl(), this.alpha());
}
else {
throw new Error();
}
}
name(closest = false) {
const thisNum = this.num();
const name = Object.keys(w3cx11).find((name) => w3cx11[name] == thisNum);
if (!name && closest) {
const [thisLStar, thisAStar, thisBStar] = this.lab();
return withMax(Object.keys(w3cx11), (name) => {
const [lStar, aStar, bStar] = num(w3cx11[name]).lab();
return -hypot(thisLStar - lStar, thisAStar - aStar, thisBStar - bStar);
});
}
return name;
}
/**
* Get the [CMYK](#CMYK) representation of this color.
*
* @example chroma.color('red').cmyk()
*/
cmyk() {
const { r, g, b } = this;
return rgb2cmyk(r, g, b);
}
/**
* Returns the [GL] representation of this color.
* @example chroma.color('33cc00').gl()
*/
gl() {
const { r, g, b, a } = this;
return [r / 255, g / 255, b / 255, a];
}
luminance(lum1) {
const { r, g, b, a } = this;
const [, Y] = rgb2xyz(r, g, b);
if (undefined === lum1) {
return Y;
}
const inverseLerp = (a, b, val) => (val - a) / (b - a);
if (lum1 > Y) {
// lerp to white
return mix(this, white, inverseLerp(Y, 1, lum1), "xyz").alpha(a);
}
else {
// lerp to black
return mix(black, this, inverseLerp(0, Y, lum1), "xyz").alpha(a);
}
}
/**
* Get color temperature of this color in Kelvin. This only makes sense for colors close to those output by
* kelvin
*
* @example [c = chroma.color('#ff3300'), c.temperature()]
* @example [c = chroma.color('#ffe3cd'), c.temperature()]
* @example [c = chroma.color('#b3ccff'), c.temperature()]
*/
temperature() {
const { r, g, b } = this;
return rgb2kelvin(r, g, b);
}
/**
* Returns a new [Color] with a channel changed.
* @example chroma.color('skyblue').set('hsl.h', 0) // change hue to 0 deg (=red)
* @example chroma.color('hotpink').set('lch.c', 30) // set chromaticity to 30
* @example chroma.color('orangered').set('lab.l', x => x / 2) // half Lab lightness
* @example chroma.color('darkseagreen').set('lch.c', x => x * 2) // double Lch saturation
*/
set(modeAndChannel, value) {
const [mode, channel] = modeAndChannel.split(".");
const src = this[mode]();
const i = mode.indexOf(channel);
if (-1 == i)
throw new Error("invalid channel");
src[i] = "number" == typeof value ? value : value(src[i]);
return color(src, mode).alpha(this.a);
}
/**
* Returns whether this color is outside the RGB color cube and will be clipped/clamped when calling .rgb()
*
* @example [c = chroma.lch( 20, 40, 50), c.clipped()]
* @example [c = chroma.lch( 40, 40, 50), c.clipped()]
* @example [c = chroma.lch( 60, 40, 50), c.clipped()]
* @example [c = chroma.lch( 80, 40, 50), c.clipped()]
* @example [c = chroma.lch(100, 40, 50), c.clipped()]
*/
clipped() {
const { r, g, b } = this;
return !(0 <= r && r <= 255 && 0 <= g && g <= 255 && 0 <= b && b <= 255);
}
/**
* Returns black or white, whichever has the highest contrast to `this`.
* In the readme you should see the result of this.
*
* @example chroma.color('red')
* @example chroma.color('yellow')
*/
textColor() {
return this.luminance() > 0.5 ? black : white;
}
alpha(alpha1) {
if (undefined === alpha1) {
return this.a;
}
const { r, g, b } = this;
return rgb(r, g, b, alpha1);
}
darker(amount = 1) {
const [l, a, b] = this.lab();
return lab(l - LAB_Kn * amount, a, b, this.alpha());
}
/**
*
* @param amount
* @example chroma.color('hotpink')
* @example chroma.color('hotpink').brighter()
* @example chroma.color('hotpink').brighter(2)
* @example chroma.color('hotpink').brighter(3)
*/
brighter(amount = 1) {
return this.darker(-amount);
}
/**
* Returns a new [Color] with increased saturation.
* @param amount How much.
* @example chroma.color('slategray')
* @example chroma.color('slategray').saturate()
* @example chroma.color('slategray').saturate(2)
* @example chroma.color('slategray').saturate(3)
*/
saturate(amount = 1) {
const [l, c, h] = this.lch();
return lch(l, max(0, c + amount * LAB_Kn), h, this.alpha());
}
/**
* Equivalent to `saturate(-amount)`.
* @see #saturate
*/
desaturate(amount = 1) {
return this.saturate(-amount);
}
premultiplied() {
const { r, g, b, a } = this;
return rgb(r * a, g * a, b * a, a);
}
/**
* Returns the [HSI] representation of this color. hue will always be in [0; 360). Values are never NaN.
*
* @example chroma.color('purple').hsi()
*/
hsi() {
const { r, g, b } = this;
return rgb2hsi(r, g, b);
}
/**
* Returns the [LAB] representation of this color.
*
* @example chroma.color('purple').lab()
*/
lab() {
const { r, g, b } = this;
return rgb2lab(r, g, b);
}
/**
* Return a hex-num of this color.
*
* @param mode
* @see #num for a hex-number representation.
* @example chroma.color('yellow').alpha(0.7).hex()
* @example chroma.color('yellow').alpha(0.7).hex('rgba')
* @example chroma.color('yellow').alpha(0.7).hex('argb')
*/
num(mode = "rgb") {
const { r, g, b, a } = this;
return rgb2num(r, g, b, a, mode);
}
/**
* Returns the [LCH] representation of this color. hue will always be in [0; 360). Values are never NaN.
*
* @example chroma.color('purple').lch()
*/
lch() {
const { r, g, b } = this;
return rgb2lch(r, g, b);
}
/**
* Returns the [XYZ] representation of this color. hue will always be in [0; 360). Values are never NaN.
*
* @example chroma.color('purple').xyz()
*/
xyz() {
const { r, g, b } = this;
return rgb2xyz(r, g, b);
}
/**
* Whether this [Color](#Color) is identical (strict equality of r, g, b, a) to `color`.
*/
equals(color) {
const { r, g, b, a } = this;
const { r: r2, g: g2, b: b2, a: a2 } = color;
return r == r2 && g == g2 && b == b2 && a == a2;
}
hashCode() {
return this.num("rgba");
}
/**
* @example chroma.color('red').toSource() // == "rgb(255, 0, 0)"
* @example chroma.rgb(-2, 100.02, 200, 0.5).toSource() // == "rgb(-2, 100.02, 200, 0.5)"
*/
toSource() {
const { r, g, b, a } = this;
return "chroma.rgb(" + r + ", " + g + ", " + b + (a === 1 ? ")" : ", " + a + ")");
}
}
Color.prototype.toString = Color.prototype.css;
Color.prototype.kelvin = Color.prototype.temperature;
/**
* @example chroma.black
*/
const black = new Color(0, 0, 0, 1);
/**
* @example chroma.black
*/
const white = new Color(255, 255, 255, 1);
const brewer = {
OrRd: [0xfff7ec, 0xfee8c8, 0xfdd49e, 0xfdbb84, 0xfc8d59, 0xef6548, 0xd7301f, 0xb30000, 0x7f0000],
PuBu: [0xfff7fb, 0xece7f2, 0xd0d1e6, 0xa6bddb, 0x74a9cf, 0x3690c0, 0x0570b0, 0x045a8d, 0x023858],
BuPu: [0xf7fcfd, 0xe0ecf4, 0xbfd3e6, 0x9ebcda, 0x8c96c6, 0x8c6bb1, 0x88419d, 0x810f7c, 0x4d004b],
Oranges: [0xfff5eb, 0xfee6ce, 0xfdd0a2, 0xfdae6b, 0xfd8d3c, 0xf16913, 0xd94801, 0xa63603, 0x7f2704],
BuGn: [0xf7fcfd, 0xe5f5f9, 0xccece6, 0x99d8c9, 0x66c2a4, 0x41ae76, 0x238b45, 0x006d2c, 0x00441b],
YlOrBr: [0xffffe5, 0xfff7bc, 0xfee391, 0xfec44f, 0xfe9929, 0xec7014, 0xcc4c02, 0x993404, 0x662506],
YlGn: [0xffffe5, 0xf7fcb9, 0xd9f0a3, 0xaddd8e, 0x78c679, 0x41ab5d, 0x238443, 0x006837, 0x004529],
Reds: [0xfff5f0, 0xfee0d2, 0xfcbba1, 0xfc9272, 0xfb6a4a, 0xef3b2c, 0xcb181d, 0xa50f15, 0x67000d],
RdPu: [0xfff7f3, 0xfde0dd, 0xfcc5c0, 0xfa9fb5, 0xf768a1, 0xdd3497, 0xae017e, 0x7a0177, 0x49006a],
Greens: [0xf7fcf5, 0xe5f5e0, 0xc7e9c0, 0xa1d99b, 0x74c476, 0x41ab5d, 0x238b45, 0x006d2c, 0x00441b],
YlGnBu: [0xffffd9, 0xedf8b1, 0xc7e9b4, 0x7fcdbb, 0x41b6c4, 0x1d91c0, 0x225ea8, 0x253494, 0x081d58],
Purples: [0xfcfbfd, 0xefedf5, 0xdadaeb, 0xbcbddc, 0x9e9ac8, 0x807dba, 0x6a51a3, 0x54278f, 0x3f007d],
GnBu: [0xf7fcf0, 0xe0f3db, 0xccebc5, 0xa8ddb5, 0x7bccc4, 0x4eb3d3, 0x2b8cbe, 0x0868ac, 0x084081],
Greys: [0xffffff, 0xf0f0f0, 0xd9d9d9, 0xbdbdbd, 0x969696, 0x737373, 0x525252, 0x252525, 0x000000],
YlOrRd: [0xffffcc, 0xffeda0, 0xfed976, 0xfeb24c, 0xfd8d3c, 0xfc4e2a, 0xe31a1c, 0xbd0026, 0x800026],
PuRd: [0xf7f4f9, 0xe7e1ef, 0xd4b9da, 0xc994c7, 0xdf65b0, 0xe7298a, 0xce1256, 0x980043, 0x67001f],
Blues: [0xf7fbff, 0xdeebf7, 0xc6dbef, 0x9ecae1, 0x6baed6, 0x4292c6, 0x2171b5, 0x08519c, 0x08306b],
PuBuGn: [0xfff7fb, 0xece2f0, 0xd0d1e6, 0xa6bddb, 0x67a9cf, 0x3690c0, 0x02818a, 0x016c59, 0x014636],
Viridis: [0x440154, 0x482777, 0x3f4a8a, 0x31678e, 0x26838f, 0x1f9d8a, 0x6cce5a, 0xb6de2b, 0xfee825],
Spectral: [
0x9e0142,
0xd53e4f,
0xf46d43,
0xfdae61,
0xfee08b,
0xffffbf,
0xe6f598,
0xabdda4,
0x66c2a5,
0x3288bd,
0x5e4fa2,
],
RdYlGn: [
0xa50026,
0xd73027,
0xf46d43,
0xfdae61,
0xfee08b,
0xffffbf,
0xd9ef8b,
0xa6d96a,
0x66bd63,
0x1a9850,
0x006837,
],
RdBu: [
0x67001f,
0xb2182b,
0xd6604d,
0xf4a582,
0xfddbc7,
0xf7f7f7,
0xd1e5f0,
0x92c5de,
0x4393c3,
0x2166ac,
0x053061,
],
PiYG: [
0x8e0152,
0xc51b7d,
0xde77ae,
0xf1b6da,
0xfde0ef,
0xf7f7f7,
0xe6f5d0,
0xb8e186,
0x7fbc41,
0x4d9221,
0x276419,
],
PRGn: [
0x40004b,
0x762a83,
0x9970ab,
0xc2a5cf,
0xe7d4e8,
0xf7f7f7,
0xd9f0d3,
0xa6dba0,
0x5aae61,
0x1b7837,
0x00441b,
],
RdYlBu: [
0xa50026,
0xd73027,
0xf46d43,
0xfdae61,
0xfee090,
0xffffbf,
0xe0f3f8,
0xabd9e9,
0x74add1,
0x4575b4,
0x313695,
],
BrBG: [
0x543005,
0x8c510a,
0xbf812d,
0xdfc27d,
0xf6e8c3,
0xf5f5f5,
0xc7eae5,
0x80cdc1,
0x35978f,
0x01665e,
0x003c30,
],
RdGy: [
0x67001f,
0xb2182b,
0xd6604d,
0xf4a582,
0xfddbc7,
0xffffff,
0xe0e0e0,
0xbababa,
0x878787,
0x4d4d4d,
0x1a1a1a,
],
PuOr: [
0x7f3b08,
0xb35806,
0xe08214,
0xfdb863,
0xfee0b6,
0xf7f7f7,
0xd8daeb,
0xb2abd2,
0x8073ac,
0x542788,
0x2d004b,
],
Set2: [0x66c2a5, 0xfc8d62, 0x8da0cb, 0xe78ac3, 0xa6d854, 0xffd92f, 0xe5c494, 0xb3b3b3],
Accent: [0x7fc97f, 0xbeaed4, 0xfdc086, 0xffff99, 0x386cb0, 0xf0027f, 0xbf5b17, 0x666666],
Set1: [0xe41a1c, 0x377eb8, 0x4daf4a, 0x984ea3, 0xff7f00, 0xffff33, 0xa65628, 0xf781bf, 0x999999],
Set3: [
0x8dd3c7,
0xffffb3,
0xbebada,
0xfb8072,
0x80b1d3,
0xfdb462,
0xb3de69,
0xfccde5,
0xd9d9d9,
0xbc80bd,
0xccebc5,
0xffed6f,
],
Dark2: [0x1b9e77, 0xd95f02, 0x7570b3, 0xe7298a, 0x66a61e, 0xe6ab02, 0xa6761d, 0x666666],
Paired: [
0xa6cee3,
0x1f78b4,
0xb2df8a,
0x33a02c,
0xfb9a99,
0xe31a1c,
0xfdbf6f,
0xff7f00,
0xcab2d6,
0x6a3d9a,
0xffff99,
0xb15928,
],
Pastel2: [0xb3e2cd, 0xfdcdac, 0xcbd5e8, 0xf4cae4, 0xe6f5c9, 0xfff2ae, 0xf1e2cc, 0xcccccc],
Pastel1: [0xfbb4ae, 0xb3cde3, 0xccebc5, 0xdecbe4, 0xfed9a6, 0xffffcc, 0xe5d8bd, 0xfddaec, 0xf2f2f2],
};
/**
* X11 color names
* http://www.w3.org/TR/css3-color/#svg-color
*
* @example chroma.Object.keys(w3cx11).slice(0, 4)
*/
const w3cx11 = {
aliceblue: 0xf0f8ff,
antiquewhite: 0xfaebd7,
aqua: 0x00ffff,
aquamarine: 0x7fffd4,
azure: 0xf0ffff,
beige: 0xf5f5dc,
bisque: 0xffe4c4,
black: 0x000000,
blanchedalmond: 0xffebcd,
blue: 0x0000ff,
blueviolet: 0x8a2be2,
brown: 0xa52a2a,
burlywood: 0xdeb887,
cadetblue: 0x5f9ea0,
chartreuse: 0x7fff00,
chocolate: 0xd2691e,
coral: 0xff7f50,
cornflower: 0x6495ed,
cornflowerblue: 0x6495ed,
cornsilk: 0xfff8dc,
crimson: 0xdc143c,
cyan: 0x00ffff,
darkblue: 0x00008b,
darkcyan: 0x008b8b,
darkgoldenrod: 0xb8860b,
darkgray: 0xa9a9a9,
darkgreen: 0x006400,
darkgrey: 0xa9a9a9,
darkkhaki: 0xbdb76b,
darkmagenta: 0x8b008b,
darkolivegreen: 0x556b2f,
darkorange: 0xff8c00,
darkorchid: 0x9932cc,
darkred: 0x8b0000,
darksalmon: 0xe9967a,
darkseagreen: 0x8fbc8f,
darkslateblue: 0x483d8b,
darkslategray: 0x2f4f4f,
darkslategrey: 0x2f4f4f,
darkturquoise: 0x00ced1,
darkviolet: 0x9400d3,
deeppink: 0xff1493,
deepskyblue: 0x00bfff,
dimgray: 0x696969,
dimgrey: 0x696969,
dodgerblue: 0x1e90ff,
firebrick: 0xb22222,
floralwhite: 0xfffaf0,
forestgreen: 0x228b22,
fuchsia: 0xff00ff,
gainsboro: 0xdcdcdc,
ghostwhite: 0xf8f8ff,
gold: 0xffd700,
goldenrod: 0xdaa520,
gray: 0x808080,
green: 0x008000,
greenyellow: 0xadff2f,
grey: 0x808080,
honeydew: 0xf0fff0,
hotpink: 0xff69b4,
indianred: 0xcd5c5c,
indigo: 0x4b0082,
ivory: 0xfffff0,
khaki: 0xf0e68c,
laserlemon: 0xffff54,
lavender: 0xe6e6fa,
lavenderblush: 0xfff0f5,
lawngreen: 0x7cfc00,
lemonchiffon: 0xfffacd,
lightblue: 0xadd8e6,
lightcoral: 0xf08080,
lightcyan: 0xe0ffff,
lightgoldenrod: 0xfafad2,
lightgoldenrodyellow: 0xfafad2,
lightgray: 0xd3d3d3,
lightgreen: 0x90ee90,
lightgrey: 0xd3d3d3,
lightpink: 0xffb6c1,
lightsalmon: 0xffa07a,
lightseagreen: 0x20b2aa,
lightskyblue: 0x87cefa,
lightslategray: 0x778899,
lightslategrey: 0x778899,
lightsteelblue: 0xb0c4de,
lightyellow: 0xffffe0,
lime: 0x00ff00,
limegreen: 0x32cd32,
linen: 0xfaf0e6,
magenta: 0xff00ff,
maroon: 0x800000,
maroon2: 0x7f0000,
maroon3: 0xb03060,
mediumaquamarine: 0x66cdaa,
mediumblue: 0x0000cd,
mediumorchid: 0xba55d3,
mediumpurple: 0x9370db,
mediumseagreen: 0x3cb371,
mediumslateblue: 0x7b68ee,
mediumspringgreen: 0x00fa9a,
mediumturquoise: 0x48d1cc,
mediumvioletred: 0xc71585,
midnightblue: 0x191970,
mintcream: 0xf5fffa,
mistyrose: 0xffe4e1,
moccasin: 0xffe4b5,
navajowhite: 0xffdead,
navy: 0x000080,
oldlace: 0xfdf5e6,
olive: 0x808000,
olivedrab: 0x6b8e23,
orange: 0xffa500,
orangered: 0xff4500,
orchid: 0xda70d6,
palegoldenrod: 0xeee8aa,
palegreen: 0x98fb98,
paleturquoise: 0xafeeee,
palevioletred: 0xdb7093,
papayawhip: 0xffefd5,
peachpuff: 0xffdab9,
peru: 0xcd853f,
pink: 0xffc0cb,
plum: 0xdda0dd,
powderblue: 0xb0e0e6,
purple: 0x800080,
purple2: 0x7f007f,
purple3: 0xa020f0,
rebeccapurple: 0x663399,
red: 0xff0000,
rosybrown: 0xbc8f8f,
royalblue: 0x4169e1,
saddlebrown: 0x8b4513,
salmon: 0xfa8072,
sandybrown: 0xf4a460,
seagreen: 0x2e8b57,
seashell: 0xfff5ee,
sienna: 0xa0522d,
silver: 0xc0c0c0,
skyblue: 0x87ceeb,
slateblue: 0x6a5acd,
slategray: 0x708090,
slategrey: 0x708090,
snow: 0xfffafa,
springgreen: 0x00ff7f,
steelblue: 0x4682b4,
tan: 0xd2b48c,
teal: 0x008080,
thistle: 0xd8bfd8,
tomato: 0xff6347,
turquoise: 0x40e0d0,
violet: 0xee82ee,
wheat: 0xf5deb3,
white: 0xffffff,
whitesmoke: 0xf5f5f5,
yellow: 0xffff00,
yellowgreen: 0x9acd32,
};
/**
* Return a new [[CubeHelix]].
*
* @example chroma.cubehelix() // use the default helix
* @example chroma.cubehelix().start(200).rotations(-0.5).gamma(0.8).lightness([0.3, 0.8])
*/
function cubehelix(start = 300, rotations = -1.5, hue = 1, gamma = 1, lightness = [0, 1]) {
const f = ((t) => f.at(t));
Object.getOwnPropertyNames(CubeHelix.prototype).forEach((key) => (f[key] = CubeHelix.prototype[key]));
f.start(start);
f.rotations(rotations);
f.hue(hue);
f.gamma(gamma);
f.lightness(lightness);
return f;
}
class CubeHelix {
start(s) {
if (undefined === s) {
return this._start;
}
this._start = s;
return this;
}
rotations(r) {
if (undefined === r) {
return this._rotations;
}
this._rotations = r;
return this;
}
gamma(g) {
if (undefined === g) {
return this._gamma;
}
this._gamma = g;
return this;
}
hue(h) {
if (undefined === h) {
return this._hue;
}
this._hue = Array.isArray(h) ? h : [h, h];
return this;
}
lightness(h) {
if (undefined === h) {
return this._lightness;
}
this._lightness = Array.isArray(h) ? h : [h, h];
return this;
}
/**
* Convert to a [[Scale]].
*
* @example chroma.cubehelix().scale().correctLightness().domain(2, 22)
*/
scale() {
return scale(this);
}
at(fract) {
const a = TWOPI * ((this._start + 120) / 360 + this._rotations * fract);
const l = lerp(this._lightness[0], this._lightness[1], fract) ** this._gamma;
const h = lerp(this._hue[0], this._hue[1], fract);
const amp = (h * l * (1 - l)) / 2;
const cos_a = cos(a);
const sin_a = sin(a);
const r = l + amp * (-0.14861 * cos_a + 1.78277 * sin_a);
const g = l + amp * (-0.29227 * cos_a - 0.90649 * sin_a);
const b = l + amp * (+1.97294 * cos_a);
return rgb([r * 255, g * 255, b * 255, 1]);
}
}
/**
* Create a new random [Color] from a random point in the RGB color space.
* @param randomSource A function which returns random `number`s in the interval [0; 1). Useful if you want to
* create a deterministic sequence of "random" colors. Defaults to `Math.random`.
*/
function random(randomSource = Math.random) {
return num((randomSource() * 16777216) | 0);
}
/**
* Create a valid RGB color (`.clipped() == false`) from a random point in the CIELAB color space. This results in
* more colors in the RGB color space where humans can perceive more differences.
* @param randomSource A function which returns random `number`s in the interval [0; 1). Useful if you want to
* create a deterministic sequence of "random" colors. Defaults to `Math.random`.
* @example chroma.random((() => { let i = 0; return () => (i = (i *Math.SQRT2) % 1); })())
*/
// export function randomLab(randomSource = Math.random) {
// const labAMin = -87,
// labAMax = 99,
// labBMin = -108,
// labBMax = 95
// let maxIterations = 100
// while (maxIterations--) {
// const u = randomSource(),
// v = randomSource(),
// w = randomSource()
// // The following matrix multiplication transform the random point (u v w) in the unit cube into the
// // oriented bounding box (OBB) of the projection of the RGB space into the LAB space. This is necessary to
// // avoid a huge number of misses.
// const color = lab(
// u * -53.903 + v * -88.755 + w * 71.7 + 99.707,
// u * -82.784 + v * 187.036 + w * -2.422 + -28.17,
// u * -75.813 + v * -141.406 + w * -48.261 + 152.469,
// )
// console.log(color.lab())
// console.log(color.rgba(false, false))
// if (!color.clipped()) return color
// }
// throw new Error("Could find a random color in 100 iterations")
// }
/**
* Mixes two colors. The mix ratio is a value between 0 and 1.
* The color mixing produces different results based the color space used for interpolation.
*
* @param col2
* @param f
* @param m
* @example chroma.mix('red', 'blue')
* @example chroma.mix('red', 'blue', 0.25)
* @example chroma.mix('red', 'blue', 0.75)
*
* @example chroma.mix('red', 'blue', 0.5, 'rgb')
* @example chroma.mix('red', 'blue', 0.5, 'hsl')
* @example chroma.mix('red', 'blue', 0.5, 'lab')
* @example chroma.mix('red', 'blue', 0.5, 'lch')
* @example chroma.mix('red', 'blue', 0.5, 'lrgb')
*/
function mix(col1, col2, f = 0.5, m = "rgb") {
const c1 = color(col1);
const c2 = color(col2);
const res = interpolators[m] && interpolators[m](c1, c2, f, m);
if (!res) {
throw new Error("color mode " + m + " is not supported");
}
return res.alpha(lerp(c1.alpha(), c2.alpha(), f));
}
/**
* Parse a CSS color. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/color) for all the possible
* variants.
*
* @example chroma.css('hsl(2rad 90% 50% / 0.9)')
* @example chroma.css('laserlemon')
*/
function css(cssString) {
const [r, g, b, a] = css2rgb(cssString);
return new Color(r, g, b, a);
}
function cmyk(...args) {
return guess(args, "cmyk");
}
function gl(...args) {
return guess(args, "gl");
}
function hcg(...args) {
return guess(args, "hcg");
}
function lch(...args) {
return guess(args, "lch");
}
function hsi(...args) {
return guess(args, "hsi");
}
function hsl(...args) {
return guess(args, "hsl");
}
function hsv(...args) {
return guess(args, "hsv");
}
/**
*
* @param temperature
* @example chroma.kelvin(2000) // candle light
* @example chroma.kelvin(3500) // sunset
* @example chroma.kelvin(6500) // daylight
* @example x0_1 => chroma.kelvin(x0_1 * 30000) // effective range: [0; 30000]
*/
function kelvin(temperature) {
const [r, g, b] = kelvin2rgb(temperature);
return new Color(r, g, b);
}
function lab(...args) {
return guess(args, "lab");
}
/**
* @example chroma.num(0x663399) // rebeccapurple
*/
function num(num) {
const [r, g, b] = num2rgb(num);
return new Color(r, g, b);
}