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