#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rgb(pub u8, pub u8, pub u8);
impl Rgb {
pub const fn hex(v: u32) -> Rgb {
Rgb((v >> 16) as u8, (v >> 8) as u8, v as u8)
}
#[cfg(test)]
pub fn luminance(self) -> f64 {
let lin = |c: u8| {
let c = f64::from(c) / 255.0;
if c <= 0.04045 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * lin(self.0) + 0.7152 * lin(self.1) + 0.0722 * lin(self.2)
}
#[cfg(test)]
pub fn contrast(self, other: Rgb) -> f64 {
let (a, b) = (self.luminance(), other.luminance());
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Colors {
pub bg: Rgb,
pub bg_sidebar: Rgb,
pub bg_raised: Rgb,
pub hairline: Rgb,
pub border_control: Rgb,
pub text: Rgb,
pub text_secondary: Rgb,
pub text_disabled: Rgb,
pub accent: Rgb,
pub on_accent: Rgb,
pub danger: Rgb,
}
pub const LIGHT: Colors = Colors {
bg: Rgb::hex(0xF2EFE6),
bg_sidebar: Rgb::hex(0xE9E5DA),
bg_raised: Rgb::hex(0xF8F6F0),
hairline: Rgb::hex(0xD3CEC1),
border_control: Rgb::hex(0x847E72),
text: Rgb::hex(0x1C1A17),
text_secondary: Rgb::hex(0x5B564D),
text_disabled: Rgb::hex(0x9C968A),
accent: Rgb::hex(0xB4481C),
on_accent: Rgb::hex(0xFFFFFF),
danger: Rgb::hex(0xA3341F),
};
pub const DARK: Colors = Colors {
bg: Rgb::hex(0x1A1917),
bg_sidebar: Rgb::hex(0x151412),
bg_raised: Rgb::hex(0x211F1C),
hairline: Rgb::hex(0x35322D),
border_control: Rgb::hex(0x77716A),
text: Rgb::hex(0xE9E5D7),
text_secondary: Rgb::hex(0xA8A296),
text_disabled: Rgb::hex(0x5F5A52),
accent: Rgb::hex(0xFF9646),
on_accent: Rgb::hex(0x1A1917),
danger: Rgb::hex(0xFF8A75),
};
pub mod space {
pub const S1: f32 = 4.0;
pub const S2: f32 = 8.0;
pub const S3: f32 = 12.0;
pub const S4: f32 = 16.0;
pub const S5: f32 = 24.0;
pub const S6: f32 = 32.0;
pub const S7: f32 = 48.0;
}
pub mod radius {
pub const CONTROL: f32 = 6.0;
pub const PANEL: f32 = 10.0;
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TypeSize {
pub size: f32,
pub line: f32,
}
pub mod text {
use super::TypeSize;
pub const SMALL: TypeSize = TypeSize {
size: 12.0,
line: 16.0,
};
pub const BODY: TypeSize = TypeSize {
size: 14.0,
line: 20.0,
};
pub const LARGE: TypeSize = TypeSize {
size: 16.0,
line: 22.0,
};
pub const TITLE: TypeSize = TypeSize {
size: 20.0,
line: 26.0,
};
}
pub mod size {
pub const CONTROL: f32 = 28.0;
const _: () = assert!(CONTROL >= 24.0, "WCAG 2.5.8 target size");
pub const BUTTON_PRIMARY: f32 = 32.0;
pub const SIDEBAR: f32 = 200.0;
pub const PAGE_MAX: f32 = 640.0;
pub const HAIRLINE: f32 = 1.0;
pub const FOCUS_RING: f32 = 2.0;
pub const FOCUS_OFFSET: f32 = 2.0;
}
pub mod motion {
pub const BASE: u32 = 160;
}
#[cfg(test)]
mod tests {
use super::*;
fn check(name: &str, c: &Colors) {
let surfaces = [
("bg", c.bg),
("sidebar", c.bg_sidebar),
("raised", c.bg_raised),
];
for (s, bg) in surfaces {
for (f, fg) in [("text", c.text), ("text_secondary", c.text_secondary)] {
let r = fg.contrast(bg);
assert!(r >= 4.5, "{name}: {f} on {s} is {r:.2}:1, needs 4.5");
}
for (f, fg) in [("border_control", c.border_control), ("accent", c.accent)] {
let r = fg.contrast(bg);
assert!(r >= 3.0, "{name}: {f} on {s} is {r:.2}:1, needs 3");
}
}
for (s, bg) in [("bg", c.bg), ("raised", c.bg_raised)] {
for (f, fg) in [("accent", c.accent), ("danger", c.danger)] {
let r = fg.contrast(bg);
assert!(r >= 4.5, "{name}: {f} text on {s} is {r:.2}:1, needs 4.5");
}
}
let r = c.on_accent.contrast(c.accent);
assert!(r >= 4.5, "{name}: on_accent is {r:.2}:1, needs 4.5");
assert!(c.text_disabled.contrast(c.bg) >= 2.0);
assert!(c.text_disabled.contrast(c.text_secondary) >= 1.5);
assert!(c.hairline.contrast(c.bg) < c.border_control.contrast(c.bg));
}
#[test]
fn colours_meet_wcag_aa() {
check("light", &LIGHT);
check("dark", &DARK);
}
#[test]
fn known_contrast_values() {
assert!((Rgb::hex(0x000000).contrast(Rgb::hex(0xFFFFFF)) - 21.0).abs() < 1e-9);
assert!((Rgb::hex(0x777777).contrast(Rgb::hex(0xFFFFFF)) - 4.48).abs() < 0.01);
}
#[test]
fn scales_are_on_the_grid() {
for s in [
space::S1,
space::S2,
space::S3,
space::S4,
space::S5,
space::S6,
space::S7,
] {
assert_eq!(s % 4.0, 0.0);
}
for t in [text::SMALL, text::BODY, text::LARGE, text::TITLE] {
assert_eq!(t.line % 2.0, 0.0);
assert!(t.line / t.size >= 1.2 && t.line / t.size <= 1.45, "{t:?}");
}
}
}