@likec4/core 1.35.0 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +1 -1
  2. package/dist/builder/index.d.mts +15 -4
  3. package/dist/builder/index.mjs +13 -10
  4. package/dist/compute-view/index.d.mts +3 -3
  5. package/dist/compute-view/index.mjs +10 -8
  6. package/dist/compute-view/relationships-view/index.d.mts +3 -3
  7. package/dist/index.d.mts +2 -2
  8. package/dist/index.mjs +7 -3
  9. package/dist/model/connection/deployment/index.d.mts +5 -5
  10. package/dist/model/connection/deployment/index.mjs +3 -2
  11. package/dist/model/connection/index.d.mts +6 -6
  12. package/dist/model/connection/index.mjs +3 -2
  13. package/dist/model/connection/model/index.d.mts +5 -5
  14. package/dist/model/index.d.mts +8 -8
  15. package/dist/model/index.mjs +6 -4
  16. package/dist/shared/{core.DAj3LuFH.d.mts → core.BFb3p5d-.d.mts} +4 -2
  17. package/dist/shared/{core.RnUJywKk.mjs → core.BlwZ4tBa.mjs} +9 -5
  18. package/dist/shared/{core.DhTO8rgm.d.mts → core.Bt3kuGUN.d.mts} +3 -3
  19. package/dist/shared/{core.CQS11dNK.mjs → core.Bus32P0S.mjs} +2 -1
  20. package/dist/shared/{core.rpF8esVq.mjs → core.BvIGRozf.mjs} +13 -3
  21. package/dist/shared/{core.B1ZN723l.mjs → core.CaJp8Amg.mjs} +3 -2
  22. package/dist/shared/{core.D56nCp7e.mjs → core.CaaYNQDM.mjs} +1 -3
  23. package/dist/shared/core.CnIyafWn.mjs +3 -0
  24. package/dist/shared/{core.72IoI17N.d.mts → core.D8bTI5KC.d.mts} +2 -2
  25. package/dist/shared/{core.NBNeVyag.d.mts → core.DHYd2-VU.d.mts} +3 -3
  26. package/dist/shared/{core.SciGxb--.d.mts → core.DoNP3Yl-.d.mts} +6 -5
  27. package/dist/shared/{core.Ddua-wiI.d.mts → core.DxDn5nB5.d.mts} +21 -6
  28. package/dist/shared/{core.BajmN8zR.mjs → core._jnYNUQ-.mjs} +4 -66
  29. package/dist/theme/index.mjs +2 -3508
  30. package/dist/types/_aux.d.mts +1 -1
  31. package/dist/types/index.d.mts +3 -3
  32. package/dist/types/index.mjs +6 -4
  33. package/dist/types/scalar.d.mts +2 -0
  34. package/dist/types/scalar.mjs +68 -0
  35. package/package.json +8 -14
  36. package/src/builder/Builder.ts +17 -6
  37. package/src/builder/__snapshots__/Builder-style1.spec.ts.snap +9 -0
  38. package/src/builder/__snapshots__/Builder-style2.compute-model.json5 +3 -0
  39. package/src/builder/__snapshots__/Builder-style2.spec.ts.snap +3 -0
  40. package/src/builder/_types.ts +10 -0
  41. package/src/compute-view/dynamic-view/compute.ts +2 -0
  42. package/src/compute-view/element-view/__test__/fixture.ts +1 -0
  43. package/src/compute-view/utils/applyCustomRelationProperties.ts +2 -0
  44. package/src/model/LikeC4Model.ts +16 -6
  45. package/src/theme/element.ts +0 -19
  46. package/src/types/_aux.ts +6 -0
  47. package/src/types/index.ts +1 -0
  48. package/src/types/model-data.ts +2 -0
  49. package/src/types/model-dump.ts +8 -1
  50. package/src/types/project.ts +9 -0
  51. package/src/types/view-computed.ts +1 -1
  52. package/src/types/view-parsed.dynamic.ts +2 -2
@@ -1,3495 +1,8 @@
1
+ import { generateColors } from '@mantine/colors-generator';
2
+ import chroma from 'chroma-js';
1
3
  import { k } from '../shared/core.BpJu1RrF.mjs';
2
4
  import '../shared/core.DbRvwARP.mjs';
3
5
 
4
- const { min: min$4, max: max$4 } = Math;
5
-
6
- const limit = (x, low = 0, high = 1) => {
7
- return min$4(max$4(low, x), high);
8
- };
9
-
10
- const clip_rgb = (rgb) => {
11
- rgb._clipped = false;
12
- rgb._unclipped = rgb.slice(0);
13
- for (let i = 0; i <= 3; i++) {
14
- if (i < 3) {
15
- if (rgb[i] < 0 || rgb[i] > 255) rgb._clipped = true;
16
- rgb[i] = limit(rgb[i], 0, 255);
17
- } else if (i === 3) {
18
- rgb[i] = limit(rgb[i], 0, 1);
19
- }
20
- }
21
- return rgb;
22
- };
23
-
24
- // ported from jQuery's $.type
25
- const classToType = {};
26
- for (let name of [
27
- 'Boolean',
28
- 'Number',
29
- 'String',
30
- 'Function',
31
- 'Array',
32
- 'Date',
33
- 'RegExp',
34
- 'Undefined',
35
- 'Null'
36
- ]) {
37
- classToType[`[object ${name}]`] = name.toLowerCase();
38
- }
39
- function type (obj) {
40
- return classToType[Object.prototype.toString.call(obj)] || 'object';
41
- }
42
-
43
- const unpack = (args, keyOrder = null) => {
44
- // if called with more than 3 arguments, we return the arguments
45
- if (args.length >= 3) return Array.prototype.slice.call(args);
46
- // with less than 3 args we check if first arg is object
47
- // and use the keyOrder string to extract and sort properties
48
- if (type(args[0]) == 'object' && keyOrder) {
49
- return keyOrder
50
- .split('')
51
- .filter((k) => args[0][k] !== undefined)
52
- .map((k) => args[0][k]);
53
- }
54
- // otherwise we just return the first argument
55
- // (which we suppose is an array of args)
56
- return args[0].slice(0);
57
- };
58
-
59
- const last = (args) => {
60
- if (args.length < 2) return null;
61
- const l = args.length - 1;
62
- if (type(args[l]) == 'string') return args[l].toLowerCase();
63
- return null;
64
- };
65
-
66
- const { PI: PI$2, min: min$3, max: max$3 } = Math;
67
-
68
- const rnd2 = (a) => Math.round(a * 100) / 100;
69
- const rnd3 = (a) => Math.round(a * 100) / 100;
70
-
71
- const TWOPI = PI$2 * 2;
72
- const PITHIRD = PI$2 / 3;
73
- const DEG2RAD = PI$2 / 180;
74
- const RAD2DEG = 180 / PI$2;
75
-
76
- /**
77
- * Reverse the first three elements of an array
78
- *
79
- * @param {any[]} arr
80
- * @returns {any[]}
81
- */
82
- function reverse3(arr) {
83
- return [...arr.slice(0, 3).reverse(), ...arr.slice(3)];
84
- }
85
-
86
- const input = {
87
- format: {},
88
- autodetect: []
89
- };
90
-
91
- class Color {
92
- constructor(...args) {
93
- const me = this;
94
- if (
95
- type(args[0]) === 'object' &&
96
- args[0].constructor &&
97
- args[0].constructor === this.constructor
98
- ) {
99
- // the argument is already a Color instance
100
- return args[0];
101
- }
102
- // last argument could be the mode
103
- let mode = last(args);
104
- let autodetect = false;
105
- if (!mode) {
106
- autodetect = true;
107
-
108
- if (!input.sorted) {
109
- input.autodetect = input.autodetect.sort((a, b) => b.p - a.p);
110
- input.sorted = true;
111
- }
112
-
113
- // auto-detect format
114
- for (let chk of input.autodetect) {
115
- mode = chk.test(...args);
116
- if (mode) break;
117
- }
118
- }
119
- if (input.format[mode]) {
120
- const rgb = input.format[mode].apply(
121
- null,
122
- autodetect ? args : args.slice(0, -1)
123
- );
124
- me._rgb = clip_rgb(rgb);
125
- } else {
126
- throw new Error('unknown format: ' + args);
127
- }
128
- // add alpha channel
129
- if (me._rgb.length === 3) me._rgb.push(1);
130
- }
131
- toString() {
132
- if (type(this.hex) == 'function') return this.hex();
133
- return `[${this._rgb.join(',')}]`;
134
- }
135
- }
136
-
137
- // this gets updated automatically
138
- const version = '3.1.2';
139
-
140
- const chroma = (...args) => {
141
- return new Color(...args);
142
- };
143
-
144
- chroma.version = version;
145
-
146
- /**
147
- X11 color names
148
-
149
- http://www.w3.org/TR/css3-color/#svg-color
150
- */
151
-
152
- const w3cx11 = {
153
- aliceblue: '#f0f8ff',
154
- antiquewhite: '#faebd7',
155
- aqua: '#00ffff',
156
- aquamarine: '#7fffd4',
157
- azure: '#f0ffff',
158
- beige: '#f5f5dc',
159
- bisque: '#ffe4c4',
160
- black: '#000000',
161
- blanchedalmond: '#ffebcd',
162
- blue: '#0000ff',
163
- blueviolet: '#8a2be2',
164
- brown: '#a52a2a',
165
- burlywood: '#deb887',
166
- cadetblue: '#5f9ea0',
167
- chartreuse: '#7fff00',
168
- chocolate: '#d2691e',
169
- coral: '#ff7f50',
170
- cornflowerblue: '#6495ed',
171
- cornsilk: '#fff8dc',
172
- crimson: '#dc143c',
173
- cyan: '#00ffff',
174
- darkblue: '#00008b',
175
- darkcyan: '#008b8b',
176
- darkgoldenrod: '#b8860b',
177
- darkgray: '#a9a9a9',
178
- darkgreen: '#006400',
179
- darkgrey: '#a9a9a9',
180
- darkkhaki: '#bdb76b',
181
- darkmagenta: '#8b008b',
182
- darkolivegreen: '#556b2f',
183
- darkorange: '#ff8c00',
184
- darkorchid: '#9932cc',
185
- darkred: '#8b0000',
186
- darksalmon: '#e9967a',
187
- darkseagreen: '#8fbc8f',
188
- darkslateblue: '#483d8b',
189
- darkslategray: '#2f4f4f',
190
- darkslategrey: '#2f4f4f',
191
- darkturquoise: '#00ced1',
192
- darkviolet: '#9400d3',
193
- deeppink: '#ff1493',
194
- deepskyblue: '#00bfff',
195
- dimgray: '#696969',
196
- dimgrey: '#696969',
197
- dodgerblue: '#1e90ff',
198
- firebrick: '#b22222',
199
- floralwhite: '#fffaf0',
200
- forestgreen: '#228b22',
201
- fuchsia: '#ff00ff',
202
- gainsboro: '#dcdcdc',
203
- ghostwhite: '#f8f8ff',
204
- gold: '#ffd700',
205
- goldenrod: '#daa520',
206
- gray: '#808080',
207
- green: '#008000',
208
- greenyellow: '#adff2f',
209
- grey: '#808080',
210
- honeydew: '#f0fff0',
211
- hotpink: '#ff69b4',
212
- indianred: '#cd5c5c',
213
- indigo: '#4b0082',
214
- ivory: '#fffff0',
215
- khaki: '#f0e68c',
216
- laserlemon: '#ffff54',
217
- lavender: '#e6e6fa',
218
- lavenderblush: '#fff0f5',
219
- lawngreen: '#7cfc00',
220
- lemonchiffon: '#fffacd',
221
- lightblue: '#add8e6',
222
- lightcoral: '#f08080',
223
- lightcyan: '#e0ffff',
224
- lightgoldenrod: '#fafad2',
225
- lightgoldenrodyellow: '#fafad2',
226
- lightgray: '#d3d3d3',
227
- lightgreen: '#90ee90',
228
- lightgrey: '#d3d3d3',
229
- lightpink: '#ffb6c1',
230
- lightsalmon: '#ffa07a',
231
- lightseagreen: '#20b2aa',
232
- lightskyblue: '#87cefa',
233
- lightslategray: '#778899',
234
- lightslategrey: '#778899',
235
- lightsteelblue: '#b0c4de',
236
- lightyellow: '#ffffe0',
237
- lime: '#00ff00',
238
- limegreen: '#32cd32',
239
- linen: '#faf0e6',
240
- magenta: '#ff00ff',
241
- maroon: '#800000',
242
- maroon2: '#7f0000',
243
- maroon3: '#b03060',
244
- mediumaquamarine: '#66cdaa',
245
- mediumblue: '#0000cd',
246
- mediumorchid: '#ba55d3',
247
- mediumpurple: '#9370db',
248
- mediumseagreen: '#3cb371',
249
- mediumslateblue: '#7b68ee',
250
- mediumspringgreen: '#00fa9a',
251
- mediumturquoise: '#48d1cc',
252
- mediumvioletred: '#c71585',
253
- midnightblue: '#191970',
254
- mintcream: '#f5fffa',
255
- mistyrose: '#ffe4e1',
256
- moccasin: '#ffe4b5',
257
- navajowhite: '#ffdead',
258
- navy: '#000080',
259
- oldlace: '#fdf5e6',
260
- olive: '#808000',
261
- olivedrab: '#6b8e23',
262
- orange: '#ffa500',
263
- orangered: '#ff4500',
264
- orchid: '#da70d6',
265
- palegoldenrod: '#eee8aa',
266
- palegreen: '#98fb98',
267
- paleturquoise: '#afeeee',
268
- palevioletred: '#db7093',
269
- papayawhip: '#ffefd5',
270
- peachpuff: '#ffdab9',
271
- peru: '#cd853f',
272
- pink: '#ffc0cb',
273
- plum: '#dda0dd',
274
- powderblue: '#b0e0e6',
275
- purple: '#800080',
276
- purple2: '#7f007f',
277
- purple3: '#a020f0',
278
- rebeccapurple: '#663399',
279
- red: '#ff0000',
280
- rosybrown: '#bc8f8f',
281
- royalblue: '#4169e1',
282
- saddlebrown: '#8b4513',
283
- salmon: '#fa8072',
284
- sandybrown: '#f4a460',
285
- seagreen: '#2e8b57',
286
- seashell: '#fff5ee',
287
- sienna: '#a0522d',
288
- silver: '#c0c0c0',
289
- skyblue: '#87ceeb',
290
- slateblue: '#6a5acd',
291
- slategray: '#708090',
292
- slategrey: '#708090',
293
- snow: '#fffafa',
294
- springgreen: '#00ff7f',
295
- steelblue: '#4682b4',
296
- tan: '#d2b48c',
297
- teal: '#008080',
298
- thistle: '#d8bfd8',
299
- tomato: '#ff6347',
300
- turquoise: '#40e0d0',
301
- violet: '#ee82ee',
302
- wheat: '#f5deb3',
303
- white: '#ffffff',
304
- whitesmoke: '#f5f5f5',
305
- yellow: '#ffff00',
306
- yellowgreen: '#9acd32'
307
- };
308
-
309
- const RE_HEX = /^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
310
- const RE_HEXA = /^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$/;
311
-
312
- const hex2rgb = (hex) => {
313
- if (hex.match(RE_HEX)) {
314
- // remove optional leading #
315
- if (hex.length === 4 || hex.length === 7) {
316
- hex = hex.substr(1);
317
- }
318
- // expand short-notation to full six-digit
319
- if (hex.length === 3) {
320
- hex = hex.split('');
321
- hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
322
- }
323
- const u = parseInt(hex, 16);
324
- const r = u >> 16;
325
- const g = (u >> 8) & 0xff;
326
- const b = u & 0xff;
327
- return [r, g, b, 1];
328
- }
329
-
330
- // match rgba hex format, eg #FF000077
331
- if (hex.match(RE_HEXA)) {
332
- if (hex.length === 5 || hex.length === 9) {
333
- // remove optional leading #
334
- hex = hex.substr(1);
335
- }
336
- // expand short-notation to full eight-digit
337
- if (hex.length === 4) {
338
- hex = hex.split('');
339
- hex =
340
- hex[0] +
341
- hex[0] +
342
- hex[1] +
343
- hex[1] +
344
- hex[2] +
345
- hex[2] +
346
- hex[3] +
347
- hex[3];
348
- }
349
- const u = parseInt(hex, 16);
350
- const r = (u >> 24) & 0xff;
351
- const g = (u >> 16) & 0xff;
352
- const b = (u >> 8) & 0xff;
353
- const a = Math.round(((u & 0xff) / 0xff) * 100) / 100;
354
- return [r, g, b, a];
355
- }
356
-
357
- // we used to check for css colors here
358
- // if _input.css? and rgb = _input.css hex
359
- // return rgb
360
-
361
- throw new Error(`unknown hex color: ${hex}`);
362
- };
363
-
364
- const { round: round$5 } = Math;
365
-
366
- const rgb2hex = (...args) => {
367
- let [r, g, b, a] = unpack(args, 'rgba');
368
- let mode = last(args) || 'auto';
369
- if (a === undefined) a = 1;
370
- if (mode === 'auto') {
371
- mode = a < 1 ? 'rgba' : 'rgb';
372
- }
373
- r = round$5(r);
374
- g = round$5(g);
375
- b = round$5(b);
376
- const u = (r << 16) | (g << 8) | b;
377
- let str = '000000' + u.toString(16); //#.toUpperCase();
378
- str = str.substr(str.length - 6);
379
- let hxa = '0' + round$5(a * 255).toString(16);
380
- hxa = hxa.substr(hxa.length - 2);
381
- switch (mode.toLowerCase()) {
382
- case 'rgba':
383
- return `#${str}${hxa}`;
384
- case 'argb':
385
- return `#${hxa}${str}`;
386
- default:
387
- return `#${str}`;
388
- }
389
- };
390
-
391
- Color.prototype.name = function () {
392
- const hex = rgb2hex(this._rgb, 'rgb');
393
- for (let n of Object.keys(w3cx11)) {
394
- if (w3cx11[n] === hex) return n.toLowerCase();
395
- }
396
- return hex;
397
- };
398
-
399
- input.format.named = (name) => {
400
- name = name.toLowerCase();
401
- if (w3cx11[name]) return hex2rgb(w3cx11[name]);
402
- throw new Error('unknown color name: ' + name);
403
- };
404
-
405
- input.autodetect.push({
406
- p: 5,
407
- test: (h, ...rest) => {
408
- if (!rest.length && type(h) === 'string' && w3cx11[h.toLowerCase()]) {
409
- return 'named';
410
- }
411
- }
412
- });
413
-
414
- Color.prototype.alpha = function (a, mutate = false) {
415
- if (a !== undefined && type(a) === 'number') {
416
- if (mutate) {
417
- this._rgb[3] = a;
418
- return this;
419
- }
420
- return new Color([this._rgb[0], this._rgb[1], this._rgb[2], a], 'rgb');
421
- }
422
- return this._rgb[3];
423
- };
424
-
425
- Color.prototype.clipped = function () {
426
- return this._rgb._clipped || false;
427
- };
428
-
429
- const labConstants = {
430
- // Corresponds roughly to RGB brighter/darker
431
- Kn: 18,
432
-
433
- // D65 standard referent
434
- labWhitePoint: 'd65',
435
- Xn: 0.95047,
436
- Yn: 1,
437
- Zn: 1.08883,
438
-
439
- kE: 216.0 / 24389.0,
440
- kKE: 8.0,
441
- kK: 24389.0 / 27.0,
442
-
443
- RefWhiteRGB: {
444
- // sRGB
445
- X: 0.95047,
446
- Y: 1,
447
- Z: 1.08883
448
- },
449
-
450
- MtxRGB2XYZ: {
451
- m00: 0.4124564390896922,
452
- m01: 0.21267285140562253,
453
- m02: 0.0193338955823293,
454
- m10: 0.357576077643909,
455
- m11: 0.715152155287818,
456
- m12: 0.11919202588130297,
457
- m20: 0.18043748326639894,
458
- m21: 0.07217499330655958,
459
- m22: 0.9503040785363679
460
- },
461
-
462
- MtxXYZ2RGB: {
463
- m00: 3.2404541621141045,
464
- m01: -0.9692660305051868,
465
- m02: 0.055643430959114726,
466
- m10: -1.5371385127977166,
467
- m11: 1.8760108454466942,
468
- m12: -0.2040259135167538,
469
- m20: -0.498531409556016,
470
- m21: 0.041556017530349834,
471
- m22: 1.0572251882231791
472
- },
473
-
474
- // used in rgb2xyz
475
- As: 0.9414285350000001,
476
- Bs: 1.040417467,
477
- Cs: 1.089532651,
478
-
479
- MtxAdaptMa: {
480
- m00: 0.8951,
481
- m01: -0.7502,
482
- m02: 0.0389,
483
- m10: 0.2664,
484
- m11: 1.7135,
485
- m12: -0.0685,
486
- m20: -0.1614,
487
- m21: 0.0367,
488
- m22: 1.0296
489
- },
490
-
491
- MtxAdaptMaI: {
492
- m00: 0.9869929054667123,
493
- m01: 0.43230526972339456,
494
- m02: -0.008528664575177328,
495
- m10: -0.14705425642099013,
496
- m11: 0.5183602715367776,
497
- m12: 0.04004282165408487,
498
- m20: 0.15996265166373125,
499
- m21: 0.0492912282128556,
500
- m22: 0.9684866957875502
501
- }
502
- };
503
-
504
- // taken from https://de.mathworks.com/help/images/ref/whitepoint.html
505
- const ILLUMINANTS = new Map([
506
- // ASTM E308-01
507
- ['a', [1.0985, 0.35585]],
508
- // Wyszecki & Stiles, p. 769
509
- ['b', [1.0985, 0.35585]],
510
- // C ASTM E308-01
511
- ['c', [0.98074, 1.18232]],
512
- // D50 (ASTM E308-01)
513
- ['d50', [0.96422, 0.82521]],
514
- // D55 (ASTM E308-01)
515
- ['d55', [0.95682, 0.92149]],
516
- // D65 (ASTM E308-01)
517
- ['d65', [0.95047, 1.08883]],
518
- // E (ASTM E308-01)
519
- ['e', [1, 1, 1]],
520
- // F2 (ASTM E308-01)
521
- ['f2', [0.99186, 0.67393]],
522
- // F7 (ASTM E308-01)
523
- ['f7', [0.95041, 1.08747]],
524
- // F11 (ASTM E308-01)
525
- ['f11', [1.00962, 0.6435]],
526
- ['icc', [0.96422, 0.82521]]
527
- ]);
528
-
529
- function setLabWhitePoint(name) {
530
- const ill = ILLUMINANTS.get(String(name).toLowerCase());
531
- if (!ill) {
532
- throw new Error('unknown Lab illuminant ' + name);
533
- }
534
- labConstants.labWhitePoint = name;
535
- labConstants.Xn = ill[0];
536
- labConstants.Zn = ill[1];
537
- }
538
-
539
- function getLabWhitePoint() {
540
- return labConstants.labWhitePoint;
541
- }
542
-
543
- /*
544
- * L* [0..100]
545
- * a [-100..100]
546
- * b [-100..100]
547
- */
548
- const lab2rgb = (...args) => {
549
- args = unpack(args, 'lab');
550
- const [L, a, b] = args;
551
- const [x, y, z] = lab2xyz(L, a, b);
552
- const [r, g, b_] = xyz2rgb(x, y, z);
553
- return [r, g, b_, args.length > 3 ? args[3] : 1];
554
- };
555
-
556
- const lab2xyz = (L, a, b) => {
557
- const { kE, kK, kKE, Xn, Yn, Zn } = labConstants;
558
-
559
- const fy = (L + 16.0) / 116.0;
560
- const fx = 0.002 * a + fy;
561
- const fz = fy - 0.005 * b;
562
-
563
- const fx3 = fx * fx * fx;
564
- const fz3 = fz * fz * fz;
565
-
566
- const xr = fx3 > kE ? fx3 : (116.0 * fx - 16.0) / kK;
567
- const yr = L > kKE ? Math.pow((L + 16.0) / 116.0, 3.0) : L / kK;
568
- const zr = fz3 > kE ? fz3 : (116.0 * fz - 16.0) / kK;
569
-
570
- const x = xr * Xn;
571
- const y = yr * Yn;
572
- const z = zr * Zn;
573
-
574
- return [x, y, z];
575
- };
576
-
577
- const compand = (linear) => {
578
- /* sRGB */
579
- const sign = Math.sign(linear);
580
- linear = Math.abs(linear);
581
- return (
582
- (linear <= 0.0031308
583
- ? linear * 12.92
584
- : 1.055 * Math.pow(linear, 1.0 / 2.4) - 0.055) * sign
585
- );
586
- };
587
-
588
- const xyz2rgb = (x, y, z) => {
589
- const { MtxAdaptMa, MtxAdaptMaI, MtxXYZ2RGB, RefWhiteRGB, Xn, Yn, Zn } =
590
- labConstants;
591
-
592
- const As = Xn * MtxAdaptMa.m00 + Yn * MtxAdaptMa.m10 + Zn * MtxAdaptMa.m20;
593
- const Bs = Xn * MtxAdaptMa.m01 + Yn * MtxAdaptMa.m11 + Zn * MtxAdaptMa.m21;
594
- const Cs = Xn * MtxAdaptMa.m02 + Yn * MtxAdaptMa.m12 + Zn * MtxAdaptMa.m22;
595
-
596
- const Ad =
597
- RefWhiteRGB.X * MtxAdaptMa.m00 +
598
- RefWhiteRGB.Y * MtxAdaptMa.m10 +
599
- RefWhiteRGB.Z * MtxAdaptMa.m20;
600
- const Bd =
601
- RefWhiteRGB.X * MtxAdaptMa.m01 +
602
- RefWhiteRGB.Y * MtxAdaptMa.m11 +
603
- RefWhiteRGB.Z * MtxAdaptMa.m21;
604
- const Cd =
605
- RefWhiteRGB.X * MtxAdaptMa.m02 +
606
- RefWhiteRGB.Y * MtxAdaptMa.m12 +
607
- RefWhiteRGB.Z * MtxAdaptMa.m22;
608
-
609
- const X1 =
610
- (x * MtxAdaptMa.m00 + y * MtxAdaptMa.m10 + z * MtxAdaptMa.m20) *
611
- (Ad / As);
612
- const Y1 =
613
- (x * MtxAdaptMa.m01 + y * MtxAdaptMa.m11 + z * MtxAdaptMa.m21) *
614
- (Bd / Bs);
615
- const Z1 =
616
- (x * MtxAdaptMa.m02 + y * MtxAdaptMa.m12 + z * MtxAdaptMa.m22) *
617
- (Cd / Cs);
618
-
619
- const X2 =
620
- X1 * MtxAdaptMaI.m00 + Y1 * MtxAdaptMaI.m10 + Z1 * MtxAdaptMaI.m20;
621
- const Y2 =
622
- X1 * MtxAdaptMaI.m01 + Y1 * MtxAdaptMaI.m11 + Z1 * MtxAdaptMaI.m21;
623
- const Z2 =
624
- X1 * MtxAdaptMaI.m02 + Y1 * MtxAdaptMaI.m12 + Z1 * MtxAdaptMaI.m22;
625
-
626
- const r = compand(
627
- X2 * MtxXYZ2RGB.m00 + Y2 * MtxXYZ2RGB.m10 + Z2 * MtxXYZ2RGB.m20
628
- );
629
- const g = compand(
630
- X2 * MtxXYZ2RGB.m01 + Y2 * MtxXYZ2RGB.m11 + Z2 * MtxXYZ2RGB.m21
631
- );
632
- const b = compand(
633
- X2 * MtxXYZ2RGB.m02 + Y2 * MtxXYZ2RGB.m12 + Z2 * MtxXYZ2RGB.m22
634
- );
635
-
636
- return [r * 255, g * 255, b * 255];
637
- };
638
-
639
- const rgb2lab = (...args) => {
640
- const [r, g, b, ...rest] = unpack(args, 'rgb');
641
- const [x, y, z] = rgb2xyz(r, g, b);
642
- const [L, a, b_] = xyz2lab(x, y, z);
643
- return [L, a, b_, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
644
- };
645
-
646
- function xyz2lab(x, y, z) {
647
- const { Xn, Yn, Zn, kE, kK } = labConstants;
648
- const xr = x / Xn;
649
- const yr = y / Yn;
650
- const zr = z / Zn;
651
-
652
- const fx = xr > kE ? Math.pow(xr, 1.0 / 3.0) : (kK * xr + 16.0) / 116.0;
653
- const fy = yr > kE ? Math.pow(yr, 1.0 / 3.0) : (kK * yr + 16.0) / 116.0;
654
- const fz = zr > kE ? Math.pow(zr, 1.0 / 3.0) : (kK * zr + 16.0) / 116.0;
655
-
656
- return [116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)];
657
- }
658
-
659
- function gammaAdjustSRGB(companded) {
660
- const sign = Math.sign(companded);
661
- companded = Math.abs(companded);
662
- const linear =
663
- companded <= 0.04045
664
- ? companded / 12.92
665
- : Math.pow((companded + 0.055) / 1.055, 2.4);
666
- return linear * sign;
667
- }
668
-
669
- const rgb2xyz = (r, g, b) => {
670
- // normalize and gamma adjust
671
- r = gammaAdjustSRGB(r / 255);
672
- g = gammaAdjustSRGB(g / 255);
673
- b = gammaAdjustSRGB(b / 255);
674
-
675
- const { MtxRGB2XYZ, MtxAdaptMa, MtxAdaptMaI, Xn, Yn, Zn, As, Bs, Cs } =
676
- labConstants;
677
-
678
- let x = r * MtxRGB2XYZ.m00 + g * MtxRGB2XYZ.m10 + b * MtxRGB2XYZ.m20;
679
- let y = r * MtxRGB2XYZ.m01 + g * MtxRGB2XYZ.m11 + b * MtxRGB2XYZ.m21;
680
- let z = r * MtxRGB2XYZ.m02 + g * MtxRGB2XYZ.m12 + b * MtxRGB2XYZ.m22;
681
-
682
- const Ad = Xn * MtxAdaptMa.m00 + Yn * MtxAdaptMa.m10 + Zn * MtxAdaptMa.m20;
683
- const Bd = Xn * MtxAdaptMa.m01 + Yn * MtxAdaptMa.m11 + Zn * MtxAdaptMa.m21;
684
- const Cd = Xn * MtxAdaptMa.m02 + Yn * MtxAdaptMa.m12 + Zn * MtxAdaptMa.m22;
685
-
686
- let X = x * MtxAdaptMa.m00 + y * MtxAdaptMa.m10 + z * MtxAdaptMa.m20;
687
- let Y = x * MtxAdaptMa.m01 + y * MtxAdaptMa.m11 + z * MtxAdaptMa.m21;
688
- let Z = x * MtxAdaptMa.m02 + y * MtxAdaptMa.m12 + z * MtxAdaptMa.m22;
689
-
690
- X *= Ad / As;
691
- Y *= Bd / Bs;
692
- Z *= Cd / Cs;
693
-
694
- x = X * MtxAdaptMaI.m00 + Y * MtxAdaptMaI.m10 + Z * MtxAdaptMaI.m20;
695
- y = X * MtxAdaptMaI.m01 + Y * MtxAdaptMaI.m11 + Z * MtxAdaptMaI.m21;
696
- z = X * MtxAdaptMaI.m02 + Y * MtxAdaptMaI.m12 + Z * MtxAdaptMaI.m22;
697
-
698
- return [x, y, z];
699
- };
700
-
701
- Color.prototype.lab = function () {
702
- return rgb2lab(this._rgb);
703
- };
704
-
705
- const lab$1 = (...args) => new Color(...args, 'lab');
706
- Object.assign(chroma, { lab: lab$1, getLabWhitePoint, setLabWhitePoint });
707
-
708
- input.format.lab = lab2rgb;
709
-
710
- input.autodetect.push({
711
- p: 2,
712
- test: (...args) => {
713
- args = unpack(args, 'lab');
714
- if (type(args) === 'array' && args.length === 3) {
715
- return 'lab';
716
- }
717
- }
718
- });
719
-
720
- Color.prototype.darken = function (amount = 1) {
721
- const me = this;
722
- const lab = me.lab();
723
- lab[0] -= labConstants.Kn * amount;
724
- return new Color(lab, 'lab').alpha(me.alpha(), true);
725
- };
726
-
727
- Color.prototype.brighten = function (amount = 1) {
728
- return this.darken(-amount);
729
- };
730
-
731
- Color.prototype.darker = Color.prototype.darken;
732
- Color.prototype.brighter = Color.prototype.brighten;
733
-
734
- Color.prototype.get = function (mc) {
735
- const [mode, channel] = mc.split('.');
736
- const src = this[mode]();
737
- if (channel) {
738
- const i = mode.indexOf(channel) - (mode.substr(0, 2) === 'ok' ? 2 : 0);
739
- if (i > -1) return src[i];
740
- throw new Error(`unknown channel ${channel} in mode ${mode}`);
741
- } else {
742
- return src;
743
- }
744
- };
745
-
746
- const { pow: pow$6 } = Math;
747
-
748
- const EPS = 1e-7;
749
- const MAX_ITER = 20;
750
-
751
- Color.prototype.luminance = function (lum, mode = 'rgb') {
752
- if (lum !== undefined && type(lum) === 'number') {
753
- if (lum === 0) {
754
- // return pure black
755
- return new Color([0, 0, 0, this._rgb[3]], 'rgb');
756
- }
757
- if (lum === 1) {
758
- // return pure white
759
- return new Color([255, 255, 255, this._rgb[3]], 'rgb');
760
- }
761
- // compute new color using...
762
- let cur_lum = this.luminance();
763
- let max_iter = MAX_ITER;
764
-
765
- const test = (low, high) => {
766
- const mid = low.interpolate(high, 0.5, mode);
767
- const lm = mid.luminance();
768
- if (Math.abs(lum - lm) < EPS || !max_iter--) {
769
- // close enough
770
- return mid;
771
- }
772
- return lm > lum ? test(low, mid) : test(mid, high);
773
- };
774
-
775
- const rgb = (
776
- cur_lum > lum
777
- ? test(new Color([0, 0, 0]), this)
778
- : test(this, new Color([255, 255, 255]))
779
- ).rgb();
780
- return new Color([...rgb, this._rgb[3]]);
781
- }
782
- return rgb2luminance(...this._rgb.slice(0, 3));
783
- };
784
-
785
- const rgb2luminance = (r, g, b) => {
786
- // relative luminance
787
- // see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
788
- r = luminance_x(r);
789
- g = luminance_x(g);
790
- b = luminance_x(b);
791
- return 0.2126 * r + 0.7152 * g + 0.0722 * b;
792
- };
793
-
794
- const luminance_x = (x) => {
795
- x /= 255;
796
- return x <= 0.03928 ? x / 12.92 : pow$6((x + 0.055) / 1.055, 2.4);
797
- };
798
-
799
- const index = {};
800
-
801
- const mix = (col1, col2, f = 0.5, ...rest) => {
802
- let mode = rest[0] || 'lrgb';
803
- if (!index[mode] && !rest.length) {
804
- // fall back to the first supported mode
805
- mode = Object.keys(index)[0];
806
- }
807
- if (!index[mode]) {
808
- throw new Error(`interpolation mode ${mode} is not defined`);
809
- }
810
- if (type(col1) !== 'object') col1 = new Color(col1);
811
- if (type(col2) !== 'object') col2 = new Color(col2);
812
- return index[mode](col1, col2, f).alpha(
813
- col1.alpha() + f * (col2.alpha() - col1.alpha())
814
- );
815
- };
816
-
817
- Color.prototype.mix = Color.prototype.interpolate = function (
818
- col2,
819
- f = 0.5,
820
- ...rest
821
- ) {
822
- return mix(this, col2, f, ...rest);
823
- };
824
-
825
- Color.prototype.premultiply = function (mutate = false) {
826
- const rgb = this._rgb;
827
- const a = rgb[3];
828
- if (mutate) {
829
- this._rgb = [rgb[0] * a, rgb[1] * a, rgb[2] * a, a];
830
- return this;
831
- } else {
832
- return new Color([rgb[0] * a, rgb[1] * a, rgb[2] * a, a], 'rgb');
833
- }
834
- };
835
-
836
- const { sin: sin$3, cos: cos$4 } = Math;
837
-
838
- const lch2lab = (...args) => {
839
- /*
840
- Convert from a qualitative parameter h and a quantitative parameter l to a 24-bit pixel.
841
- These formulas were invented by David Dalrymple to obtain maximum contrast without going
842
- out of gamut if the parameters are in the range 0-1.
843
-
844
- A saturation multiplier was added by Gregor Aisch
845
- */
846
- let [l, c, h] = unpack(args, 'lch');
847
- if (isNaN(h)) h = 0;
848
- h = h * DEG2RAD;
849
- return [l, cos$4(h) * c, sin$3(h) * c];
850
- };
851
-
852
- const lch2rgb = (...args) => {
853
- args = unpack(args, 'lch');
854
- const [l, c, h] = args;
855
- const [L, a, b_] = lch2lab(l, c, h);
856
- const [r, g, b] = lab2rgb(L, a, b_);
857
- return [r, g, b, args.length > 3 ? args[3] : 1];
858
- };
859
-
860
- const hcl2rgb = (...args) => {
861
- const hcl = reverse3(unpack(args, 'hcl'));
862
- return lch2rgb(...hcl);
863
- };
864
-
865
- const { sqrt: sqrt$4, atan2: atan2$2, round: round$4 } = Math;
866
-
867
- const lab2lch = (...args) => {
868
- const [l, a, b] = unpack(args, 'lab');
869
- const c = sqrt$4(a * a + b * b);
870
- let h = (atan2$2(b, a) * RAD2DEG + 360) % 360;
871
- if (round$4(c * 10000) === 0) h = Number.NaN;
872
- return [l, c, h];
873
- };
874
-
875
- const rgb2lch = (...args) => {
876
- const [r, g, b, ...rest] = unpack(args, 'rgb');
877
- const [l, a, b_] = rgb2lab(r, g, b);
878
- const [L, c, h] = lab2lch(l, a, b_);
879
- return [L, c, h, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
880
- };
881
-
882
- Color.prototype.lch = function () {
883
- return rgb2lch(this._rgb);
884
- };
885
- Color.prototype.hcl = function () {
886
- return reverse3(rgb2lch(this._rgb));
887
- };
888
-
889
- const lch$1 = (...args) => new Color(...args, 'lch');
890
- const hcl = (...args) => new Color(...args, 'hcl');
891
-
892
- Object.assign(chroma, { lch: lch$1, hcl });
893
-
894
- input.format.lch = lch2rgb;
895
- input.format.hcl = hcl2rgb;
896
- ['lch', 'hcl'].forEach((m) =>
897
- input.autodetect.push({
898
- p: 2,
899
- test: (...args) => {
900
- args = unpack(args, m);
901
- if (type(args) === 'array' && args.length === 3) {
902
- return m;
903
- }
904
- }
905
- })
906
- );
907
-
908
- Color.prototype.saturate = function (amount = 1) {
909
- const me = this;
910
- const lch = me.lch();
911
- lch[1] += labConstants.Kn * amount;
912
- if (lch[1] < 0) lch[1] = 0;
913
- return new Color(lch, 'lch').alpha(me.alpha(), true);
914
- };
915
-
916
- Color.prototype.desaturate = function (amount = 1) {
917
- return this.saturate(-amount);
918
- };
919
-
920
- Color.prototype.set = function (mc, value, mutate = false) {
921
- const [mode, channel] = mc.split('.');
922
- const src = this[mode]();
923
- if (channel) {
924
- const i = mode.indexOf(channel) - (mode.substr(0, 2) === 'ok' ? 2 : 0);
925
- if (i > -1) {
926
- if (type(value) == 'string') {
927
- switch (value.charAt(0)) {
928
- case '+':
929
- src[i] += +value;
930
- break;
931
- case '-':
932
- src[i] += +value;
933
- break;
934
- case '*':
935
- src[i] *= +value.substr(1);
936
- break;
937
- case '/':
938
- src[i] /= +value.substr(1);
939
- break;
940
- default:
941
- src[i] = +value;
942
- }
943
- } else if (type(value) === 'number') {
944
- src[i] = value;
945
- } else {
946
- throw new Error(`unsupported value for Color.set`);
947
- }
948
- const out = new Color(src, mode);
949
- if (mutate) {
950
- this._rgb = out._rgb;
951
- return this;
952
- }
953
- return out;
954
- }
955
- throw new Error(`unknown channel ${channel} in mode ${mode}`);
956
- } else {
957
- return src;
958
- }
959
- };
960
-
961
- Color.prototype.tint = function (f = 0.5, ...rest) {
962
- return mix(this, 'white', f, ...rest);
963
- };
964
-
965
- Color.prototype.shade = function (f = 0.5, ...rest) {
966
- return mix(this, 'black', f, ...rest);
967
- };
968
-
969
- const rgb$1 = (col1, col2, f) => {
970
- const xyz0 = col1._rgb;
971
- const xyz1 = col2._rgb;
972
- return new Color(
973
- xyz0[0] + f * (xyz1[0] - xyz0[0]),
974
- xyz0[1] + f * (xyz1[1] - xyz0[1]),
975
- xyz0[2] + f * (xyz1[2] - xyz0[2]),
976
- 'rgb'
977
- );
978
- };
979
-
980
- // register interpolator
981
- index.rgb = rgb$1;
982
-
983
- const { sqrt: sqrt$3, pow: pow$5 } = Math;
984
-
985
- const lrgb = (col1, col2, f) => {
986
- const [x1, y1, z1] = col1._rgb;
987
- const [x2, y2, z2] = col2._rgb;
988
- return new Color(
989
- sqrt$3(pow$5(x1, 2) * (1 - f) + pow$5(x2, 2) * f),
990
- sqrt$3(pow$5(y1, 2) * (1 - f) + pow$5(y2, 2) * f),
991
- sqrt$3(pow$5(z1, 2) * (1 - f) + pow$5(z2, 2) * f),
992
- 'rgb'
993
- );
994
- };
995
-
996
- // register interpolator
997
- index.lrgb = lrgb;
998
-
999
- const lab = (col1, col2, f) => {
1000
- const xyz0 = col1.lab();
1001
- const xyz1 = col2.lab();
1002
- return new Color(
1003
- xyz0[0] + f * (xyz1[0] - xyz0[0]),
1004
- xyz0[1] + f * (xyz1[1] - xyz0[1]),
1005
- xyz0[2] + f * (xyz1[2] - xyz0[2]),
1006
- 'lab'
1007
- );
1008
- };
1009
-
1010
- // register interpolator
1011
- index.lab = lab;
1012
-
1013
- const interpolate_hsx = (col1, col2, f, m) => {
1014
- let xyz0, xyz1;
1015
- if (m === 'hsl') {
1016
- xyz0 = col1.hsl();
1017
- xyz1 = col2.hsl();
1018
- } else if (m === 'hsv') {
1019
- xyz0 = col1.hsv();
1020
- xyz1 = col2.hsv();
1021
- } else if (m === 'hcg') {
1022
- xyz0 = col1.hcg();
1023
- xyz1 = col2.hcg();
1024
- } else if (m === 'hsi') {
1025
- xyz0 = col1.hsi();
1026
- xyz1 = col2.hsi();
1027
- } else if (m === 'lch' || m === 'hcl') {
1028
- m = 'hcl';
1029
- xyz0 = col1.hcl();
1030
- xyz1 = col2.hcl();
1031
- } else if (m === 'oklch') {
1032
- xyz0 = col1.oklch().reverse();
1033
- xyz1 = col2.oklch().reverse();
1034
- }
1035
-
1036
- let hue0, hue1, sat0, sat1, lbv0, lbv1;
1037
- if (m.substr(0, 1) === 'h' || m === 'oklch') {
1038
- [hue0, sat0, lbv0] = xyz0;
1039
- [hue1, sat1, lbv1] = xyz1;
1040
- }
1041
-
1042
- let sat, hue, lbv, dh;
1043
-
1044
- if (!isNaN(hue0) && !isNaN(hue1)) {
1045
- // both colors have hue
1046
- if (hue1 > hue0 && hue1 - hue0 > 180) {
1047
- dh = hue1 - (hue0 + 360);
1048
- } else if (hue1 < hue0 && hue0 - hue1 > 180) {
1049
- dh = hue1 + 360 - hue0;
1050
- } else {
1051
- dh = hue1 - hue0;
1052
- }
1053
- hue = hue0 + f * dh;
1054
- } else if (!isNaN(hue0)) {
1055
- hue = hue0;
1056
- if ((lbv1 == 1 || lbv1 == 0) && m != 'hsv') sat = sat0;
1057
- } else if (!isNaN(hue1)) {
1058
- hue = hue1;
1059
- if ((lbv0 == 1 || lbv0 == 0) && m != 'hsv') sat = sat1;
1060
- } else {
1061
- hue = Number.NaN;
1062
- }
1063
-
1064
- if (sat === undefined) sat = sat0 + f * (sat1 - sat0);
1065
- lbv = lbv0 + f * (lbv1 - lbv0);
1066
- return m === 'oklch'
1067
- ? new Color([lbv, sat, hue], m)
1068
- : new Color([hue, sat, lbv], m);
1069
- };
1070
-
1071
- const lch = (col1, col2, f) => {
1072
- return interpolate_hsx(col1, col2, f, 'lch');
1073
- };
1074
-
1075
- // register interpolator
1076
- index.lch = lch;
1077
- index.hcl = lch;
1078
-
1079
- const num2rgb = (num) => {
1080
- if (type(num) == 'number' && num >= 0 && num <= 0xffffff) {
1081
- const r = num >> 16;
1082
- const g = (num >> 8) & 0xff;
1083
- const b = num & 0xff;
1084
- return [r, g, b, 1];
1085
- }
1086
- throw new Error('unknown num color: ' + num);
1087
- };
1088
-
1089
- const rgb2num = (...args) => {
1090
- const [r, g, b] = unpack(args, 'rgb');
1091
- return (r << 16) + (g << 8) + b;
1092
- };
1093
-
1094
- Color.prototype.num = function () {
1095
- return rgb2num(this._rgb);
1096
- };
1097
-
1098
- const num$1 = (...args) => new Color(...args, 'num');
1099
-
1100
- Object.assign(chroma, { num: num$1 });
1101
-
1102
- input.format.num = num2rgb;
1103
-
1104
- input.autodetect.push({
1105
- p: 5,
1106
- test: (...args) => {
1107
- if (
1108
- args.length === 1 &&
1109
- type(args[0]) === 'number' &&
1110
- args[0] >= 0 &&
1111
- args[0] <= 0xffffff
1112
- ) {
1113
- return 'num';
1114
- }
1115
- }
1116
- });
1117
-
1118
- const num = (col1, col2, f) => {
1119
- const c1 = col1.num();
1120
- const c2 = col2.num();
1121
- return new Color(c1 + f * (c2 - c1), 'num');
1122
- };
1123
-
1124
- // register interpolator
1125
- index.num = num;
1126
-
1127
- const { floor: floor$3 } = Math;
1128
-
1129
- /*
1130
- * this is basically just HSV with some minor tweaks
1131
- *
1132
- * hue.. [0..360]
1133
- * chroma .. [0..1]
1134
- * grayness .. [0..1]
1135
- */
1136
-
1137
- const hcg2rgb = (...args) => {
1138
- args = unpack(args, 'hcg');
1139
- let [h, c, _g] = args;
1140
- let r, g, b;
1141
- _g = _g * 255;
1142
- const _c = c * 255;
1143
- if (c === 0) {
1144
- r = g = b = _g;
1145
- } else {
1146
- if (h === 360) h = 0;
1147
- if (h > 360) h -= 360;
1148
- if (h < 0) h += 360;
1149
- h /= 60;
1150
- const i = floor$3(h);
1151
- const f = h - i;
1152
- const p = _g * (1 - c);
1153
- const q = p + _c * (1 - f);
1154
- const t = p + _c * f;
1155
- const v = p + _c;
1156
- switch (i) {
1157
- case 0:
1158
- [r, g, b] = [v, t, p];
1159
- break;
1160
- case 1:
1161
- [r, g, b] = [q, v, p];
1162
- break;
1163
- case 2:
1164
- [r, g, b] = [p, v, t];
1165
- break;
1166
- case 3:
1167
- [r, g, b] = [p, q, v];
1168
- break;
1169
- case 4:
1170
- [r, g, b] = [t, p, v];
1171
- break;
1172
- case 5:
1173
- [r, g, b] = [v, p, q];
1174
- break;
1175
- }
1176
- }
1177
- return [r, g, b, args.length > 3 ? args[3] : 1];
1178
- };
1179
-
1180
- const rgb2hcg = (...args) => {
1181
- const [r, g, b] = unpack(args, 'rgb');
1182
- const minRgb = min$3(r, g, b);
1183
- const maxRgb = max$3(r, g, b);
1184
- const delta = maxRgb - minRgb;
1185
- const c = (delta * 100) / 255;
1186
- const _g = (minRgb / (255 - delta)) * 100;
1187
- let h;
1188
- if (delta === 0) {
1189
- h = Number.NaN;
1190
- } else {
1191
- if (r === maxRgb) h = (g - b) / delta;
1192
- if (g === maxRgb) h = 2 + (b - r) / delta;
1193
- if (b === maxRgb) h = 4 + (r - g) / delta;
1194
- h *= 60;
1195
- if (h < 0) h += 360;
1196
- }
1197
- return [h, c, _g];
1198
- };
1199
-
1200
- Color.prototype.hcg = function () {
1201
- return rgb2hcg(this._rgb);
1202
- };
1203
-
1204
- const hcg$1 = (...args) => new Color(...args, 'hcg');
1205
- chroma.hcg = hcg$1;
1206
-
1207
- input.format.hcg = hcg2rgb;
1208
-
1209
- input.autodetect.push({
1210
- p: 1,
1211
- test: (...args) => {
1212
- args = unpack(args, 'hcg');
1213
- if (type(args) === 'array' && args.length === 3) {
1214
- return 'hcg';
1215
- }
1216
- }
1217
- });
1218
-
1219
- const hcg = (col1, col2, f) => {
1220
- return interpolate_hsx(col1, col2, f, 'hcg');
1221
- };
1222
-
1223
- // register interpolator
1224
- index.hcg = hcg;
1225
-
1226
- const { cos: cos$3 } = Math;
1227
-
1228
- /*
1229
- * hue [0..360]
1230
- * saturation [0..1]
1231
- * intensity [0..1]
1232
- */
1233
- const hsi2rgb = (...args) => {
1234
- /*
1235
- borrowed from here:
1236
- http://hummer.stanford.edu/museinfo/doc/examples/humdrum/keyscape2/hsi2rgb.cpp
1237
- */
1238
- args = unpack(args, 'hsi');
1239
- let [h, s, i] = args;
1240
- let r, g, b;
1241
-
1242
- if (isNaN(h)) h = 0;
1243
- if (isNaN(s)) s = 0;
1244
- // normalize hue
1245
- if (h > 360) h -= 360;
1246
- if (h < 0) h += 360;
1247
- h /= 360;
1248
- if (h < 1 / 3) {
1249
- b = (1 - s) / 3;
1250
- r = (1 + (s * cos$3(TWOPI * h)) / cos$3(PITHIRD - TWOPI * h)) / 3;
1251
- g = 1 - (b + r);
1252
- } else if (h < 2 / 3) {
1253
- h -= 1 / 3;
1254
- r = (1 - s) / 3;
1255
- g = (1 + (s * cos$3(TWOPI * h)) / cos$3(PITHIRD - TWOPI * h)) / 3;
1256
- b = 1 - (r + g);
1257
- } else {
1258
- h -= 2 / 3;
1259
- g = (1 - s) / 3;
1260
- b = (1 + (s * cos$3(TWOPI * h)) / cos$3(PITHIRD - TWOPI * h)) / 3;
1261
- r = 1 - (g + b);
1262
- }
1263
- r = limit(i * r * 3);
1264
- g = limit(i * g * 3);
1265
- b = limit(i * b * 3);
1266
- return [r * 255, g * 255, b * 255, args.length > 3 ? args[3] : 1];
1267
- };
1268
-
1269
- const { min: min$2, sqrt: sqrt$2, acos } = Math;
1270
-
1271
- const rgb2hsi = (...args) => {
1272
- /*
1273
- borrowed from here:
1274
- http://hummer.stanford.edu/museinfo/doc/examples/humdrum/keyscape2/rgb2hsi.cpp
1275
- */
1276
- let [r, g, b] = unpack(args, 'rgb');
1277
- r /= 255;
1278
- g /= 255;
1279
- b /= 255;
1280
- let h;
1281
- const min_ = min$2(r, g, b);
1282
- const i = (r + g + b) / 3;
1283
- const s = i > 0 ? 1 - min_ / i : 0;
1284
- if (s === 0) {
1285
- h = NaN;
1286
- } else {
1287
- h = (r - g + (r - b)) / 2;
1288
- h /= sqrt$2((r - g) * (r - g) + (r - b) * (g - b));
1289
- h = acos(h);
1290
- if (b > g) {
1291
- h = TWOPI - h;
1292
- }
1293
- h /= TWOPI;
1294
- }
1295
- return [h * 360, s, i];
1296
- };
1297
-
1298
- Color.prototype.hsi = function () {
1299
- return rgb2hsi(this._rgb);
1300
- };
1301
-
1302
- const hsi$1 = (...args) => new Color(...args, 'hsi');
1303
- chroma.hsi = hsi$1;
1304
-
1305
- input.format.hsi = hsi2rgb;
1306
-
1307
- input.autodetect.push({
1308
- p: 2,
1309
- test: (...args) => {
1310
- args = unpack(args, 'hsi');
1311
- if (type(args) === 'array' && args.length === 3) {
1312
- return 'hsi';
1313
- }
1314
- }
1315
- });
1316
-
1317
- const hsi = (col1, col2, f) => {
1318
- return interpolate_hsx(col1, col2, f, 'hsi');
1319
- };
1320
-
1321
- // register interpolator
1322
- index.hsi = hsi;
1323
-
1324
- const hsl2rgb = (...args) => {
1325
- args = unpack(args, 'hsl');
1326
- const [h, s, l] = args;
1327
- let r, g, b;
1328
- if (s === 0) {
1329
- r = g = b = l * 255;
1330
- } else {
1331
- const t3 = [0, 0, 0];
1332
- const c = [0, 0, 0];
1333
- const t2 = l < 0.5 ? l * (1 + s) : l + s - l * s;
1334
- const t1 = 2 * l - t2;
1335
- const h_ = h / 360;
1336
- t3[0] = h_ + 1 / 3;
1337
- t3[1] = h_;
1338
- t3[2] = h_ - 1 / 3;
1339
- for (let i = 0; i < 3; i++) {
1340
- if (t3[i] < 0) t3[i] += 1;
1341
- if (t3[i] > 1) t3[i] -= 1;
1342
- if (6 * t3[i] < 1) c[i] = t1 + (t2 - t1) * 6 * t3[i];
1343
- else if (2 * t3[i] < 1) c[i] = t2;
1344
- else if (3 * t3[i] < 2) c[i] = t1 + (t2 - t1) * (2 / 3 - t3[i]) * 6;
1345
- else c[i] = t1;
1346
- }
1347
- [r, g, b] = [c[0] * 255, c[1] * 255, c[2] * 255];
1348
- }
1349
- if (args.length > 3) {
1350
- // keep alpha channel
1351
- return [r, g, b, args[3]];
1352
- }
1353
- return [r, g, b, 1];
1354
- };
1355
-
1356
- /*
1357
- * supported arguments:
1358
- * - rgb2hsl(r,g,b)
1359
- * - rgb2hsl(r,g,b,a)
1360
- * - rgb2hsl([r,g,b])
1361
- * - rgb2hsl([r,g,b,a])
1362
- * - rgb2hsl({r,g,b,a})
1363
- */
1364
- const rgb2hsl$1 = (...args) => {
1365
- args = unpack(args, 'rgba');
1366
- let [r, g, b] = args;
1367
-
1368
- r /= 255;
1369
- g /= 255;
1370
- b /= 255;
1371
-
1372
- const minRgb = min$3(r, g, b);
1373
- const maxRgb = max$3(r, g, b);
1374
-
1375
- const l = (maxRgb + minRgb) / 2;
1376
- let s, h;
1377
-
1378
- if (maxRgb === minRgb) {
1379
- s = 0;
1380
- h = Number.NaN;
1381
- } else {
1382
- s =
1383
- l < 0.5
1384
- ? (maxRgb - minRgb) / (maxRgb + minRgb)
1385
- : (maxRgb - minRgb) / (2 - maxRgb - minRgb);
1386
- }
1387
-
1388
- if (r == maxRgb) h = (g - b) / (maxRgb - minRgb);
1389
- else if (g == maxRgb) h = 2 + (b - r) / (maxRgb - minRgb);
1390
- else if (b == maxRgb) h = 4 + (r - g) / (maxRgb - minRgb);
1391
-
1392
- h *= 60;
1393
- if (h < 0) h += 360;
1394
- if (args.length > 3 && args[3] !== undefined) return [h, s, l, args[3]];
1395
- return [h, s, l];
1396
- };
1397
-
1398
- Color.prototype.hsl = function () {
1399
- return rgb2hsl$1(this._rgb);
1400
- };
1401
-
1402
- const hsl$1 = (...args) => new Color(...args, 'hsl');
1403
- chroma.hsl = hsl$1;
1404
-
1405
- input.format.hsl = hsl2rgb;
1406
-
1407
- input.autodetect.push({
1408
- p: 2,
1409
- test: (...args) => {
1410
- args = unpack(args, 'hsl');
1411
- if (type(args) === 'array' && args.length === 3) {
1412
- return 'hsl';
1413
- }
1414
- }
1415
- });
1416
-
1417
- const hsl = (col1, col2, f) => {
1418
- return interpolate_hsx(col1, col2, f, 'hsl');
1419
- };
1420
-
1421
- // register interpolator
1422
- index.hsl = hsl;
1423
-
1424
- const { floor: floor$2 } = Math;
1425
-
1426
- const hsv2rgb = (...args) => {
1427
- args = unpack(args, 'hsv');
1428
- let [h, s, v] = args;
1429
- let r, g, b;
1430
- v *= 255;
1431
- if (s === 0) {
1432
- r = g = b = v;
1433
- } else {
1434
- if (h === 360) h = 0;
1435
- if (h > 360) h -= 360;
1436
- if (h < 0) h += 360;
1437
- h /= 60;
1438
-
1439
- const i = floor$2(h);
1440
- const f = h - i;
1441
- const p = v * (1 - s);
1442
- const q = v * (1 - s * f);
1443
- const t = v * (1 - s * (1 - f));
1444
-
1445
- switch (i) {
1446
- case 0:
1447
- [r, g, b] = [v, t, p];
1448
- break;
1449
- case 1:
1450
- [r, g, b] = [q, v, p];
1451
- break;
1452
- case 2:
1453
- [r, g, b] = [p, v, t];
1454
- break;
1455
- case 3:
1456
- [r, g, b] = [p, q, v];
1457
- break;
1458
- case 4:
1459
- [r, g, b] = [t, p, v];
1460
- break;
1461
- case 5:
1462
- [r, g, b] = [v, p, q];
1463
- break;
1464
- }
1465
- }
1466
- return [r, g, b, args.length > 3 ? args[3] : 1];
1467
- };
1468
-
1469
- const { min: min$1, max: max$2 } = Math;
1470
-
1471
- /*
1472
- * supported arguments:
1473
- * - rgb2hsv(r,g,b)
1474
- * - rgb2hsv([r,g,b])
1475
- * - rgb2hsv({r,g,b})
1476
- */
1477
- const rgb2hsl = (...args) => {
1478
- args = unpack(args, 'rgb');
1479
- let [r, g, b] = args;
1480
- const min_ = min$1(r, g, b);
1481
- const max_ = max$2(r, g, b);
1482
- const delta = max_ - min_;
1483
- let h, s, v;
1484
- v = max_ / 255.0;
1485
- if (max_ === 0) {
1486
- h = Number.NaN;
1487
- s = 0;
1488
- } else {
1489
- s = delta / max_;
1490
- if (r === max_) h = (g - b) / delta;
1491
- if (g === max_) h = 2 + (b - r) / delta;
1492
- if (b === max_) h = 4 + (r - g) / delta;
1493
- h *= 60;
1494
- if (h < 0) h += 360;
1495
- }
1496
- return [h, s, v];
1497
- };
1498
-
1499
- Color.prototype.hsv = function () {
1500
- return rgb2hsl(this._rgb);
1501
- };
1502
-
1503
- const hsv$1 = (...args) => new Color(...args, 'hsv');
1504
- chroma.hsv = hsv$1;
1505
-
1506
- input.format.hsv = hsv2rgb;
1507
-
1508
- input.autodetect.push({
1509
- p: 2,
1510
- test: (...args) => {
1511
- args = unpack(args, 'hsv');
1512
- if (type(args) === 'array' && args.length === 3) {
1513
- return 'hsv';
1514
- }
1515
- }
1516
- });
1517
-
1518
- const hsv = (col1, col2, f) => {
1519
- return interpolate_hsx(col1, col2, f, 'hsv');
1520
- };
1521
-
1522
- // register interpolator
1523
- index.hsv = hsv;
1524
-
1525
- // from https://www.w3.org/TR/css-color-4/multiply-matrices.js
1526
- function multiplyMatrices(A, B) {
1527
- let m = A.length;
1528
-
1529
- if (!Array.isArray(A[0])) {
1530
- // A is vector, convert to [[a, b, c, ...]]
1531
- A = [A];
1532
- }
1533
-
1534
- if (!Array.isArray(B[0])) {
1535
- // B is vector, convert to [[a], [b], [c], ...]]
1536
- B = B.map((x) => [x]);
1537
- }
1538
-
1539
- let p = B[0].length;
1540
- let B_cols = B[0].map((_, i) => B.map((x) => x[i])); // transpose B
1541
- let product = A.map((row) =>
1542
- B_cols.map((col) => {
1543
- if (!Array.isArray(row)) {
1544
- return col.reduce((a, c) => a + c * row, 0);
1545
- }
1546
-
1547
- return row.reduce((a, c, i) => a + c * (col[i] || 0), 0);
1548
- })
1549
- );
1550
-
1551
- if (m === 1) {
1552
- product = product[0]; // Avoid [[a, b, c, ...]]
1553
- }
1554
-
1555
- if (p === 1) {
1556
- return product.map((x) => x[0]); // Avoid [[a], [b], [c], ...]]
1557
- }
1558
-
1559
- return product;
1560
- }
1561
-
1562
- const oklab2rgb = (...args) => {
1563
- args = unpack(args, 'lab');
1564
- const [L, a, b, ...rest] = args;
1565
- const [X, Y, Z] = OKLab_to_XYZ([L, a, b]);
1566
- const [r, g, b_] = xyz2rgb(X, Y, Z);
1567
- return [r, g, b_, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
1568
- };
1569
-
1570
- // from https://www.w3.org/TR/css-color-4/#color-conversion-code
1571
- function OKLab_to_XYZ(OKLab) {
1572
- // Given OKLab, convert to XYZ relative to D65
1573
- var LMStoXYZ = [
1574
- [1.2268798758459243, -0.5578149944602171, 0.2813910456659647],
1575
- [-0.0405757452148008, 1.112286803280317, -0.0717110580655164],
1576
- [-0.0763729366746601, -0.4214933324022432, 1.5869240198367816]
1577
- ];
1578
- var OKLabtoLMS = [
1579
- [1.0, 0.3963377773761749, 0.2158037573099136],
1580
- [1.0, -0.1055613458156586, -0.0638541728258133],
1581
- [1.0, -0.0894841775298119, -1.2914855480194092]
1582
- ];
1583
-
1584
- var LMSnl = multiplyMatrices(OKLabtoLMS, OKLab);
1585
- return multiplyMatrices(
1586
- LMStoXYZ,
1587
- LMSnl.map((c) => c ** 3)
1588
- );
1589
- }
1590
-
1591
- const rgb2oklab = (...args) => {
1592
- const [r, g, b, ...rest] = unpack(args, 'rgb');
1593
- const xyz = rgb2xyz(r, g, b);
1594
- const oklab = XYZ_to_OKLab(xyz);
1595
- return [...oklab, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
1596
- };
1597
-
1598
- // from https://www.w3.org/TR/css-color-4/#color-conversion-code
1599
- function XYZ_to_OKLab(XYZ) {
1600
- // Given XYZ relative to D65, convert to OKLab
1601
- const XYZtoLMS = [
1602
- [0.819022437996703, 0.3619062600528904, -0.1288737815209879],
1603
- [0.0329836539323885, 0.9292868615863434, 0.0361446663506424],
1604
- [0.0481771893596242, 0.2642395317527308, 0.6335478284694309]
1605
- ];
1606
- const LMStoOKLab = [
1607
- [0.210454268309314, 0.7936177747023054, -0.0040720430116193],
1608
- [1.9779985324311684, -2.42859224204858, 0.450593709617411],
1609
- [0.0259040424655478, 0.7827717124575296, -0.8086757549230774]
1610
- ];
1611
-
1612
- const LMS = multiplyMatrices(XYZtoLMS, XYZ);
1613
- // JavaScript Math.cbrt returns a sign-matched cube root
1614
- // beware if porting to other languages
1615
- // especially if tempted to use a general power function
1616
- return multiplyMatrices(
1617
- LMStoOKLab,
1618
- LMS.map((c) => Math.cbrt(c))
1619
- );
1620
- // L in range [0,1]. For use in CSS, multiply by 100 and add a percent
1621
- }
1622
-
1623
- Color.prototype.oklab = function () {
1624
- return rgb2oklab(this._rgb);
1625
- };
1626
-
1627
- const oklab$1 = (...args) => new Color(...args, 'oklab');
1628
- Object.assign(chroma, { oklab: oklab$1 });
1629
-
1630
- input.format.oklab = oklab2rgb;
1631
-
1632
- input.autodetect.push({
1633
- p: 2,
1634
- test: (...args) => {
1635
- args = unpack(args, 'oklab');
1636
- if (type(args) === 'array' && args.length === 3) {
1637
- return 'oklab';
1638
- }
1639
- }
1640
- });
1641
-
1642
- const oklab = (col1, col2, f) => {
1643
- const xyz0 = col1.oklab();
1644
- const xyz1 = col2.oklab();
1645
- return new Color(
1646
- xyz0[0] + f * (xyz1[0] - xyz0[0]),
1647
- xyz0[1] + f * (xyz1[1] - xyz0[1]),
1648
- xyz0[2] + f * (xyz1[2] - xyz0[2]),
1649
- 'oklab'
1650
- );
1651
- };
1652
-
1653
- // register interpolator
1654
- index.oklab = oklab;
1655
-
1656
- const oklch$1 = (col1, col2, f) => {
1657
- return interpolate_hsx(col1, col2, f, 'oklch');
1658
- };
1659
-
1660
- // register interpolator
1661
- index.oklch = oklch$1;
1662
-
1663
- const { pow: pow$4, sqrt: sqrt$1, PI: PI$1, cos: cos$2, sin: sin$2, atan2: atan2$1 } = Math;
1664
-
1665
- const average = (colors, mode = 'lrgb', weights = null) => {
1666
- const l = colors.length;
1667
- if (!weights) weights = Array.from(new Array(l)).map(() => 1);
1668
- // normalize weights
1669
- const k =
1670
- l /
1671
- weights.reduce(function (a, b) {
1672
- return a + b;
1673
- });
1674
- weights.forEach((w, i) => {
1675
- weights[i] *= k;
1676
- });
1677
- // convert colors to Color objects
1678
- colors = colors.map((c) => new Color(c));
1679
- if (mode === 'lrgb') {
1680
- return _average_lrgb(colors, weights);
1681
- }
1682
- const first = colors.shift();
1683
- const xyz = first.get(mode);
1684
- const cnt = [];
1685
- let dx = 0;
1686
- let dy = 0;
1687
- // initial color
1688
- for (let i = 0; i < xyz.length; i++) {
1689
- xyz[i] = (xyz[i] || 0) * weights[0];
1690
- cnt.push(isNaN(xyz[i]) ? 0 : weights[0]);
1691
- if (mode.charAt(i) === 'h' && !isNaN(xyz[i])) {
1692
- const A = (xyz[i] / 180) * PI$1;
1693
- dx += cos$2(A) * weights[0];
1694
- dy += sin$2(A) * weights[0];
1695
- }
1696
- }
1697
-
1698
- let alpha = first.alpha() * weights[0];
1699
- colors.forEach((c, ci) => {
1700
- const xyz2 = c.get(mode);
1701
- alpha += c.alpha() * weights[ci + 1];
1702
- for (let i = 0; i < xyz.length; i++) {
1703
- if (!isNaN(xyz2[i])) {
1704
- cnt[i] += weights[ci + 1];
1705
- if (mode.charAt(i) === 'h') {
1706
- const A = (xyz2[i] / 180) * PI$1;
1707
- dx += cos$2(A) * weights[ci + 1];
1708
- dy += sin$2(A) * weights[ci + 1];
1709
- } else {
1710
- xyz[i] += xyz2[i] * weights[ci + 1];
1711
- }
1712
- }
1713
- }
1714
- });
1715
-
1716
- for (let i = 0; i < xyz.length; i++) {
1717
- if (mode.charAt(i) === 'h') {
1718
- let A = (atan2$1(dy / cnt[i], dx / cnt[i]) / PI$1) * 180;
1719
- while (A < 0) A += 360;
1720
- while (A >= 360) A -= 360;
1721
- xyz[i] = A;
1722
- } else {
1723
- xyz[i] = xyz[i] / cnt[i];
1724
- }
1725
- }
1726
- alpha /= l;
1727
- return new Color(xyz, mode).alpha(alpha > 0.99999 ? 1 : alpha, true);
1728
- };
1729
-
1730
- const _average_lrgb = (colors, weights) => {
1731
- const l = colors.length;
1732
- const xyz = [0, 0, 0, 0];
1733
- for (let i = 0; i < colors.length; i++) {
1734
- const col = colors[i];
1735
- const f = weights[i] / l;
1736
- const rgb = col._rgb;
1737
- xyz[0] += pow$4(rgb[0], 2) * f;
1738
- xyz[1] += pow$4(rgb[1], 2) * f;
1739
- xyz[2] += pow$4(rgb[2], 2) * f;
1740
- xyz[3] += rgb[3] * f;
1741
- }
1742
- xyz[0] = sqrt$1(xyz[0]);
1743
- xyz[1] = sqrt$1(xyz[1]);
1744
- xyz[2] = sqrt$1(xyz[2]);
1745
- if (xyz[3] > 0.9999999) xyz[3] = 1;
1746
- return new Color(clip_rgb(xyz));
1747
- };
1748
-
1749
- // minimal multi-purpose interface
1750
-
1751
-
1752
- const { pow: pow$3 } = Math;
1753
-
1754
- function scale (colors) {
1755
- // constructor
1756
- let _mode = 'rgb';
1757
- let _nacol = chroma('#ccc');
1758
- let _spread = 0;
1759
- // const _fixed = false;
1760
- let _domain = [0, 1];
1761
- let _pos = [];
1762
- let _padding = [0, 0];
1763
- let _classes = false;
1764
- let _colors = [];
1765
- let _out = false;
1766
- let _min = 0;
1767
- let _max = 1;
1768
- let _correctLightness = false;
1769
- let _colorCache = {};
1770
- let _useCache = true;
1771
- let _gamma = 1;
1772
-
1773
- // private methods
1774
-
1775
- const setColors = function (colors) {
1776
- colors = colors || ['#fff', '#000'];
1777
- if (
1778
- colors &&
1779
- type(colors) === 'string' &&
1780
- chroma.brewer &&
1781
- chroma.brewer[colors.toLowerCase()]
1782
- ) {
1783
- colors = chroma.brewer[colors.toLowerCase()];
1784
- }
1785
- if (type(colors) === 'array') {
1786
- // handle single color
1787
- if (colors.length === 1) {
1788
- colors = [colors[0], colors[0]];
1789
- }
1790
- // make a copy of the colors
1791
- colors = colors.slice(0);
1792
- // convert to chroma classes
1793
- for (let c = 0; c < colors.length; c++) {
1794
- colors[c] = chroma(colors[c]);
1795
- }
1796
- // auto-fill color position
1797
- _pos.length = 0;
1798
- for (let c = 0; c < colors.length; c++) {
1799
- _pos.push(c / (colors.length - 1));
1800
- }
1801
- }
1802
- resetCache();
1803
- return (_colors = colors);
1804
- };
1805
-
1806
- const getClass = function (value) {
1807
- if (_classes != null) {
1808
- const n = _classes.length - 1;
1809
- let i = 0;
1810
- while (i < n && value >= _classes[i]) {
1811
- i++;
1812
- }
1813
- return i - 1;
1814
- }
1815
- return 0;
1816
- };
1817
-
1818
- let tMapLightness = (t) => t;
1819
- let tMapDomain = (t) => t;
1820
-
1821
- // const classifyValue = function(value) {
1822
- // let val = value;
1823
- // if (_classes.length > 2) {
1824
- // const n = _classes.length-1;
1825
- // const i = getClass(value);
1826
- // const minc = _classes[0] + ((_classes[1]-_classes[0]) * (0 + (_spread * 0.5))); // center of 1st class
1827
- // const maxc = _classes[n-1] + ((_classes[n]-_classes[n-1]) * (1 - (_spread * 0.5))); // center of last class
1828
- // val = _min + ((((_classes[i] + ((_classes[i+1] - _classes[i]) * 0.5)) - minc) / (maxc-minc)) * (_max - _min));
1829
- // }
1830
- // return val;
1831
- // };
1832
-
1833
- const getColor = function (val, bypassMap) {
1834
- let col, t;
1835
- if (bypassMap == null) {
1836
- bypassMap = false;
1837
- }
1838
- if (isNaN(val) || val === null) {
1839
- return _nacol;
1840
- }
1841
- if (!bypassMap) {
1842
- if (_classes && _classes.length > 2) {
1843
- // find the class
1844
- const c = getClass(val);
1845
- t = c / (_classes.length - 2);
1846
- } else if (_max !== _min) {
1847
- // just interpolate between min/max
1848
- t = (val - _min) / (_max - _min);
1849
- } else {
1850
- t = 1;
1851
- }
1852
- } else {
1853
- t = val;
1854
- }
1855
-
1856
- // domain map
1857
- t = tMapDomain(t);
1858
-
1859
- if (!bypassMap) {
1860
- t = tMapLightness(t); // lightness correction
1861
- }
1862
-
1863
- if (_gamma !== 1) {
1864
- t = pow$3(t, _gamma);
1865
- }
1866
-
1867
- t = _padding[0] + t * (1 - _padding[0] - _padding[1]);
1868
-
1869
- t = limit(t, 0, 1);
1870
-
1871
- const k = Math.floor(t * 10000);
1872
-
1873
- if (_useCache && _colorCache[k]) {
1874
- col = _colorCache[k];
1875
- } else {
1876
- if (type(_colors) === 'array') {
1877
- //for i in [0.._pos.length-1]
1878
- for (let i = 0; i < _pos.length; i++) {
1879
- const p = _pos[i];
1880
- if (t <= p) {
1881
- col = _colors[i];
1882
- break;
1883
- }
1884
- if (t >= p && i === _pos.length - 1) {
1885
- col = _colors[i];
1886
- break;
1887
- }
1888
- if (t > p && t < _pos[i + 1]) {
1889
- t = (t - p) / (_pos[i + 1] - p);
1890
- col = chroma.interpolate(
1891
- _colors[i],
1892
- _colors[i + 1],
1893
- t,
1894
- _mode
1895
- );
1896
- break;
1897
- }
1898
- }
1899
- } else if (type(_colors) === 'function') {
1900
- col = _colors(t);
1901
- }
1902
- if (_useCache) {
1903
- _colorCache[k] = col;
1904
- }
1905
- }
1906
- return col;
1907
- };
1908
-
1909
- var resetCache = () => (_colorCache = {});
1910
-
1911
- setColors(colors);
1912
-
1913
- // public interface
1914
-
1915
- const f = function (v) {
1916
- const c = chroma(getColor(v));
1917
- if (_out && c[_out]) {
1918
- return c[_out]();
1919
- } else {
1920
- return c;
1921
- }
1922
- };
1923
-
1924
- f.classes = function (classes) {
1925
- if (classes != null) {
1926
- if (type(classes) === 'array') {
1927
- _classes = classes;
1928
- _domain = [classes[0], classes[classes.length - 1]];
1929
- } else {
1930
- const d = chroma.analyze(_domain);
1931
- if (classes === 0) {
1932
- _classes = [d.min, d.max];
1933
- } else {
1934
- _classes = chroma.limits(d, 'e', classes);
1935
- }
1936
- }
1937
- return f;
1938
- }
1939
- return _classes;
1940
- };
1941
-
1942
- f.domain = function (domain) {
1943
- if (!arguments.length) {
1944
- return _domain;
1945
- }
1946
- _min = domain[0];
1947
- _max = domain[domain.length - 1];
1948
- _pos = [];
1949
- const k = _colors.length;
1950
- if (domain.length === k && _min !== _max) {
1951
- // update positions
1952
- for (let d of Array.from(domain)) {
1953
- _pos.push((d - _min) / (_max - _min));
1954
- }
1955
- } else {
1956
- for (let c = 0; c < k; c++) {
1957
- _pos.push(c / (k - 1));
1958
- }
1959
- if (domain.length > 2) {
1960
- // set domain map
1961
- const tOut = domain.map((d, i) => i / (domain.length - 1));
1962
- const tBreaks = domain.map((d) => (d - _min) / (_max - _min));
1963
- if (!tBreaks.every((val, i) => tOut[i] === val)) {
1964
- tMapDomain = (t) => {
1965
- if (t <= 0 || t >= 1) return t;
1966
- let i = 0;
1967
- while (t >= tBreaks[i + 1]) i++;
1968
- const f =
1969
- (t - tBreaks[i]) / (tBreaks[i + 1] - tBreaks[i]);
1970
- const out = tOut[i] + f * (tOut[i + 1] - tOut[i]);
1971
- return out;
1972
- };
1973
- }
1974
- }
1975
- }
1976
- _domain = [_min, _max];
1977
- return f;
1978
- };
1979
-
1980
- f.mode = function (_m) {
1981
- if (!arguments.length) {
1982
- return _mode;
1983
- }
1984
- _mode = _m;
1985
- resetCache();
1986
- return f;
1987
- };
1988
-
1989
- f.range = function (colors, _pos) {
1990
- setColors(colors);
1991
- return f;
1992
- };
1993
-
1994
- f.out = function (_o) {
1995
- _out = _o;
1996
- return f;
1997
- };
1998
-
1999
- f.spread = function (val) {
2000
- if (!arguments.length) {
2001
- return _spread;
2002
- }
2003
- _spread = val;
2004
- return f;
2005
- };
2006
-
2007
- f.correctLightness = function (v) {
2008
- if (v == null) {
2009
- v = true;
2010
- }
2011
- _correctLightness = v;
2012
- resetCache();
2013
- if (_correctLightness) {
2014
- tMapLightness = function (t) {
2015
- const L0 = getColor(0, true).lab()[0];
2016
- const L1 = getColor(1, true).lab()[0];
2017
- const pol = L0 > L1;
2018
- let L_actual = getColor(t, true).lab()[0];
2019
- const L_ideal = L0 + (L1 - L0) * t;
2020
- let L_diff = L_actual - L_ideal;
2021
- let t0 = 0;
2022
- let t1 = 1;
2023
- let max_iter = 20;
2024
- while (Math.abs(L_diff) > 1e-2 && max_iter-- > 0) {
2025
- (function () {
2026
- if (pol) {
2027
- L_diff *= -1;
2028
- }
2029
- if (L_diff < 0) {
2030
- t0 = t;
2031
- t += (t1 - t) * 0.5;
2032
- } else {
2033
- t1 = t;
2034
- t += (t0 - t) * 0.5;
2035
- }
2036
- L_actual = getColor(t, true).lab()[0];
2037
- return (L_diff = L_actual - L_ideal);
2038
- })();
2039
- }
2040
- return t;
2041
- };
2042
- } else {
2043
- tMapLightness = (t) => t;
2044
- }
2045
- return f;
2046
- };
2047
-
2048
- f.padding = function (p) {
2049
- if (p != null) {
2050
- if (type(p) === 'number') {
2051
- p = [p, p];
2052
- }
2053
- _padding = p;
2054
- return f;
2055
- } else {
2056
- return _padding;
2057
- }
2058
- };
2059
-
2060
- f.colors = function (numColors, out) {
2061
- // If no arguments are given, return the original colors that were provided
2062
- if (arguments.length < 2) {
2063
- out = 'hex';
2064
- }
2065
- let result = [];
2066
-
2067
- if (arguments.length === 0) {
2068
- result = _colors.slice(0);
2069
- } else if (numColors === 1) {
2070
- result = [f(0.5)];
2071
- } else if (numColors > 1) {
2072
- const dm = _domain[0];
2073
- const dd = _domain[1] - dm;
2074
- result = __range__(0, numColors).map((i) =>
2075
- f(dm + (i / (numColors - 1)) * dd)
2076
- );
2077
- } else {
2078
- // returns all colors based on the defined classes
2079
- colors = [];
2080
- let samples = [];
2081
- if (_classes && _classes.length > 2) {
2082
- for (
2083
- let i = 1, end = _classes.length, asc = 1 <= end;
2084
- asc ? i < end : i > end;
2085
- asc ? i++ : i--
2086
- ) {
2087
- samples.push((_classes[i - 1] + _classes[i]) * 0.5);
2088
- }
2089
- } else {
2090
- samples = _domain;
2091
- }
2092
- result = samples.map((v) => f(v));
2093
- }
2094
-
2095
- if (chroma[out]) {
2096
- result = result.map((c) => c[out]());
2097
- }
2098
- return result;
2099
- };
2100
-
2101
- f.cache = function (c) {
2102
- if (c != null) {
2103
- _useCache = c;
2104
- return f;
2105
- } else {
2106
- return _useCache;
2107
- }
2108
- };
2109
-
2110
- f.gamma = function (g) {
2111
- if (g != null) {
2112
- _gamma = g;
2113
- return f;
2114
- } else {
2115
- return _gamma;
2116
- }
2117
- };
2118
-
2119
- f.nodata = function (d) {
2120
- if (d != null) {
2121
- _nacol = chroma(d);
2122
- return f;
2123
- } else {
2124
- return _nacol;
2125
- }
2126
- };
2127
-
2128
- return f;
2129
- }
2130
-
2131
- function __range__(left, right, inclusive) {
2132
- let range = [];
2133
- let ascending = left < right;
2134
- let end = right ;
2135
- for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
2136
- range.push(i);
2137
- }
2138
- return range;
2139
- }
2140
-
2141
- //
2142
- // interpolates between a set of colors uzing a bezier spline
2143
- //
2144
-
2145
-
2146
- // nth row of the pascal triangle
2147
- const binom_row = function (n) {
2148
- let row = [1, 1];
2149
- for (let i = 1; i < n; i++) {
2150
- let newrow = [1];
2151
- for (let j = 1; j <= row.length; j++) {
2152
- newrow[j] = (row[j] || 0) + row[j - 1];
2153
- }
2154
- row = newrow;
2155
- }
2156
- return row;
2157
- };
2158
-
2159
- const bezier = function (colors) {
2160
- let I, lab0, lab1, lab2;
2161
- colors = colors.map((c) => new Color(c));
2162
- if (colors.length === 2) {
2163
- // linear interpolation
2164
- [lab0, lab1] = colors.map((c) => c.lab());
2165
- I = function (t) {
2166
- const lab = [0, 1, 2].map((i) => lab0[i] + t * (lab1[i] - lab0[i]));
2167
- return new Color(lab, 'lab');
2168
- };
2169
- } else if (colors.length === 3) {
2170
- // quadratic bezier interpolation
2171
- [lab0, lab1, lab2] = colors.map((c) => c.lab());
2172
- I = function (t) {
2173
- const lab = [0, 1, 2].map(
2174
- (i) =>
2175
- (1 - t) * (1 - t) * lab0[i] +
2176
- 2 * (1 - t) * t * lab1[i] +
2177
- t * t * lab2[i]
2178
- );
2179
- return new Color(lab, 'lab');
2180
- };
2181
- } else if (colors.length === 4) {
2182
- // cubic bezier interpolation
2183
- let lab3;
2184
- [lab0, lab1, lab2, lab3] = colors.map((c) => c.lab());
2185
- I = function (t) {
2186
- const lab = [0, 1, 2].map(
2187
- (i) =>
2188
- (1 - t) * (1 - t) * (1 - t) * lab0[i] +
2189
- 3 * (1 - t) * (1 - t) * t * lab1[i] +
2190
- 3 * (1 - t) * t * t * lab2[i] +
2191
- t * t * t * lab3[i]
2192
- );
2193
- return new Color(lab, 'lab');
2194
- };
2195
- } else if (colors.length >= 5) {
2196
- // general case (degree n bezier)
2197
- let labs, row, n;
2198
- labs = colors.map((c) => c.lab());
2199
- n = colors.length - 1;
2200
- row = binom_row(n);
2201
- I = function (t) {
2202
- const u = 1 - t;
2203
- const lab = [0, 1, 2].map((i) =>
2204
- labs.reduce(
2205
- (sum, el, j) =>
2206
- sum + row[j] * u ** (n - j) * t ** j * el[i],
2207
- 0
2208
- )
2209
- );
2210
- return new Color(lab, 'lab');
2211
- };
2212
- } else {
2213
- throw new RangeError('No point in running bezier with only one color.');
2214
- }
2215
- return I;
2216
- };
2217
-
2218
- const bezier$1 = (colors) => {
2219
- const f = bezier(colors);
2220
- f.scale = () => scale(f);
2221
- return f;
2222
- };
2223
-
2224
- const { round: round$3 } = Math;
2225
-
2226
- Color.prototype.rgb = function (rnd = true) {
2227
- if (rnd === false) return this._rgb.slice(0, 3);
2228
- return this._rgb.slice(0, 3).map(round$3);
2229
- };
2230
-
2231
- Color.prototype.rgba = function (rnd = true) {
2232
- return this._rgb.slice(0, 4).map((v, i) => {
2233
- return i < 3 ? (rnd === false ? v : round$3(v)) : v;
2234
- });
2235
- };
2236
-
2237
- const rgb = (...args) => new Color(...args, 'rgb');
2238
- Object.assign(chroma, { rgb });
2239
-
2240
- input.format.rgb = (...args) => {
2241
- const rgba = unpack(args, 'rgba');
2242
- if (rgba[3] === undefined) rgba[3] = 1;
2243
- return rgba;
2244
- };
2245
-
2246
- input.autodetect.push({
2247
- p: 3,
2248
- test: (...args) => {
2249
- args = unpack(args, 'rgba');
2250
- if (
2251
- type(args) === 'array' &&
2252
- (args.length === 3 ||
2253
- (args.length === 4 &&
2254
- type(args[3]) == 'number' &&
2255
- args[3] >= 0 &&
2256
- args[3] <= 1))
2257
- ) {
2258
- return 'rgb';
2259
- }
2260
- }
2261
- });
2262
-
2263
- /*
2264
- * interpolates between a set of colors uzing a bezier spline
2265
- * blend mode formulas taken from https://web.archive.org/web/20180110014946/http://www.venture-ware.com/kevin/coding/lets-learn-math-photoshop-blend-modes/
2266
- */
2267
-
2268
-
2269
- const blend = (bottom, top, mode) => {
2270
- if (!blend[mode]) {
2271
- throw new Error('unknown blend mode ' + mode);
2272
- }
2273
- return blend[mode](bottom, top);
2274
- };
2275
-
2276
- const blend_f = (f) => (bottom, top) => {
2277
- const c0 = chroma(top).rgb();
2278
- const c1 = chroma(bottom).rgb();
2279
- return chroma.rgb(f(c0, c1));
2280
- };
2281
-
2282
- const each = (f) => (c0, c1) => {
2283
- const out = [];
2284
- out[0] = f(c0[0], c1[0]);
2285
- out[1] = f(c0[1], c1[1]);
2286
- out[2] = f(c0[2], c1[2]);
2287
- return out;
2288
- };
2289
-
2290
- const normal = (a) => a;
2291
- const multiply = (a, b) => (a * b) / 255;
2292
- const darken = (a, b) => (a > b ? b : a);
2293
- const lighten = (a, b) => (a > b ? a : b);
2294
- const screen = (a, b) => 255 * (1 - (1 - a / 255) * (1 - b / 255));
2295
- const overlay = (a, b) =>
2296
- b < 128 ? (2 * a * b) / 255 : 255 * (1 - 2 * (1 - a / 255) * (1 - b / 255));
2297
- const burn = (a, b) => 255 * (1 - (1 - b / 255) / (a / 255));
2298
- const dodge = (a, b) => {
2299
- if (a === 255) return 255;
2300
- a = (255 * (b / 255)) / (1 - a / 255);
2301
- return a > 255 ? 255 : a;
2302
- };
2303
-
2304
- // # add = (a,b) ->
2305
- // # if (a + b > 255) then 255 else a + b
2306
-
2307
- blend.normal = blend_f(each(normal));
2308
- blend.multiply = blend_f(each(multiply));
2309
- blend.screen = blend_f(each(screen));
2310
- blend.overlay = blend_f(each(overlay));
2311
- blend.darken = blend_f(each(darken));
2312
- blend.lighten = blend_f(each(lighten));
2313
- blend.dodge = blend_f(each(dodge));
2314
- blend.burn = blend_f(each(burn));
2315
-
2316
- // cubehelix interpolation
2317
- // based on D.A. Green "A colour scheme for the display of astronomical intensity images"
2318
- // http://astron-soc.in/bulletin/11June/289392011.pdf
2319
- const { pow: pow$2, sin: sin$1, cos: cos$1 } = Math;
2320
-
2321
- function cubehelix (
2322
- start = 300,
2323
- rotations = -1.5,
2324
- hue = 1,
2325
- gamma = 1,
2326
- lightness = [0, 1]
2327
- ) {
2328
- let dh = 0,
2329
- dl;
2330
- if (type(lightness) === 'array') {
2331
- dl = lightness[1] - lightness[0];
2332
- } else {
2333
- dl = 0;
2334
- lightness = [lightness, lightness];
2335
- }
2336
- const f = function (fract) {
2337
- const a = TWOPI * ((start + 120) / 360 + rotations * fract);
2338
- const l = pow$2(lightness[0] + dl * fract, gamma);
2339
- const h = dh !== 0 ? hue[0] + fract * dh : hue;
2340
- const amp = (h * l * (1 - l)) / 2;
2341
- const cos_a = cos$1(a);
2342
- const sin_a = sin$1(a);
2343
- const r = l + amp * (-0.14861 * cos_a + 1.78277 * sin_a);
2344
- const g = l + amp * (-0.29227 * cos_a - 0.90649 * sin_a);
2345
- const b = l + amp * (1.97294 * cos_a);
2346
- return chroma(clip_rgb([r * 255, g * 255, b * 255, 1]));
2347
- };
2348
- f.start = function (s) {
2349
- if (s == null) {
2350
- return start;
2351
- }
2352
- start = s;
2353
- return f;
2354
- };
2355
- f.rotations = function (r) {
2356
- if (r == null) {
2357
- return rotations;
2358
- }
2359
- rotations = r;
2360
- return f;
2361
- };
2362
- f.gamma = function (g) {
2363
- if (g == null) {
2364
- return gamma;
2365
- }
2366
- gamma = g;
2367
- return f;
2368
- };
2369
- f.hue = function (h) {
2370
- if (h == null) {
2371
- return hue;
2372
- }
2373
- hue = h;
2374
- if (type(hue) === 'array') {
2375
- dh = hue[1] - hue[0];
2376
- if (dh === 0) {
2377
- hue = hue[1];
2378
- }
2379
- } else {
2380
- dh = 0;
2381
- }
2382
- return f;
2383
- };
2384
- f.lightness = function (h) {
2385
- if (h == null) {
2386
- return lightness;
2387
- }
2388
- if (type(h) === 'array') {
2389
- lightness = h;
2390
- dl = h[1] - h[0];
2391
- } else {
2392
- lightness = [h, h];
2393
- dl = 0;
2394
- }
2395
- return f;
2396
- };
2397
- f.scale = () => chroma.scale(f);
2398
- f.hue(hue);
2399
- return f;
2400
- }
2401
-
2402
- const digits = '0123456789abcdef';
2403
-
2404
- const { floor: floor$1, random } = Math;
2405
-
2406
- const random$1 = () => {
2407
- let code = '#';
2408
- for (let i = 0; i < 6; i++) {
2409
- code += digits.charAt(floor$1(random() * 16));
2410
- }
2411
- return new Color(code, 'hex');
2412
- };
2413
-
2414
- const { log: log$1, pow: pow$1, floor, abs: abs$1 } = Math;
2415
-
2416
- function analyze(data, key = null) {
2417
- const r = {
2418
- min: Number.MAX_VALUE,
2419
- max: Number.MAX_VALUE * -1,
2420
- sum: 0,
2421
- values: [],
2422
- count: 0
2423
- };
2424
- if (type(data) === 'object') {
2425
- data = Object.values(data);
2426
- }
2427
- data.forEach((val) => {
2428
- if (key && type(val) === 'object') val = val[key];
2429
- if (val !== undefined && val !== null && !isNaN(val)) {
2430
- r.values.push(val);
2431
- r.sum += val;
2432
- if (val < r.min) r.min = val;
2433
- if (val > r.max) r.max = val;
2434
- r.count += 1;
2435
- }
2436
- });
2437
-
2438
- r.domain = [r.min, r.max];
2439
-
2440
- r.limits = (mode, num) => limits(r, mode, num);
2441
-
2442
- return r;
2443
- }
2444
-
2445
- function limits(data, mode = 'equal', num = 7) {
2446
- if (type(data) == 'array') {
2447
- data = analyze(data);
2448
- }
2449
- const { min, max } = data;
2450
- const values = data.values.sort((a, b) => a - b);
2451
-
2452
- if (num === 1) {
2453
- return [min, max];
2454
- }
2455
-
2456
- const limits = [];
2457
-
2458
- if (mode.substr(0, 1) === 'c') {
2459
- // continuous
2460
- limits.push(min);
2461
- limits.push(max);
2462
- }
2463
-
2464
- if (mode.substr(0, 1) === 'e') {
2465
- // equal interval
2466
- limits.push(min);
2467
- for (let i = 1; i < num; i++) {
2468
- limits.push(min + (i / num) * (max - min));
2469
- }
2470
- limits.push(max);
2471
- } else if (mode.substr(0, 1) === 'l') {
2472
- // log scale
2473
- if (min <= 0) {
2474
- throw new Error(
2475
- 'Logarithmic scales are only possible for values > 0'
2476
- );
2477
- }
2478
- const min_log = Math.LOG10E * log$1(min);
2479
- const max_log = Math.LOG10E * log$1(max);
2480
- limits.push(min);
2481
- for (let i = 1; i < num; i++) {
2482
- limits.push(pow$1(10, min_log + (i / num) * (max_log - min_log)));
2483
- }
2484
- limits.push(max);
2485
- } else if (mode.substr(0, 1) === 'q') {
2486
- // quantile scale
2487
- limits.push(min);
2488
- for (let i = 1; i < num; i++) {
2489
- const p = ((values.length - 1) * i) / num;
2490
- const pb = floor(p);
2491
- if (pb === p) {
2492
- limits.push(values[pb]);
2493
- } else {
2494
- // p > pb
2495
- const pr = p - pb;
2496
- limits.push(values[pb] * (1 - pr) + values[pb + 1] * pr);
2497
- }
2498
- }
2499
- limits.push(max);
2500
- } else if (mode.substr(0, 1) === 'k') {
2501
- // k-means clustering
2502
- /*
2503
- implementation based on
2504
- http://code.google.com/p/figue/source/browse/trunk/figue.js#336
2505
- simplified for 1-d input values
2506
- */
2507
- let cluster;
2508
- const n = values.length;
2509
- const assignments = new Array(n);
2510
- const clusterSizes = new Array(num);
2511
- let repeat = true;
2512
- let nb_iters = 0;
2513
- let centroids = null;
2514
-
2515
- // get seed values
2516
- centroids = [];
2517
- centroids.push(min);
2518
- for (let i = 1; i < num; i++) {
2519
- centroids.push(min + (i / num) * (max - min));
2520
- }
2521
- centroids.push(max);
2522
-
2523
- while (repeat) {
2524
- // assignment step
2525
- for (let j = 0; j < num; j++) {
2526
- clusterSizes[j] = 0;
2527
- }
2528
- for (let i = 0; i < n; i++) {
2529
- const value = values[i];
2530
- let mindist = Number.MAX_VALUE;
2531
- let best;
2532
- for (let j = 0; j < num; j++) {
2533
- const dist = abs$1(centroids[j] - value);
2534
- if (dist < mindist) {
2535
- mindist = dist;
2536
- best = j;
2537
- }
2538
- clusterSizes[best]++;
2539
- assignments[i] = best;
2540
- }
2541
- }
2542
-
2543
- // update centroids step
2544
- const newCentroids = new Array(num);
2545
- for (let j = 0; j < num; j++) {
2546
- newCentroids[j] = null;
2547
- }
2548
- for (let i = 0; i < n; i++) {
2549
- cluster = assignments[i];
2550
- if (newCentroids[cluster] === null) {
2551
- newCentroids[cluster] = values[i];
2552
- } else {
2553
- newCentroids[cluster] += values[i];
2554
- }
2555
- }
2556
- for (let j = 0; j < num; j++) {
2557
- newCentroids[j] *= 1 / clusterSizes[j];
2558
- }
2559
-
2560
- // check convergence
2561
- repeat = false;
2562
- for (let j = 0; j < num; j++) {
2563
- if (newCentroids[j] !== centroids[j]) {
2564
- repeat = true;
2565
- break;
2566
- }
2567
- }
2568
-
2569
- centroids = newCentroids;
2570
- nb_iters++;
2571
-
2572
- if (nb_iters > 200) {
2573
- repeat = false;
2574
- }
2575
- }
2576
-
2577
- // finished k-means clustering
2578
- // the next part is borrowed from gabrielflor.it
2579
- const kClusters = {};
2580
- for (let j = 0; j < num; j++) {
2581
- kClusters[j] = [];
2582
- }
2583
- for (let i = 0; i < n; i++) {
2584
- cluster = assignments[i];
2585
- kClusters[cluster].push(values[i]);
2586
- }
2587
- let tmpKMeansBreaks = [];
2588
- for (let j = 0; j < num; j++) {
2589
- tmpKMeansBreaks.push(kClusters[j][0]);
2590
- tmpKMeansBreaks.push(kClusters[j][kClusters[j].length - 1]);
2591
- }
2592
- tmpKMeansBreaks = tmpKMeansBreaks.sort((a, b) => a - b);
2593
- limits.push(tmpKMeansBreaks[0]);
2594
- for (let i = 1; i < tmpKMeansBreaks.length; i += 2) {
2595
- const v = tmpKMeansBreaks[i];
2596
- if (!isNaN(v) && limits.indexOf(v) === -1) {
2597
- limits.push(v);
2598
- }
2599
- }
2600
- }
2601
- return limits;
2602
- }
2603
-
2604
- const contrast = (a, b) => {
2605
- // WCAG contrast ratio
2606
- // see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
2607
- a = new Color(a);
2608
- b = new Color(b);
2609
- const l1 = a.luminance();
2610
- const l2 = b.luminance();
2611
- return l1 > l2 ? (l1 + 0.05) / (l2 + 0.05) : (l2 + 0.05) / (l1 + 0.05);
2612
- };
2613
-
2614
- /**
2615
- * @license
2616
- *
2617
- * The APCA contrast prediction algorithm is based of the formulas published
2618
- * in the APCA-1.0.98G specification by Myndex. The specification is available at:
2619
- * https://raw.githubusercontent.com/Myndex/apca-w3/master/images/APCAw3_0.1.17_APCA0.0.98G.svg
2620
- *
2621
- * Note that the APCA implementation is still beta, so please update to
2622
- * future versions of chroma.js when they become available.
2623
- *
2624
- * You can read more about the APCA Readability Criterion at
2625
- * https://readtech.org/ARC/
2626
- */
2627
-
2628
- // constants
2629
- const W_offset = 0.027;
2630
- const P_in = 0.0005;
2631
- const P_out = 0.1;
2632
- const R_scale = 1.14;
2633
- const B_threshold = 0.022;
2634
- const B_exp = 1.414;
2635
-
2636
- const contrastAPCA = (text, bg) => {
2637
- // parse input colors
2638
- text = new Color(text);
2639
- bg = new Color(bg);
2640
- // if text color has alpha, blend against background
2641
- if (text.alpha() < 1) {
2642
- text = mix(bg, text, text.alpha(), 'rgb');
2643
- }
2644
- const l_text = lum(...text.rgb());
2645
- const l_bg = lum(...bg.rgb());
2646
-
2647
- // soft clamp black levels
2648
- const Y_text =
2649
- l_text >= B_threshold
2650
- ? l_text
2651
- : l_text + Math.pow(B_threshold - l_text, B_exp);
2652
- const Y_bg =
2653
- l_bg >= B_threshold ? l_bg : l_bg + Math.pow(B_threshold - l_bg, B_exp);
2654
-
2655
- // normal polarity (dark text on light background)
2656
- const S_norm = Math.pow(Y_bg, 0.56) - Math.pow(Y_text, 0.57);
2657
- // reverse polarity (light text on dark background)
2658
- const S_rev = Math.pow(Y_bg, 0.65) - Math.pow(Y_text, 0.62);
2659
- // clamp noise then scale
2660
- const C =
2661
- Math.abs(Y_bg - Y_text) < P_in
2662
- ? 0
2663
- : Y_text < Y_bg
2664
- ? S_norm * R_scale
2665
- : S_rev * R_scale;
2666
- // clamp minimum contrast then offset
2667
- const S_apc = Math.abs(C) < P_out ? 0 : C > 0 ? C - W_offset : C + W_offset;
2668
- // scale to 100
2669
- return S_apc * 100;
2670
- };
2671
-
2672
- function lum(r, g, b) {
2673
- return (
2674
- 0.2126729 * Math.pow(r / 255, 2.4) +
2675
- 0.7151522 * Math.pow(g / 255, 2.4) +
2676
- 0.072175 * Math.pow(b / 255, 2.4)
2677
- );
2678
- }
2679
-
2680
- const { sqrt, pow, min, max: max$1, atan2, abs, cos, sin, exp, PI } = Math;
2681
-
2682
- function deltaE (a, b, Kl = 1, Kc = 1, Kh = 1) {
2683
- // Delta E (CIE 2000)
2684
- // see http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE2000.html
2685
- var rad2deg = function (rad) {
2686
- return (360 * rad) / (2 * PI);
2687
- };
2688
- var deg2rad = function (deg) {
2689
- return (2 * PI * deg) / 360;
2690
- };
2691
- a = new Color(a);
2692
- b = new Color(b);
2693
- const [L1, a1, b1] = Array.from(a.lab());
2694
- const [L2, a2, b2] = Array.from(b.lab());
2695
- const avgL = (L1 + L2) / 2;
2696
- const C1 = sqrt(pow(a1, 2) + pow(b1, 2));
2697
- const C2 = sqrt(pow(a2, 2) + pow(b2, 2));
2698
- const avgC = (C1 + C2) / 2;
2699
- const G = 0.5 * (1 - sqrt(pow(avgC, 7) / (pow(avgC, 7) + pow(25, 7))));
2700
- const a1p = a1 * (1 + G);
2701
- const a2p = a2 * (1 + G);
2702
- const C1p = sqrt(pow(a1p, 2) + pow(b1, 2));
2703
- const C2p = sqrt(pow(a2p, 2) + pow(b2, 2));
2704
- const avgCp = (C1p + C2p) / 2;
2705
- const arctan1 = rad2deg(atan2(b1, a1p));
2706
- const arctan2 = rad2deg(atan2(b2, a2p));
2707
- const h1p = arctan1 >= 0 ? arctan1 : arctan1 + 360;
2708
- const h2p = arctan2 >= 0 ? arctan2 : arctan2 + 360;
2709
- const avgHp =
2710
- abs(h1p - h2p) > 180 ? (h1p + h2p + 360) / 2 : (h1p + h2p) / 2;
2711
- const T =
2712
- 1 -
2713
- 0.17 * cos(deg2rad(avgHp - 30)) +
2714
- 0.24 * cos(deg2rad(2 * avgHp)) +
2715
- 0.32 * cos(deg2rad(3 * avgHp + 6)) -
2716
- 0.2 * cos(deg2rad(4 * avgHp - 63));
2717
- let deltaHp = h2p - h1p;
2718
- deltaHp =
2719
- abs(deltaHp) <= 180
2720
- ? deltaHp
2721
- : h2p <= h1p
2722
- ? deltaHp + 360
2723
- : deltaHp - 360;
2724
- deltaHp = 2 * sqrt(C1p * C2p) * sin(deg2rad(deltaHp) / 2);
2725
- const deltaL = L2 - L1;
2726
- const deltaCp = C2p - C1p;
2727
- const sl = 1 + (0.015 * pow(avgL - 50, 2)) / sqrt(20 + pow(avgL - 50, 2));
2728
- const sc = 1 + 0.045 * avgCp;
2729
- const sh = 1 + 0.015 * avgCp * T;
2730
- const deltaTheta = 30 * exp(-pow((avgHp - 275) / 25, 2));
2731
- const Rc = 2 * sqrt(pow(avgCp, 7) / (pow(avgCp, 7) + pow(25, 7)));
2732
- const Rt = -Rc * sin(2 * deg2rad(deltaTheta));
2733
- const result = sqrt(
2734
- pow(deltaL / (Kl * sl), 2) +
2735
- pow(deltaCp / (Kc * sc), 2) +
2736
- pow(deltaHp / (Kh * sh), 2) +
2737
- Rt * (deltaCp / (Kc * sc)) * (deltaHp / (Kh * sh))
2738
- );
2739
- return max$1(0, min(100, result));
2740
- }
2741
-
2742
- // simple Euclidean distance
2743
- function distance (a, b, mode = 'lab') {
2744
- // Delta E (CIE 1976)
2745
- // see http://www.brucelindbloom.com/index.html?Equations.html
2746
- a = new Color(a);
2747
- b = new Color(b);
2748
- const l1 = a.get(mode);
2749
- const l2 = b.get(mode);
2750
- let sum_sq = 0;
2751
- for (let i in l1) {
2752
- const d = (l1[i] || 0) - (l2[i] || 0);
2753
- sum_sq += d * d;
2754
- }
2755
- return Math.sqrt(sum_sq);
2756
- }
2757
-
2758
- const valid = (...args) => {
2759
- try {
2760
- new Color(...args);
2761
- return true;
2762
- // eslint-disable-next-line
2763
- } catch (e) {
2764
- return false;
2765
- }
2766
- };
2767
-
2768
- // some pre-defined color scales:
2769
-
2770
- const scales = {
2771
- cool() {
2772
- return scale([chroma.hsl(180, 1, 0.9), chroma.hsl(250, 0.7, 0.4)]);
2773
- },
2774
- hot() {
2775
- return scale(['#000', '#f00', '#ff0', '#fff']).mode(
2776
- 'rgb'
2777
- );
2778
- }
2779
- };
2780
-
2781
- /**
2782
- ColorBrewer colors for chroma.js
2783
-
2784
- Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and The
2785
- Pennsylvania State University.
2786
-
2787
- Licensed under the Apache License, Version 2.0 (the "License");
2788
- you may not use this file except in compliance with the License.
2789
- You may obtain a copy of the License at
2790
- http://www.apache.org/licenses/LICENSE-2.0
2791
-
2792
- Unless required by applicable law or agreed to in writing, software distributed
2793
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
2794
- CONDITIONS OF ANY KIND, either express or implied. See the License for the
2795
- specific language governing permissions and limitations under the License.
2796
- */
2797
-
2798
- const colorbrewer = {
2799
- // sequential
2800
- OrRd: ['#fff7ec', '#fee8c8', '#fdd49e', '#fdbb84', '#fc8d59', '#ef6548', '#d7301f', '#b30000', '#7f0000'],
2801
- PuBu: ['#fff7fb', '#ece7f2', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#045a8d', '#023858'],
2802
- BuPu: ['#f7fcfd', '#e0ecf4', '#bfd3e6', '#9ebcda', '#8c96c6', '#8c6bb1', '#88419d', '#810f7c', '#4d004b'],
2803
- Oranges: ['#fff5eb', '#fee6ce', '#fdd0a2', '#fdae6b', '#fd8d3c', '#f16913', '#d94801', '#a63603', '#7f2704'],
2804
- BuGn: ['#f7fcfd', '#e5f5f9', '#ccece6', '#99d8c9', '#66c2a4', '#41ae76', '#238b45', '#006d2c', '#00441b'],
2805
- YlOrBr: ['#ffffe5', '#fff7bc', '#fee391', '#fec44f', '#fe9929', '#ec7014', '#cc4c02', '#993404', '#662506'],
2806
- YlGn: ['#ffffe5', '#f7fcb9', '#d9f0a3', '#addd8e', '#78c679', '#41ab5d', '#238443', '#006837', '#004529'],
2807
- Reds: ['#fff5f0', '#fee0d2', '#fcbba1', '#fc9272', '#fb6a4a', '#ef3b2c', '#cb181d', '#a50f15', '#67000d'],
2808
- RdPu: ['#fff7f3', '#fde0dd', '#fcc5c0', '#fa9fb5', '#f768a1', '#dd3497', '#ae017e', '#7a0177', '#49006a'],
2809
- Greens: ['#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'],
2810
- YlGnBu: ['#ffffd9', '#edf8b1', '#c7e9b4', '#7fcdbb', '#41b6c4', '#1d91c0', '#225ea8', '#253494', '#081d58'],
2811
- Purples: ['#fcfbfd', '#efedf5', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#54278f', '#3f007d'],
2812
- GnBu: ['#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5', '#7bccc4', '#4eb3d3', '#2b8cbe', '#0868ac', '#084081'],
2813
- Greys: ['#ffffff', '#f0f0f0', '#d9d9d9', '#bdbdbd', '#969696', '#737373', '#525252', '#252525', '#000000'],
2814
- YlOrRd: ['#ffffcc', '#ffeda0', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#bd0026', '#800026'],
2815
- PuRd: ['#f7f4f9', '#e7e1ef', '#d4b9da', '#c994c7', '#df65b0', '#e7298a', '#ce1256', '#980043', '#67001f'],
2816
- Blues: ['#f7fbff', '#deebf7', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#08519c', '#08306b'],
2817
- PuBuGn: ['#fff7fb', '#ece2f0', '#d0d1e6', '#a6bddb', '#67a9cf', '#3690c0', '#02818a', '#016c59', '#014636'],
2818
- Viridis: ['#440154', '#482777', '#3f4a8a', '#31678e', '#26838f', '#1f9d8a', '#6cce5a', '#b6de2b', '#fee825'],
2819
-
2820
- // diverging
2821
- Spectral: ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2'],
2822
- RdYlGn: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850', '#006837'],
2823
- RdBu: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#f7f7f7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'],
2824
- PiYG: ['#8e0152', '#c51b7d', '#de77ae', '#f1b6da', '#fde0ef', '#f7f7f7', '#e6f5d0', '#b8e186', '#7fbc41', '#4d9221', '#276419'],
2825
- PRGn: ['#40004b', '#762a83', '#9970ab', '#c2a5cf', '#e7d4e8', '#f7f7f7', '#d9f0d3', '#a6dba0', '#5aae61', '#1b7837', '#00441b'],
2826
- RdYlBu: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695'],
2827
- BrBG: ['#543005', '#8c510a', '#bf812d', '#dfc27d', '#f6e8c3', '#f5f5f5', '#c7eae5', '#80cdc1', '#35978f', '#01665e', '#003c30'],
2828
- RdGy: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#ffffff', '#e0e0e0', '#bababa', '#878787', '#4d4d4d', '#1a1a1a'],
2829
- PuOr: ['#7f3b08', '#b35806', '#e08214', '#fdb863', '#fee0b6', '#f7f7f7', '#d8daeb', '#b2abd2', '#8073ac', '#542788', '#2d004b'],
2830
-
2831
- // qualitative
2832
- Set2: ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f', '#e5c494', '#b3b3b3'],
2833
- Accent: ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666'],
2834
- Set1: ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'],
2835
- Set3: ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f'],
2836
- Dark2: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666'],
2837
- Paired: ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'],
2838
- Pastel2: ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae', '#f1e2cc', '#cccccc'],
2839
- Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']
2840
- };
2841
-
2842
- const colorbrewerTypes = Object.keys(colorbrewer);
2843
- const typeMap = new Map(colorbrewerTypes.map((key) => [key.toLowerCase(), key]));
2844
-
2845
- // use Proxy to allow case-insensitive access to palettes
2846
- const colorbrewerProxy =
2847
- typeof Proxy === 'function'
2848
- ? new Proxy(colorbrewer, {
2849
- get(target, prop) {
2850
- const lower = prop.toLowerCase();
2851
- if (typeMap.has(lower)) {
2852
- return target[typeMap.get(lower)];
2853
- }
2854
- },
2855
- getOwnPropertyNames() {
2856
- return Object.getOwnPropertyNames(colorbrewerTypes);
2857
- }
2858
- })
2859
- : colorbrewer;
2860
-
2861
- const cmyk2rgb = (...args) => {
2862
- args = unpack(args, 'cmyk');
2863
- const [c, m, y, k] = args;
2864
- const alpha = args.length > 4 ? args[4] : 1;
2865
- if (k === 1) return [0, 0, 0, alpha];
2866
- return [
2867
- c >= 1 ? 0 : 255 * (1 - c) * (1 - k), // r
2868
- m >= 1 ? 0 : 255 * (1 - m) * (1 - k), // g
2869
- y >= 1 ? 0 : 255 * (1 - y) * (1 - k), // b
2870
- alpha
2871
- ];
2872
- };
2873
-
2874
- const { max } = Math;
2875
-
2876
- const rgb2cmyk = (...args) => {
2877
- let [r, g, b] = unpack(args, 'rgb');
2878
- r = r / 255;
2879
- g = g / 255;
2880
- b = b / 255;
2881
- const k = 1 - max(r, max(g, b));
2882
- const f = k < 1 ? 1 / (1 - k) : 0;
2883
- const c = (1 - r - k) * f;
2884
- const m = (1 - g - k) * f;
2885
- const y = (1 - b - k) * f;
2886
- return [c, m, y, k];
2887
- };
2888
-
2889
- Color.prototype.cmyk = function () {
2890
- return rgb2cmyk(this._rgb);
2891
- };
2892
-
2893
- const cmyk = (...args) => new Color(...args, 'cmyk');
2894
- Object.assign(chroma, { cmyk });
2895
-
2896
- input.format.cmyk = cmyk2rgb;
2897
-
2898
- input.autodetect.push({
2899
- p: 2,
2900
- test: (...args) => {
2901
- args = unpack(args, 'cmyk');
2902
- if (type(args) === 'array' && args.length === 4) {
2903
- return 'cmyk';
2904
- }
2905
- }
2906
- });
2907
-
2908
- /*
2909
- * supported arguments:
2910
- * - hsl2css(h,s,l)
2911
- * - hsl2css(h,s,l,a)
2912
- * - hsl2css([h,s,l], mode)
2913
- * - hsl2css([h,s,l,a], mode)
2914
- * - hsl2css({h,s,l,a}, mode)
2915
- */
2916
- const hsl2css = (...args) => {
2917
- const hsla = unpack(args, 'hsla');
2918
- let mode = last(args) || 'lsa';
2919
- hsla[0] = rnd2(hsla[0] || 0) + 'deg';
2920
- hsla[1] = rnd2(hsla[1] * 100) + '%';
2921
- hsla[2] = rnd2(hsla[2] * 100) + '%';
2922
- if (mode === 'hsla' || (hsla.length > 3 && hsla[3] < 1)) {
2923
- hsla[3] = '/ ' + (hsla.length > 3 ? hsla[3] : 1);
2924
- mode = 'hsla';
2925
- } else {
2926
- hsla.length = 3;
2927
- }
2928
- return `${mode.substr(0, 3)}(${hsla.join(' ')})`;
2929
- };
2930
-
2931
- /*
2932
- * supported arguments:
2933
- * - lab2css(l,a,b)
2934
- * - lab2css(l,a,b,alpha)
2935
- * - lab2css([l,a,b], mode)
2936
- * - lab2css([l,a,b,alpha], mode)
2937
- */
2938
- const lab2css = (...args) => {
2939
- const laba = unpack(args, 'lab');
2940
- let mode = last(args) || 'lab';
2941
- laba[0] = rnd2(laba[0]) + '%';
2942
- laba[1] = rnd2(laba[1]);
2943
- laba[2] = rnd2(laba[2]);
2944
- if (mode === 'laba' || (laba.length > 3 && laba[3] < 1)) {
2945
- laba[3] = '/ ' + (laba.length > 3 ? laba[3] : 1);
2946
- } else {
2947
- laba.length = 3;
2948
- }
2949
- return `lab(${laba.join(' ')})`;
2950
- };
2951
-
2952
- /*
2953
- * supported arguments:
2954
- * - lab2css(l,a,b)
2955
- * - lab2css(l,a,b,alpha)
2956
- * - lab2css([l,a,b], mode)
2957
- * - lab2css([l,a,b,alpha], mode)
2958
- */
2959
- const lch2css = (...args) => {
2960
- const lcha = unpack(args, 'lch');
2961
- let mode = last(args) || 'lab';
2962
- lcha[0] = rnd2(lcha[0]) + '%';
2963
- lcha[1] = rnd2(lcha[1]);
2964
- lcha[2] = isNaN(lcha[2]) ? 'none' : rnd2(lcha[2]) + 'deg'; // add deg unit to hue
2965
- if (mode === 'lcha' || (lcha.length > 3 && lcha[3] < 1)) {
2966
- lcha[3] = '/ ' + (lcha.length > 3 ? lcha[3] : 1);
2967
- } else {
2968
- lcha.length = 3;
2969
- }
2970
- return `lch(${lcha.join(' ')})`;
2971
- };
2972
-
2973
- const oklab2css = (...args) => {
2974
- const laba = unpack(args, 'lab');
2975
- laba[0] = rnd2(laba[0] * 100) + '%';
2976
- laba[1] = rnd3(laba[1]);
2977
- laba[2] = rnd3(laba[2]);
2978
- if (laba.length > 3 && laba[3] < 1) {
2979
- laba[3] = '/ ' + (laba.length > 3 ? laba[3] : 1);
2980
- } else {
2981
- laba.length = 3;
2982
- }
2983
- return `oklab(${laba.join(' ')})`;
2984
- };
2985
-
2986
- const rgb2oklch = (...args) => {
2987
- const [r, g, b, ...rest] = unpack(args, 'rgb');
2988
- const [l, a, b_] = rgb2oklab(r, g, b);
2989
- const [L, c, h] = lab2lch(l, a, b_);
2990
- return [L, c, h, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
2991
- };
2992
-
2993
- const oklch2css = (...args) => {
2994
- const lcha = unpack(args, 'lch');
2995
- lcha[0] = rnd2(lcha[0] * 100) + '%';
2996
- lcha[1] = rnd3(lcha[1]);
2997
- lcha[2] = isNaN(lcha[2]) ? 'none' : rnd2(lcha[2]) + 'deg'; // add deg unit to hue
2998
- if (lcha.length > 3 && lcha[3] < 1) {
2999
- lcha[3] = '/ ' + (lcha.length > 3 ? lcha[3] : 1);
3000
- } else {
3001
- lcha.length = 3;
3002
- }
3003
- return `oklch(${lcha.join(' ')})`;
3004
- };
3005
-
3006
- const { round: round$2 } = Math;
3007
-
3008
- /*
3009
- * supported arguments:
3010
- * - rgb2css(r,g,b)
3011
- * - rgb2css(r,g,b,a)
3012
- * - rgb2css([r,g,b], mode)
3013
- * - rgb2css([r,g,b,a], mode)
3014
- * - rgb2css({r,g,b,a}, mode)
3015
- */
3016
- const rgb2css = (...args) => {
3017
- const rgba = unpack(args, 'rgba');
3018
- let mode = last(args) || 'rgb';
3019
- if (mode.substr(0, 3) === 'hsl') {
3020
- return hsl2css(rgb2hsl$1(rgba), mode);
3021
- }
3022
- if (mode.substr(0, 3) === 'lab') {
3023
- // change to D50 lab whitepoint since this is what W3C is using for CSS Lab colors
3024
- const prevWhitePoint = getLabWhitePoint();
3025
- setLabWhitePoint('d50');
3026
- const cssColor = lab2css(rgb2lab(rgba), mode);
3027
- setLabWhitePoint(prevWhitePoint);
3028
- return cssColor;
3029
- }
3030
- if (mode.substr(0, 3) === 'lch') {
3031
- // change to D50 lab whitepoint since this is what W3C is using for CSS Lab colors
3032
- const prevWhitePoint = getLabWhitePoint();
3033
- setLabWhitePoint('d50');
3034
- const cssColor = lch2css(rgb2lch(rgba), mode);
3035
- setLabWhitePoint(prevWhitePoint);
3036
- return cssColor;
3037
- }
3038
- if (mode.substr(0, 5) === 'oklab') {
3039
- return oklab2css(rgb2oklab(rgba));
3040
- }
3041
- if (mode.substr(0, 5) === 'oklch') {
3042
- return oklch2css(rgb2oklch(rgba));
3043
- }
3044
- rgba[0] = round$2(rgba[0]);
3045
- rgba[1] = round$2(rgba[1]);
3046
- rgba[2] = round$2(rgba[2]);
3047
- if (mode === 'rgba' || (rgba.length > 3 && rgba[3] < 1)) {
3048
- rgba[3] = '/ ' + (rgba.length > 3 ? rgba[3] : 1);
3049
- mode = 'rgba';
3050
- }
3051
- return `${mode.substr(0, 3)}(${rgba.slice(0, mode === 'rgb' ? 3 : 4).join(' ')})`;
3052
- };
3053
-
3054
- const oklch2rgb = (...args) => {
3055
- args = unpack(args, 'lch');
3056
- const [l, c, h, ...rest] = args;
3057
- const [L, a, b_] = lch2lab(l, c, h);
3058
- const [r, g, b] = oklab2rgb(L, a, b_);
3059
- return [r, g, b, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])];
3060
- };
3061
-
3062
- const INT_OR_PCT = /((?:-?\d+)|(?:-?\d+(?:\.\d+)?)%|none)/.source;
3063
- const FLOAT_OR_PCT = /((?:-?(?:\d+(?:\.\d*)?|\.\d+)%?)|none)/.source;
3064
- const PCT = /((?:-?(?:\d+(?:\.\d*)?|\.\d+)%)|none)/.source;
3065
- const RE_S = /\s*/.source;
3066
- const SEP = /\s+/.source;
3067
- const COMMA = /\s*,\s*/.source;
3068
- const ANLGE = /((?:-?(?:\d+(?:\.\d*)?|\.\d+)(?:deg)?)|none)/.source;
3069
- const ALPHA = /\s*(?:\/\s*((?:[01]|[01]?\.\d+)|\d+(?:\.\d+)?%))?/.source;
3070
-
3071
- // e.g. rgb(250 20 0), rgb(100% 50% 20%), rgb(100% 50% 20% / 0.5)
3072
- const RE_RGB = new RegExp(
3073
- '^rgba?\\(' +
3074
- RE_S +
3075
- [INT_OR_PCT, INT_OR_PCT, INT_OR_PCT].join(SEP) +
3076
- ALPHA +
3077
- '\\)$'
3078
- );
3079
- const RE_RGB_LEGACY = new RegExp(
3080
- '^rgb\\(' +
3081
- RE_S +
3082
- [INT_OR_PCT, INT_OR_PCT, INT_OR_PCT].join(COMMA) +
3083
- RE_S +
3084
- '\\)$'
3085
- );
3086
- const RE_RGBA_LEGACY = new RegExp(
3087
- '^rgba\\(' +
3088
- RE_S +
3089
- [INT_OR_PCT, INT_OR_PCT, INT_OR_PCT, FLOAT_OR_PCT].join(COMMA) +
3090
- RE_S +
3091
- '\\)$'
3092
- );
3093
-
3094
- const RE_HSL = new RegExp(
3095
- '^hsla?\\(' + RE_S + [ANLGE, PCT, PCT].join(SEP) + ALPHA + '\\)$'
3096
- );
3097
- const RE_HSL_LEGACY = new RegExp(
3098
- '^hsl?\\(' + RE_S + [ANLGE, PCT, PCT].join(COMMA) + RE_S + '\\)$'
3099
- );
3100
- const RE_HSLA_LEGACY =
3101
- /^hsla\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/;
3102
-
3103
- const RE_LAB = new RegExp(
3104
- '^lab\\(' +
3105
- RE_S +
3106
- [FLOAT_OR_PCT, FLOAT_OR_PCT, FLOAT_OR_PCT].join(SEP) +
3107
- ALPHA +
3108
- '\\)$'
3109
- );
3110
- const RE_LCH = new RegExp(
3111
- '^lch\\(' +
3112
- RE_S +
3113
- [FLOAT_OR_PCT, FLOAT_OR_PCT, ANLGE].join(SEP) +
3114
- ALPHA +
3115
- '\\)$'
3116
- );
3117
- const RE_OKLAB = new RegExp(
3118
- '^oklab\\(' +
3119
- RE_S +
3120
- [FLOAT_OR_PCT, FLOAT_OR_PCT, FLOAT_OR_PCT].join(SEP) +
3121
- ALPHA +
3122
- '\\)$'
3123
- );
3124
- const RE_OKLCH = new RegExp(
3125
- '^oklch\\(' +
3126
- RE_S +
3127
- [FLOAT_OR_PCT, FLOAT_OR_PCT, ANLGE].join(SEP) +
3128
- ALPHA +
3129
- '\\)$'
3130
- );
3131
-
3132
- const { round: round$1 } = Math;
3133
-
3134
- const roundRGB = (rgb) => {
3135
- return rgb.map((v, i) => (i <= 2 ? limit(round$1(v), 0, 255) : v));
3136
- };
3137
-
3138
- const percentToAbsolute = (pct, min = 0, max = 100, signed = false) => {
3139
- if (typeof pct === 'string' && pct.endsWith('%')) {
3140
- pct = parseFloat(pct.substring(0, pct.length - 1)) / 100;
3141
- if (signed) {
3142
- // signed percentages are in the range -100% to 100%
3143
- pct = min + (pct + 1) * 0.5 * (max - min);
3144
- } else {
3145
- pct = min + pct * (max - min);
3146
- }
3147
- }
3148
- return +pct;
3149
- };
3150
-
3151
- const noneToValue = (v, noneValue) => {
3152
- return v === 'none' ? noneValue : v;
3153
- };
3154
-
3155
- const css2rgb = (css) => {
3156
- css = css.toLowerCase().trim();
3157
-
3158
- if (css === 'transparent') {
3159
- return [0, 0, 0, 0];
3160
- }
3161
-
3162
- let m;
3163
-
3164
- if (input.format.named) {
3165
- try {
3166
- return input.format.named(css);
3167
- // eslint-disable-next-line
3168
- } catch (e) {}
3169
- }
3170
-
3171
- // rgb(250 20 0) or rgb(250,20,0)
3172
- if ((m = css.match(RE_RGB)) || (m = css.match(RE_RGB_LEGACY))) {
3173
- let rgb = m.slice(1, 4);
3174
- for (let i = 0; i < 3; i++) {
3175
- rgb[i] = +percentToAbsolute(noneToValue(rgb[i], 0), 0, 255);
3176
- }
3177
- rgb = roundRGB(rgb);
3178
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3179
- rgb[3] = alpha; // default alpha
3180
- return rgb;
3181
- }
3182
-
3183
- // rgba(250,20,0,0.4)
3184
- if ((m = css.match(RE_RGBA_LEGACY))) {
3185
- const rgb = m.slice(1, 5);
3186
- for (let i = 0; i < 4; i++) {
3187
- rgb[i] = +percentToAbsolute(rgb[i], 0, 255);
3188
- }
3189
- return rgb;
3190
- }
3191
-
3192
- // hsl(0,100%,50%)
3193
- if ((m = css.match(RE_HSL)) || (m = css.match(RE_HSL_LEGACY))) {
3194
- const hsl = m.slice(1, 4);
3195
- hsl[0] = +noneToValue(hsl[0].replace('deg', ''), 0);
3196
- hsl[1] = +percentToAbsolute(noneToValue(hsl[1], 0), 0, 100) * 0.01;
3197
- hsl[2] = +percentToAbsolute(noneToValue(hsl[2], 0), 0, 100) * 0.01;
3198
- const rgb = roundRGB(hsl2rgb(hsl));
3199
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3200
- rgb[3] = alpha;
3201
- return rgb;
3202
- }
3203
-
3204
- // hsla(0,100%,50%,0.5)
3205
- if ((m = css.match(RE_HSLA_LEGACY))) {
3206
- const hsl = m.slice(1, 4);
3207
- hsl[1] *= 0.01;
3208
- hsl[2] *= 0.01;
3209
- const rgb = hsl2rgb(hsl);
3210
- for (let i = 0; i < 3; i++) {
3211
- rgb[i] = round$1(rgb[i]);
3212
- }
3213
- rgb[3] = +m[4]; // default alpha = 1
3214
- return rgb;
3215
- }
3216
-
3217
- if ((m = css.match(RE_LAB))) {
3218
- const lab = m.slice(1, 4);
3219
- lab[0] = percentToAbsolute(noneToValue(lab[0], 0), 0, 100);
3220
- lab[1] = percentToAbsolute(noneToValue(lab[1], 0), -125, 125, true);
3221
- lab[2] = percentToAbsolute(noneToValue(lab[2], 0), -125, 125, true);
3222
- // convert to D50 Lab whitepoint
3223
- const wp = getLabWhitePoint();
3224
- setLabWhitePoint('d50');
3225
- const rgb = roundRGB(lab2rgb(lab));
3226
- // convert back to original Lab whitepoint
3227
- setLabWhitePoint(wp);
3228
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3229
- rgb[3] = alpha;
3230
- return rgb;
3231
- }
3232
-
3233
- if ((m = css.match(RE_LCH))) {
3234
- const lch = m.slice(1, 4);
3235
- lch[0] = percentToAbsolute(lch[0], 0, 100);
3236
- lch[1] = percentToAbsolute(noneToValue(lch[1], 0), 0, 150, false);
3237
- lch[2] = +noneToValue(lch[2].replace('deg', ''), 0);
3238
- // convert to D50 Lab whitepoint
3239
- const wp = getLabWhitePoint();
3240
- setLabWhitePoint('d50');
3241
- const rgb = roundRGB(lch2rgb(lch));
3242
- // convert back to original Lab whitepoint
3243
- setLabWhitePoint(wp);
3244
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3245
- rgb[3] = alpha;
3246
- return rgb;
3247
- }
3248
-
3249
- if ((m = css.match(RE_OKLAB))) {
3250
- const oklab = m.slice(1, 4);
3251
- oklab[0] = percentToAbsolute(noneToValue(oklab[0], 0), 0, 1);
3252
- oklab[1] = percentToAbsolute(noneToValue(oklab[1], 0), -0.4, 0.4, true);
3253
- oklab[2] = percentToAbsolute(noneToValue(oklab[2], 0), -0.4, 0.4, true);
3254
- const rgb = roundRGB(oklab2rgb(oklab));
3255
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3256
- rgb[3] = alpha;
3257
- return rgb;
3258
- }
3259
-
3260
- if ((m = css.match(RE_OKLCH))) {
3261
- const oklch = m.slice(1, 4);
3262
- oklch[0] = percentToAbsolute(noneToValue(oklch[0], 0), 0, 1);
3263
- oklch[1] = percentToAbsolute(noneToValue(oklch[1], 0), 0, 0.4, false);
3264
- oklch[2] = +noneToValue(oklch[2].replace('deg', ''), 0);
3265
- const rgb = roundRGB(oklch2rgb(oklch));
3266
- const alpha = m[4] !== undefined ? +percentToAbsolute(m[4], 0, 1) : 1;
3267
- rgb[3] = alpha;
3268
- return rgb;
3269
- }
3270
- };
3271
-
3272
- css2rgb.test = (s) => {
3273
- return (
3274
- // modern
3275
- RE_RGB.test(s) ||
3276
- RE_HSL.test(s) ||
3277
- RE_LAB.test(s) ||
3278
- RE_LCH.test(s) ||
3279
- RE_OKLAB.test(s) ||
3280
- RE_OKLCH.test(s) ||
3281
- // legacy
3282
- RE_RGB_LEGACY.test(s) ||
3283
- RE_RGBA_LEGACY.test(s) ||
3284
- RE_HSL_LEGACY.test(s) ||
3285
- RE_HSLA_LEGACY.test(s) ||
3286
- s === 'transparent'
3287
- );
3288
- };
3289
-
3290
- Color.prototype.css = function (mode) {
3291
- return rgb2css(this._rgb, mode);
3292
- };
3293
-
3294
- const css = (...args) => new Color(...args, 'css');
3295
- chroma.css = css;
3296
-
3297
- input.format.css = css2rgb;
3298
-
3299
- input.autodetect.push({
3300
- p: 5,
3301
- test: (h, ...rest) => {
3302
- if (!rest.length && type(h) === 'string' && css2rgb.test(h)) {
3303
- return 'css';
3304
- }
3305
- }
3306
- });
3307
-
3308
- input.format.gl = (...args) => {
3309
- const rgb = unpack(args, 'rgba');
3310
- rgb[0] *= 255;
3311
- rgb[1] *= 255;
3312
- rgb[2] *= 255;
3313
- return rgb;
3314
- };
3315
-
3316
- const gl = (...args) => new Color(...args, 'gl');
3317
- chroma.gl = gl;
3318
-
3319
- Color.prototype.gl = function () {
3320
- const rgb = this._rgb;
3321
- return [rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, rgb[3]];
3322
- };
3323
-
3324
- Color.prototype.hex = function (mode) {
3325
- return rgb2hex(this._rgb, mode);
3326
- };
3327
-
3328
- const hex = (...args) => new Color(...args, 'hex');
3329
- chroma.hex = hex;
3330
-
3331
- input.format.hex = hex2rgb;
3332
- input.autodetect.push({
3333
- p: 4,
3334
- test: (h, ...rest) => {
3335
- if (
3336
- !rest.length &&
3337
- type(h) === 'string' &&
3338
- [3, 4, 5, 6, 7, 8, 9].indexOf(h.length) >= 0
3339
- ) {
3340
- return 'hex';
3341
- }
3342
- }
3343
- });
3344
-
3345
- /*
3346
- * Based on implementation by Neil Bartlett
3347
- * https://github.com/neilbartlett/color-temperature
3348
- */
3349
-
3350
- const { log } = Math;
3351
-
3352
- const temperature2rgb = (kelvin) => {
3353
- const temp = kelvin / 100;
3354
- let r, g, b;
3355
- if (temp < 66) {
3356
- r = 255;
3357
- g =
3358
- temp < 6
3359
- ? 0
3360
- : -155.25485562709179 -
3361
- 0.44596950469579133 * (g = temp - 2) +
3362
- 104.49216199393888 * log(g);
3363
- b =
3364
- temp < 20
3365
- ? 0
3366
- : -254.76935184120902 +
3367
- 0.8274096064007395 * (b = temp - 10) +
3368
- 115.67994401066147 * log(b);
3369
- } else {
3370
- r =
3371
- 351.97690566805693 +
3372
- 0.114206453784165 * (r = temp - 55) -
3373
- 40.25366309332127 * log(r);
3374
- g =
3375
- 325.4494125711974 +
3376
- 0.07943456536662342 * (g = temp - 50) -
3377
- 28.0852963507957 * log(g);
3378
- b = 255;
3379
- }
3380
- return [r, g, b, 1];
3381
- };
3382
-
3383
- /*
3384
- * Based on implementation by Neil Bartlett
3385
- * https://github.com/neilbartlett/color-temperature
3386
- **/
3387
-
3388
- const { round } = Math;
3389
-
3390
- const rgb2temperature = (...args) => {
3391
- const rgb = unpack(args, 'rgb');
3392
- const r = rgb[0],
3393
- b = rgb[2];
3394
- let minTemp = 1000;
3395
- let maxTemp = 40000;
3396
- const eps = 0.4;
3397
- let temp;
3398
- while (maxTemp - minTemp > eps) {
3399
- temp = (maxTemp + minTemp) * 0.5;
3400
- const rgb = temperature2rgb(temp);
3401
- if (rgb[2] / rgb[0] >= b / r) {
3402
- maxTemp = temp;
3403
- } else {
3404
- minTemp = temp;
3405
- }
3406
- }
3407
- return round(temp);
3408
- };
3409
-
3410
- Color.prototype.temp =
3411
- Color.prototype.kelvin =
3412
- Color.prototype.temperature =
3413
- function () {
3414
- return rgb2temperature(this._rgb);
3415
- };
3416
-
3417
- const temp = (...args) => new Color(...args, 'temp');
3418
- Object.assign(chroma, { temp, kelvin: temp, temperature: temp });
3419
-
3420
- input.format.temp =
3421
- input.format.kelvin =
3422
- input.format.temperature =
3423
- temperature2rgb;
3424
-
3425
- Color.prototype.oklch = function () {
3426
- return rgb2oklch(this._rgb);
3427
- };
3428
-
3429
- const oklch = (...args) => new Color(...args, 'oklch');
3430
- Object.assign(chroma, { oklch });
3431
-
3432
- input.format.oklch = oklch2rgb;
3433
-
3434
- input.autodetect.push({
3435
- p: 2,
3436
- test: (...args) => {
3437
- args = unpack(args, 'oklch');
3438
- if (type(args) === 'array' && args.length === 3) {
3439
- return 'oklch';
3440
- }
3441
- }
3442
- });
3443
-
3444
- // feel free to comment out anything to rollup
3445
- // a smaller chroma.js bundle
3446
-
3447
- Object.assign(chroma, {
3448
- analyze,
3449
- average,
3450
- bezier: bezier$1,
3451
- blend,
3452
- brewer: colorbrewerProxy,
3453
- Color,
3454
- colors: w3cx11,
3455
- contrast,
3456
- contrastAPCA,
3457
- cubehelix,
3458
- deltaE,
3459
- distance,
3460
- input,
3461
- interpolate: mix,
3462
- limits,
3463
- mix,
3464
- random: random$1,
3465
- scale,
3466
- scales,
3467
- valid
3468
- });
3469
-
3470
- const LIGHTNESS_MAP = [0.96, 0.907, 0.805, 0.697, 0.605, 0.547, 0.518, 0.445, 0.395, 0.34];
3471
- const SATURATION_MAP = [0.32, 0.16, 0.08, 0.04, 0, 0, 0.04, 0.08, 0.16, 0.32];
3472
- function getClosestLightness(colorObject) {
3473
- const lightnessGoal = colorObject.get("hsl.l");
3474
- return LIGHTNESS_MAP.reduce(
3475
- (prev, curr) => Math.abs(curr - lightnessGoal) < Math.abs(prev - lightnessGoal) ? curr : prev
3476
- );
3477
- }
3478
- function generateColorsMap(color) {
3479
- const colorObject = chroma(color);
3480
- const closestLightness = getClosestLightness(colorObject);
3481
- const baseColorIndex = LIGHTNESS_MAP.findIndex((l) => l === closestLightness);
3482
- const colors = LIGHTNESS_MAP.map((l) => colorObject.set("hsl.l", l)).map((c) => chroma(c)).map((c, i) => {
3483
- const saturationDelta = SATURATION_MAP[i] - SATURATION_MAP[baseColorIndex];
3484
- return saturationDelta >= 0 ? c.saturate(saturationDelta) : c.desaturate(saturationDelta * -1);
3485
- });
3486
- colors[baseColorIndex] = chroma(color);
3487
- return { baseColorIndex, colors };
3488
- }
3489
- function generateColors(color) {
3490
- return generateColorsMap(color).colors.map((c) => c.hex());
3491
- }
3492
-
3493
6
  const blue$1 = {
3494
7
  fill: "#3b82f6",
3495
8
  stroke: "#2563eb",
@@ -3516,30 +29,15 @@ const ElementColors = {
3516
29
  muted: slate$1,
3517
30
  slate: slate$1,
3518
31
  gray: {
3519
- // fill: colors.neutral[500],
3520
- // stroke: colors.neutral[600],
3521
- // hiContrast: colors.neutral[50],
3522
- // loContrast: colors.neutral[200],
3523
32
  fill: "#737373",
3524
33
  stroke: "#525252",
3525
34
  hiContrast: "#fafafa",
3526
35
  loContrast: "#d4d4d4"
3527
36
  },
3528
37
  red: {
3529
- // fill: colors.red[500],
3530
- // stroke: colors.red[600],
3531
- // hiContrast: colors.red[50],
3532
- // loContrast: colors.red[200],
3533
38
  fill: "#AC4D39",
3534
- // fill: '#b54548',
3535
39
  stroke: "#853A2D",
3536
- // hiContrast: '#fef2f2',
3537
- // loContrast: '#fecaca',
3538
- // hiContrast: '#191111', // colors.gray[900],
3539
- // loContrast: '#3b1219' // colors.gray[800],
3540
40
  hiContrast: "#FBD3CB",
3541
- // hiContrast: '#f8fafc',
3542
- // loContrast: '#fdd8d8' // radix black red 12
3543
41
  loContrast: "#f5b2a3"
3544
42
  },
3545
43
  green: {
@@ -3555,10 +53,6 @@ const ElementColors = {
3555
53
  loContrast: "#f9b27c"
3556
54
  },
3557
55
  indigo: {
3558
- // fill: colors.indigo[500],
3559
- // stroke: colors.indigo[600],
3560
- // hiContrast: colors.indigo[50],
3561
- // loContrast: colors.indigo[200],
3562
56
  fill: "#6366f1",
3563
57
  stroke: "#4f46e5",
3564
58
  hiContrast: "#eef2ff",