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