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