1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use material_color_utils::{
blend::blend_functions::Blend,
dynamic::color_spec::SpecVersion as McuSpec,
palettes::tonal_palette::TonalPalette,
scheme::{
SchemeContent, SchemeExpressive, SchemeFidelity, SchemeMonochrome, SchemeNeutral,
SchemeTonalSpot, SchemeVibrant,
},
utils::color_utils::Argb as McuArgb,
};
use crate::{
Appearance, Argb, ColorPair, ColorRole, ColorScheme, MaterialTheme, generated::dynamic_colors,
};
/// Resolved Material 3 four-role custom color group for one appearance.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CustomColorGroup {
/// Accent color for high-emphasis custom fills and outlines.
pub color: Argb,
/// Foreground color paired with [`Self::color`].
pub on_color: Argb,
/// Tonal container fill derived from the custom seed.
pub color_container: Argb,
/// Foreground color paired with [`Self::color_container`].
pub on_color_container: Argb,
}
impl CustomColorGroup {
/// Returns the primary `(on_color, color)` foreground/background pair.
pub const fn pair(self) -> ColorPair {
ColorPair {
foreground: self.on_color,
background: self.color,
}
}
/// Returns the tonal `(on_color_container, color_container)` pair.
pub const fn container_pair(self) -> ColorPair {
ColorPair {
foreground: self.on_color_container,
background: self.color_container,
}
}
}
/// Shifts `design`'s hue toward `source` using Material Color Utilities harmonization.
/// Input alpha channels are ignored and the returned color is opaque.
pub fn harmonize_color(design: Argb, source: Argb) -> Argb {
let design = McuArgb(design.to_u32() | 0xff00_0000);
let source = McuArgb(source.to_u32() | 0xff00_0000);
Argb::from_u32(Blend::harmonize(design, source).0)
}
/// The Material dynamic color palette to derive from a seed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DynamicVariant {
/// Default Material 3 balanced tonal palette with moderate chroma.
#[default]
TonalSpot,
/// High-chroma accent palette with bold secondary and tertiary hues.
Vibrant,
/// Detached, playful hue rotation for expressive Material 3 palettes.
Expressive,
/// Preserves the seed color closely in the primary palette.
Fidelity,
/// Derives accents faithful to source content colors.
Content,
/// Achromatic grayscale palette.
Monochrome,
/// Low-chroma near-neutral palette.
Neutral,
}
/// The requested Material Color Utilities specification.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SpecVersion {
/// Material Color Utilities 2021 specification (default).
#[default]
Spec2021,
/// Material Color Utilities 2025 specification (applies to `TonalSpot`, `Vibrant`, `Expressive`, and `Neutral`).
Spec2025,
}
impl ColorScheme {
/// Generates a scheme for an opaque sRGB seed. Contrast clamps to −1–1,
/// with NaN using 0. Fidelity, Content, and Monochrome resolve with Spec2021.
pub fn from_seed_with(
seed: Argb,
appearance: Appearance,
contrast: f64,
variant: DynamicVariant,
spec_version: SpecVersion,
) -> Self {
let seed = McuArgb(seed.to_u32() | 0xff00_0000);
let contrast = if contrast.is_nan() {
0.0
} else {
contrast.clamp(-1.0, 1.0)
};
let dark = appearance == Appearance::Dark;
let spec = match (spec_version, variant) {
(
SpecVersion::Spec2025,
DynamicVariant::TonalSpot
| DynamicVariant::Vibrant
| DynamicVariant::Expressive
| DynamicVariant::Neutral,
) => McuSpec::Spec2025,
_ => McuSpec::Spec2021,
};
macro_rules! build {
($scheme:ident) => {
$scheme::builder(seed, dark, contrast)
.spec_version(spec)
.build()
};
}
let mut scheme = match variant {
DynamicVariant::TonalSpot => build!(SchemeTonalSpot),
DynamicVariant::Vibrant => build!(SchemeVibrant),
DynamicVariant::Expressive => build!(SchemeExpressive),
DynamicVariant::Fidelity => build!(SchemeFidelity),
DynamicVariant::Content => build!(SchemeContent),
DynamicVariant::Monochrome => build!(SchemeMonochrome),
DynamicVariant::Neutral => build!(SchemeNeutral),
};
let mut colors = Self::baseline(appearance);
for (role, color) in dynamic_colors(&scheme) {
colors.set_color(role, color);
}
use ColorRole::*;
for (status_seed, roles) in [
(0xff00_61a4, [Info, OnInfo, InfoContainer, OnInfoContainer]),
(
0xff00_6c4e,
[Success, OnSuccess, SuccessContainer, OnSuccessContainer],
),
(
0xff97_4800,
[Warning, OnWarning, WarningContainer, OnWarningContainer],
),
] {
// Resolve each custom palette with the same tone and contrast
// relationships as Error, against the generated surface palette.
scheme.error_palette =
TonalPalette::from_hct(Blend::harmonize(McuArgb(status_seed), seed).into());
scheme.argb_cache.pin().clear();
scheme.hct_cache.pin().clear();
scheme.tone_cache.pin().clear();
let values = [
scheme.error(),
scheme.on_error(),
scheme.error_container(),
scheme.on_error_container(),
];
for (role, color) in roles.into_iter().zip(values) {
colors.set_color(role, Argb::from_u32(color.0));
}
}
colors
}
/// Resolves a Material 3 four-role [`CustomColorGroup`] for `seed` in this
/// scheme's appearance at default contrast (`0.0`).
///
/// When `harmonize` is `true`, `seed` is shifted toward this scheme's
/// [`ColorRole::Primary`] role before tone derivation.
pub fn custom_color(&self, seed: Argb, harmonize: bool) -> CustomColorGroup {
self.custom_color_with_contrast(seed, 0.0, harmonize)
}
/// Resolves a Material 3 four-role [`CustomColorGroup`] for `seed` in this
/// scheme's appearance and requested contrast (`-1.0..=1.0`, `NaN` -> `0.0`).
///
/// Uses MCU's error-role contrast curve against a Tonal Spot scheme seeded
/// from this scheme's [`ColorRole::Primary`] role.
pub fn custom_color_with_contrast(
&self,
seed: Argb,
contrast: f64,
harmonize: bool,
) -> CustomColorGroup {
let primary = McuArgb(self.color(ColorRole::Primary).to_u32() | 0xff00_0000);
let target = if harmonize {
McuArgb(harmonize_color(seed, self.color(ColorRole::Primary)).to_u32())
} else {
McuArgb(seed.to_u32() | 0xff00_0000)
};
let contrast = if contrast.is_nan() {
0.0
} else {
contrast.clamp(-1.0, 1.0)
};
let mut scheme =
SchemeTonalSpot::builder(primary, self.appearance() == Appearance::Dark, contrast)
.spec_version(McuSpec::Spec2021)
.build();
scheme.error_palette = TonalPalette::from_hct(target.into());
CustomColorGroup {
color: Argb::from_u32(scheme.error().0),
on_color: Argb::from_u32(scheme.on_error().0),
color_container: Argb::from_u32(scheme.error_container().0),
on_color_container: Argb::from_u32(scheme.on_error_container().0),
}
}
}
impl MaterialTheme {
/// Generates colors while keeping the default typography, motion, and layout.
pub fn from_seed_with(
seed: Argb,
appearance: Appearance,
contrast: f64,
variant: DynamicVariant,
spec_version: SpecVersion,
) -> Self {
Self {
colors: ColorScheme::from_seed_with(seed, appearance, contrast, variant, spec_version),
..Self::default()
}
}
}
/// Bundled palette recipes, with seed colors inspired by md3.io.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ThemePreset {
/// Standard Material 3 baseline purple palette (default).
#[default]
Baseline,
/// Tonal Spot dynamic scheme derived from the baseline seed.
TonalSpot,
/// Vibrant dynamic scheme derived from the baseline seed.
Vibrant,
/// Expressive dynamic scheme derived from the baseline seed.
Expressive,
/// Fidelity dynamic scheme derived from the baseline seed.
Fidelity,
/// Content dynamic scheme derived from the baseline seed.
Content,
/// Monochrome grayscale dynamic scheme.
Monochrome,
/// Low-chroma neutral dynamic scheme.
Neutral,
/// High-contrast (`contrast = 1.0`) Tonal Spot scheme derived from the baseline seed.
HighContrast,
/// Deep ocean blue Tonal Spot palette.
Ocean,
/// Forest green Tonal Spot palette.
Forest,
/// Bright spring green Tonal Spot palette.
Spring,
/// Warm sunset orange Tonal Spot palette.
Sunset,
/// Earthy desert ochre Tonal Spot palette.
Desert,
/// Clear summer sky blue Tonal Spot palette.
Summer,
/// Warm brown and beige Tonal Spot palette.
BrownBeige,
/// Muted sage and ivory Tonal Spot palette.
SageIvory,
/// Deep teal and caramel Tonal Spot palette.
TealCaramel,
}
impl ThemePreset {
/// All bundled presets in display order.
pub const ALL: [Self; 18] = [
Self::Baseline,
Self::TonalSpot,
Self::Vibrant,
Self::Expressive,
Self::Fidelity,
Self::Content,
Self::Monochrome,
Self::Neutral,
Self::HighContrast,
Self::Ocean,
Self::Forest,
Self::Spring,
Self::Sunset,
Self::Desert,
Self::Summer,
Self::BrownBeige,
Self::SageIvory,
Self::TealCaramel,
];
/// Resolves the preset for the requested appearance.
pub fn colors(self, appearance: Appearance) -> ColorScheme {
use DynamicVariant as Variant;
const BASELINE_SEED: u32 = 0xff67_50a4;
let (seed, variant) = match self {
Self::Baseline => return ColorScheme::baseline(appearance),
Self::TonalSpot | Self::HighContrast => (BASELINE_SEED, Variant::TonalSpot),
Self::Vibrant => (BASELINE_SEED, Variant::Vibrant),
Self::Expressive => (BASELINE_SEED, Variant::Expressive),
Self::Fidelity => (BASELINE_SEED, Variant::Fidelity),
Self::Content => (BASELINE_SEED, Variant::Content),
Self::Monochrome => (BASELINE_SEED, Variant::Monochrome),
Self::Neutral => (BASELINE_SEED, Variant::Neutral),
Self::Ocean => (0xff00_6c9c, Variant::TonalSpot),
Self::Forest => (0xff4b_5d3a, Variant::TonalSpot),
Self::Spring => (0xff76_d275, Variant::TonalSpot),
Self::Sunset => (0xffd3_5400, Variant::TonalSpot),
Self::Desert => (0xff9a_7a3e, Variant::TonalSpot),
Self::Summer => (0xff41_96cb, Variant::TonalSpot),
Self::BrownBeige => (0xff3e_2723, Variant::TonalSpot),
Self::SageIvory => (0xff7d_8c73, Variant::TonalSpot),
Self::TealCaramel => (0xff2b_4d56, Variant::TonalSpot),
};
ColorScheme::from_seed_with(
Argb::from_u32(seed),
appearance,
f64::from(self == Self::HighContrast),
variant,
SpecVersion::Spec2021,
)
}
/// Builds a complete [`MaterialTheme`] using this preset's colors and default typography, motion, and layout.
pub fn theme(self, appearance: Appearance) -> MaterialTheme {
MaterialTheme {
colors: self.colors(appearance),
..MaterialTheme::default()
}
}
}