Tunal/crates/tunal-theme/src/palette.rs
2026-11-18 09:33:29 -06:00

83 lines
2 KiB
Rust

//! Light and dark Material 3 schemes from a seed colour (`material-colors`, tonal spot).
use material_colors::{color::Rgb, scheme::Scheme, theme::ThemeBuilder};
/// Seed used when the desktop sets no accent (`0xRRGGBB`, a muted blue; D-022).
pub const FALLBACK_SEED: u32 = 0x3F6FB5;
macro_rules! palette {
($($role:ident),* $(,)?) => {
/// The M3 colour roles the UI uses, as opaque ARGB (`0xAARRGGBB`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Palette {
$(pub $role: u32,)*
}
impl From<&Scheme> for Palette {
fn from(scheme: &Scheme) -> Self {
Self { $($role: argb(scheme.$role),)* }
}
}
};
}
palette!(
primary,
on_primary,
primary_container,
on_primary_container,
inverse_primary,
secondary,
on_secondary,
secondary_container,
on_secondary_container,
tertiary,
on_tertiary,
tertiary_container,
on_tertiary_container,
error,
on_error,
error_container,
on_error_container,
surface,
surface_dim,
surface_bright,
surface_container_lowest,
surface_container_low,
surface_container,
surface_container_high,
surface_container_highest,
on_surface,
surface_variant,
on_surface_variant,
inverse_surface,
inverse_on_surface,
outline,
outline_variant,
shadow,
scrim,
);
/// Both schemes for one seed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Palettes {
pub light: Palette,
pub dark: Palette,
}
impl Palettes {
/// Tonal-spot schemes for `seed` (`0xRRGGBB`; any alpha byte is ignored).
pub fn from_seed(seed: u32) -> Self {
let schemes = ThemeBuilder::with_source(Rgb::from_u32(seed))
.build()
.schemes;
Self {
light: Palette::from(&schemes.light),
dark: Palette::from(&schemes.dark),
}
}
}
fn argb(rgb: Rgb) -> u32 {
0xFF00_0000 | u32::from(rgb.red) << 16 | u32::from(rgb.green) << 8 | u32::from(rgb.blue)
}