@udixio/theme 0.4.1 → 0.5.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 (91) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +11 -0
  3. package/index.cjs +3070 -0
  4. package/index.js +3070 -0
  5. package/package.json +11 -5
  6. package/src/{lib/API.d.ts → API.d.ts} +1 -0
  7. package/src/API.d.ts.map +1 -0
  8. package/src/{lib/app.container.d.ts → app.container.d.ts} +1 -0
  9. package/src/app.container.d.ts.map +1 -0
  10. package/src/{lib/app.module.d.ts → app.module.d.ts} +1 -0
  11. package/src/app.module.d.ts.map +1 -0
  12. package/src/{lib/color → color}/color.api.d.ts +1 -0
  13. package/src/color/color.api.d.ts.map +1 -0
  14. package/src/{lib/color → color}/color.manager.d.ts +1 -0
  15. package/src/color/color.manager.d.ts.map +1 -0
  16. package/src/color/color.module.d.ts +3 -0
  17. package/src/color/color.module.d.ts.map +1 -0
  18. package/src/{lib/color → color}/color.utils.d.ts +1 -0
  19. package/src/color/color.utils.d.ts.map +1 -0
  20. package/src/{lib/color → color}/configurable-color.d.ts +1 -0
  21. package/src/color/configurable-color.d.ts.map +1 -0
  22. package/src/{lib/color → color}/default-color.d.ts +1 -0
  23. package/src/color/default-color.d.ts.map +1 -0
  24. package/src/{lib/color → color}/index.d.ts +1 -0
  25. package/src/color/index.d.ts.map +1 -0
  26. package/src/{lib/config → config}/config.interface.d.ts +1 -0
  27. package/src/config/config.interface.d.ts.map +1 -0
  28. package/src/{lib/config → config}/config.module.d.ts +1 -0
  29. package/src/config/config.module.d.ts.map +1 -0
  30. package/src/{lib/config → config}/config.service.d.ts +1 -0
  31. package/src/config/config.service.d.ts.map +1 -0
  32. package/src/{lib/config → config}/index.d.ts +1 -0
  33. package/src/config/index.d.ts.map +1 -0
  34. package/src/index.d.ts +12 -1
  35. package/src/index.d.ts.map +1 -0
  36. package/src/{lib/main.d.ts → main.d.ts} +1 -0
  37. package/src/main.d.ts.map +1 -0
  38. package/src/{lib/material-color-utilities → material-color-utilities}/contrastCurve.d.ts +1 -0
  39. package/src/material-color-utilities/contrastCurve.d.ts.map +1 -0
  40. package/src/{lib/material-color-utilities → material-color-utilities}/dynamic_color.d.ts +1 -16
  41. package/src/material-color-utilities/dynamic_color.d.ts.map +1 -0
  42. package/src/{lib/material-color-utilities → material-color-utilities}/hct_solver.d.ts +1 -16
  43. package/src/material-color-utilities/hct_solver.d.ts.map +1 -0
  44. package/src/{lib/material-color-utilities → material-color-utilities}/htc.d.ts +1 -31
  45. package/src/material-color-utilities/htc.d.ts.map +1 -0
  46. package/src/{lib/material-color-utilities → material-color-utilities}/index.d.ts +1 -0
  47. package/src/material-color-utilities/index.d.ts.map +1 -0
  48. package/src/{lib/material-color-utilities → material-color-utilities}/toneDeltaPair.d.ts +2 -17
  49. package/src/material-color-utilities/toneDeltaPair.d.ts.map +1 -0
  50. package/src/{lib/plugin → plugin}/index.d.ts +1 -0
  51. package/src/plugin/index.d.ts.map +1 -0
  52. package/src/{lib/plugin → plugin}/plugin.module.d.ts +1 -0
  53. package/src/plugin/plugin.module.d.ts.map +1 -0
  54. package/src/{lib/plugin → plugin}/pluginAbstract.d.ts +1 -0
  55. package/src/plugin/pluginAbstract.d.ts.map +1 -0
  56. package/src/{lib/plugin → plugin}/pluginApi.d.ts +1 -0
  57. package/src/plugin/pluginApi.d.ts.map +1 -0
  58. package/src/{lib/plugins → plugins}/font/font.plugin.d.ts +1 -0
  59. package/src/plugins/font/font.plugin.d.ts.map +1 -0
  60. package/src/plugins/font/index.d.ts +2 -0
  61. package/src/plugins/font/index.d.ts.map +1 -0
  62. package/src/{lib/theme → theme}/index.d.ts +1 -0
  63. package/src/theme/index.d.ts.map +1 -0
  64. package/src/{lib/theme → theme}/scheme.d.ts +1 -0
  65. package/src/theme/scheme.d.ts.map +1 -0
  66. package/src/{lib/theme → theme}/scheme.manager.d.ts +1 -0
  67. package/src/theme/scheme.manager.d.ts.map +1 -0
  68. package/src/{lib/theme → theme}/theme.api.d.ts +1 -0
  69. package/src/theme/theme.api.d.ts.map +1 -0
  70. package/src/{lib/theme → theme}/theme.module.d.ts +1 -0
  71. package/src/theme/theme.module.d.ts.map +1 -0
  72. package/src/{lib/theme → theme}/variant.d.ts +1 -0
  73. package/src/theme/variant.d.ts.map +1 -0
  74. package/src/{lib/theme → theme}/variant.manager.d.ts +1 -0
  75. package/src/theme/variant.manager.d.ts.map +1 -0
  76. package/src/{lib/theme → theme}/variants/expressive.variant.d.ts +1 -0
  77. package/src/theme/variants/expressive.variant.d.ts.map +1 -0
  78. package/src/{lib/theme → theme}/variants/index.d.ts +5 -4
  79. package/src/theme/variants/index.d.ts.map +1 -0
  80. package/src/{lib/theme → theme}/variants/neutral.variant.d.ts +1 -0
  81. package/src/theme/variants/neutral.variant.d.ts.map +1 -0
  82. package/src/{lib/theme → theme}/variants/tonal-spot.variant.d.ts +1 -0
  83. package/src/theme/variants/tonal-spot.variant.d.ts.map +1 -0
  84. package/src/{lib/theme → theme}/variants/vibrant.variant.d.ts +1 -0
  85. package/src/theme/variants/vibrant.variant.d.ts.map +1 -0
  86. package/index.cjs.js +0 -2880
  87. package/index.d.ts +0 -1
  88. package/index.esm.js +0 -2842
  89. package/src/lib/color/color.module.d.ts +0 -2
  90. package/src/lib/index.d.ts +0 -11
  91. package/src/lib/plugins/font/index.d.ts +0 -1
package/index.cjs.js DELETED
@@ -1,2880 +0,0 @@
1
- 'use strict';
2
-
3
- var awilix = require('awilix');
4
- var materialColorUtilities = require('@material/material-color-utilities');
5
-
6
- class ColorApi {
7
- constructor({ colorManager }) {
8
- this.colorManager = colorManager;
9
- }
10
- getColors() {
11
- return this.colorManager.getAll();
12
- }
13
- addColor(key, color) {
14
- return this.colorManager.createOrUpdate(key, color);
15
- }
16
- addColors(args) {
17
- if (!Array.isArray(args))
18
- args = [args];
19
- args.forEach((args) => {
20
- if (typeof args === 'function') {
21
- args = args(this);
22
- }
23
- if (args.fromPalettes) {
24
- if (!Array.isArray(args.fromPalettes))
25
- args.fromPalettes = [args.fromPalettes];
26
- args.fromPalettes.map((paletteKey) => {
27
- this.colorManager.addFromPalette(paletteKey);
28
- });
29
- }
30
- if (args.colors) {
31
- Object.keys(args.colors).map((key) => this.addColor(key, args.colors[key]));
32
- }
33
- });
34
- }
35
- getColor(key) {
36
- return this.colorManager.get(key);
37
- }
38
- removeColor(key) {
39
- return this.colorManager.remove(key);
40
- }
41
- updateColor(key, newColor) {
42
- return this.colorManager.createOrUpdate(key, newColor);
43
- }
44
- }
45
-
46
- /**
47
- * @license
48
- * Copyright 2023 Google LLC
49
- *
50
- * Licensed under the Apache License, Version 2.0 (the "License");
51
- * you may not use this file except in compliance with the License.
52
- * You may obtain a copy of the License at
53
- *
54
- * http://www.apache.org/licenses/LICENSE-2.0
55
- *
56
- * Unless required by applicable law or agreed to in writing, software
57
- * distributed under the License is distributed on an "AS IS" BASIS,
58
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
59
- * See the License for the specific language governing permissions and
60
- * limitations under the License.
61
- */
62
- /**
63
- * A class containing a value that changes with the contrast level.
64
- *
65
- * Usually represents the contrast requirements for a dynamic color on its
66
- * background. The four values correspond to values for contrast levels -1.0,
67
- * 0.0, 0.5, and 1.0, respectively.
68
- */
69
- class ContrastCurve {
70
- /**
71
- * Creates a `ContrastCurve` object.
72
- *
73
- * @param low Value for contrast level -1.0
74
- * @param normal Value for contrast level 0.0
75
- * @param medium Value for contrast level 0.5
76
- * @param high Value for contrast level 1.0
77
- */
78
- constructor(low, normal, medium, high) {
79
- this.low = low;
80
- this.normal = normal;
81
- this.medium = medium;
82
- this.high = high;
83
- }
84
- /**
85
- * Returns the value at a given contrast level.
86
- *
87
- * @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0
88
- * is the lowest; 1.0 is the highest.
89
- * @return The value. For contrast ratios, a number between 1.0 and 21.0.
90
- */
91
- get(contrastLevel) {
92
- if (contrastLevel <= -1) {
93
- return this.low;
94
- }
95
- else if (contrastLevel < 0.0) {
96
- return materialColorUtilities.lerp(this.low, this.normal, (contrastLevel - -1) / 1);
97
- }
98
- else if (contrastLevel < 0.5) {
99
- return materialColorUtilities.lerp(this.normal, this.medium, (contrastLevel - 0) / 0.5);
100
- }
101
- else if (contrastLevel < 1.0) {
102
- return materialColorUtilities.lerp(this.medium, this.high, (contrastLevel - 0.5) / 0.5);
103
- }
104
- else {
105
- return this.high;
106
- }
107
- }
108
- }
109
-
110
- /**
111
- * @license
112
- * Copyright 2021 Google LLC
113
- *
114
- * Licensed under the Apache License, Version 2.0 (the "License");
115
- * you may not use this file except in compliance with the License.
116
- * You may obtain a copy of the License at
117
- *
118
- * http://www.apache.org/licenses/LICENSE-2.0
119
- *
120
- * Unless required by applicable law or agreed to in writing, software
121
- * distributed under the License is distributed on an "AS IS" BASIS,
122
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123
- * See the License for the specific language governing permissions and
124
- * limitations under the License.
125
- */
126
- // This file is automatically generated. Do not modify it.
127
- // material_color_utilities is designed to have a consistent API across
128
- // platforms and modular components that can be moved around easily. Using a
129
- // class as a namespace facilitates this.
130
- //
131
- // tslint:disable:class-as-namespace
132
- /**
133
- * A class that solves the HCT equation.
134
- */
135
- class HctSolver {
136
- /**
137
- * Finds an sRGB color with the given hue, chroma, and L*, if
138
- * possible.
139
- *
140
- * @param hueDegrees The desired hue, in degrees.
141
- * @param chroma The desired chroma.
142
- * @param lstar The desired L*.
143
- * @return A hexadecimal representing the sRGB color. The color
144
- * has sufficiently close hue, chroma, and L* to the desired
145
- * values, if possible; otherwise, the hue and L* will be
146
- * sufficiently close, and chroma will be maximized.
147
- */
148
- static solveToInt(hueDegrees, chroma, lstar) {
149
- if (chroma < 0.0001 || lstar < 0.0001 || lstar > 99.9999) {
150
- return materialColorUtilities.argbFromLstar(lstar);
151
- }
152
- hueDegrees = materialColorUtilities.sanitizeDegreesDouble(hueDegrees);
153
- const hueRadians = (hueDegrees / 180) * Math.PI;
154
- const y = materialColorUtilities.yFromLstar(lstar);
155
- const exactAnswer = HctSolver.findResultByJ(hueRadians, chroma, y);
156
- if (exactAnswer !== 0) {
157
- return exactAnswer;
158
- }
159
- const linrgb = HctSolver.bisectToLimit(y, hueRadians);
160
- return materialColorUtilities.argbFromLinrgb(linrgb);
161
- }
162
- /**
163
- * Finds an sRGB color with the given hue, chroma, and L*, if
164
- * possible.
165
- *
166
- * @param hueDegrees The desired hue, in degrees.
167
- * @param chroma The desired chroma.
168
- * @param lstar The desired L*.
169
- * @return An CAM16 object representing the sRGB color. The color
170
- * has sufficiently close hue, chroma, and L* to the desired
171
- * values, if possible; otherwise, the hue and L* will be
172
- * sufficiently close, and chroma will be maximized.
173
- */
174
- static solveToCam(hueDegrees, chroma, lstar) {
175
- return materialColorUtilities.Cam16.fromInt(HctSolver.solveToInt(hueDegrees, chroma, lstar));
176
- }
177
- /**
178
- * Sanitizes a small enough angle in radians.
179
- *
180
- * @param angle An angle in radians; must not deviate too much
181
- * from 0.
182
- * @return A coterminal angle between 0 and 2pi.
183
- */
184
- static sanitizeRadians(angle) {
185
- return (angle + Math.PI * 8) % (Math.PI * 2);
186
- }
187
- /**
188
- * Delinearizes an RGB component, returning a floating-point
189
- * number.
190
- *
191
- * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
192
- * linear R/G/B channel
193
- * @return 0.0 <= output <= 255.0, color channel converted to
194
- * regular RGB space
195
- */
196
- static trueDelinearized(rgbComponent) {
197
- const normalized = rgbComponent / 100.0;
198
- let delinearized = 0.0;
199
- if (normalized <= 0.0031308) {
200
- delinearized = normalized * 12.92;
201
- }
202
- else {
203
- delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
204
- }
205
- return delinearized * 255.0;
206
- }
207
- static chromaticAdaptation(component) {
208
- const af = Math.pow(Math.abs(component), 0.42);
209
- return (materialColorUtilities.signum(component) * 400.0 * af) / (af + 27.13);
210
- }
211
- /**
212
- * Returns the hue of a linear RGB color in CAM16.
213
- *
214
- * @param linrgb The linear RGB coordinates of a color.
215
- * @return The hue of the color in CAM16, in radians.
216
- */
217
- static hueOf(linrgb) {
218
- const scaledDiscount = materialColorUtilities.matrixMultiply(linrgb, HctSolver.SCALED_DISCOUNT_FROM_LINRGB);
219
- const rA = HctSolver.chromaticAdaptation(scaledDiscount[0]);
220
- const gA = HctSolver.chromaticAdaptation(scaledDiscount[1]);
221
- const bA = HctSolver.chromaticAdaptation(scaledDiscount[2]);
222
- // redness-greenness
223
- const a = (11.0 * rA + -12 * gA + bA) / 11.0;
224
- // yellowness-blueness
225
- const b = (rA + gA - 2.0 * bA) / 9.0;
226
- return Math.atan2(b, a);
227
- }
228
- static areInCyclicOrder(a, b, c) {
229
- const deltaAB = HctSolver.sanitizeRadians(b - a);
230
- const deltaAC = HctSolver.sanitizeRadians(c - a);
231
- return deltaAB < deltaAC;
232
- }
233
- /**
234
- * Solves the lerp equation.
235
- *
236
- * @param source The starting number.
237
- * @param mid The number in the middle.
238
- * @param target The ending number.
239
- * @return A number t such that lerp(source, target, t) = mid.
240
- */
241
- static intercept(source, mid, target) {
242
- return (mid - source) / (target - source);
243
- }
244
- static lerpPoint(source, t, target) {
245
- return [
246
- source[0] + (target[0] - source[0]) * t,
247
- source[1] + (target[1] - source[1]) * t,
248
- source[2] + (target[2] - source[2]) * t,
249
- ];
250
- }
251
- /**
252
- * Intersects a segment with a plane.
253
- *
254
- * @param source The coordinates of point A.
255
- * @param coordinate The R-, G-, or B-coordinate of the plane.
256
- * @param target The coordinates of point B.
257
- * @param axis The axis the plane is perpendicular with. (0: R, 1:
258
- * G, 2: B)
259
- * @return The intersection point of the segment AB with the plane
260
- * R=coordinate, G=coordinate, or B=coordinate
261
- */
262
- static setCoordinate(source, coordinate, target, axis) {
263
- const t = HctSolver.intercept(source[axis], coordinate, target[axis]);
264
- return HctSolver.lerpPoint(source, t, target);
265
- }
266
- static isBounded(x) {
267
- return 0.0 <= x && x <= 100.0;
268
- }
269
- /**
270
- * Returns the nth possible vertex of the polygonal intersection.
271
- *
272
- * @param y The Y value of the plane.
273
- * @param n The zero-based index of the point. 0 <= n <= 11.
274
- * @return The nth possible vertex of the polygonal intersection
275
- * of the y plane and the RGB cube, in linear RGB coordinates, if
276
- * it exists. If this possible vertex lies outside of the cube,
277
- * [-1.0, -1.0, -1.0] is returned.
278
- */
279
- static nthVertex(y, n) {
280
- const kR = HctSolver.Y_FROM_LINRGB[0];
281
- const kG = HctSolver.Y_FROM_LINRGB[1];
282
- const kB = HctSolver.Y_FROM_LINRGB[2];
283
- const coordA = n % 4 <= 1 ? 0.0 : 100.0;
284
- const coordB = n % 2 === 0 ? 0.0 : 100.0;
285
- if (n < 4) {
286
- const g = coordA;
287
- const b = coordB;
288
- const r = (y - g * kG - b * kB) / kR;
289
- if (HctSolver.isBounded(r)) {
290
- return [r, g, b];
291
- }
292
- else {
293
- return [-1, -1, -1];
294
- }
295
- }
296
- else if (n < 8) {
297
- const b = coordA;
298
- const r = coordB;
299
- const g = (y - r * kR - b * kB) / kG;
300
- if (HctSolver.isBounded(g)) {
301
- return [r, g, b];
302
- }
303
- else {
304
- return [-1, -1, -1];
305
- }
306
- }
307
- else {
308
- const r = coordA;
309
- const g = coordB;
310
- const b = (y - r * kR - g * kG) / kB;
311
- if (HctSolver.isBounded(b)) {
312
- return [r, g, b];
313
- }
314
- else {
315
- return [-1, -1, -1];
316
- }
317
- }
318
- }
319
- /**
320
- * Finds the segment containing the desired color.
321
- *
322
- * @param y The Y value of the color.
323
- * @param targetHue The hue of the color.
324
- * @return A list of two sets of linear RGB coordinates, each
325
- * corresponding to an endpoint of the segment containing the
326
- * desired color.
327
- */
328
- static bisectToSegment(y, targetHue) {
329
- let left = [-1, -1, -1];
330
- let right = left;
331
- let leftHue = 0.0;
332
- let rightHue = 0.0;
333
- let initialized = false;
334
- let uncut = true;
335
- for (let n = 0; n < 12; n++) {
336
- const mid = HctSolver.nthVertex(y, n);
337
- if (mid[0] < 0) {
338
- continue;
339
- }
340
- const midHue = HctSolver.hueOf(mid);
341
- if (!initialized) {
342
- left = mid;
343
- right = mid;
344
- leftHue = midHue;
345
- rightHue = midHue;
346
- initialized = true;
347
- continue;
348
- }
349
- if (uncut || HctSolver.areInCyclicOrder(leftHue, midHue, rightHue)) {
350
- uncut = false;
351
- if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
352
- right = mid;
353
- rightHue = midHue;
354
- }
355
- else {
356
- left = mid;
357
- leftHue = midHue;
358
- }
359
- }
360
- }
361
- return [left, right];
362
- }
363
- static midpoint(a, b) {
364
- return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2];
365
- }
366
- static criticalPlaneBelow(x) {
367
- return Math.floor(x - 0.5);
368
- }
369
- static criticalPlaneAbove(x) {
370
- return Math.ceil(x - 0.5);
371
- }
372
- /**
373
- * Finds a color with the given Y and hue on the boundary of the
374
- * cube.
375
- *
376
- * @param y The Y value of the color.
377
- * @param targetHue The hue of the color.
378
- * @return The desired color, in linear RGB coordinates.
379
- */
380
- static bisectToLimit(y, targetHue) {
381
- const segment = HctSolver.bisectToSegment(y, targetHue);
382
- let left = segment[0];
383
- let leftHue = HctSolver.hueOf(left);
384
- let right = segment[1];
385
- for (let axis = 0; axis < 3; axis++) {
386
- if (left[axis] !== right[axis]) {
387
- let lPlane = -1;
388
- let rPlane = 255;
389
- if (left[axis] < right[axis]) {
390
- lPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(left[axis]));
391
- rPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(right[axis]));
392
- }
393
- else {
394
- lPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(left[axis]));
395
- rPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(right[axis]));
396
- }
397
- for (let i = 0; i < 8; i++) {
398
- if (Math.abs(rPlane - lPlane) <= 1) {
399
- break;
400
- }
401
- else {
402
- const mPlane = Math.floor((lPlane + rPlane) / 2.0);
403
- const midPlaneCoordinate = HctSolver.CRITICAL_PLANES[mPlane];
404
- const mid = HctSolver.setCoordinate(left, midPlaneCoordinate, right, axis);
405
- const midHue = HctSolver.hueOf(mid);
406
- if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
407
- right = mid;
408
- rPlane = mPlane;
409
- }
410
- else {
411
- left = mid;
412
- leftHue = midHue;
413
- lPlane = mPlane;
414
- }
415
- }
416
- }
417
- }
418
- }
419
- return HctSolver.midpoint(left, right);
420
- }
421
- static inverseChromaticAdaptation(adapted) {
422
- const adaptedAbs = Math.abs(adapted);
423
- const base = Math.max(0, (27.13 * adaptedAbs) / (400.0 - adaptedAbs));
424
- return materialColorUtilities.signum(adapted) * Math.pow(base, 1.0 / 0.42);
425
- }
426
- /**
427
- * Finds a color with the given hue, chroma, and Y.
428
- *
429
- * @param hueRadians The desired hue in radians.
430
- * @param chroma The desired chroma.
431
- * @param y The desired Y.
432
- * @return The desired color as a hexadecimal integer, if found; 0
433
- * otherwise.
434
- */
435
- static findResultByJ(hueRadians, chroma, y) {
436
- // Initial estimate of j.
437
- let j = Math.sqrt(y) * 11.0;
438
- // ===========================================================
439
- // Operations inlined from Cam16 to avoid repeated calculation
440
- // ===========================================================
441
- const viewingConditions = materialColorUtilities.ViewingConditions.DEFAULT;
442
- const tInnerCoeff = 1 / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
443
- const eHue = 0.25 * (Math.cos(hueRadians + 2.0) + 3.8);
444
- const p1 = eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
445
- const hSin = Math.sin(hueRadians);
446
- const hCos = Math.cos(hueRadians);
447
- for (let iterationRound = 0; iterationRound < 5; iterationRound++) {
448
- // ===========================================================
449
- // Operations inlined from Cam16 to avoid repeated calculation
450
- // ===========================================================
451
- const jNormalized = j / 100.0;
452
- const alpha = chroma === 0.0 || j === 0.0 ? 0.0 : chroma / Math.sqrt(jNormalized);
453
- const t = Math.pow(alpha * tInnerCoeff, 1.0 / 0.9);
454
- const ac = viewingConditions.aw *
455
- Math.pow(jNormalized, 1.0 / viewingConditions.c / viewingConditions.z);
456
- const p2 = ac / viewingConditions.nbb;
457
- const gamma = (23.0 * (p2 + 0.305) * t) /
458
- (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
459
- const a = gamma * hCos;
460
- const b = gamma * hSin;
461
- const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
462
- const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
463
- const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
464
- const rCScaled = HctSolver.inverseChromaticAdaptation(rA);
465
- const gCScaled = HctSolver.inverseChromaticAdaptation(gA);
466
- const bCScaled = HctSolver.inverseChromaticAdaptation(bA);
467
- const linrgb = materialColorUtilities.matrixMultiply([rCScaled, gCScaled, bCScaled], HctSolver.LINRGB_FROM_SCALED_DISCOUNT);
468
- // ===========================================================
469
- // Operations inlined from Cam16 to avoid repeated calculation
470
- // ===========================================================
471
- if (linrgb[0] < 0 || linrgb[1] < 0 || linrgb[2] < 0) {
472
- return 0;
473
- }
474
- const kR = HctSolver.Y_FROM_LINRGB[0];
475
- const kG = HctSolver.Y_FROM_LINRGB[1];
476
- const kB = HctSolver.Y_FROM_LINRGB[2];
477
- const fnj = kR * linrgb[0] + kG * linrgb[1] + kB * linrgb[2];
478
- if (fnj <= 0) {
479
- return 0;
480
- }
481
- if (iterationRound === 4 || Math.abs(fnj - y) < 0.002) {
482
- if (linrgb[0] > 100.01 || linrgb[1] > 100.01 || linrgb[2] > 100.01) {
483
- return 0;
484
- }
485
- return materialColorUtilities.argbFromLinrgb(linrgb);
486
- }
487
- // Iterates with Newton method,
488
- // Using 2 * fn(j) / j as the approximation of fn'(j)
489
- j = j - ((fnj - y) * j) / (2 * fnj);
490
- }
491
- return 0;
492
- }
493
- }
494
- HctSolver.SCALED_DISCOUNT_FROM_LINRGB = [
495
- [0.001200833568784504, 0.002389694492170889, 0.0002795742885861124],
496
- [0.0005891086651375999, 0.0029785502573438758, 0.0003270666104008398],
497
- [0.00010146692491640572, 0.0005364214359186694, 0.0032979401770712076],
498
- ];
499
- HctSolver.LINRGB_FROM_SCALED_DISCOUNT = [
500
- [1373.2198709594231, -1100.4251190754821, -7.278681089101213],
501
- [-271.815969077903, 559.6580465940733, -32.46047482791194],
502
- [1.9622899599665666, -57.173814538844006, 308.7233197812385],
503
- ];
504
- HctSolver.Y_FROM_LINRGB = [0.2126, 0.7152, 0.0722];
505
- HctSolver.CRITICAL_PLANES = [
506
- 0.015176349177441876, 0.045529047532325624, 0.07588174588720938,
507
- 0.10623444424209313, 0.13658714259697685, 0.16693984095186062,
508
- 0.19729253930674434, 0.2276452376616281, 0.2579979360165119,
509
- 0.28835063437139563, 0.3188300904430532, 0.350925934958123,
510
- 0.3848314933096426, 0.42057480301049466, 0.458183274052838,
511
- 0.4976837250274023, 0.5391024159806381, 0.5824650784040898,
512
- 0.6277969426914107, 0.6751227633498623, 0.7244668422128921,
513
- 0.775853049866786, 0.829304845476233, 0.8848452951698498, 0.942497089126609,
514
- 1.0022825574869039, 1.0642236851973577, 1.1283421258858297,
515
- 1.1946592148522128, 1.2631959812511864, 1.3339731595349034,
516
- 1.407011200216447, 1.4823302800086415, 1.5599503113873272,
517
- 1.6398909516233677, 1.7221716113234105, 1.8068114625156377,
518
- 1.8938294463134073, 1.9832442801866852, 2.075074464868551,
519
- 2.1693382909216234, 2.2660538449872063, 2.36523901573795,
520
- 2.4669114995532007, 2.5710888059345764, 2.6777882626779785,
521
- 2.7870270208169257, 2.898822059350997, 3.0131901897720907,
522
- 3.1301480604002863, 3.2497121605402226, 3.3718988244681087,
523
- 3.4967242352587946, 3.624204428461639, 3.754355295633311, 3.887192587735158,
524
- 4.022731918402185, 4.160988767090289, 4.301978482107941, 4.445716283538092,
525
- 4.592217266055746, 4.741496401646282, 4.893568542229298, 5.048448422192488,
526
- 5.20615066083972, 5.3666897647573375, 5.5300801301023865, 5.696336044816294,
527
- 5.865471690767354, 6.037501145825082, 6.212438385869475, 6.390297286737924,
528
- 6.571091626112461, 6.7548350853498045, 6.941541251256611, 7.131223617812143,
529
- 7.323895587840543, 7.5195704746346665, 7.7182615035334345,
530
- 7.919981813454504, 8.124744458384042, 8.332562408825165, 8.543448553206703,
531
- 8.757415699253682, 8.974476575321063, 9.194643831691977, 9.417930041841839,
532
- 9.644347703669503, 9.873909240696694, 10.106627003236781,
533
- 10.342513269534024, 10.58158024687427, 10.8238400726681, 11.069304815507364,
534
- 11.317986476196008, 11.569896988756009, 11.825048221409341,
535
- 12.083451977536606, 12.345119996613247, 12.610063955123938,
536
- 12.878295467455942, 13.149826086772048, 13.42466730586372,
537
- 13.702830557985108, 13.984327217668513, 14.269168601521828,
538
- 14.55736596900856, 14.848930523210871, 15.143873411576273,
539
- 15.44220572664832, 15.743938506781891, 16.04908273684337, 16.35764934889634,
540
- 16.66964922287304, 16.985093187232053, 17.30399201960269, 17.62635644741625,
541
- 17.95219714852476, 18.281524751807332, 18.614349837764564,
542
- 18.95068293910138, 19.290534541298456, 19.633915083172692,
543
- 19.98083495742689, 20.331304511189067, 20.685334046541502,
544
- 21.042933821039977, 21.404114048223256, 21.76888489811322,
545
- 22.137256497705877, 22.50923893145328, 22.884842241736916,
546
- 23.264076429332462, 23.6469514538663, 24.033477234264016, 24.42366364919083,
547
- 24.817520537484558, 25.21505769858089, 25.61628489293138,
548
- 26.021211842414342, 26.429848230738664, 26.842203703840827,
549
- 27.258287870275353, 27.678110301598522, 28.10168053274597,
550
- 28.529008062403893, 28.96010235337422, 29.39497283293396, 29.83362889318845,
551
- 30.276079891419332, 30.722335150426627, 31.172403958865512,
552
- 31.62629557157785, 32.08401920991837, 32.54558406207592, 33.010999283389665,
553
- 33.4802739966603, 33.953417292456834, 34.430438229418264,
554
- 34.911345834551085, 35.39614910352207, 35.88485700094671, 36.37747846067349,
555
- 36.87402238606382, 37.37449765026789, 37.87891309649659, 38.38727753828926,
556
- 38.89959975977785, 39.41588851594697, 39.93615253289054, 40.460400508064545,
557
- 40.98864111053629, 41.520882981230194, 42.05713473317016,
558
- 42.597404951718396, 43.141702194811224, 43.6900349931913, 44.24241185063697,
559
- 44.798841244188324, 45.35933162437017, 45.92389141541209, 46.49252901546552,
560
- 47.065252796817916, 47.64207110610409, 48.22299226451468,
561
- 48.808024568002054, 49.3971762874833, 49.9904556690408, 50.587870934119984,
562
- 51.189430279724725, 51.79514187861014, 52.40501387947288, 53.0190544071392,
563
- 53.637271562750364, 54.259673423945976, 54.88626804504493,
564
- 55.517063457223934, 56.15206766869424, 56.79128866487574, 57.43473440856916,
565
- 58.08241284012621, 58.734331877617365, 59.39049941699807, 60.05092333227251,
566
- 60.715611475655585, 61.38457167773311, 62.057811747619894, 62.7353394731159,
567
- 63.417162620860914, 64.10328893648692, 64.79372614476921, 65.48848194977529,
568
- 66.18756403501224, 66.89098006357258, 67.59873767827808, 68.31084450182222,
569
- 69.02730813691093, 69.74813616640164, 70.47333615344107, 71.20291564160104,
570
- 71.93688215501312, 72.67524319850172, 73.41800625771542, 74.16517879925733,
571
- 74.9167682708136, 75.67278210128072, 76.43322770089146, 77.1981124613393,
572
- 77.96744375590167, 78.74122893956174, 79.51947534912904, 80.30219030335869,
573
- 81.08938110306934, 81.88105503125999, 82.67721935322541, 83.4778813166706,
574
- 84.28304815182372, 85.09272707154808, 85.90692527145302, 86.72564993000343,
575
- 87.54890820862819, 88.3767072518277, 89.2090541872801, 90.04595612594655,
576
- 90.88742016217518, 91.73345337380438, 92.58406282226491, 93.43925555268066,
577
- 94.29903859396902, 95.16341895893969, 96.03240364439274, 96.9059996312159,
578
- 97.78421388448044, 98.6670533535366, 99.55452497210776,
579
- ];
580
-
581
- /**
582
- * @license
583
- * Copyright 2021 Google LLC
584
- *
585
- * Licensed under the Apache License, Version 2.0 (the "License");
586
- * you may not use this file except in compliance with the License.
587
- * You may obtain a copy of the License at
588
- *
589
- * http://www.apache.org/licenses/LICENSE-2.0
590
- *
591
- * Unless required by applicable law or agreed to in writing, software
592
- * distributed under the License is distributed on an "AS IS" BASIS,
593
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
594
- * See the License for the specific language governing permissions and
595
- * limitations under the License.
596
- */
597
- /**
598
- * A color system built using CAM16 hue and chroma, and L* from
599
- * L*a*b*.
600
- *
601
- * Using L* creates a link between the color system, contrast, and thus
602
- * accessibility. Contrast ratio depends on relative luminance, or Y in the XYZ
603
- * color space. L*, or perceptual luminance can be calculated from Y.
604
- *
605
- * Unlike Y, L* is linear to human perception, allowing trivial creation of
606
- * accurate color tones.
607
- *
608
- * Unlike contrast ratio, measuring contrast in L* is linear, and simple to
609
- * calculate. A difference of 40 in HCT tone guarantees a contrast ratio >= 3.0,
610
- * and a difference of 50 guarantees a contrast ratio >= 4.5.
611
- */
612
- /**
613
- * HCT, hue, chroma, and tone. A color system that provides a perceptually
614
- * accurate color measurement system that can also accurately render what colors
615
- * will appear as in different lighting environments.
616
- */
617
- class Hct {
618
- constructor(argb) {
619
- this.argb = argb;
620
- const cam = materialColorUtilities.Cam16.fromInt(argb);
621
- this.internalHue = cam.hue;
622
- this.internalChroma = cam.chroma;
623
- this.internalTone = materialColorUtilities.lstarFromArgb(argb);
624
- this.argb = argb;
625
- }
626
- /**
627
- * A number, in degrees, representing ex. red, orange, yellow, etc.
628
- * Ranges from 0 <= hue < 360.
629
- */
630
- get hue() {
631
- return this.internalHue;
632
- }
633
- /**
634
- * @param newHue 0 <= newHue < 360; invalid values are corrected.
635
- * Chroma may decrease because chroma has a different maximum for any given
636
- * hue and tone.
637
- */
638
- set hue(newHue) {
639
- this.setInternalState(HctSolver.solveToInt(newHue, this.internalChroma, this.internalTone));
640
- }
641
- get chroma() {
642
- return this.internalChroma;
643
- }
644
- /**
645
- * @param newChroma 0 <= newChroma < ?
646
- * Chroma may decrease because chroma has a different maximum for any given
647
- * hue and tone.
648
- */
649
- set chroma(newChroma) {
650
- this.setInternalState(HctSolver.solveToInt(this.internalHue, newChroma, this.internalTone));
651
- }
652
- /** Lightness. Ranges from 0 to 100. */
653
- get tone() {
654
- return this.internalTone;
655
- }
656
- /**
657
- * @param newTone 0 <= newTone <= 100; invalid valids are corrected.
658
- * Chroma may decrease because chroma has a different maximum for any given
659
- * hue and tone.
660
- */
661
- set tone(newTone) {
662
- this.setInternalState(HctSolver.solveToInt(this.internalHue, this.internalChroma, newTone));
663
- }
664
- static from(hue, chroma, tone) {
665
- return new Hct(HctSolver.solveToInt(hue, chroma, tone));
666
- }
667
- /**
668
- * @param argb ARGB representation of a color.
669
- * @return HCT representation of a color in default viewing conditions
670
- */
671
- static fromInt(argb) {
672
- return new Hct(argb);
673
- }
674
- static isBlue(hue) {
675
- return hue >= 250 && hue < 270;
676
- }
677
- static isYellow(hue) {
678
- return hue >= 105 && hue < 125;
679
- }
680
- static isCyan(hue) {
681
- return hue >= 170 && hue < 207;
682
- }
683
- toInt() {
684
- return this.argb;
685
- }
686
- /** Sets a property of the Hct object. */
687
- setValue(propertyName, value) {
688
- this[propertyName] = value;
689
- }
690
- toString() {
691
- return `HCT(${this.hue.toFixed(0)}, ${this.chroma.toFixed(0)}, ${this.tone.toFixed(0)})`;
692
- }
693
- /**
694
- * Translates a color into different [ViewingConditions].
695
- *
696
- * Colors change appearance. They look different with lights on versus off,
697
- * the same color, as in hex code, on white looks different when on black.
698
- * This is called color relativity, most famously explicated by Josef Albers
699
- * in Interaction of Color.
700
- *
701
- * In color science, color appearance models can account for this and
702
- * calculate the appearance of a color in different settings. HCT is based on
703
- * CAM16, a color appearance model, and uses it to make these calculations.
704
- *
705
- * See [ViewingConditions.make] for parameters affecting color appearance.
706
- */
707
- inViewingConditions(vc) {
708
- // 1. Use CAM16 to find XYZ coordinates of color in specified VC.
709
- const cam = materialColorUtilities.Cam16.fromInt(this.toInt());
710
- const viewedInVc = cam.xyzInViewingConditions(vc);
711
- // 2. Create CAM16 of those XYZ coordinates in default VC.
712
- const recastInVc = materialColorUtilities.Cam16.fromXyzInViewingConditions(viewedInVc[0], viewedInVc[1], viewedInVc[2], materialColorUtilities.ViewingConditions.make());
713
- // 3. Create HCT from:
714
- // - CAM16 using default VC with XYZ coordinates in specified VC.
715
- // - L* converted from Y in XYZ coordinates in specified VC.
716
- const recastHct = Hct.from(recastInVc.hue, recastInVc.chroma, materialColorUtilities.lstarFromY(viewedInVc[1]));
717
- return recastHct;
718
- }
719
- setInternalState(argb) {
720
- const cam = materialColorUtilities.Cam16.fromInt(argb);
721
- this.internalHue = cam.hue;
722
- this.internalChroma = cam.chroma;
723
- this.internalTone = materialColorUtilities.lstarFromArgb(argb);
724
- this.argb = argb;
725
- }
726
- }
727
-
728
- /**
729
- * @license
730
- * Copyright 2022 Google LLC
731
- *
732
- * Licensed under the Apache License, Version 2.0 (the "License");
733
- * you may not use this file except in compliance with the License.
734
- * You may obtain a copy of the License at
735
- *
736
- * http://www.apache.org/licenses/LICENSE-2.0
737
- *
738
- * Unless required by applicable law or agreed to in writing, software
739
- * distributed under the License is distributed on an "AS IS" BASIS,
740
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
741
- * See the License for the specific language governing permissions and
742
- * limitations under the License.
743
- */
744
- /**
745
- * Returns a new DynamicColor that is the same as the original color, but with
746
- * the extended dynamic color's constraints for the given spec version.
747
- *
748
- * @param originlColor The original color.
749
- * @param specVersion The spec version to extend.
750
- * @param extendedColor The color with the values to extend.
751
- */
752
- function extendSpecVersion(originlColor, extendedColor) {
753
- return DynamicColor.fromPalette({
754
- name: originlColor.name,
755
- palette: (s) => extendedColor.palette(s),
756
- tone: (s) => extendedColor.tone(s),
757
- isBackground: originlColor.isBackground,
758
- chromaMultiplier: (s) => {
759
- const chromaMultiplier = extendedColor.chromaMultiplier;
760
- return chromaMultiplier !== undefined ? chromaMultiplier(s) : 1;
761
- },
762
- background: (s) => {
763
- const background = extendedColor.background;
764
- return background !== undefined ? background(s) : undefined;
765
- },
766
- secondBackground: (s) => {
767
- const secondBackground = extendedColor.secondBackground;
768
- return secondBackground !== undefined ? secondBackground(s) : undefined;
769
- },
770
- contrastCurve: (s) => {
771
- const contrastCurve = extendedColor.contrastCurve;
772
- return contrastCurve !== undefined ? contrastCurve(s) : undefined;
773
- },
774
- toneDeltaPair: (s) => {
775
- const toneDeltaPair = extendedColor.toneDeltaPair;
776
- return toneDeltaPair !== undefined ? toneDeltaPair(s) : undefined;
777
- },
778
- });
779
- }
780
- /**
781
- * A color that adjusts itself based on UI state provided by DynamicScheme.
782
- *
783
- * Colors without backgrounds do not change tone when contrast changes. Colors
784
- * with backgrounds become closer to their background as contrast lowers, and
785
- * further when contrast increases.
786
- *
787
- * Prefer static constructors. They require either a hexcode, a palette and
788
- * tone, or a hue and chroma. Optionally, they can provide a background
789
- * DynamicColor.
790
- */
791
- class DynamicColor {
792
- /**
793
- * The base constructor for DynamicColor.
794
- *
795
- * _Strongly_ prefer using one of the convenience constructors. This class is
796
- * arguably too flexible to ensure it can support any scenario. Functional
797
- * arguments allow overriding without risks that come with subclasses.
798
- *
799
- * For example, the default behavior of adjust tone at max contrast
800
- * to be at a 7.0 ratio with its background is principled and
801
- * matches accessibility guidance. That does not mean it's the desired
802
- * approach for _every_ design system, and every color pairing,
803
- * always, in every case.
804
- *
805
- * @param name The name of the dynamic color. Defaults to empty.
806
- * @param palette Function that provides a TonalPalette given DynamicScheme. A
807
- * TonalPalette is defined by a hue and chroma, so this replaces the need
808
- * to specify hue/chroma. By providing a tonal palette, when contrast
809
- * adjustments are made, intended chroma can be preserved.
810
- * @param tone Function that provides a tone, given a DynamicScheme.
811
- * @param isBackground Whether this dynamic color is a background, with some
812
- * other color as the foreground. Defaults to false.
813
- * @param chromaMultiplier A factor that multiplies the chroma for this color.
814
- * @param background The background of the dynamic color (as a function of a
815
- * `DynamicScheme`), if it exists.
816
- * @param secondBackground A second background of the dynamic color (as a
817
- * function of a `DynamicScheme`), if it exists.
818
- * @param contrastCurve A `ContrastCurve` object specifying how its contrast
819
- * against its background should behave in various contrast levels
820
- * options.
821
- * @param toneDeltaPair A `ToneDeltaPair` object specifying a tone delta
822
- * constraint between two colors. One of them must be the color being
823
- * constructed.
824
- */
825
- constructor(name, palette, tone, isBackground, chromaMultiplier, background, secondBackground, contrastCurve, toneDeltaPair) {
826
- this.name = name;
827
- this.palette = palette;
828
- this.tone = tone;
829
- this.isBackground = isBackground;
830
- this.chromaMultiplier = chromaMultiplier;
831
- this.background = background;
832
- this.secondBackground = secondBackground;
833
- this.contrastCurve = contrastCurve;
834
- this.toneDeltaPair = toneDeltaPair;
835
- this.hctCache = new Map();
836
- if (!background && secondBackground) {
837
- throw new Error(`Color ${name} has secondBackground` +
838
- `defined, but background is not defined.`);
839
- }
840
- if (!background && contrastCurve) {
841
- throw new Error(`Color ${name} has contrastCurve` +
842
- `defined, but background is not defined.`);
843
- }
844
- if (background && !contrastCurve) {
845
- throw new Error(`Color ${name} has background` +
846
- `defined, but contrastCurve is not defined.`);
847
- }
848
- }
849
- /**
850
- * Create a DynamicColor defined by a TonalPalette and HCT tone.
851
- *
852
- * @param args Functions with DynamicScheme as input. Must provide a palette
853
- * and tone. May provide a background DynamicColor and ToneDeltaPair.
854
- */
855
- static fromPalette(args) {
856
- var _a, _b, _c;
857
- return new DynamicColor((_a = args.name) !== null && _a !== void 0 ? _a : '', args.palette, (_b = args.tone) !== null && _b !== void 0 ? _b : DynamicColor.getInitialToneFromBackground(args.background), (_c = args.isBackground) !== null && _c !== void 0 ? _c : false, args.chromaMultiplier, args.background, args.secondBackground, args.contrastCurve, args.toneDeltaPair);
858
- }
859
- static getInitialToneFromBackground(background) {
860
- if (background === undefined) {
861
- return (s) => 50;
862
- }
863
- return (s) => (background(s) ? background(s).getTone(s) : 50);
864
- }
865
- /**
866
- * Given a background tone, finds a foreground tone, while ensuring they reach
867
- * a contrast ratio that is as close to [ratio] as possible.
868
- *
869
- * @param bgTone Tone in HCT. Range is 0 to 100, undefined behavior when it
870
- * falls outside that range.
871
- * @param ratio The contrast ratio desired between bgTone and the return
872
- * value.
873
- */
874
- static foregroundTone(bgTone, ratio) {
875
- const lighterTone = materialColorUtilities.Contrast.lighterUnsafe(bgTone, ratio);
876
- const darkerTone = materialColorUtilities.Contrast.darkerUnsafe(bgTone, ratio);
877
- const lighterRatio = materialColorUtilities.Contrast.ratioOfTones(lighterTone, bgTone);
878
- const darkerRatio = materialColorUtilities.Contrast.ratioOfTones(darkerTone, bgTone);
879
- const preferLighter = DynamicColor.tonePrefersLightForeground(bgTone);
880
- if (preferLighter) {
881
- // This handles an edge case where the initial contrast ratio is high
882
- // (ex. 13.0), and the ratio passed to the function is that high
883
- // ratio, and both the lighter and darker ratio fails to pass that
884
- // ratio.
885
- //
886
- // This was observed with Tonal Spot's On Primary Container turning
887
- // black momentarily between high and max contrast in light mode. PC's
888
- // standard tone was T90, OPC's was T10, it was light mode, and the
889
- // contrast value was 0.6568521221032331.
890
- const negligibleDifference = Math.abs(lighterRatio - darkerRatio) < 0.1 &&
891
- lighterRatio < ratio &&
892
- darkerRatio < ratio;
893
- return lighterRatio >= ratio ||
894
- lighterRatio >= darkerRatio ||
895
- negligibleDifference
896
- ? lighterTone
897
- : darkerTone;
898
- }
899
- else {
900
- return darkerRatio >= ratio || darkerRatio >= lighterRatio
901
- ? darkerTone
902
- : lighterTone;
903
- }
904
- }
905
- /**
906
- * Returns whether [tone] prefers a light foreground.
907
- *
908
- * People prefer white foregrounds on ~T60-70. Observed over time, and also
909
- * by Andrew Somers during research for APCA.
910
- *
911
- * T60 used as to create the smallest discontinuity possible when skipping
912
- * down to T49 in order to ensure light foregrounds.
913
- * Since `tertiaryContainer` in dark monochrome scheme requires a tone of
914
- * 60, it should not be adjusted. Therefore, 60 is excluded here.
915
- */
916
- static tonePrefersLightForeground(tone) {
917
- return Math.round(tone) < 60.0;
918
- }
919
- /**
920
- * Returns whether [tone] can reach a contrast ratio of 4.5 with a lighter
921
- * color.
922
- */
923
- static toneAllowsLightForeground(tone) {
924
- return Math.round(tone) <= 49.0;
925
- }
926
- /**
927
- * Adjusts a tone such that white has 4.5 contrast, if the tone is
928
- * reasonably close to supporting it.
929
- */
930
- static enableLightForeground(tone) {
931
- if (DynamicColor.tonePrefersLightForeground(tone) &&
932
- !DynamicColor.toneAllowsLightForeground(tone)) {
933
- return 49.0;
934
- }
935
- return tone;
936
- }
937
- /**
938
- * Returns a deep copy of this DynamicColor.
939
- */
940
- clone() {
941
- return DynamicColor.fromPalette({
942
- name: this.name,
943
- palette: this.palette,
944
- tone: this.tone,
945
- isBackground: this.isBackground,
946
- chromaMultiplier: this.chromaMultiplier,
947
- background: this.background,
948
- secondBackground: this.secondBackground,
949
- contrastCurve: this.contrastCurve,
950
- toneDeltaPair: this.toneDeltaPair,
951
- });
952
- }
953
- /**
954
- * Clears the cache of HCT values for this color. For testing or debugging
955
- * purposes.
956
- */
957
- clearCache() {
958
- this.hctCache.clear();
959
- }
960
- /**
961
- * Returns a ARGB integer (i.e. a hex code).
962
- *
963
- * @param scheme Defines the conditions of the user interface, for example,
964
- * whether or not it is dark mode or light mode, and what the desired
965
- * contrast level is.
966
- */
967
- getArgb(scheme) {
968
- return this.getHct(scheme).toInt();
969
- }
970
- /**
971
- * Returns a color, expressed in the HCT color space, that this
972
- * DynamicColor is under the conditions in scheme.
973
- *
974
- * @param scheme Defines the conditions of the user interface, for example,
975
- * whether or not it is dark mode or light mode, and what the desired
976
- * contrast level is.
977
- */
978
- getHct(scheme) {
979
- const palette = this.palette(scheme);
980
- const tone = this.getTone(scheme);
981
- const hue = palette.hue;
982
- const chroma = palette.chroma *
983
- (this.chromaMultiplier ? this.chromaMultiplier(scheme) : 1);
984
- return Hct.from(hue, chroma, tone);
985
- }
986
- /**
987
- * Returns a tone, T in the HCT color space, that this DynamicColor is under
988
- * the conditions in scheme.
989
- *
990
- * @param scheme Defines the conditions of the user interface, for example,
991
- * whether or not it is dark mode or light mode, and what the desired
992
- * contrast level is.
993
- */
994
- getTone(scheme) {
995
- const toneDeltaPair = this.toneDeltaPair
996
- ? this.toneDeltaPair(scheme)
997
- : undefined;
998
- // Case 0: tone delta constraint.
999
- if (toneDeltaPair) {
1000
- const roleA = toneDeltaPair.roleA;
1001
- const roleB = toneDeltaPair.roleB;
1002
- const polarity = toneDeltaPair.polarity;
1003
- const constraint = toneDeltaPair.constraint;
1004
- const absoluteDelta = polarity === 'darker' ||
1005
- (polarity === 'relative_lighter' && scheme.isDark) ||
1006
- (polarity === 'relative_darker' && !scheme.isDark)
1007
- ? -toneDeltaPair.delta
1008
- : toneDeltaPair.delta;
1009
- const amRoleA = this.name === roleA.name;
1010
- const selfRole = amRoleA ? roleA : roleB;
1011
- const refRole = amRoleA ? roleB : roleA;
1012
- let selfTone = selfRole.tone(scheme);
1013
- const refTone = refRole.getTone(scheme);
1014
- const relativeDelta = absoluteDelta * (amRoleA ? 1 : -1);
1015
- if (constraint === 'exact') {
1016
- selfTone = materialColorUtilities.clampDouble(0, 100, refTone + relativeDelta);
1017
- }
1018
- else if (constraint === 'nearer') {
1019
- if (relativeDelta > 0) {
1020
- selfTone = materialColorUtilities.clampDouble(0, 100, materialColorUtilities.clampDouble(refTone, refTone + relativeDelta, selfTone));
1021
- }
1022
- else {
1023
- selfTone = materialColorUtilities.clampDouble(0, 100, materialColorUtilities.clampDouble(refTone + relativeDelta, refTone, selfTone));
1024
- }
1025
- }
1026
- else if (constraint === 'farther') {
1027
- if (relativeDelta > 0) {
1028
- selfTone = materialColorUtilities.clampDouble(refTone + relativeDelta, 100, selfTone);
1029
- }
1030
- else {
1031
- selfTone = materialColorUtilities.clampDouble(0, refTone + relativeDelta, selfTone);
1032
- }
1033
- }
1034
- if (this.background && this.contrastCurve) {
1035
- const background = this.background(scheme);
1036
- const contrastCurve = this.contrastCurve(scheme);
1037
- if (background && contrastCurve) {
1038
- // Adjust the tones for contrast, if background and contrast curve
1039
- // are defined.
1040
- const bgTone = background.getTone(scheme);
1041
- const selfContrast = contrastCurve.get(scheme.contrastLevel);
1042
- selfTone =
1043
- materialColorUtilities.Contrast.ratioOfTones(bgTone, selfTone) >= selfContrast &&
1044
- scheme.contrastLevel >= 0
1045
- ? selfTone
1046
- : DynamicColor.foregroundTone(bgTone, selfContrast);
1047
- }
1048
- }
1049
- // This can avoid the awkward tones for background colors including the
1050
- // access fixed colors. Accent fixed dim colors should not be adjusted.
1051
- if (this.isBackground && !this.name.endsWith('_fixed_dim')) {
1052
- if (selfTone >= 57) {
1053
- selfTone = materialColorUtilities.clampDouble(65, 100, selfTone);
1054
- }
1055
- else {
1056
- selfTone = materialColorUtilities.clampDouble(0, 49, selfTone);
1057
- }
1058
- }
1059
- return selfTone;
1060
- }
1061
- else {
1062
- // Case 1: No tone delta pair; just solve for itself.
1063
- let answer = this.tone(scheme);
1064
- if (this.background == undefined ||
1065
- this.background(scheme) === undefined ||
1066
- this.contrastCurve == undefined ||
1067
- this.contrastCurve(scheme) === undefined) {
1068
- return answer; // No adjustment for colors with no background.
1069
- }
1070
- const bgTone = this.background(scheme).getTone(scheme);
1071
- const desiredRatio = this.contrastCurve(scheme).get(scheme.contrastLevel);
1072
- // Recalculate the tone from desired contrast ratio if the current
1073
- // contrast ratio is not enough or desired contrast level is decreasing
1074
- // (<0).
1075
- answer =
1076
- materialColorUtilities.Contrast.ratioOfTones(bgTone, answer) >= desiredRatio &&
1077
- scheme.contrastLevel >= 0
1078
- ? answer
1079
- : DynamicColor.foregroundTone(bgTone, desiredRatio);
1080
- // This can avoid the awkward tones for background colors including the
1081
- // access fixed colors. Accent fixed dim colors should not be adjusted.
1082
- if (this.isBackground && !this.name.endsWith('_fixed_dim')) {
1083
- if (answer >= 57) {
1084
- answer = materialColorUtilities.clampDouble(65, 100, answer);
1085
- }
1086
- else {
1087
- answer = materialColorUtilities.clampDouble(0, 49, answer);
1088
- }
1089
- }
1090
- if (this.secondBackground == undefined ||
1091
- this.secondBackground(scheme) === undefined) {
1092
- return answer;
1093
- }
1094
- // Case 2: Adjust for dual backgrounds.
1095
- const [bg1, bg2] = [this.background, this.secondBackground];
1096
- const [bgTone1, bgTone2] = [
1097
- bg1(scheme).getTone(scheme),
1098
- bg2(scheme).getTone(scheme),
1099
- ];
1100
- const [upper, lower] = [
1101
- Math.max(bgTone1, bgTone2),
1102
- Math.min(bgTone1, bgTone2),
1103
- ];
1104
- if (materialColorUtilities.Contrast.ratioOfTones(upper, answer) >= desiredRatio &&
1105
- materialColorUtilities.Contrast.ratioOfTones(lower, answer) >= desiredRatio) {
1106
- return answer;
1107
- }
1108
- // The darkest light tone that satisfies the desired ratio,
1109
- // or -1 if such ratio cannot be reached.
1110
- const lightOption = materialColorUtilities.Contrast.lighter(upper, desiredRatio);
1111
- // The lightest dark tone that satisfies the desired ratio,
1112
- // or -1 if such ratio cannot be reached.
1113
- const darkOption = materialColorUtilities.Contrast.darker(lower, desiredRatio);
1114
- // Tones suitable for the foreground.
1115
- const availables = [];
1116
- if (lightOption !== -1)
1117
- availables.push(lightOption);
1118
- if (darkOption !== -1)
1119
- availables.push(darkOption);
1120
- const prefersLight = DynamicColor.tonePrefersLightForeground(bgTone1) ||
1121
- DynamicColor.tonePrefersLightForeground(bgTone2);
1122
- if (prefersLight) {
1123
- return lightOption < 0 ? 100 : lightOption;
1124
- }
1125
- if (availables.length === 1) {
1126
- return availables[0];
1127
- }
1128
- return darkOption < 0 ? 0 : darkOption;
1129
- }
1130
- }
1131
- }
1132
-
1133
- /**
1134
- * @license
1135
- * Copyright 2023 Google LLC
1136
- *
1137
- * Licensed under the Apache License, Version 2.0 (the "License");
1138
- * you may not use this file except in compliance with the License.
1139
- * You may obtain a copy of the License at
1140
- *
1141
- * http://www.apache.org/licenses/LICENSE-2.0
1142
- *
1143
- * Unless required by applicable law or agreed to in writing, software
1144
- * distributed under the License is distributed on an "AS IS" BASIS,
1145
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1146
- * See the License for the specific language governing permissions and
1147
- * limitations under the License.
1148
- */
1149
- /**
1150
- * Documents a constraint between two DynamicColors, in which their tones must
1151
- * have a certain distance from each other.
1152
- *
1153
- * Prefer a DynamicColor with a background, this is for special cases when
1154
- * designers want tonal distance, literally contrast, between two colors that
1155
- * don't have a background / foreground relationship or a contrast guarantee.
1156
- */
1157
- class ToneDeltaPair {
1158
- /**
1159
- * Documents a constraint in tone distance between two DynamicColors.
1160
- *
1161
- * The polarity is an adjective that describes "A", compared to "B".
1162
- *
1163
- * For instance, ToneDeltaPair(A, B, 15, 'darker', 'exact') states that
1164
- * A's tone should be exactly 15 darker than B's.
1165
- *
1166
- * 'relative_darker' and 'relative_lighter' describes the tone adjustment
1167
- * relative to the surface color trend (white in light mode; black in dark
1168
- * mode). For instance, ToneDeltaPair(A, B, 10, 'relative_lighter',
1169
- * 'farther') states that A should be at least 10 lighter than B in light
1170
- * mode, and at least 10 darker than B in dark mode.
1171
- *
1172
- * @param roleA The first role in a pair.
1173
- * @param roleB The second role in a pair.
1174
- * @param delta Required difference between tones. Absolute value, negative
1175
- * values have undefined behavior.
1176
- * @param polarity The relative relation between tones of roleA and roleB,
1177
- * as described above.
1178
- * @param constraint How to fulfill the tone delta pair constraint.
1179
- * @param stayTogether Whether these two roles should stay on the same side
1180
- * of the "awkward zone" (T50-59). This is necessary for certain cases where
1181
- * one role has two backgrounds.
1182
- */
1183
- constructor(roleA, roleB, delta, polarity, stayTogether, constraint) {
1184
- this.roleA = roleA;
1185
- this.roleB = roleB;
1186
- this.delta = delta;
1187
- this.polarity = polarity;
1188
- this.stayTogether = stayTogether;
1189
- this.constraint = constraint;
1190
- this.constraint = constraint !== null && constraint !== void 0 ? constraint : 'exact';
1191
- }
1192
- }
1193
-
1194
- function argbToRgb(argb) {
1195
- return {
1196
- r: (argb >> 16) & 0xff,
1197
- g: (argb >> 8) & 0xff,
1198
- b: argb & 0xff,
1199
- };
1200
- }
1201
- class ConfigurableColor {
1202
- constructor(option, schemeService, colorService) {
1203
- this.option = option;
1204
- this.schemeService = schemeService;
1205
- this.colorService = colorService;
1206
- this.dynamicColor = null;
1207
- }
1208
- update(args) {
1209
- this.dynamicColor = null;
1210
- this.option = Object.assign(Object.assign({}, this.option), args);
1211
- }
1212
- getHex() {
1213
- return materialColorUtilities.hexFromArgb(this.getArgb());
1214
- }
1215
- getArgb() {
1216
- return this.getMaterialColor().getArgb(this.schemeService.get());
1217
- }
1218
- getRgb() {
1219
- return argbToRgb(this.getArgb());
1220
- }
1221
- getName() {
1222
- return this.option.name.replace(/([A-Z])/g, '_$1').toLowerCase();
1223
- }
1224
- getMaterialColor() {
1225
- if ('alias' in this.option) {
1226
- if (!this.option.alias) {
1227
- throw new Error('Alias option must be defined');
1228
- }
1229
- return this.colorService.get(this.option.alias).getMaterialColor();
1230
- }
1231
- if (!this.dynamicColor) {
1232
- this.dynamicColor = DynamicColor.fromPalette(Object.assign(Object.assign({}, this.option), { name: this.getName() }));
1233
- }
1234
- return this.dynamicColor;
1235
- }
1236
- }
1237
-
1238
- function getCurve(defaultContrast) {
1239
- if (defaultContrast === 1.5) {
1240
- return new ContrastCurve(1.5, 1.5, 3, 4.5);
1241
- }
1242
- else if (defaultContrast === 3) {
1243
- return new ContrastCurve(3, 3, 4.5, 7);
1244
- }
1245
- else if (defaultContrast === 4.5) {
1246
- return new ContrastCurve(4.5, 4.5, 7, 11);
1247
- }
1248
- else if (defaultContrast === 6) {
1249
- return new ContrastCurve(6, 6, 7, 11);
1250
- }
1251
- else if (defaultContrast === 7) {
1252
- return new ContrastCurve(7, 7, 11, 21);
1253
- }
1254
- else if (defaultContrast === 9) {
1255
- return new ContrastCurve(9, 9, 11, 21);
1256
- }
1257
- else if (defaultContrast === 11) {
1258
- return new ContrastCurve(11, 11, 21, 21);
1259
- }
1260
- else if (defaultContrast === 21) {
1261
- return new ContrastCurve(21, 21, 21, 21);
1262
- }
1263
- else {
1264
- // Shouldn't happen.
1265
- return new ContrastCurve(defaultContrast, defaultContrast, 7, 21);
1266
- }
1267
- }
1268
- function tMaxC(palette, lowerBound = 0, upperBound = 100, chromaMultiplier = 1) {
1269
- const answer = findBestToneForChroma(palette.hue, palette.chroma * chromaMultiplier, 100, true);
1270
- return materialColorUtilities.clampDouble(lowerBound, upperBound, answer);
1271
- }
1272
- function tMinC(palette, lowerBound = 0, upperBound = 100) {
1273
- const answer = findBestToneForChroma(palette.hue, palette.chroma, 0, false);
1274
- return materialColorUtilities.clampDouble(lowerBound, upperBound, answer);
1275
- }
1276
- function findBestToneForChroma(hue, chroma, tone, byDecreasingTone) {
1277
- let answer = tone;
1278
- let bestCandidate = Hct.from(hue, chroma, answer);
1279
- while (bestCandidate.chroma < chroma) {
1280
- if (tone < 0 || tone > 100) {
1281
- break;
1282
- }
1283
- tone += byDecreasingTone ? -1 : 1.0;
1284
- const newCandidate = Hct.from(hue, chroma, tone);
1285
- if (bestCandidate.chroma < newCandidate.chroma) {
1286
- bestCandidate = newCandidate;
1287
- answer = tone;
1288
- }
1289
- }
1290
- return answer;
1291
- }
1292
-
1293
- function capitalizeFirstLetter(string) {
1294
- return string.charAt(0).toUpperCase() + string.slice(1);
1295
- }
1296
- const highestSurface = (s, colorService) => {
1297
- if (colorService instanceof ColorApi) {
1298
- return s.isDark
1299
- ? colorService.getColor('surfaceBright').getMaterialColor()
1300
- : colorService.getColor('surfaceDim').getMaterialColor();
1301
- }
1302
- else {
1303
- return s.isDark
1304
- ? colorService.get('surfaceBright').getMaterialColor()
1305
- : colorService.get('surfaceDim').getMaterialColor();
1306
- }
1307
- };
1308
- class ColorManager {
1309
- constructor({ schemeManager }) {
1310
- this.colorMap = new Map();
1311
- this.schemeManager = schemeManager;
1312
- }
1313
- createOrUpdate(key, args) {
1314
- let colorEntity = this.colorMap.get(key);
1315
- if (!colorEntity) {
1316
- const { palette, alias } = args;
1317
- if (palette) {
1318
- colorEntity = new ConfigurableColor(Object.assign(Object.assign({}, args), { palette: palette, name: key }), this.schemeManager, this);
1319
- this.colorMap.set(key, colorEntity);
1320
- }
1321
- else if (alias) {
1322
- colorEntity = new ConfigurableColor(Object.assign(Object.assign({}, args), { alias: alias, name: key }), this.schemeManager, this);
1323
- this.colorMap.set(key, colorEntity);
1324
- }
1325
- else {
1326
- throw new Error(`Palette ${palette} does not exist from ${key}`);
1327
- }
1328
- }
1329
- else {
1330
- colorEntity.update(Object.assign(Object.assign({}, args), { name: key }));
1331
- this.colorMap.set(key, colorEntity);
1332
- }
1333
- return colorEntity;
1334
- }
1335
- remove(key) {
1336
- return this.colorMap.delete(key);
1337
- }
1338
- get(key) {
1339
- const colorEntity = this.colorMap.get(key);
1340
- if (colorEntity) {
1341
- return colorEntity;
1342
- }
1343
- else {
1344
- throw new Error(`Color ${key} does not exist`);
1345
- }
1346
- }
1347
- getAll() {
1348
- return this.colorMap;
1349
- }
1350
- addFromPalette(key) {
1351
- const colorKey = key;
1352
- const colorDimKey = (colorKey + 'Dim');
1353
- const ColorKey = capitalizeFirstLetter(key);
1354
- const onColorKey = ('on' + ColorKey);
1355
- const colorContainerKey = (colorKey + 'Container');
1356
- const onColorContainerKey = ('on' +
1357
- ColorKey +
1358
- 'Container');
1359
- const colorFixedKey = (colorKey + 'Fixed');
1360
- const colorFixedDimKey = (colorKey + 'FixedDim');
1361
- const onColorFixedKey = ('on' + ColorKey + 'Fixed');
1362
- const onColorFixedVariantKey = ('on' +
1363
- ColorKey +
1364
- 'FixedVariant');
1365
- this.createOrUpdate(colorKey, {
1366
- palette: (s) => s.getPalette(colorKey),
1367
- tone: (s) => {
1368
- if (s.variant === 'neutral') {
1369
- return s.isDark
1370
- ? tMinC(s.getPalette(colorKey), 0, 98)
1371
- : tMaxC(s.getPalette(colorKey));
1372
- }
1373
- else if (s.variant === 'vibrant') {
1374
- return tMaxC(s.getPalette(colorKey), 0, s.isDark ? 90 : 98);
1375
- }
1376
- else {
1377
- return s.isDark ? 80 : tMaxC(s.getPalette(colorKey));
1378
- }
1379
- },
1380
- isBackground: true,
1381
- background: (s) => highestSurface(s, this),
1382
- contrastCurve: (s) => getCurve(4.5),
1383
- toneDeltaPair: (s) => new ToneDeltaPair(this.get(colorContainerKey).getMaterialColor(), this.get(colorKey).getMaterialColor(), 5, 'relative_lighter', true, 'farther'),
1384
- });
1385
- this.createOrUpdate(colorDimKey, {
1386
- palette: (s) => s.getPalette(colorKey),
1387
- tone: (s) => {
1388
- if (s.variant === 'neutral') {
1389
- return 85;
1390
- }
1391
- else {
1392
- return tMaxC(s.getPalette(colorKey), 0, 90);
1393
- }
1394
- },
1395
- isBackground: true,
1396
- background: (s) => this.get('surfaceContainerHigh').getMaterialColor(),
1397
- contrastCurve: (s) => getCurve(4.5),
1398
- toneDeltaPair: (s) => new ToneDeltaPair(this.get(colorDimKey).getMaterialColor(), this.get(colorKey).getMaterialColor(), 5, 'darker', true, 'farther'),
1399
- });
1400
- this.createOrUpdate(onColorKey, {
1401
- palette: (s) => s.getPalette(colorKey),
1402
- background: (s) => this.get(colorKey).getMaterialColor(),
1403
- contrastCurve: (s) => getCurve(6),
1404
- });
1405
- this.createOrUpdate(colorContainerKey, {
1406
- palette: (s) => s.getPalette(colorKey),
1407
- tone: (s) => {
1408
- if (s.variant === 'vibrant') {
1409
- return s.isDark
1410
- ? tMinC(s.getPalette(colorKey), 30, 40)
1411
- : tMaxC(s.getPalette(colorKey), 84, 90);
1412
- }
1413
- else if (s.variant === 'expressive') {
1414
- return s.isDark ? 15 : tMaxC(s.getPalette(colorKey), 90, 95);
1415
- }
1416
- else {
1417
- return s.isDark ? 25 : 90;
1418
- }
1419
- },
1420
- isBackground: true,
1421
- background: (s) => highestSurface(s, this),
1422
- toneDeltaPair: (s) => undefined,
1423
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
1424
- });
1425
- this.createOrUpdate(onColorContainerKey, {
1426
- palette: (s) => s.getPalette(colorKey),
1427
- background: (s) => this.get(colorContainerKey).getMaterialColor(),
1428
- contrastCurve: (s) => getCurve(6),
1429
- });
1430
- this.createOrUpdate(colorFixedKey, {
1431
- palette: (s) => s.getPalette(colorKey),
1432
- tone: (s) => {
1433
- const tempS = Object.assign({}, s, { isDark: false, contrastLevel: 0 });
1434
- return this.get(colorContainerKey).getMaterialColor().getTone(tempS);
1435
- },
1436
- isBackground: true,
1437
- background: (s) => highestSurface(s, this),
1438
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
1439
- });
1440
- this.createOrUpdate(colorFixedDimKey, {
1441
- palette: (s) => s.getPalette(colorKey),
1442
- tone: (s) => this.get(colorFixedKey).getMaterialColor().getTone(s),
1443
- isBackground: true,
1444
- toneDeltaPair: (s) => new ToneDeltaPair(this.get(colorFixedDimKey).getMaterialColor(), this.get(colorFixedKey).getMaterialColor(), 5, 'darker', true, 'exact'),
1445
- });
1446
- this.createOrUpdate(onColorFixedKey, {
1447
- palette: (s) => s.getPalette(colorKey),
1448
- background: (s) => this.get(colorFixedDimKey).getMaterialColor(),
1449
- contrastCurve: (s) => getCurve(7),
1450
- });
1451
- this.createOrUpdate(onColorFixedVariantKey, {
1452
- palette: (s) => s.getPalette(colorKey),
1453
- background: (s) => this.get(colorFixedDimKey).getMaterialColor(),
1454
- contrastCurve: (s) => getCurve(4.5),
1455
- });
1456
- }
1457
- }
1458
-
1459
- const ColorModule = {
1460
- colorManager: awilix.asClass(ColorManager).singleton(),
1461
- colorApi: awilix.asClass(ColorApi).singleton(),
1462
- };
1463
-
1464
- class Scheme {
1465
- constructor(options) {
1466
- this.options = options;
1467
- }
1468
- get variant() {
1469
- return this.options.variant.name;
1470
- }
1471
- get contrastLevel() {
1472
- if (!this.options) {
1473
- throw new Error('Scheme options is not set');
1474
- }
1475
- return this.options.contrastLevel;
1476
- }
1477
- get isDark() {
1478
- if (!this.options) {
1479
- throw new Error('Scheme options is not set');
1480
- }
1481
- return this.options.isDark;
1482
- }
1483
- get sourceColorHct() {
1484
- if (!this.options) {
1485
- throw new Error('Scheme options is not set');
1486
- }
1487
- return Hct.fromInt(this.options.sourceColorArgb);
1488
- }
1489
- getPalette(key) {
1490
- if (!this.options) {
1491
- throw new Error('Scheme options is not set');
1492
- }
1493
- const palette = this.options.palettes.get(key);
1494
- if (!palette) {
1495
- throw new Error(`Palette ${key} not found`);
1496
- }
1497
- return palette;
1498
- }
1499
- }
1500
-
1501
- class SchemeManager {
1502
- createOrUpdate(options) {
1503
- var _a, _b, _c, _d;
1504
- this.options = Object.assign(Object.assign(Object.assign({}, this.options), options), { sourcesColorHex: Object.assign(Object.assign({}, (_a = this.options) === null || _a === void 0 ? void 0 : _a.sourcesColorHex), options.sourcesColorHex), palettes: Object.assign(Object.assign({}, (_b = this.options) === null || _b === void 0 ? void 0 : _b.palettes), options.palettes) });
1505
- const palettes = new Map();
1506
- if (!this.options.sourcesColorHex.primary) {
1507
- throw new Error('Primary source color is not set');
1508
- }
1509
- const sourceColorArgb = materialColorUtilities.argbFromHex(this.options.sourcesColorHex.primary);
1510
- const sourceColorHct = Hct.fromInt(sourceColorArgb);
1511
- if (!this.options.palettes) {
1512
- return;
1513
- }
1514
- for (const [key, { sourceColorkey, tonalPalette: paletteFunction },] of Object.entries(this.options.palettes)) {
1515
- let palette;
1516
- if (typeof sourceColorkey != 'string') {
1517
- palette = paletteFunction({
1518
- sourceColorHct: sourceColorHct,
1519
- isDark: (_c = options.isDark) !== null && _c !== void 0 ? _c : false,
1520
- });
1521
- }
1522
- else {
1523
- const sourceColorArgb = materialColorUtilities.argbFromHex(this.options.sourcesColorHex[sourceColorkey]);
1524
- const colorHct = Hct.fromInt(sourceColorArgb);
1525
- palette = paletteFunction({
1526
- sourceColorHct: sourceColorHct,
1527
- colorHct: colorHct,
1528
- isDark: (_d = options.isDark) !== null && _d !== void 0 ? _d : false,
1529
- });
1530
- }
1531
- palettes.set(key, palette);
1532
- }
1533
- this.schemeEntity = new Scheme(Object.assign(Object.assign({}, this.options), { palettes: palettes, sourceColorArgb: sourceColorArgb }));
1534
- }
1535
- get() {
1536
- if (!this.schemeEntity) {
1537
- throw new Error('Scheme is not created');
1538
- }
1539
- return this.schemeEntity;
1540
- }
1541
- }
1542
-
1543
- class VariantManager {
1544
- constructor({ schemeManager }) {
1545
- this.customPalettes = {};
1546
- this.schemeManager = schemeManager;
1547
- }
1548
- addCustomPalette(key, colorHex) {
1549
- this.customPalettes[key] = colorHex;
1550
- this.update();
1551
- }
1552
- set(variantEntity) {
1553
- this.variantEntity = variantEntity;
1554
- this.update();
1555
- }
1556
- update() {
1557
- if (!this.variantEntity)
1558
- return;
1559
- const palettes = {};
1560
- Object.keys(this.variantEntity.palettes).forEach((key) => {
1561
- palettes[key] = { tonalPalette: this.variantEntity.palettes[key] };
1562
- });
1563
- const customPalettes = this.variantEntity.customPalettes;
1564
- if (customPalettes) {
1565
- Object.keys(this.customPalettes).forEach((key) => {
1566
- palettes[key] = {
1567
- sourceColorkey: key,
1568
- tonalPalette: customPalettes,
1569
- };
1570
- });
1571
- }
1572
- this.schemeManager.createOrUpdate({
1573
- sourcesColorHex: this.customPalettes,
1574
- palettes: palettes,
1575
- });
1576
- }
1577
- }
1578
-
1579
- materialColorUtilities.DynamicColor.fromPalette({
1580
- name: 'primary_palette_key_color',
1581
- palette: (s) => s.primaryPalette,
1582
- tone: (s) => s.primaryPalette.keyColor.tone,
1583
- });
1584
- class ThemeApi {
1585
- constructor({ schemeManager, variantManager, }) {
1586
- this.schemeManager = schemeManager;
1587
- this.variantManager = variantManager;
1588
- // this.addPalette({key: "primary", addDefaultColors: true})
1589
- // this.addPalette({key: "secondary", addDefaultColors: true})
1590
- // this.addPalette({key: "tertiary", addDefaultColors: true})
1591
- // this.addPalette({key: "error", palette: TonalPalette.fromHueAndChroma(25.0, 84.0)})
1592
- // this.addPalette({key: "neutral"})
1593
- // this.addPalette({key: "neutralVariant"})
1594
- }
1595
- // addPalette({key, palette, addDefaultColors}: {key: string; palette: TonalPalette; addDefaultColors: boolean}) {
1596
- // this.themeOptions.palettes.set(key, palette);
1597
- // if (addDefaultColors){
1598
- // this.colorService.addPalette(key)
1599
- // }
1600
- // }
1601
- // create(args: ThemeOptions): SchemeService {
1602
- // return new SchemeService(args, this.colorService)
1603
- // }
1604
- //
1605
- // update(options: Partial<ThemeOptions>): SchemeService {
1606
- // Object.assign(this.themeOptions, options);
1607
- // return this.theme();
1608
- // }
1609
- create(options) {
1610
- this.schemeManager.createOrUpdate(Object.assign(Object.assign({}, options), { sourcesColorHex: { primary: options.sourceColorHex } }));
1611
- this.variantManager.set(options.variant);
1612
- }
1613
- update(options) {
1614
- const themeOptions = Object.assign({}, options);
1615
- if (options.sourceColorHex)
1616
- themeOptions.sourcesColorHex = { primary: options.sourceColorHex };
1617
- this.schemeManager.createOrUpdate(themeOptions);
1618
- if (options.variant)
1619
- this.variantManager.set(options.variant);
1620
- }
1621
- addCustomPalette(key, colorHex) {
1622
- this.variantManager.addCustomPalette(key, colorHex);
1623
- }
1624
- }
1625
-
1626
- const ThemeModule = {
1627
- schemeManager: awilix.asClass(SchemeManager).singleton(),
1628
- variantManager: awilix.asClass(VariantManager).singleton(),
1629
- themeApi: awilix.asClass(ThemeApi).singleton(),
1630
- };
1631
-
1632
- const getPiecewiseHue = (sourceColorHct, hueBreakpoints, hues) => {
1633
- const size = Math.min(hueBreakpoints.length - 1, hues.length);
1634
- const sourceHue = sourceColorHct.hue;
1635
- for (let i = 0; i < size; i++) {
1636
- if (sourceHue >= hueBreakpoints[i] && sourceHue < hueBreakpoints[i + 1]) {
1637
- return materialColorUtilities.sanitizeDegreesDouble(hues[i]);
1638
- }
1639
- }
1640
- return sourceHue;
1641
- };
1642
- const getRotatedHue = (sourceColorHct, hueBreakpoints, rotations) => {
1643
- let rotation = getPiecewiseHue(sourceColorHct, hueBreakpoints, rotations);
1644
- if (Math.min(hueBreakpoints.length - 1, rotations.length) <= 0) {
1645
- rotation = 0;
1646
- }
1647
- return materialColorUtilities.sanitizeDegreesDouble(sourceColorHct.hue + rotation);
1648
- };
1649
- class Variant {
1650
- constructor(palettes = {}, name, customPalettes,
1651
- /** TODO
1652
- * Defines color modifications through variation.
1653
- * Allows customization of specific colors in the theme.
1654
- */
1655
- colors) {
1656
- this.palettes = palettes;
1657
- this.name = name;
1658
- this.customPalettes = customPalettes;
1659
- this.colors = colors;
1660
- }
1661
- }
1662
-
1663
- const getExpressiveNeutralHue = (sourceColorHct) => {
1664
- const hue = getRotatedHue(sourceColorHct, [0, 71, 124, 253, 278, 300, 360], [10, 0, 10, 0, 10, 0]);
1665
- return hue;
1666
- };
1667
- const getExpressiveNeutralChroma = (sourceColorHct, isDark) => {
1668
- const neutralHue = getExpressiveNeutralHue(sourceColorHct);
1669
- return isDark ? (Hct.isYellow(neutralHue) ? 6 : 14) : 18;
1670
- };
1671
- const expressiveVariant = {
1672
- name: 'expressive',
1673
- palettes: {
1674
- primary: ({ sourceColorHct, isDark }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, isDark ? 36 : 48),
1675
- secondary: ({ sourceColorHct, isDark }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 105, 140, 204, 253, 278, 300, 333, 360], [-160, 155, -100, 96, -96, -156, -165, -160]), isDark ? 16 : 24),
1676
- tertiary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 105, 140, 204, 253, 278, 300, 333, 360], [-165, 160, -105, 101, -101, -160, -170, -165]), 48),
1677
- neutral: ({ sourceColorHct, isDark }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getExpressiveNeutralHue(sourceColorHct), getExpressiveNeutralChroma(sourceColorHct, isDark)),
1678
- neutralVariant: ({ sourceColorHct, isDark }) => {
1679
- const expressiveNeutralHue = getExpressiveNeutralHue(sourceColorHct);
1680
- const expressiveNeutralChroma = getExpressiveNeutralChroma(sourceColorHct, isDark);
1681
- return materialColorUtilities.TonalPalette.fromHueAndChroma(expressiveNeutralHue, expressiveNeutralChroma *
1682
- (expressiveNeutralHue >= 105 && expressiveNeutralHue < 125
1683
- ? 1.6
1684
- : 2.3));
1685
- },
1686
- error: ({ sourceColorHct }) => {
1687
- const errorHue = getPiecewiseHue(sourceColorHct, [0, 3, 13, 23, 33, 43, 153, 273, 360], [12, 22, 32, 12, 22, 32, 22, 12]);
1688
- return materialColorUtilities.TonalPalette.fromHueAndChroma(errorHue, 64);
1689
- },
1690
- },
1691
- customPalettes: ({ colorHct, isDark }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(colorHct, [0, 105, 140, 204, 253, 278, 300, 333, 360], [-160, 155, -100, 96, -96, -156, -165, -160]), isDark ? 16 : 24),
1692
- };
1693
-
1694
- const neutralVariant = {
1695
- name: 'neutral',
1696
- palettes: {
1697
- primary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, Hct.isBlue(sourceColorHct.hue) ? 12 : 8),
1698
- secondary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, Hct.isBlue(sourceColorHct.hue) ? 6 : 4),
1699
- tertiary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 38, 105, 161, 204, 278, 333, 360], [-32, 26, 10, -39, 24, -15, -32]), 20),
1700
- neutral: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 1.4),
1701
- neutralVariant: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 1.4 * 2.2),
1702
- error: ({ sourceColorHct }) => {
1703
- const errorHue = getPiecewiseHue(sourceColorHct, [0, 3, 13, 23, 33, 43, 153, 273, 360], [12, 22, 32, 12, 22, 32, 22, 12]);
1704
- return materialColorUtilities.TonalPalette.fromHueAndChroma(errorHue, 50);
1705
- },
1706
- },
1707
- customPalettes: ({ colorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(colorHct.hue, Hct.isBlue(colorHct.hue) ? 6 : 4),
1708
- };
1709
-
1710
- const tonalSpotVariant = {
1711
- name: 'tonalSpot',
1712
- palettes: {
1713
- primary: ({ sourceColorHct, isDark }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, isDark ? 26 : 32),
1714
- secondary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 16),
1715
- tertiary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 20, 71, 161, 333, 360], [-40, 48, -32, 40, -32]), 28),
1716
- neutral: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 5),
1717
- neutralVariant: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 5 * 1.7),
1718
- error: ({ sourceColorHct }) => {
1719
- const errorHue = getPiecewiseHue(sourceColorHct, [0, 3, 13, 23, 33, 43, 153, 273, 360], [12, 22, 32, 12, 22, 32, 22, 12]);
1720
- return materialColorUtilities.TonalPalette.fromHueAndChroma(errorHue, 60);
1721
- },
1722
- },
1723
- customPalettes: ({ colorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(colorHct.hue, 16),
1724
- };
1725
-
1726
- const getVibrantNeutralHue = (sourceColorHct) => {
1727
- return getRotatedHue(sourceColorHct, [0, 38, 105, 140, 333, 360], [-14, 10, -14, 10, -14]);
1728
- };
1729
- const getVibrantNeutralChroma = (sourceColorHct) => {
1730
- getVibrantNeutralHue(sourceColorHct);
1731
- return 28;
1732
- };
1733
- const vibrantVariant = {
1734
- name: 'vibrant',
1735
- palettes: {
1736
- primary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(sourceColorHct.hue, 74),
1737
- secondary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 38, 105, 140, 333, 360], [-14, 10, -14, 10, -14]), 56),
1738
- tertiary: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(sourceColorHct, [0, 38, 71, 105, 140, 161, 253, 333, 360], [-72, 35, 24, -24, 62, 50, 62, -72]), 56),
1739
- neutral: ({ sourceColorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getVibrantNeutralHue(sourceColorHct), getVibrantNeutralChroma(sourceColorHct)),
1740
- neutralVariant: ({ sourceColorHct }) => {
1741
- const vibrantNeutralHue = getVibrantNeutralHue(sourceColorHct);
1742
- const vibrantNeutralChroma = getVibrantNeutralChroma(sourceColorHct);
1743
- return materialColorUtilities.TonalPalette.fromHueAndChroma(vibrantNeutralHue, vibrantNeutralChroma * 1.29);
1744
- },
1745
- error: ({ sourceColorHct }) => {
1746
- const errorHue = getPiecewiseHue(sourceColorHct, [0, 3, 13, 23, 33, 43, 153, 273, 360], [12, 22, 32, 12, 22, 32, 22, 12]);
1747
- return materialColorUtilities.TonalPalette.fromHueAndChroma(errorHue, 80);
1748
- },
1749
- },
1750
- customPalettes: ({ colorHct }) => materialColorUtilities.TonalPalette.fromHueAndChroma(getRotatedHue(colorHct, [0, 38, 105, 140, 333, 360], [-14, 10, -14, 10, -14]), 56),
1751
- };
1752
-
1753
- const Variants = {
1754
- Expressive: expressiveVariant,
1755
- Neutral: neutralVariant,
1756
- TonalSpot: tonalSpotVariant,
1757
- Vibrant: vibrantVariant,
1758
- };
1759
-
1760
- const defaultColors = (colorService) => {
1761
- const getColor = (key) => {
1762
- return colorService.getColor(key).getMaterialColor();
1763
- };
1764
- const colors = {
1765
- ////////////////////////////////////////////////////////////////
1766
- // Surfaces [S] //
1767
- ////////////////////////////////////////////////////////////////
1768
- surface: {
1769
- palette: (s) => s.getPalette('neutral'),
1770
- tone: (s) => {
1771
- if (s.isDark) {
1772
- return 4;
1773
- }
1774
- else {
1775
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1776
- return 99;
1777
- }
1778
- else if (s.variant === 'vibrant') {
1779
- return 97;
1780
- }
1781
- else {
1782
- return 98;
1783
- }
1784
- }
1785
- },
1786
- isBackground: true,
1787
- },
1788
- surfaceDim: {
1789
- palette: (s) => s.getPalette('neutral'),
1790
- tone: (s) => {
1791
- if (s.isDark) {
1792
- return 4;
1793
- }
1794
- else {
1795
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1796
- return 90;
1797
- }
1798
- else if (s.variant === 'vibrant') {
1799
- return 85;
1800
- }
1801
- else {
1802
- return 87;
1803
- }
1804
- }
1805
- },
1806
- isBackground: true,
1807
- chromaMultiplier: (s) => {
1808
- if (!s.isDark) {
1809
- if (s.variant === 'neutral') {
1810
- return 2.5;
1811
- }
1812
- else if (s.variant === 'tonalSpot') {
1813
- return 1.7;
1814
- }
1815
- else if (s.variant === 'expressive') {
1816
- return Hct.isYellow(s.getPalette('neutral').hue) ? 2.7 : 1.75;
1817
- }
1818
- else if (s.variant === 'vibrant') {
1819
- return 1.36;
1820
- }
1821
- }
1822
- return 1;
1823
- },
1824
- },
1825
- surfaceBright: {
1826
- palette: (s) => s.getPalette('neutral'),
1827
- tone: (s) => {
1828
- if (s.isDark) {
1829
- return 18;
1830
- }
1831
- else {
1832
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1833
- return 99;
1834
- }
1835
- else if (s.variant === 'vibrant') {
1836
- return 97;
1837
- }
1838
- else {
1839
- return 98;
1840
- }
1841
- }
1842
- },
1843
- isBackground: true,
1844
- chromaMultiplier: (s) => {
1845
- if (s.isDark) {
1846
- if (s.variant === 'neutral') {
1847
- return 2.5;
1848
- }
1849
- else if (s.variant === 'tonalSpot') {
1850
- return 1.7;
1851
- }
1852
- else if (s.variant === 'expressive') {
1853
- return Hct.isYellow(s.getPalette('neutral').hue) ? 2.7 : 1.75;
1854
- }
1855
- else if (s.variant === 'vibrant') {
1856
- return 1.36;
1857
- }
1858
- }
1859
- return 1;
1860
- },
1861
- },
1862
- surfaceContainerLowest: {
1863
- palette: (s) => s.getPalette('neutral'),
1864
- tone: (s) => (s.isDark ? 0 : 100),
1865
- isBackground: true,
1866
- },
1867
- surfaceContainerLow: {
1868
- palette: (s) => s.getPalette('neutral'),
1869
- tone: (s) => {
1870
- if (s.isDark) {
1871
- return 6;
1872
- }
1873
- else {
1874
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1875
- return 98;
1876
- }
1877
- else if (s.variant === 'vibrant') {
1878
- return 95;
1879
- }
1880
- else {
1881
- return 96;
1882
- }
1883
- }
1884
- },
1885
- isBackground: true,
1886
- chromaMultiplier: (s) => {
1887
- if (s.variant === 'neutral') {
1888
- return 1.3;
1889
- }
1890
- else if (s.variant === 'tonalSpot') {
1891
- return 1.25;
1892
- }
1893
- else if (s.variant === 'expressive') {
1894
- return Hct.isYellow(s.getPalette('neutral').hue) ? 1.3 : 1.15;
1895
- }
1896
- else if (s.variant === 'vibrant') {
1897
- return 1.08;
1898
- }
1899
- return 1;
1900
- },
1901
- },
1902
- surfaceContainer: {
1903
- palette: (s) => s.getPalette('neutral'),
1904
- tone: (s) => {
1905
- if (s.isDark) {
1906
- return 9;
1907
- }
1908
- else {
1909
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1910
- return 96;
1911
- }
1912
- else if (s.variant === 'vibrant') {
1913
- return 92;
1914
- }
1915
- else {
1916
- return 94;
1917
- }
1918
- }
1919
- },
1920
- isBackground: true,
1921
- chromaMultiplier: (s) => {
1922
- if (s.variant === 'neutral') {
1923
- return 1.6;
1924
- }
1925
- else if (s.variant === 'tonalSpot') {
1926
- return 1.4;
1927
- }
1928
- else if (s.variant === 'expressive') {
1929
- return Hct.isYellow(s.getPalette('neutral').hue) ? 1.6 : 1.3;
1930
- }
1931
- else if (s.variant === 'vibrant') {
1932
- return 1.15;
1933
- }
1934
- return 1;
1935
- },
1936
- },
1937
- surfaceContainerHigh: {
1938
- palette: (s) => s.getPalette('neutral'),
1939
- tone: (s) => {
1940
- if (s.isDark) {
1941
- return 12;
1942
- }
1943
- else {
1944
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1945
- return 94;
1946
- }
1947
- else if (s.variant === 'vibrant') {
1948
- return 90;
1949
- }
1950
- else {
1951
- return 92;
1952
- }
1953
- }
1954
- },
1955
- isBackground: true,
1956
- chromaMultiplier: (s) => {
1957
- if (s.variant === 'neutral') {
1958
- return 1.9;
1959
- }
1960
- else if (s.variant === 'tonalSpot') {
1961
- return 1.5;
1962
- }
1963
- else if (s.variant === 'expressive') {
1964
- return Hct.isYellow(s.getPalette('neutral').hue) ? 1.95 : 1.45;
1965
- }
1966
- else if (s.variant === 'vibrant') {
1967
- return 1.22;
1968
- }
1969
- return 1;
1970
- },
1971
- },
1972
- surfaceContainerHighest: {
1973
- palette: (s) => s.getPalette('neutral'),
1974
- tone: (s) => {
1975
- if (s.isDark) {
1976
- return 15;
1977
- }
1978
- else {
1979
- if (Hct.isYellow(s.getPalette('neutral').hue)) {
1980
- return 92;
1981
- }
1982
- else if (s.variant === 'vibrant') {
1983
- return 88;
1984
- }
1985
- else {
1986
- return 90;
1987
- }
1988
- }
1989
- },
1990
- isBackground: true,
1991
- chromaMultiplier: (s) => {
1992
- if (s.variant === 'neutral') {
1993
- return 2.2;
1994
- }
1995
- else if (s.variant === 'tonalSpot') {
1996
- return 1.7;
1997
- }
1998
- else if (s.variant === 'expressive') {
1999
- return Hct.isYellow(s.getPalette('neutral').hue) ? 2.3 : 1.6;
2000
- }
2001
- else if (s.variant === 'vibrant') {
2002
- return 1.29;
2003
- }
2004
- else {
2005
- // default
2006
- return 1;
2007
- }
2008
- },
2009
- },
2010
- onSurface: {
2011
- palette: (s) => s.getPalette('neutral'),
2012
- tone: (s) => {
2013
- if (s.variant === 'vibrant') {
2014
- return tMaxC(s.getPalette('neutral'), 0, 100, 1.1);
2015
- }
2016
- else {
2017
- // For all other variants, the initial tone should be the default
2018
- // tone, which is the same as the background color.
2019
- return DynamicColor.getInitialToneFromBackground((s) => highestSurface(s, colorService))(s);
2020
- }
2021
- },
2022
- chromaMultiplier: (s) => {
2023
- if (s.variant === 'neutral') {
2024
- return 2.2;
2025
- }
2026
- else if (s.variant === 'tonalSpot') {
2027
- return 1.7;
2028
- }
2029
- else if (s.variant === 'expressive') {
2030
- return Hct.isYellow(s.getPalette('neutral').hue)
2031
- ? s.isDark
2032
- ? 3.0
2033
- : 2.3
2034
- : 1.6;
2035
- }
2036
- return 1;
2037
- },
2038
- background: (s) => highestSurface(s, colorService),
2039
- contrastCurve: (s) => (s.isDark ? getCurve(11) : getCurve(9)),
2040
- },
2041
- onSurfaceVariant: {
2042
- palette: (s) => s.getPalette('neutralVariant'),
2043
- chromaMultiplier: (s) => {
2044
- if (s.variant === 'neutral') {
2045
- return 2.2;
2046
- }
2047
- else if (s.variant === 'tonalSpot') {
2048
- return 1.7;
2049
- }
2050
- else if (s.variant === 'expressive') {
2051
- return Hct.isYellow(s.getPalette('neutral').hue)
2052
- ? s.isDark
2053
- ? 3.0
2054
- : 2.3
2055
- : 1.6;
2056
- }
2057
- return 1;
2058
- },
2059
- background: (s) => highestSurface(s, colorService),
2060
- contrastCurve: (s) => (s.isDark ? getCurve(6) : getCurve(4.5)),
2061
- },
2062
- outline: {
2063
- palette: (s) => s.getPalette('neutralVariant'),
2064
- chromaMultiplier: (s) => {
2065
- if (s.variant === 'neutral') {
2066
- return 2.2;
2067
- }
2068
- else if (s.variant === 'tonalSpot') {
2069
- return 1.7;
2070
- }
2071
- else if (s.variant === 'expressive') {
2072
- return Hct.isYellow(s.getPalette('neutral').hue)
2073
- ? s.isDark
2074
- ? 3.0
2075
- : 2.3
2076
- : 1.6;
2077
- }
2078
- return 1;
2079
- },
2080
- background: (s) => highestSurface(s, colorService),
2081
- contrastCurve: (s) => getCurve(3),
2082
- },
2083
- outlineVariant: {
2084
- palette: (s) => s.getPalette('neutralVariant'),
2085
- chromaMultiplier: (s) => {
2086
- if (s.variant === 'neutral') {
2087
- return 2.2;
2088
- }
2089
- else if (s.variant === 'tonalSpot') {
2090
- return 1.7;
2091
- }
2092
- else if (s.variant === 'expressive') {
2093
- return Hct.isYellow(s.getPalette('neutral').hue)
2094
- ? s.isDark
2095
- ? 3.0
2096
- : 2.3
2097
- : 1.6;
2098
- }
2099
- return 1;
2100
- },
2101
- background: (s) => highestSurface(s, colorService),
2102
- contrastCurve: (s) => getCurve(1.5),
2103
- },
2104
- inverseSurface: {
2105
- palette: (s) => s.getPalette('neutral'),
2106
- tone: (s) => (s.isDark ? 98 : 4),
2107
- isBackground: true,
2108
- },
2109
- inverseOnSurface: {
2110
- palette: (s) => s.getPalette('neutral'),
2111
- tone: (s) => (s.isDark ? 20 : 95),
2112
- background: (s) => colorService.getColor('inverseSurface').getMaterialColor(),
2113
- contrastCurve: (s) => getCurve(7),
2114
- },
2115
- ////////////////////////////////////////////////////////////////
2116
- // Primaries [P] //
2117
- ////////////////////////////////////////////////////////////////
2118
- primary: {
2119
- palette: (s) => s.getPalette('primary'),
2120
- tone: (s) => {
2121
- if (s.variant === 'neutral') {
2122
- return s.isDark ? 80 : 40;
2123
- }
2124
- else if (s.variant === 'tonalSpot') {
2125
- if (s.isDark) {
2126
- return 80;
2127
- }
2128
- else {
2129
- return tMaxC(s.getPalette('primary'));
2130
- }
2131
- }
2132
- else if (s.variant === 'expressive') {
2133
- return tMaxC(s.getPalette('primary'), 0, Hct.isYellow(s.getPalette('primary').hue)
2134
- ? 25
2135
- : Hct.isCyan(s.getPalette('primary').hue)
2136
- ? 88
2137
- : 98);
2138
- }
2139
- else {
2140
- return tMaxC(s.getPalette('primary'), 0, Hct.isCyan(s.getPalette('primary').hue) ? 88 : 98);
2141
- }
2142
- },
2143
- isBackground: true,
2144
- background: (s) => highestSurface(s, colorService),
2145
- contrastCurve: (s) => getCurve(4.5),
2146
- toneDeltaPair: (s) => new ToneDeltaPair(colorService.getColor('primaryContainer').getMaterialColor(), colorService.getColor('primary').getMaterialColor(), 5, 'relative_lighter', true, 'farther'),
2147
- },
2148
- primaryDim: {
2149
- palette: (s) => s.getPalette('primary'),
2150
- tone: (s) => {
2151
- if (s.variant === 'neutral') {
2152
- return 85;
2153
- }
2154
- else if (s.variant === 'tonalSpot') {
2155
- return tMaxC(s.getPalette('primary'), 0, 90);
2156
- }
2157
- else {
2158
- return tMaxC(s.getPalette('primary'));
2159
- }
2160
- },
2161
- isBackground: true,
2162
- background: (s) => getColor('surfaceContainerHigh'),
2163
- contrastCurve: (s) => getCurve(4.5),
2164
- toneDeltaPair: (s) => new ToneDeltaPair(colorService.getColor('primaryDim').getMaterialColor(), colorService.getColor('primary').getMaterialColor(), 5, 'darker', true, 'farther'),
2165
- },
2166
- onPrimary: {
2167
- palette: (s) => s.getPalette('primary'),
2168
- background: (s) => colorService.getColor('primary').getMaterialColor(),
2169
- contrastCurve: (s) => getCurve(6),
2170
- },
2171
- primaryContainer: {
2172
- palette: (s) => s.getPalette('primary'),
2173
- tone: (s) => {
2174
- if (s.variant === 'neutral') {
2175
- return s.isDark ? 30 : 90;
2176
- }
2177
- else if (s.variant === 'tonalSpot') {
2178
- return s.isDark
2179
- ? tMinC(s.getPalette('primary'), 35, 93)
2180
- : tMaxC(s.getPalette('primary'), 0, 90);
2181
- }
2182
- else if (s.variant === 'expressive') {
2183
- return s.isDark
2184
- ? tMaxC(s.getPalette('primary'), 30, 93)
2185
- : tMaxC(s.getPalette('primary'), 78, Hct.isCyan(s.getPalette('primary').hue) ? 88 : 90);
2186
- }
2187
- else {
2188
- // VIBRANT
2189
- return s.isDark
2190
- ? tMinC(s.getPalette('primary'), 66, 93)
2191
- : tMaxC(s.getPalette('primary'), 66, Hct.isCyan(s.getPalette('primary').hue) ? 88 : 93);
2192
- }
2193
- },
2194
- isBackground: true,
2195
- background: (s) => highestSurface(s, colorService),
2196
- toneDeltaPair: (s) => undefined,
2197
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2198
- },
2199
- onPrimaryContainer: {
2200
- palette: (s) => s.getPalette('primary'),
2201
- background: (s) => colorService.getColor('primaryContainer').getMaterialColor(),
2202
- contrastCurve: (s) => getCurve(6),
2203
- },
2204
- primaryFixed: {
2205
- palette: (s) => s.getPalette('primary'),
2206
- tone: (s) => {
2207
- const tempS = new Scheme(Object.assign(Object.assign({}, s.options), { isDark: false, contrastLevel: 0 }));
2208
- return getColor('primaryContainer').getTone(tempS);
2209
- },
2210
- isBackground: true,
2211
- background: (s) => highestSurface(s, colorService),
2212
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2213
- },
2214
- primaryFixedDim: {
2215
- palette: (s) => s.getPalette('primary'),
2216
- tone: (s) => colorService.getColor('primaryFixed').getMaterialColor().getTone(s),
2217
- isBackground: true,
2218
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('primaryFixedDim'), getColor('primaryFixed'), 5, 'darker', true, 'exact'),
2219
- },
2220
- onPrimaryFixed: {
2221
- palette: (s) => s.getPalette('primary'),
2222
- background: (s) => colorService.getColor('primaryFixedDim').getMaterialColor(),
2223
- contrastCurve: (s) => getCurve(7),
2224
- },
2225
- onPrimaryFixedVariant: {
2226
- palette: (s) => s.getPalette('primary'),
2227
- background: (s) => colorService.getColor('primaryFixedDim').getMaterialColor(),
2228
- contrastCurve: (s) => getCurve(4.5),
2229
- },
2230
- inversePrimary: {
2231
- palette: (s) => s.getPalette('primary'),
2232
- tone: (s) => tMaxC(s.getPalette('primary')),
2233
- background: (s) => colorService.getColor('inverseSurface').getMaterialColor(),
2234
- contrastCurve: (s) => getCurve(6),
2235
- },
2236
- ////////////////////////////////////////////////////////////////
2237
- // Secondaries [Q] //
2238
- ////////////////////////////////////////////////////////////////
2239
- secondary: {
2240
- palette: (s) => s.getPalette('secondary'),
2241
- tone: (s) => {
2242
- if (s.variant === 'neutral') {
2243
- return s.isDark
2244
- ? tMinC(s.getPalette('secondary'), 0, 98)
2245
- : tMaxC(s.getPalette('secondary'));
2246
- }
2247
- else if (s.variant === 'vibrant') {
2248
- return tMaxC(s.getPalette('secondary'), 0, s.isDark ? 90 : 98);
2249
- }
2250
- else {
2251
- // EXPRESSIVE and TONAL_SPOT
2252
- return s.isDark ? 80 : tMaxC(s.getPalette('secondary'));
2253
- }
2254
- },
2255
- isBackground: true,
2256
- background: (s) => highestSurface(s, colorService),
2257
- contrastCurve: (s) => getCurve(4.5),
2258
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('secondaryContainer'), getColor('secondary'), 5, 'relative_lighter', true, 'farther'),
2259
- },
2260
- secondaryDim: {
2261
- palette: (s) => s.getPalette('secondary'),
2262
- tone: (s) => {
2263
- if (s.variant === 'neutral') {
2264
- return 85;
2265
- }
2266
- else {
2267
- return tMaxC(s.getPalette('secondary'), 0, 90);
2268
- }
2269
- },
2270
- isBackground: true,
2271
- background: (s) => getColor('surfaceContainerHigh'),
2272
- contrastCurve: (s) => getCurve(4.5),
2273
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('secondaryDim'), getColor('secondary'), 5, 'darker', true, 'farther'),
2274
- },
2275
- onSecondary: {
2276
- palette: (s) => s.getPalette('secondary'),
2277
- background: (s) => getColor('secondary'),
2278
- contrastCurve: (s) => getCurve(6),
2279
- },
2280
- secondaryContainer: {
2281
- palette: (s) => s.getPalette('secondary'),
2282
- tone: (s) => {
2283
- if (s.variant === 'vibrant') {
2284
- return s.isDark
2285
- ? tMinC(s.getPalette('secondary'), 30, 40)
2286
- : tMaxC(s.getPalette('secondary'), 84, 90);
2287
- }
2288
- else if (s.variant === 'expressive') {
2289
- return s.isDark ? 15 : tMaxC(s.getPalette('secondary'), 90, 95);
2290
- }
2291
- else {
2292
- return s.isDark ? 25 : 90;
2293
- }
2294
- },
2295
- isBackground: true,
2296
- background: (s) => highestSurface(s, colorService),
2297
- toneDeltaPair: (s) => undefined,
2298
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2299
- },
2300
- onSecondaryContainer: {
2301
- palette: (s) => s.getPalette('secondary'),
2302
- background: (s) => getColor('secondaryContainer'),
2303
- contrastCurve: (s) => getCurve(6),
2304
- },
2305
- secondaryFixed: {
2306
- palette: (s) => s.getPalette('secondary'),
2307
- tone: (s) => {
2308
- const tempS = new Scheme(Object.assign(Object.assign({}, s.options), { isDark: false, contrastLevel: 0 }));
2309
- return getColor('secondaryContainer').getTone(tempS);
2310
- },
2311
- isBackground: true,
2312
- background: (s) => highestSurface(s, colorService),
2313
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2314
- },
2315
- secondaryFixedDim: {
2316
- palette: (s) => s.getPalette('secondary'),
2317
- tone: (s) => getColor('secondaryFixed').getTone(s),
2318
- isBackground: true,
2319
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('secondaryFixedDim'), getColor('secondaryFixed'), 5, 'darker', true, 'exact'),
2320
- },
2321
- onSecondaryFixed: {
2322
- palette: (s) => s.getPalette('secondary'),
2323
- background: (s) => getColor('secondaryFixedDim'),
2324
- contrastCurve: (s) => getCurve(7),
2325
- },
2326
- onSecondaryFixedVariant: {
2327
- palette: (s) => s.getPalette('secondary'),
2328
- background: (s) => getColor('secondaryFixedDim'),
2329
- contrastCurve: (s) => getCurve(4.5),
2330
- },
2331
- ////////////////////////////////////////////////////////////////
2332
- // Tertiaries [T] //
2333
- ////////////////////////////////////////////////////////////////
2334
- tertiary: {
2335
- palette: (s) => s.getPalette('tertiary'),
2336
- tone: (s) => {
2337
- if (s.variant === 'expressive' || s.variant === 'vibrant') {
2338
- return tMaxC(s.getPalette('tertiary'), 0, Hct.isCyan(s.getPalette('tertiary').hue) ? 88 : s.isDark ? 98 : 100);
2339
- }
2340
- else {
2341
- // NEUTRAL and TONAL_SPOT
2342
- return s.isDark
2343
- ? tMaxC(s.getPalette('tertiary'), 0, 98)
2344
- : tMaxC(s.getPalette('tertiary'));
2345
- }
2346
- },
2347
- isBackground: true,
2348
- background: (s) => highestSurface(s, colorService),
2349
- contrastCurve: (s) => getCurve(4.5),
2350
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('tertiaryContainer'), getColor('tertiary'), 5, 'relative_lighter', true, 'farther'),
2351
- },
2352
- tertiaryDim: {
2353
- palette: (s) => s.getPalette('tertiary'),
2354
- tone: (s) => {
2355
- if (s.variant === 'tonalSpot') {
2356
- return tMaxC(s.getPalette('tertiary'), 0, 90);
2357
- }
2358
- else {
2359
- return tMaxC(s.getPalette('tertiary'));
2360
- }
2361
- },
2362
- isBackground: true,
2363
- background: (s) => getColor('surfaceContainerHigh'),
2364
- contrastCurve: (s) => getCurve(4.5),
2365
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('tertiaryDim'), getColor('tertiary'), 5, 'darker', true, 'farther'),
2366
- },
2367
- onTertiary: {
2368
- palette: (s) => s.getPalette('tertiary'),
2369
- background: (s) => getColor('tertiary'),
2370
- contrastCurve: (s) => getCurve(6),
2371
- },
2372
- tertiaryContainer: {
2373
- palette: (s) => s.getPalette('tertiary'),
2374
- tone: (s) => {
2375
- if (s.variant === 'neutral') {
2376
- return s.isDark
2377
- ? tMaxC(s.getPalette('tertiary'), 0, 93)
2378
- : tMaxC(s.getPalette('tertiary'), 0, 96);
2379
- }
2380
- else if (s.variant === 'tonalSpot') {
2381
- return tMaxC(s.getPalette('tertiary'), 0, s.isDark ? 93 : 100);
2382
- }
2383
- else if (s.variant === 'expressive') {
2384
- return tMaxC(s.getPalette('tertiary'), 75, Hct.isCyan(s.getPalette('tertiary').hue) ? 88 : s.isDark ? 93 : 100);
2385
- }
2386
- else {
2387
- // VIBRANT
2388
- return s.isDark
2389
- ? tMaxC(s.getPalette('tertiary'), 0, 93)
2390
- : tMaxC(s.getPalette('tertiary'), 72, 100);
2391
- }
2392
- },
2393
- isBackground: true,
2394
- background: (s) => highestSurface(s, colorService),
2395
- toneDeltaPair: (s) => undefined,
2396
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2397
- },
2398
- onTertiaryContainer: {
2399
- palette: (s) => s.getPalette('tertiary'),
2400
- background: (s) => getColor('tertiaryContainer'),
2401
- contrastCurve: (s) => getCurve(6),
2402
- },
2403
- tertiaryFixed: {
2404
- palette: (s) => s.getPalette('tertiary'),
2405
- tone: (s) => {
2406
- const tempS = new Scheme(Object.assign(Object.assign({}, s.options), { isDark: false, contrastLevel: 0 }));
2407
- return getColor('tertiaryContainer').getTone(tempS);
2408
- },
2409
- isBackground: true,
2410
- background: (s) => highestSurface(s, colorService),
2411
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2412
- },
2413
- tertiaryFixedDim: {
2414
- palette: (s) => s.getPalette('tertiary'),
2415
- tone: (s) => getColor('tertiaryFixed').getTone(s),
2416
- isBackground: true,
2417
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('tertiaryFixedDim'), getColor('tertiaryFixed'), 5, 'darker', true, 'exact'),
2418
- },
2419
- onTertiaryFixed: {
2420
- palette: (s) => s.getPalette('tertiary'),
2421
- background: (s) => getColor('tertiaryFixedDim'),
2422
- contrastCurve: (s) => getCurve(7),
2423
- },
2424
- onTertiaryFixedVariant: {
2425
- palette: (s) => s.getPalette('tertiary'),
2426
- background: (s) => getColor('tertiaryFixedDim'),
2427
- contrastCurve: (s) => getCurve(4.5),
2428
- },
2429
- ////////////////////////////////////////////////////////////////
2430
- // Errors [E] //
2431
- ////////////////////////////////////////////////////////////////
2432
- error: {
2433
- palette: (s) => s.getPalette('error'),
2434
- tone: (s) => {
2435
- return s.isDark
2436
- ? tMinC(s.getPalette('error'), 0, 98)
2437
- : tMaxC(s.getPalette('error'));
2438
- },
2439
- isBackground: true,
2440
- background: (s) => highestSurface(s, colorService),
2441
- contrastCurve: (s) => getCurve(4.5),
2442
- toneDeltaPair: (s) => new ToneDeltaPair(colorService.getColor('errorContainer').getMaterialColor(), colorService.getColor('error').getMaterialColor(), 5, 'relative_lighter', true, 'farther'),
2443
- },
2444
- errorDim: {
2445
- palette: (s) => s.getPalette('error'),
2446
- tone: (s) => tMinC(s.getPalette('error')),
2447
- isBackground: true,
2448
- background: (s) => getColor('surfaceContainerHigh'),
2449
- contrastCurve: (s) => getCurve(4.5),
2450
- toneDeltaPair: (s) => new ToneDeltaPair(getColor('errorDim'), getColor('error'), 5, 'darker', true, 'farther'),
2451
- },
2452
- onError: {
2453
- palette: (s) => s.getPalette('error'),
2454
- background: (s) => colorService.getColor('error').getMaterialColor(),
2455
- contrastCurve: (s) => getCurve(6),
2456
- },
2457
- errorContainer: {
2458
- palette: (s) => s.getPalette('error'),
2459
- tone: (s) => {
2460
- return s.isDark
2461
- ? tMinC(s.getPalette('error'), 30, 93)
2462
- : tMaxC(s.getPalette('error'), 0, 90);
2463
- },
2464
- isBackground: true,
2465
- background: (s) => highestSurface(s, colorService),
2466
- toneDeltaPair: (s) => undefined,
2467
- contrastCurve: (s) => (s.contrastLevel > 0 ? getCurve(1.5) : undefined),
2468
- },
2469
- onErrorContainer: {
2470
- palette: (s) => s.getPalette('error'),
2471
- background: (s) => colorService.getColor('errorContainer').getMaterialColor(),
2472
- contrastCurve: (s) => getCurve(4.5),
2473
- },
2474
- /////////////////////////////////////////////////////////////////
2475
- // Remapped Colors //
2476
- /////////////////////////////////////////////////////////////////
2477
- surfaceVariant: {
2478
- alias: 'surfaceContainerHighest',
2479
- },
2480
- surfaceTint: {
2481
- alias: 'primary',
2482
- },
2483
- background: {
2484
- alias: 'surface',
2485
- },
2486
- onBackground: {
2487
- alias: 'onSurface',
2488
- },
2489
- };
2490
- return {
2491
- colors,
2492
- };
2493
- };
2494
-
2495
- class API {
2496
- constructor({ colorApi, themeApi, pluginApi, }) {
2497
- this.plugins = pluginApi;
2498
- this.colors = colorApi;
2499
- this.themes = themeApi;
2500
- }
2501
- }
2502
-
2503
- const AppModule = {
2504
- api: awilix.asClass(API).singleton(),
2505
- };
2506
-
2507
- function defineConfig(configObject) {
2508
- if (!configObject || typeof configObject !== 'object') {
2509
- throw new Error('The configuration is missing or not an object');
2510
- }
2511
- if (!('sourceColor' in configObject)) {
2512
- throw new Error('Invalid configuration');
2513
- }
2514
- return configObject;
2515
- }
2516
- class ConfigService {
2517
- constructor({ api }) {
2518
- this.configPath = './theme.config';
2519
- this.api = api;
2520
- }
2521
- loadConfig(config) {
2522
- const { sourceColor, contrastLevel = 0, isDark = false, variant = tonalSpotVariant, palettes, colors, useDefaultColors = true, plugins, } = config !== null && config !== void 0 ? config : this.getConfig();
2523
- this.api.themes.create({
2524
- contrastLevel: contrastLevel,
2525
- isDark: isDark,
2526
- sourceColorHex: sourceColor,
2527
- variant: variant,
2528
- });
2529
- if (palettes) {
2530
- Object.entries(palettes).forEach(([key, value]) => this.api.themes.addCustomPalette(key, value));
2531
- }
2532
- if (useDefaultColors) {
2533
- this.api.colors.addColors(defaultColors);
2534
- }
2535
- if (colors) {
2536
- this.api.colors.addColors(colors);
2537
- }
2538
- if (plugins) {
2539
- plugins.forEach((plugin) => {
2540
- this.api.plugins.addPlugin(plugin);
2541
- });
2542
- this.api.plugins.loadPlugins(this.api);
2543
- }
2544
- }
2545
- getConfig() {
2546
- if (typeof process !== 'undefined' &&
2547
- process.release &&
2548
- process.release.name === 'node') {
2549
- // eslint-disable-next-line @typescript-eslint/no-var-requires
2550
- const path = require('path');
2551
- // eslint-disable-next-line @typescript-eslint/no-var-requires
2552
- const fs = require('fs');
2553
- const base = path.resolve(this.configPath);
2554
- const extensions = ['.js', '.ts', '.jms', '.jcs'];
2555
- let configImport = null;
2556
- for (const ext of extensions) {
2557
- const path = base + ext;
2558
- if (fs.existsSync(path)) {
2559
- configImport = require(path);
2560
- break;
2561
- }
2562
- }
2563
- if (!configImport) {
2564
- throw new Error(`Configuration file not found, looked for: ${base} with extensions: ${extensions.join(', ')}`);
2565
- }
2566
- let config;
2567
- if ('default' in configImport) {
2568
- config = configImport.default;
2569
- }
2570
- else {
2571
- config = configImport;
2572
- }
2573
- return config;
2574
- }
2575
- else {
2576
- throw new Error('You must provide configuration object when using this library in a browser.');
2577
- }
2578
- }
2579
- }
2580
-
2581
- const ConfigModule = {
2582
- configService: awilix.asClass(ConfigService).singleton(),
2583
- };
2584
-
2585
- class PluginAbstract {
2586
- constructor(options) {
2587
- this.options = options;
2588
- }
2589
- init(api) {
2590
- this.pluginInstance = new this.pluginClass(api, this.options);
2591
- this.pluginInstance.onInit();
2592
- return this;
2593
- }
2594
- getInstance() {
2595
- if (!this.pluginInstance) {
2596
- throw new Error(`Plugin ${this.name} is not initialized`);
2597
- }
2598
- return this.pluginInstance;
2599
- }
2600
- }
2601
- class PluginImplAbstract {
2602
- constructor(api, options) {
2603
- this.api = api;
2604
- this.options = options;
2605
- this.onInit();
2606
- }
2607
- }
2608
-
2609
- class PluginApi {
2610
- constructor() {
2611
- this.plugins = new Map();
2612
- }
2613
- addPlugin(plugin) {
2614
- this.plugins.set(plugin.name, plugin);
2615
- }
2616
- loadPlugins(api) {
2617
- const plugins = new Map(this.plugins);
2618
- let size = 0;
2619
- do {
2620
- size = plugins.size;
2621
- plugins.forEach((plugin, key) => {
2622
- const deps = plugin.dependencies.filter((dep) => !this.plugins.has(new dep().name));
2623
- if (deps.length === 0) {
2624
- this.plugins.set(plugin.name, plugin.init(api));
2625
- plugins.delete(key);
2626
- }
2627
- });
2628
- } while (plugins.size != 0 && plugins.size < size);
2629
- if (plugins.size > 0)
2630
- throw new Error("Some plugins couldn't be loaded due to missing dependencies: " +
2631
- Array.from(plugins.keys()));
2632
- }
2633
- getPlugin(plugin) {
2634
- const pluginInstance = this.plugins.get(new plugin().name);
2635
- if (!pluginInstance)
2636
- throw new Error(`Plugin ${plugin.name} not found`);
2637
- return pluginInstance;
2638
- }
2639
- }
2640
-
2641
- const PluginModule = {
2642
- pluginApi: awilix.asClass(PluginApi).singleton(),
2643
- };
2644
-
2645
- function importContainer(container, services) {
2646
- services.forEach((service) => {
2647
- Object.entries(service).forEach(([name, serviceClass]) => {
2648
- container.register(name, serviceClass);
2649
- });
2650
- });
2651
- return container;
2652
- }
2653
- const AppContainer = awilix.createContainer({
2654
- injectionMode: awilix.InjectionMode.PROXY,
2655
- });
2656
- importContainer(AppContainer, [
2657
- ConfigModule,
2658
- AppModule,
2659
- PluginModule,
2660
- ColorModule,
2661
- ThemeModule,
2662
- ]);
2663
-
2664
- function bootstrap() {
2665
- return AppContainer.resolve('api');
2666
- }
2667
- function bootstrapFromConfig(args) {
2668
- const configService = AppContainer.resolve('configService');
2669
- if (args === null || args === void 0 ? void 0 : args.path)
2670
- configService.configPath = args.path;
2671
- configService.loadConfig(args === null || args === void 0 ? void 0 : args.config);
2672
- return AppContainer.resolve('api');
2673
- }
2674
-
2675
- exports.FontFamily = void 0;
2676
- (function (FontFamily) {
2677
- FontFamily["Expressive"] = "expressive";
2678
- FontFamily["Neutral"] = "neutral";
2679
- })(exports.FontFamily || (exports.FontFamily = {}));
2680
- class FontPlugin extends PluginAbstract {
2681
- constructor() {
2682
- super(...arguments);
2683
- this.dependencies = [];
2684
- this.name = 'font';
2685
- this.pluginClass = FontPluginImpl;
2686
- }
2687
- }
2688
- class FontPluginImpl extends PluginImplAbstract {
2689
- get fontFamily() {
2690
- if (!this._fontFamily)
2691
- throw new Error('Font family not initialized');
2692
- return this._fontFamily;
2693
- }
2694
- set fontFamily(value) {
2695
- this._fontFamily = value;
2696
- }
2697
- get fontStyles() {
2698
- if (!this._fontStyles)
2699
- throw new Error('Font styles not initialized');
2700
- return this._fontStyles;
2701
- }
2702
- set fontStyles(value) {
2703
- this._fontStyles = value;
2704
- }
2705
- getFonts() {
2706
- return {
2707
- fontStyles: this.fontStyles,
2708
- fontFamily: this.fontFamily,
2709
- };
2710
- }
2711
- onInit() {
2712
- var _a, _b, _c, _d, _e, _f;
2713
- this.fontFamily = {
2714
- expressive: (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.fontFamily) === null || _b === void 0 ? void 0 : _b.expressive) !== null && _c !== void 0 ? _c : [
2715
- 'Roboto',
2716
- 'sans-serif',
2717
- ],
2718
- neutral: (_f = (_e = (_d = this.options) === null || _d === void 0 ? void 0 : _d.fontFamily) === null || _e === void 0 ? void 0 : _e.neutral) !== null && _f !== void 0 ? _f : ['Roboto', 'sans-serif'],
2719
- };
2720
- this.fontStyles = {
2721
- display: {
2722
- large: {
2723
- fontWeight: 400,
2724
- fontSize: 3.5625,
2725
- lineHeight: 4,
2726
- letterSpacing: -0.015625,
2727
- fontFamily: exports.FontFamily.Expressive,
2728
- },
2729
- medium: {
2730
- fontWeight: 400,
2731
- fontSize: 2.8125,
2732
- lineHeight: 3.25,
2733
- fontFamily: exports.FontFamily.Expressive,
2734
- },
2735
- small: {
2736
- fontWeight: 400,
2737
- fontSize: 2.25,
2738
- lineHeight: 2.75,
2739
- fontFamily: exports.FontFamily.Expressive,
2740
- },
2741
- },
2742
- headline: {
2743
- large: {
2744
- fontWeight: 400,
2745
- fontSize: 2,
2746
- lineHeight: 2.5,
2747
- fontFamily: exports.FontFamily.Expressive,
2748
- },
2749
- medium: {
2750
- fontWeight: 400,
2751
- fontSize: 1.75,
2752
- lineHeight: 2.25,
2753
- fontFamily: exports.FontFamily.Expressive,
2754
- },
2755
- small: {
2756
- fontWeight: 400,
2757
- fontSize: 1.5,
2758
- lineHeight: 2,
2759
- fontFamily: exports.FontFamily.Expressive,
2760
- },
2761
- },
2762
- title: {
2763
- large: {
2764
- fontWeight: 400,
2765
- fontSize: 1.375,
2766
- lineHeight: 1.75,
2767
- fontFamily: exports.FontFamily.Neutral,
2768
- },
2769
- medium: {
2770
- fontWeight: 500,
2771
- fontSize: 1,
2772
- lineHeight: 1.5,
2773
- fontFamily: exports.FontFamily.Neutral,
2774
- letterSpacing: 0.009375,
2775
- },
2776
- small: {
2777
- fontWeight: 500,
2778
- fontSize: 0.875,
2779
- lineHeight: 1.25,
2780
- fontFamily: exports.FontFamily.Neutral,
2781
- letterSpacing: 0.00625,
2782
- },
2783
- },
2784
- label: {
2785
- large: {
2786
- fontWeight: 500,
2787
- fontSize: 0.875,
2788
- lineHeight: 1.25,
2789
- fontFamily: exports.FontFamily.Neutral,
2790
- letterSpacing: 0.00625,
2791
- },
2792
- medium: {
2793
- fontWeight: 500,
2794
- fontSize: 0.75,
2795
- lineHeight: 1,
2796
- fontFamily: exports.FontFamily.Neutral,
2797
- letterSpacing: 0.03125,
2798
- },
2799
- small: {
2800
- fontWeight: 500,
2801
- fontSize: 0.6875,
2802
- lineHeight: 1,
2803
- fontFamily: exports.FontFamily.Neutral,
2804
- letterSpacing: 0.03125,
2805
- },
2806
- },
2807
- body: {
2808
- large: {
2809
- fontWeight: 400,
2810
- fontSize: 1,
2811
- lineHeight: 1.5625,
2812
- fontFamily: exports.FontFamily.Neutral,
2813
- letterSpacing: 0.03125,
2814
- },
2815
- medium: {
2816
- fontWeight: 400,
2817
- fontSize: 0.875,
2818
- lineHeight: 1.25,
2819
- fontFamily: exports.FontFamily.Neutral,
2820
- letterSpacing: 0.015625,
2821
- },
2822
- small: {
2823
- fontWeight: 400,
2824
- fontSize: 0.75,
2825
- lineHeight: 1,
2826
- fontFamily: exports.FontFamily.Neutral,
2827
- letterSpacing: 0.025,
2828
- },
2829
- },
2830
- };
2831
- if (this.options && this.options.fontStyles)
2832
- Object.entries(this.options.fontStyles).forEach(([key, fontParam]) => {
2833
- const fontRole = key;
2834
- Object.entries(fontParam).forEach(([size, fontStyle]) => {
2835
- const fontSize = size;
2836
- if (fontStyle) {
2837
- this.fontStyles[fontRole][fontSize] = Object.assign(Object.assign({}, this.fontStyles[fontRole][fontSize]), fontStyle);
2838
- }
2839
- });
2840
- });
2841
- }
2842
- }
2843
-
2844
- exports.API = API;
2845
- exports.AppContainer = AppContainer;
2846
- exports.AppModule = AppModule;
2847
- exports.ColorApi = ColorApi;
2848
- exports.ColorManager = ColorManager;
2849
- exports.ColorModule = ColorModule;
2850
- exports.ConfigModule = ConfigModule;
2851
- exports.ConfigService = ConfigService;
2852
- exports.ConfigurableColor = ConfigurableColor;
2853
- exports.ContrastCurve = ContrastCurve;
2854
- exports.DynamicColor = DynamicColor;
2855
- exports.FontPlugin = FontPlugin;
2856
- exports.PluginAbstract = PluginAbstract;
2857
- exports.PluginApi = PluginApi;
2858
- exports.PluginImplAbstract = PluginImplAbstract;
2859
- exports.PluginModule = PluginModule;
2860
- exports.Scheme = Scheme;
2861
- exports.SchemeManager = SchemeManager;
2862
- exports.ThemeApi = ThemeApi;
2863
- exports.ThemeModule = ThemeModule;
2864
- exports.ToneDeltaPair = ToneDeltaPair;
2865
- exports.Variant = Variant;
2866
- exports.VariantManager = VariantManager;
2867
- exports.Variants = Variants;
2868
- exports.bootstrap = bootstrap;
2869
- exports.bootstrapFromConfig = bootstrapFromConfig;
2870
- exports.defaultColors = defaultColors;
2871
- exports.defineConfig = defineConfig;
2872
- exports.expressiveVariant = expressiveVariant;
2873
- exports.extendSpecVersion = extendSpecVersion;
2874
- exports.getPiecewiseHue = getPiecewiseHue;
2875
- exports.getRotatedHue = getRotatedHue;
2876
- exports.highestSurface = highestSurface;
2877
- exports.importContainer = importContainer;
2878
- exports.neutralVariant = neutralVariant;
2879
- exports.tonalSpotVariant = tonalSpotVariant;
2880
- exports.vibrantVariant = vibrantVariant;