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