#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rgb {
pub red: u8,
pub green: u8,
pub blue: u8,
}
impl Rgb {
#[must_use]
pub const fn new(red: u8, green: u8, blue: u8) -> Self {
Self { red, green, blue }
}
#[must_use]
pub fn from_hex(value: &str) -> Option<Self> {
let digits = match value.strip_prefix('#') {
Some(rest) => rest,
None => value,
};
let bytes = digits.as_bytes();
if bytes.len() != 6 {
return None;
}
let red = hex_byte(bytes[0], bytes[1])?;
let green = hex_byte(bytes[2], bytes[3])?;
let blue = hex_byte(bytes[4], bytes[5])?;
Some(Self::new(red, green, blue))
}
#[must_use]
pub fn hex(self) -> String {
format!(
"#{}",
lgwks_std::hex::encode([self.red, self.green, self.blue])
)
}
#[must_use]
pub fn css(self) -> String {
format!("rgb({}, {}, {})", self.red, self.green, self.blue)
}
#[must_use]
pub fn luminance(self) -> f64 {
let channel = |value: u8| {
let value = f64::from(value) / 255.0;
if value <= 0.039_28 {
value / 12.92
} else {
((value + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * channel(self.red) + 0.7152 * channel(self.green) + 0.0722 * channel(self.blue)
}
#[must_use]
pub fn contrast(self, other: Self) -> f64 {
let (high, low) = {
let first = self.luminance();
let second = other.luminance();
if first >= second {
(first, second)
} else {
(second, first)
}
};
(high + 0.05) / (low + 0.05)
}
}
fn hex_digit(byte: u8) -> Option<u8> {
match byte {
b'0'..=b'9' => Some(byte.wrapping_sub(b'0')),
b'a'..=b'f' => Some(byte.wrapping_sub(b'a').wrapping_add(10)),
b'A'..=b'F' => Some(byte.wrapping_sub(b'A').wrapping_add(10)),
_ => None,
}
}
fn hex_byte(high: u8, low: u8) -> Option<u8> {
let high = hex_digit(high)?;
let low = hex_digit(low)?;
Some(high.wrapping_mul(16).wrapping_add(low))
}
pub mod primitive {
use super::Rgb;
pub const INK_900: Rgb = Rgb::new(0x22, 0x26, 0x20);
pub const INK_700: Rgb = Rgb::new(0x45, 0x44, 0x3e);
pub const INK_500: Rgb = Rgb::new(0x62, 0x60, 0x5a);
pub const CHALK_50: Rgb = Rgb::new(0xfa, 0xf7, 0xee);
pub const CHALK_100: Rgb = Rgb::new(0xf5, 0xf1, 0xe8);
pub const CHALK_300: Rgb = Rgb::new(0xe5, 0xe0, 0xd6);
pub const CLAY_400: Rgb = Rgb::new(0xe0, 0x58, 0x3a);
pub const CLAY_500: Rgb = Rgb::new(0xc8, 0x40, 0x25);
pub const CLAY_700: Rgb = Rgb::new(0x9a, 0x3b, 0x22);
pub const GRAPHITE_950: Rgb = Rgb::new(0x10, 0x12, 0x10);
pub const GRAPHITE_900: Rgb = Rgb::new(0x16, 0x19, 0x15);
pub const GRAPHITE_800: Rgb = Rgb::new(0x1f, 0x23, 0x1e);
pub const GRAPHITE_600: Rgb = Rgb::new(0x3a, 0x3f, 0x37);
pub const CLAY_300: Rgb = Rgb::new(0xee, 0x6f, 0x50);
pub const PINE_600: Rgb = Rgb::new(0x2e, 0x6b, 0x45);
pub const AMBER_400: Rgb = Rgb::new(0xf2, 0xb1, 0x34);
pub const BLUE_600: Rgb = Rgb::new(0x1a, 0x4f, 0xd6);
pub const RED_700: Rgb = Rgb::new(0xb3, 0x26, 0x1e);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Role {
Canvas,
Surface,
SurfaceSunken,
Text,
TextMuted,
TextSubtle,
Border,
BorderStrong,
Accent,
AccentHover,
OnAccent,
Inverse,
OnInverse,
Focus,
Success,
Warning,
Danger,
Info,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Theme {
Light,
Dark,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Colourway {
ClayOnChalk,
ChalkOnInk,
ClayGround,
BlackRed,
BlackBlue,
NavyGround,
AmberDawn,
Sky,
MonoBlack,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Paint {
pub ground: Rgb,
pub ink: Rgb,
pub accent: Rgb,
}
impl Colourway {
#[must_use]
pub const fn all() -> [Self; 9] {
[
Self::ClayOnChalk,
Self::ChalkOnInk,
Self::ClayGround,
Self::BlackRed,
Self::BlackBlue,
Self::NavyGround,
Self::AmberDawn,
Self::Sky,
Self::MonoBlack,
]
}
#[must_use]
pub const fn id(self) -> &'static str {
match self {
Self::ClayOnChalk => "5a",
Self::ChalkOnInk => "5b",
Self::ClayGround => "5c",
Self::BlackRed => "5d",
Self::BlackBlue => "5e",
Self::NavyGround => "5f",
Self::AmberDawn => "5g",
Self::Sky => "5h",
Self::MonoBlack => "5i",
}
}
#[must_use]
pub const fn paint(self) -> Paint {
match self {
Self::ClayOnChalk => Paint {
ground: primitive::CHALK_100,
ink: primitive::INK_900,
accent: primitive::CLAY_500,
},
Self::ChalkOnInk => Paint {
ground: primitive::INK_900,
ink: primitive::CHALK_100,
accent: primitive::CLAY_400,
},
Self::ClayGround => Paint {
ground: primitive::CLAY_500,
ink: primitive::CHALK_100,
accent: primitive::INK_900,
},
Self::BlackRed => Paint {
ground: Rgb::new(0xff, 0xff, 0xff),
ink: Rgb::new(0x0a, 0x0a, 0x0a),
accent: Rgb::new(0xd7, 0x19, 0x20),
},
Self::BlackBlue => Paint {
ground: Rgb::new(0xff, 0xff, 0xff),
ink: Rgb::new(0x0a, 0x0a, 0x0a),
accent: primitive::BLUE_600,
},
Self::NavyGround => Paint {
ground: Rgb::new(0x0b, 0x1f, 0x3a),
ink: primitive::CHALK_100,
accent: Rgb::new(0xff, 0x6a, 0x3d),
},
Self::AmberDawn => Paint {
ground: Rgb::new(0x1b, 0x19, 0x17),
ink: primitive::CHALK_100,
accent: primitive::AMBER_400,
},
Self::Sky => Paint {
ground: Rgb::new(0xdf, 0xe9, 0xf2),
ink: Rgb::new(0x14, 0x21, 0x3a),
accent: Rgb::new(0xe8, 0x55, 0x2f),
},
Self::MonoBlack => Paint {
ground: primitive::CHALK_100,
ink: Rgb::new(0x0a, 0x0a, 0x0a),
accent: Rgb::new(0x0a, 0x0a, 0x0a),
},
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Palette {
pub theme: Theme,
pub colourway: Colourway,
}
impl Palette {
#[must_use]
pub const fn light() -> Self {
Self {
theme: Theme::Light,
colourway: Colourway::ClayOnChalk,
}
}
#[must_use]
pub const fn dark() -> Self {
Self {
theme: Theme::Dark,
colourway: Colourway::ClayOnChalk,
}
}
#[must_use]
pub const fn role(self, role: Role) -> Rgb {
match self.theme {
Theme::Dark => match role {
Role::Canvas => primitive::GRAPHITE_900,
Role::Surface => primitive::GRAPHITE_800,
Role::SurfaceSunken => primitive::GRAPHITE_950,
Role::Text | Role::BorderStrong | Role::Inverse => primitive::CHALK_100,
Role::TextMuted => Rgb::new(0xcf, 0xcb, 0xbe),
Role::TextSubtle => Rgb::new(0xa3, 0xa0, 0x94),
Role::Border => primitive::GRAPHITE_600,
Role::Accent => primitive::CLAY_300,
Role::AccentHover => Rgb::new(0xf5, 0x8a, 0x70),
Role::OnAccent | Role::OnInverse => primitive::GRAPHITE_900,
Role::Focus => Rgb::new(0x8f, 0xb0, 0xff),
Role::Success => Rgb::new(0x6f, 0xcf, 0x97),
Role::Warning => primitive::AMBER_400,
Role::Danger => Rgb::new(0xff, 0x8a, 0x80),
Role::Info => Rgb::new(0x8f, 0xb0, 0xff),
},
Theme::Light => {
let paint = self.colourway.paint();
match role {
Role::Canvas | Role::Surface => paint.ground,
Role::SurfaceSunken => primitive::CHALK_300,
Role::Text | Role::BorderStrong | Role::Inverse => paint.ink,
Role::TextMuted => primitive::INK_700,
Role::TextSubtle => primitive::INK_500,
Role::Border => primitive::CHALK_300,
Role::Accent => paint.accent,
Role::AccentHover => primitive::CLAY_700,
Role::OnAccent => paint.ground,
Role::OnInverse => primitive::CHALK_100,
Role::Focus => primitive::BLUE_600,
Role::Success => primitive::PINE_600,
Role::Warning => primitive::AMBER_400,
Role::Danger => primitive::RED_700,
Role::Info => primitive::BLUE_600,
}
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TypeRole {
Display,
Text,
Label,
Script,
Mono,
}
impl TypeRole {
#[must_use]
pub const fn family(self) -> &'static str {
match self {
Self::Display => "League Gothic",
Self::Text | Self::Label => "Fira Sans",
Self::Script => "Rochester",
Self::Mono => "Fragment Mono",
}
}
#[must_use]
pub const fn tracking_milli_em(self) -> i32 {
match self {
Self::Display => 12,
Self::Label => 30,
Self::Script => 0,
Self::Text | Self::Mono => 0,
}
}
}
pub mod duration {
pub const INSTANT: u32 = 90;
pub const FAST: u32 = 160;
pub const BASE: u32 = 280;
pub const SLOW: u32 = 480;
pub const INK: u32 = 900;
}
pub mod easing {
pub const STANDARD: [f64; 4] = [0.4, 0.0, 0.2, 1.0];
pub const SETTLE: [f64; 4] = [0.22, 1.0, 0.36, 1.0];
pub const INK: [f64; 4] = [0.65, 0.0, 0.35, 1.0];
pub const EXIT: [f64; 4] = [0.4, 0.0, 1.0, 1.0];
}
pub const SPACE_SCALE: [u32; 10] = [4, 8, 12, 16, 24, 32, 48, 64, 96, 128];
pub mod radius {
pub const NONE: u32 = 0;
pub const SM: u32 = 2;
pub const MD: u32 = 4;
pub const PILL: u32 = 999;
}
pub mod breakpoint {
pub const EXTRA_SMALL: u32 = 0;
pub const SMALL: u32 = 480;
pub const MEDIUM: u32 = 760;
pub const LARGE: u32 = 1000;
pub const EXTRA_LARGE: u32 = 1300;
pub const DOUBLE_EXTRA_LARGE: u32 = 1760;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Wcag {
Aaa,
Aa,
LargeOnly,
Fail,
}
impl Wcag {
#[must_use]
pub fn grade(ratio: f64, large: bool) -> Self {
if ratio >= 7.0 {
Self::Aaa
} else if ratio >= 4.5 {
Self::Aa
} else if large && ratio >= 3.0 {
Self::LargeOnly
} else {
Self::Fail
}
}
}
#[cfg(test)]
mod tests {
use super::{Colourway, Palette, Rgb, Role, Theme, Wcag};
#[test]
fn hex_round_trips_and_rejects_garbage() {
let clay = Rgb::from_hex("#c84025");
assert_eq!(clay, Some(Rgb::new(0xc8, 0x40, 0x25)));
assert_eq!(clay.map(Rgb::hex).as_deref(), Some("#c84025"));
assert_eq!(Rgb::from_hex("222620"), Some(Rgb::new(0x22, 0x26, 0x20)));
for bad in ["", "#12345", "#1234567", "#gggggg", "not a colour"] {
assert_eq!(Rgb::from_hex(bad), None, "accepted {bad:?}");
}
}
#[test]
fn brand_contrast_pairs_match_the_published_table() {
let ratio = Palette::light()
.role(Role::Text)
.contrast(Palette::light().role(Role::Canvas));
assert!(ratio > 13.0, "ink on chalk measured {ratio:.1}");
let accent = Palette::light()
.role(Role::Accent)
.contrast(Palette::light().role(Role::Canvas));
assert!((4.0..4.6).contains(&accent), "accent measured {accent:.2}");
assert_eq!(Wcag::grade(accent, true), Wcag::LargeOnly);
assert_eq!(Wcag::grade(accent, false), Wcag::Fail);
let dark = Palette::dark();
assert!(dark.role(Role::Text).contrast(dark.role(Role::Canvas)) >= 7.0);
}
#[test]
fn every_colourway_has_a_legible_ink_on_its_ground() {
for colourway in Colourway::all() {
let paint = colourway.paint();
let ratio = paint.ink.contrast(paint.ground);
assert!(
ratio >= 3.0,
"{} ink/ground contrast {ratio:.2} is below the 3:1 UI floor",
colourway.id()
);
}
for primary in [Colourway::ClayOnChalk, Colourway::ChalkOnInk] {
let paint = primary.paint();
assert!(
paint.ink.contrast(paint.ground) >= 4.5,
"primary {} must clear AA for body text",
primary.id()
);
}
}
#[test]
fn dark_theme_is_graphite_and_light_default_is_5a() {
assert_eq!(Palette::dark().theme, Theme::Dark);
assert_eq!(
Palette::dark().role(Role::Canvas),
Rgb::new(0x16, 0x19, 0x15)
);
assert_eq!(
Palette::light().role(Role::Accent),
Rgb::new(0xc8, 0x40, 0x25)
);
}
}