use crate::render::Color;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Theme {
pub background: Color,
pub surface: Color,
pub ink: Color,
pub muted: Color,
pub accent: Color,
pub on_accent: Color,
pub selected: Color,
pub track: Color,
pub outline: Color,
pub divider: Color,
pub inverse: Color,
pub on_inverse: Color,
}
impl Default for Theme {
fn default() -> Self {
Theme {
background: Color::WHITE,
surface: Color::WHITE,
ink: Color::hex(0x1d2030),
muted: Color::hex(0x6b7080),
accent: Color::hex(0x3b6fe0),
on_accent: Color::WHITE,
selected: Color::hex(0xe8eefc),
track: Color::hex(0xd5d8e0),
outline: Color::hex(0x9a9fb0),
divider: Color::hex(0xdfe1e8),
inverse: Color::hex(0x323232),
on_inverse: Color::WHITE,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Role {
Background,
Surface,
Ink,
Muted,
Accent,
OnAccent,
Selected,
Track,
Outline,
Divider,
Inverse,
OnInverse,
}
impl Theme {
pub fn color(&self, role: Role) -> Color {
match role {
Role::Background => self.background,
Role::Surface => self.surface,
Role::Ink => self.ink,
Role::Muted => self.muted,
Role::Accent => self.accent,
Role::OnAccent => self.on_accent,
Role::Selected => self.selected,
Role::Track => self.track,
Role::Outline => self.outline,
Role::Divider => self.divider,
Role::Inverse => self.inverse,
Role::OnInverse => self.on_inverse,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Ink {
Fixed(Color),
Role(Role),
#[default]
Content,
}
impl From<Color> for Ink {
fn from(c: Color) -> Self {
Ink::Fixed(c)
}
}
impl From<Role> for Ink {
fn from(r: Role) -> Self {
Ink::Role(r)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn contrast(a: Color, b: Color) -> f32 {
let lin = |v: f32| match v <= 0.04045 {
true => v / 12.92,
false => ((v + 0.055) / 1.055).powf(2.4),
};
let l = |c: Color| 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b);
let (a, b) = (l(a), l(b));
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}
#[test]
fn default_text_roles_reach_wcag_aa() {
let t = Theme::default();
for (name, fg, bg) in [
("ink/background", t.ink, t.background),
("ink/surface", t.ink, t.surface),
("muted/surface", t.muted, t.surface),
("on_accent/accent", t.on_accent, t.accent),
("on_inverse/inverse", t.on_inverse, t.inverse),
] {
assert!(contrast(fg, bg) >= 4.5, "{name}: {}", contrast(fg, bg));
}
}
}