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

62 lines
1.8 KiB
Rust

//! XDG Settings portal access and Material colour scheme generation.
mod palette;
mod portal;
pub use palette::{FALLBACK_SEED, Palette, Palettes};
pub use portal::{read, watch};
/// Accent colour as the portal reports it: sRGB components in `0.0..=1.0`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Accent {
pub red: f64,
pub green: f64,
pub blue: f64,
}
impl Accent {
/// `None` when a component is out of range (the portal's "no accent").
pub fn new(red: f64, green: f64, blue: f64) -> Option<Self> {
let valid = |c: f64| (0.0..=1.0).contains(&c);
(valid(red) && valid(green) && valid(blue)).then_some(Self { red, green, blue })
}
/// The accent as `0xRRGGBB`.
pub fn rgb(&self) -> u32 {
let byte = |c: f64| (c * 255.0).round() as u32;
byte(self.red) << 16 | byte(self.green) << 8 | byte(self.blue)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorScheme {
#[default]
Light,
Dark,
}
/// Desktop appearance settings read from the portal.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Appearance {
/// `None`: no accent set; use [`FALLBACK_SEED`].
pub accent: Option<Accent>,
pub scheme: ColorScheme,
/// `org.gnome.desktop.interface` `enable-animations`; `None` when the portal does not offer it.
pub animations: Option<bool>,
}
impl Appearance {
/// The seed colour for the Material schemes (`0xRRGGBB`).
pub fn seed(&self) -> u32 {
self.accent.map_or(FALLBACK_SEED, |a| a.rgb())
}
/// The palette for the current seed and colour scheme.
pub fn palette(&self) -> Palette {
let palettes = Palettes::from_seed(self.seed());
match self.scheme {
ColorScheme::Light => palettes.light,
ColorScheme::Dark => palettes.dark,
}
}
}