use std::cell::Cell;
use crate::Color;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum SystemColor {
Accent,
Text,
SecondaryText,
Background,
Surface,
TertiaryText,
Placeholder,
Link,
Separator,
ControlFill,
GroupedBackground,
ElevatedSurface,
Selection,
Destructive,
Success,
Warning,
}
impl SystemColor {
pub const COUNT: usize = 16;
pub const ALL: [Self; Self::COUNT] = [
Self::Accent,
Self::Text,
Self::SecondaryText,
Self::Background,
Self::Surface,
Self::TertiaryText,
Self::Placeholder,
Self::Link,
Self::Separator,
Self::ControlFill,
Self::GroupedBackground,
Self::ElevatedSurface,
Self::Selection,
Self::Destructive,
Self::Success,
Self::Warning,
];
#[must_use]
pub const fn wire(self) -> u8 {
self as u8
}
#[must_use]
pub const fn from_wire(n: u8) -> Option<SystemColor> {
if (n as usize) < Self::COUNT {
Some(Self::ALL[n as usize])
} else {
None
}
}
#[must_use]
pub const fn with_alpha(self, alpha: f64) -> Ink {
Ink::System {
role: self,
alpha: Some(alpha),
}
}
#[must_use]
pub const fn fallback(self, dark: bool) -> Color {
match (self, dark) {
(Self::Accent, false) => Color::hex(0x000a_66ff),
(Self::Accent, true) => Color::hex(0x003f_83ff),
(Self::Text, false) => Color::hex(0x001d_1d1f),
(Self::Text, true) => Color::hex(0x00f5_f5f7),
(Self::SecondaryText, false) => Color::hex(0x006e_6e73),
(Self::SecondaryText, true) => Color::hex(0x0098_989d),
(Self::Background | Self::ElevatedSurface, false) => Color::hex(0x00ff_ffff),
(Self::Background, true) => Color::hex(0x001e_1e20),
(Self::Surface, false) => Color::hex(0x00f3_f2f7),
(Self::Surface, true) => Color::hex(0x002c_2c2e),
(Self::TertiaryText | Self::Placeholder, false) => rgba8(60, 60, 67, 0.3),
(Self::TertiaryText | Self::Placeholder, true) => rgba8(235, 235, 245, 0.3),
(Self::Link, false) => Color::hex(0x0000_66cc),
(Self::Link, true) => Color::hex(0x004d_a3ff),
(Self::Separator, false) => rgba8(60, 60, 67, 0.29),
(Self::Separator, true) => rgba8(84, 84, 88, 0.6),
(Self::ControlFill, false) => rgba8(120, 120, 128, 0.2),
(Self::ControlFill, true) => rgba8(120, 120, 128, 0.36),
(Self::GroupedBackground, false) => Color::hex(0x00f2_f2f7),
(Self::GroupedBackground, true) => Color::hex(0x0016_1618),
(Self::ElevatedSurface, true) => Color::hex(0x003a_3a3c),
(Self::Selection, false) => rgba8(10, 102, 255, 0.25),
(Self::Selection, true) => rgba8(63, 131, 255, 0.35),
(Self::Destructive, false) => Color::hex(0x00ff_3b30),
(Self::Destructive, true) => Color::hex(0x00ff_453a),
(Self::Success, false) => Color::hex(0x0034_c759),
(Self::Success, true) => Color::hex(0x0030_d158),
(Self::Warning, false) => Color::hex(0x00ff_9500),
(Self::Warning, true) => Color::hex(0x00ff_9f0a),
}
}
}
const fn rgba8(r: u8, g: u8, b: u8, a: f64) -> Color {
Color::rgba(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0, a)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Ink {
Fixed(Color),
Adaptive { light: Color, dark: Color },
System {
role: SystemColor,
alpha: Option<f64>,
},
}
impl Default for Ink {
fn default() -> Self {
Ink::Fixed(Color::CLEAR)
}
}
impl From<Color> for Ink {
fn from(c: Color) -> Self {
Ink::Fixed(c)
}
}
impl From<SystemColor> for Ink {
fn from(role: SystemColor) -> Self {
Ink::system(role)
}
}
impl Ink {
pub const CLEAR: Ink = Ink::Fixed(Color::CLEAR);
#[must_use]
pub const fn adaptive(light: Color, dark: Color) -> Ink {
Ink::Adaptive { light, dark }
}
#[must_use]
pub const fn system(role: SystemColor) -> Ink {
Ink::System { role, alpha: None }
}
#[must_use]
pub const fn with_alpha(self, alpha: f64) -> Ink {
match self {
Ink::Fixed(c) => Ink::Fixed(Color::rgba(c.r, c.g, c.b, alpha)),
Ink::Adaptive { light, dark } => Ink::Adaptive {
light: Color::rgba(light.r, light.g, light.b, alpha),
dark: Color::rgba(dark.r, dark.g, dark.b, alpha),
},
Ink::System { role, .. } => Ink::System {
role,
alpha: Some(alpha),
},
}
}
#[must_use]
pub const fn fixed(self) -> Option<Color> {
match self {
Ink::Fixed(c) => Some(c),
_ => None,
}
}
#[must_use]
pub const fn is_dynamic(self) -> bool {
!matches!(self, Ink::Fixed(_))
}
#[must_use]
pub fn resolve(self, palette: &Palette) -> Color {
match self {
Ink::Fixed(c) => c,
Ink::Adaptive { light, dark } => {
if palette.dark {
dark
} else {
light
}
}
Ink::System { role, alpha } => {
let c = palette.color(role);
alpha.map_or(c, |a| c.with_alpha(a))
}
}
}
#[must_use]
pub fn resolved(self) -> Color {
self.resolve(&Palette::current())
}
#[must_use]
pub fn wire(self) -> [u32; 3] {
match self {
Ink::Fixed(c) => [0, argb32(c), 0],
Ink::Adaptive { light, dark } => [1, argb32(light), argb32(dark)],
Ink::System { role, alpha } => [2, u32::from(role.wire()), alpha.map_or(256, channel8)],
}
}
#[must_use]
pub fn from_wire(wire: [u32; 3]) -> Option<Ink> {
match wire {
[0, c, _] => Some(Ink::Fixed(from_argb32(c))),
[1, l, d] => Some(Ink::adaptive(from_argb32(l), from_argb32(d))),
[2, role, alpha] => Some(Ink::System {
role: SystemColor::from_wire(u8::try_from(role).ok()?)?,
alpha: (alpha <= 255).then(|| f64::from(alpha) / 255.0),
}),
_ => None,
}
}
#[must_use]
pub fn pair(self, light: &Palette, dark: &Palette) -> (Color, Color) {
(self.resolve(light), self.resolve(dark))
}
#[must_use]
pub fn map(self, f: impl Fn(Color) -> Color) -> Ink {
match self {
Ink::Fixed(c) => Ink::Fixed(f(c)),
Ink::Adaptive { light, dark } => Ink::adaptive(f(light), f(dark)),
Ink::System { .. } => {
let (light, dark) = Palette::both();
let (l, d) = self.pair(&light, &dark);
Ink::adaptive(f(l), f(d))
}
}
}
}
fn channel8(v: f64) -> u32 {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value is clamped to 0..=255 and rounded first"
)]
let byte = (v.clamp(0.0, 1.0) * 255.0).round() as u32;
byte
}
fn argb32(c: Color) -> u32 {
(channel8(c.a) << 24) | (channel8(c.r) << 16) | (channel8(c.g) << 8) | channel8(c.b)
}
fn from_argb32(v: u32) -> Color {
let ch = |shift: u32| f64::from((v >> shift) & 0xff) / 255.0;
Color::rgba(ch(16), ch(8), ch(0), ch(24))
}
#[must_use]
pub fn resolved_stops(stops: &[(f64, Ink)]) -> Vec<(f64, Color)> {
stops
.iter()
.map(|(at, ink)| (*at, ink.resolved()))
.collect()
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Palette {
pub dark: bool,
pub colors: [Color; SystemColor::COUNT],
}
thread_local! {
static CURRENT: Cell<Option<Palette>> = const { Cell::new(None) };
}
impl Palette {
#[must_use]
pub const fn fallback(dark: bool) -> Palette {
let mut colors = [Color::CLEAR; SystemColor::COUNT];
let mut i = 0;
while i < SystemColor::COUNT {
colors[i] = SystemColor::ALL[i].fallback(dark);
i += 1;
}
Palette { dark, colors }
}
#[must_use]
pub const fn color(&self, role: SystemColor) -> Color {
self.colors[role as usize]
}
#[must_use]
pub fn current() -> Palette {
CURRENT.with(Cell::get).unwrap_or_else(|| {
Palette::fallback(std::env::var("DAY_THEME").ok().as_deref() == Some("dark"))
})
}
#[must_use]
pub fn both() -> (Palette, Palette) {
let current = Palette::current();
if current.dark {
(Palette::fallback(false), current)
} else {
(current, Palette::fallback(true))
}
}
pub fn set_current(palette: Palette) {
CURRENT.with(|c| c.set(Some(palette)));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roles_round_trip_their_wire_numbers() {
for (i, role) in SystemColor::ALL.iter().enumerate() {
assert_eq!(role.wire() as usize, i);
assert_eq!(SystemColor::from_wire(role.wire()), Some(*role));
}
assert_eq!(SystemColor::from_wire(SystemColor::COUNT as u8), None);
}
#[test]
fn inks_resolve_per_appearance() {
let (light, dark) = (Palette::fallback(false), Palette::fallback(true));
let pair = Ink::adaptive(Color::WHITE, Color::BLACK);
assert_eq!(pair.resolve(&light), Color::WHITE);
assert_eq!(pair.resolve(&dark), Color::BLACK);
let role = Ink::from(SystemColor::Text);
assert_eq!(role.resolve(&dark), SystemColor::Text.fallback(true));
let wash = SystemColor::Accent.with_alpha(0.25).resolve(&light);
assert!((wash.a - 0.25).abs() < 1e-12);
assert_eq!(
Ink::from(Color::BLACK).with_alpha(0.5),
Ink::Fixed(Color::rgba(0.0, 0.0, 0.0, 0.5))
);
assert_eq!(pair.with_alpha(0.0).resolve(&dark).a, 0.0);
assert!(!Ink::from(Color::WHITE).is_dynamic() && pair.is_dynamic());
}
#[test]
fn inks_round_trip_their_wire_form() {
for ink in [
Ink::Fixed(Color::rgba(1.0, 0.0, 0.0, 0.5)),
Ink::adaptive(Color::WHITE, Color::BLACK),
Ink::system(SystemColor::Separator),
SystemColor::Accent.with_alpha(0.2),
] {
let back = Ink::from_wire(ink.wire()).expect("decodes");
let (light, dark) = (Palette::fallback(false), Palette::fallback(true));
for p in [light, dark] {
let (a, b) = (ink.resolve(&p), back.resolve(&p));
assert!(
(a.r - b.r).abs() < 0.003 && (a.a - b.a).abs() < 0.003,
"{ink:?}"
);
}
}
assert_eq!(Ink::system(SystemColor::Link).wire(), [2, 7, 256]);
assert_eq!(Ink::from_wire([3, 0, 0]), None);
assert_eq!(Ink::from_wire([2, 99, 256]), None);
}
#[test]
fn the_current_palette_is_what_resolved_reads() {
let dark = Palette::fallback(true);
Palette::set_current(dark);
assert_eq!(
Ink::system(SystemColor::Background).resolved(),
SystemColor::Background.fallback(true)
);
Palette::set_current(Palette::fallback(false));
assert_eq!(
Ink::system(SystemColor::Background).resolved(),
Color::WHITE
);
}
}