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