@udixio/theme 1.2.0 → 1.3.1

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 (38) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/bin.cjs +1 -1
  3. package/dist/bin.js +1 -1
  4. package/dist/browser.cjs +3 -4
  5. package/dist/browser.js +11 -12
  6. package/dist/color/color.manager.d.ts.map +1 -1
  7. package/dist/color/default-color.d.ts.map +1 -1
  8. package/dist/{define-config-CGG8l3fb.js → define-config-BasMdCqD.js} +32 -2
  9. package/dist/{define-config-BFo7Sy7c.cjs → define-config-CKSsLMnc.cjs} +32 -2
  10. package/dist/{load-from-path-CFpw2P_Y.js → load-from-path-Bo1kCfh9.js} +1 -1
  11. package/dist/{load-from-path-BvdfXUD0.cjs → load-from-path-DoZnR1-p.cjs} +2 -2
  12. package/dist/loader/loader.d.ts.map +1 -1
  13. package/dist/{loader-C_BIrsO2.js → loader-C8_TyOuS.js} +132 -119
  14. package/dist/{loader-TksB6_mM.cjs → loader-R7hccp8_.cjs} +124 -111
  15. package/dist/material-color-utilities/dynamic_color.d.ts +9 -15
  16. package/dist/material-color-utilities/dynamic_color.d.ts.map +1 -1
  17. package/dist/material-color-utilities/toneDeltaPair.d.ts +9 -2
  18. package/dist/material-color-utilities/toneDeltaPair.d.ts.map +1 -1
  19. package/dist/node.cjs +4 -5
  20. package/dist/node.js +13 -14
  21. package/dist/theme/scheme.d.ts +1 -1
  22. package/dist/theme/scheme.d.ts.map +1 -1
  23. package/dist/theme/theme.api.d.ts +1 -0
  24. package/dist/theme/theme.api.d.ts.map +1 -1
  25. package/dist/theme/variants/fidelity.variant.d.ts +3 -0
  26. package/dist/theme/variants/fidelity.variant.d.ts.map +1 -0
  27. package/dist/theme/variants/index.d.ts +1 -0
  28. package/dist/theme/variants/index.d.ts.map +1 -1
  29. package/package.json +1 -1
  30. package/src/color/color.manager.ts +10 -8
  31. package/src/color/default-color.ts +82 -34
  32. package/src/loader/loader.ts +1 -5
  33. package/src/material-color-utilities/dynamic_color.ts +18 -120
  34. package/src/material-color-utilities/toneDeltaPair.ts +91 -2
  35. package/src/theme/scheme.ts +7 -1
  36. package/src/theme/theme.api.ts +4 -0
  37. package/src/theme/variants/fidelity.variant.ts +38 -0
  38. package/src/theme/variants/index.ts +2 -0
@@ -15,7 +15,9 @@
15
15
  * limitations under the License.
16
16
  */
17
17
 
18
- import { DynamicColor } from './dynamic_color';
18
+ import { AdjustTone, DynamicColor } from './dynamic_color';
19
+ import { clampDouble, Contrast } from '@material/material-color-utilities';
20
+ import { Scheme } from '../theme';
19
21
 
20
22
  /**
21
23
  * Describes the different in tone between colors.
@@ -43,7 +45,7 @@ export type DeltaConstraint = 'exact' | 'nearer' | 'farther';
43
45
  * designers want tonal distance, literally contrast, between two colors that
44
46
  * don't have a background / foreground relationship or a contrast guarantee.
45
47
  */
46
- export class ToneDeltaPair {
48
+ class ToneDeltaPair {
47
49
  /**
48
50
  * Documents a constraint in tone distance between two DynamicColors.
49
51
  *
@@ -79,4 +81,91 @@ export class ToneDeltaPair {
79
81
  ) {
80
82
  this.constraint = constraint ?? 'exact';
81
83
  }
84
+
85
+ adjustedTone({
86
+ scheme,
87
+ dynamicColor,
88
+ }: {
89
+ scheme: Scheme;
90
+ dynamicColor: DynamicColor;
91
+ }) {
92
+ const roleA = this.roleA;
93
+ const roleB = this.roleB;
94
+ const polarity = this.polarity;
95
+ const constraint = this.constraint;
96
+ const absoluteDelta =
97
+ polarity === 'darker' ||
98
+ (polarity === 'relative_lighter' && scheme.isDark) ||
99
+ (polarity === 'relative_darker' && !scheme.isDark)
100
+ ? -this.delta
101
+ : this.delta;
102
+
103
+ const amRoleA = dynamicColor.name === roleA.name;
104
+ const selfRole = amRoleA ? roleA : roleB;
105
+ const refRole = amRoleA ? roleB : roleA;
106
+ let selfTone = selfRole.tone(scheme);
107
+ const refTone = refRole.getTone(scheme);
108
+ const relativeDelta = absoluteDelta * (amRoleA ? 1 : -1);
109
+
110
+ if (constraint === 'exact') {
111
+ selfTone = clampDouble(0, 100, refTone + relativeDelta);
112
+ } else if (constraint === 'nearer') {
113
+ if (relativeDelta > 0) {
114
+ selfTone = clampDouble(
115
+ 0,
116
+ 100,
117
+ clampDouble(refTone, refTone + relativeDelta, selfTone),
118
+ );
119
+ } else {
120
+ selfTone = clampDouble(
121
+ 0,
122
+ 100,
123
+ clampDouble(refTone + relativeDelta, refTone, selfTone),
124
+ );
125
+ }
126
+ } else if (constraint === 'farther') {
127
+ if (relativeDelta > 0) {
128
+ selfTone = clampDouble(refTone + relativeDelta, 100, selfTone);
129
+ } else {
130
+ selfTone = clampDouble(0, refTone + relativeDelta, selfTone);
131
+ }
132
+ }
133
+
134
+ if (dynamicColor.background && dynamicColor.contrastCurve) {
135
+ const background = dynamicColor.background(scheme);
136
+ const contrastCurve = dynamicColor.contrastCurve(scheme);
137
+ if (background && contrastCurve) {
138
+ // Adjust the tones for contrast, if background and contrast curve
139
+ // are defined.
140
+ const bgTone = background.getTone(scheme);
141
+ const selfContrast = contrastCurve.get(scheme.contrastLevel);
142
+ selfTone =
143
+ Contrast.ratioOfTones(bgTone, selfTone) >= selfContrast &&
144
+ scheme.contrastLevel >= 0
145
+ ? selfTone
146
+ : DynamicColor.foregroundTone(bgTone, selfContrast);
147
+ }
148
+ }
149
+
150
+ // This can avoid the awkward tones for background colors including the
151
+ // access fixed colors. Accent fixed dim colors should not be adjusted.
152
+ if (
153
+ dynamicColor.isBackground &&
154
+ !dynamicColor.name.endsWith('_fixed_dim')
155
+ ) {
156
+ if (selfTone >= 57) {
157
+ selfTone = clampDouble(65, 100, selfTone);
158
+ } else {
159
+ selfTone = clampDouble(0, 49, selfTone);
160
+ }
161
+ }
162
+
163
+ return selfTone;
164
+ }
82
165
  }
166
+
167
+ export const toneDeltaPair = (
168
+ ...params: ConstructorParameters<typeof ToneDeltaPair>
169
+ ): AdjustTone => {
170
+ return (args) => new ToneDeltaPair(...params).adjustedTone(args);
171
+ };
@@ -13,7 +13,13 @@ export interface SchemeOptions {
13
13
  export class Scheme {
14
14
  constructor(readonly options: SchemeOptions) {}
15
15
 
16
- get variant(): 'expressive' | 'neutral' | 'tonalSpot' | 'vibrant' | string {
16
+ get variant():
17
+ | 'expressive'
18
+ | 'neutral'
19
+ | 'tonalSpot'
20
+ | 'vibrant'
21
+ | 'fidelity'
22
+ | string {
17
23
  return this.options.variant.name;
18
24
  }
19
25
 
@@ -52,6 +52,10 @@ export class ThemeApi {
52
52
  // return this.theme();
53
53
  // }
54
54
 
55
+ get sourceColorHex() {
56
+ return this.schemeManager.get().sourceColorHct;
57
+ }
58
+
55
59
  create(options: ThemeOptions & { variant: Variant }) {
56
60
  this.schemeManager.createOrUpdate({
57
61
  ...options,
@@ -0,0 +1,38 @@
1
+ import { getPiecewiseHue, getRotatedHue, Variant } from '../variant';
2
+ import { TonalPalette } from '@material/material-color-utilities';
3
+
4
+ export const fidelityVariant: Variant = {
5
+ name: 'fidelity',
6
+ palettes: {
7
+ primary: ({ sourceColorHct, isDark }) =>
8
+ TonalPalette.fromHueAndChroma(sourceColorHct.hue, sourceColorHct.chroma),
9
+ secondary: ({ sourceColorHct }) =>
10
+ TonalPalette.fromHueAndChroma(
11
+ sourceColorHct.hue,
12
+ sourceColorHct.chroma * 0.5,
13
+ ),
14
+ tertiary: ({ sourceColorHct }) =>
15
+ TonalPalette.fromHueAndChroma(
16
+ getRotatedHue(
17
+ sourceColorHct,
18
+ [0, 20, 71, 161, 333, 360],
19
+ [-40, 48, -32, 40, -32],
20
+ ),
21
+ sourceColorHct.chroma,
22
+ ),
23
+ neutral: ({ sourceColorHct }) =>
24
+ TonalPalette.fromHueAndChroma(sourceColorHct.hue, 5),
25
+ neutralVariant: ({ sourceColorHct }) =>
26
+ TonalPalette.fromHueAndChroma(sourceColorHct.hue, 5 * 1.7),
27
+ error: ({ sourceColorHct }) => {
28
+ const errorHue = getPiecewiseHue(
29
+ sourceColorHct,
30
+ [0, 3, 13, 23, 33, 43, 153, 273, 360],
31
+ [12, 22, 32, 12, 22, 32, 22, 12],
32
+ );
33
+ return TonalPalette.fromHueAndChroma(errorHue, 60);
34
+ },
35
+ },
36
+ customPalettes: ({ colorHct , sourceColorHct}) =>
37
+ TonalPalette.fromHueAndChroma(colorHct.hue, sourceColorHct.chroma),
38
+ };
@@ -2,6 +2,7 @@ import { expressiveVariant } from './expressive.variant';
2
2
  import { neutralVariant } from './neutral.variant';
3
3
  import { tonalSpotVariant } from './tonal-spot.variant';
4
4
  import { vibrantVariant } from './vibrant.variant';
5
+ import { fidelityVariant } from './fidelity.variant';
5
6
 
6
7
  export * from './tonal-spot.variant';
7
8
  export * from './vibrant.variant';
@@ -13,4 +14,5 @@ export const Variants = {
13
14
  Neutral: neutralVariant,
14
15
  TonalSpot: tonalSpotVariant,
15
16
  Vibrant: vibrantVariant,
17
+ Fidelity: fidelityVariant,
16
18
  };