use crate::color::Color;
use crate::preferences::Preferences;
use crate::text::Font;
#[derive(Clone, Copy, Debug, PartialEq)]
#[doc(alias("style"))]
pub struct Theme {
pub bg: Color,
pub panel: Color,
pub raised: Color,
pub hover: Color,
pub selected: Color,
pub line: Color,
pub line_strong: Color,
pub text: Color,
pub text_dim: Color,
pub text_faint: Color,
pub accent: Color,
pub on_accent: Color,
pub red: Color,
pub amber: Color,
pub green: Color,
pub blue: Color,
pub font: Font,
pub small_font: Font,
pub radius: f32,
pub control_height: f32,
pub gap: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ContrastColors {
pub background: Color,
pub text: Color,
pub link: Color,
pub disabled: Color,
pub selected: Color,
pub selected_text: Color,
pub button: Color,
pub button_text: Color,
}
impl ContrastColors {
pub const DARK: ContrastColors = ContrastColors {
background: Color::BLACK,
text: Color::WHITE,
link: Color::rgb(0xffff00),
disabled: Color::rgb(0xa6a6a6),
selected: Color::rgb(0x1aebff),
selected_text: Color::BLACK,
button: Color::BLACK,
button_text: Color::WHITE,
};
pub const LIGHT: ContrastColors = ContrastColors {
background: Color::WHITE,
text: Color::BLACK,
link: Color::rgb(0x00009f),
disabled: Color::rgb(0x676767),
selected: Color::rgb(0x37006e),
selected_text: Color::WHITE,
button: Color::WHITE,
button_text: Color::BLACK,
};
}
impl Theme {
pub fn dark() -> Self {
Theme {
bg: Color::rgb(0x0f1115),
panel: Color::rgb(0x151821),
raised: Color::rgb(0x1b1f2a),
hover: Color::rgb(0x1d2130),
selected: Color::rgb(0x232840),
line: Color::rgb(0x262b38),
line_strong: Color::rgb(0x323848),
text: Color::rgb(0xe6e8ee),
text_dim: Color::rgb(0xa4acbd),
text_faint: Color::rgb(0x6f778a),
accent: Color::rgb(0x8b93ff),
on_accent: Color::rgb(0x0f1115),
red: Color::rgb(0xf2706b),
amber: Color::rgb(0xe8b04b),
green: Color::rgb(0x5fcf8f),
blue: Color::rgb(0x5fb2f5),
font: Font::new(13.0),
small_font: Font::new(11.5),
radius: 6.0,
control_height: 28.0,
gap: 8.0,
}
}
pub fn light() -> Self {
Theme {
bg: Color::rgb(0xf5f6f8),
panel: Color::rgb(0xffffff),
raised: Color::rgb(0xffffff),
hover: Color::rgb(0xe6e9f1),
selected: Color::rgb(0xdfe3fa),
line: Color::rgb(0xe0e3ea),
line_strong: Color::rgb(0xcfd4de),
text: Color::rgb(0x1b1f2c),
text_dim: Color::rgb(0x535c70),
text_faint: Color::rgb(0x8189a0),
accent: Color::rgb(0x4b52d6),
on_accent: Color::rgb(0xffffff),
red: Color::rgb(0xcf423d),
amber: Color::rgb(0xa86f08),
green: Color::rgb(0x1a8f52),
blue: Color::rgb(0x1b76c4),
..Theme::dark()
}
}
pub fn high_contrast(colors: ContrastColors) -> Self {
let ContrastColors { background, text, selected: selection, .. } = colors;
let usual = if background.is_dark() { Theme::dark() } else { Theme::light() };
let legible = |color: Color| readable(color, background, 4.5);
let selected = tinted(background, selection, text, 0.4);
Theme {
bg: background,
panel: background,
raised: colors.button,
hover: background.lerp(selected, 0.5),
selected,
line: readable(text.lerp(background, 0.65), background, 3.0),
line_strong: between(text, background),
text,
text_dim: text,
text_faint: colors.disabled,
accent: readable(selection, background, 3.0),
on_accent: colors.selected_text,
red: legible(usual.red),
amber: legible(usual.amber),
green: legible(usual.green),
blue: legible(usual.blue),
..usual
}
}
pub fn accented(self, color: Color) -> Self {
let accent = readable(color.solid(), self.bg, 4.5);
let dark = if self.bg.is_dark() { self.bg } else { self.text };
let on_accent = if contrast(Color::WHITE, accent) >= contrast(dark, accent) { Color::WHITE } else { dark };
Theme { accent, on_accent, hover: self.panel.lerp(accent, 0.08), selected: self.panel.lerp(accent, 0.15), ..self }
}
pub fn dense(self) -> Self {
Theme { control_height: self.control_height * 6.0 / 7.0, gap: self.gap * 0.5, radius: self.radius * 2.0 / 3.0, ..self }
}
pub fn roomy(self) -> Self {
Theme { control_height: self.control_height * 8.0 / 7.0, gap: self.gap * 1.5, radius: self.radius * 4.0 / 3.0, ..self }
}
pub fn text_scaled(self, by: f32) -> Self {
let by = if by.is_finite() { by.max(0.5) } else { 1.0 };
let grown = |font: Font| Font { size: font.size * by, ..font };
let control = self.control_height + self.font.size * (by - 1.0) * 1.35;
Theme { font: grown(self.font), small_font: grown(self.small_font), control_height: control, ..self }
}
pub fn following(preferences: &Preferences) -> Self {
let base = match (preferences.high_contrast, preferences.dark) {
(Some(colors), _) => Theme::high_contrast(colors),
(None, true) => Theme::dark(),
(None, false) => Theme::light(),
};
let base = match (preferences.high_contrast, preferences.accent) {
(None, Some(color)) => base.accented(color),
_ => base,
};
if preferences.text_scale == 1.0 { base } else { base.text_scaled(preferences.text_scale) }
}
}
fn light_of(color: Color) -> f32 {
let linear = |part: u32| {
let part = (part & 0xff) as f32 / 255.0;
if part <= 0.04045 { part / 12.92 } else { ((part + 0.055) / 1.055).powf(2.4) }
};
0.2126 * linear(color.0 >> 16) + 0.7152 * linear(color.0 >> 8) + 0.0722 * linear(color.0)
}
pub(crate) fn contrast(one: Color, other: Color) -> f32 {
let (one, other) = (light_of(one), light_of(other));
(one.max(other) + 0.05) / (one.min(other) + 0.05)
}
fn readable(color: Color, ground: Color, least: f32) -> Color {
let end = if ground.is_dark() { Color::WHITE } else { Color::BLACK };
(0..=20).map(|step| color.lerp(end, step as f32 / 20.0)).find(|color| contrast(*color, ground) >= least).unwrap_or(end)
}
fn between(one: Color, other: Color) -> Color {
let apart = |color: &Color| contrast(*color, one).min(contrast(*color, other));
(0..=20).map(|step| one.lerp(other, step as f32 / 20.0)).max_by(|a, b| apart(a).total_cmp(&apart(b))).unwrap_or(one)
}
fn tinted(ground: Color, color: Color, text: Color, most: f32) -> Color {
let (steps, least) = ((most * 20.0) as u32, (contrast(text, ground) * 0.75).min(7.0));
(0..=steps).rev().map(|step| ground.lerp(color, step as f32 / 20.0)).find(|fill| contrast(text, *fill) >= least).unwrap_or(ground)
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) const WINDOWS: [(&str, ContrastColors); 4] = [
("Aquatic", windows([0x202020, 0xffffff, 0x75e9fc, 0xa6a6a6, 0x8ee3f0, 0x263b50, 0x202020, 0xffffff])),
("Desert", windows([0xfffaef, 0x3d3d3d, 0x1c5e75, 0x676767, 0x903909, 0xfff5e3, 0xfffaef, 0x202020])),
("Dusk", windows([0x2d3236, 0xffffff, 0x70ebde, 0xa6a6a6, 0xa1bfde, 0x212d3b, 0x2d3236, 0xb6f6f0])),
("Night sky", windows([0x000000, 0xffffff, 0x8080ff, 0xa6a6a6, 0xd6b4fd, 0x2b2b2b, 0x000000, 0xffee32])),
];
const fn windows([ground, text, link, off, picked, on_picked, button, button_text]: [u32; 8]) -> ContrastColors {
let [ground, text, link, off] = [Color::rgb(ground), Color::rgb(text), Color::rgb(link), Color::rgb(off)];
let [picked, on_picked, button, button_text] = [Color::rgb(picked), Color::rgb(on_picked), Color::rgb(button), Color::rgb(button_text)];
ContrastColors { background: ground, text, link, disabled: off, selected: picked, selected_text: on_picked, button, button_text }
}
const PICKED: [u32; 9] = [0x0078d4, 0xff0000, 0xffff00, 0x00ff00, 0x0000ff, 0xffffff, 0x000000, 0x808080, 0xe3008c];
fn readings(t: &Theme, strong: bool) -> Vec<(&'static str, Color, Color, f32)> {
let text = if strong { 7.0 } else { 4.5 };
let mut pairs = Vec::new();
for (name, fill) in [("ground", t.bg), ("panel", t.panel), ("raise", t.raised), ("hover", t.hover), ("selected", t.selected)] {
pairs.push((name, t.text, fill, text));
pairs.push((name, t.text_dim, fill, 4.5));
}
pairs.extend([("accent", t.accent, t.bg, 4.5), ("on accent", t.on_accent, t.accent, 4.5)]);
pairs.push(("switch", t.text, t.line_strong, 3.0));
pairs.push(("selected text", t.text, Color(t.accent.with_alpha(0.35).over(t.bg.0, 255)), 4.5));
pairs.extend([("red", t.red, t.bg, 3.0), ("amber", t.amber, t.bg, 3.0), ("green", t.green, t.bg, 3.0), ("blue", t.blue, t.bg, 3.0)]);
if strong {
pairs.extend([("line", t.line, t.bg, 3.0), ("strong line", t.line_strong, t.bg, 3.0), ("faint", t.text_faint, t.bg, 4.5)]);
pairs.extend([("red", t.red, t.bg, 4.5), ("amber", t.amber, t.bg, 4.5), ("green", t.green, t.bg, 4.5), ("blue", t.blue, t.bg, 4.5)]);
}
pairs
}
fn unreadable(t: &Theme, strong: bool) -> Vec<String> {
let far = |(name, ink, fill, least): (&str, Color, Color, f32)| (contrast(ink, fill) < least).then(|| format!("{name}: {ink} on {fill} is {:.2}", contrast(ink, fill)));
readings(t, strong).into_iter().filter_map(far).collect()
}
#[test]
fn light_is_weighed_as_the_web_weighs_it() {
assert!((contrast(Color::BLACK, Color::WHITE) - 21.0).abs() < 1e-4 && contrast(Color::WHITE, Color::BLACK) == contrast(Color::BLACK, Color::WHITE));
assert_eq!(contrast(Color::rgb(0x123456), Color::rgb(0x123456)), 1.0);
assert!((contrast(Color::rgb(0x767676), Color::WHITE) - 4.54).abs() < 0.01);
assert!((contrast(Color::rgb(0x0078d4), Color::WHITE) - 4.53).abs() < 0.01);
}
#[test]
fn every_theme_keeps_its_text_readable() {
for (name, theme) in [("dark", Theme::dark()), ("light", Theme::light())] {
assert_eq!(unreadable(&theme, false), Vec::<String>::new(), "{name}");
}
let mut contrasts = vec![("dark", ContrastColors::DARK), ("light", ContrastColors::LIGHT)];
contrasts.extend(WINDOWS);
for (name, colors) in contrasts {
let theme = Theme::high_contrast(colors);
assert_eq!(unreadable(&theme, true), Vec::<String>::new(), "high contrast: {name}");
assert_eq!([theme.bg, theme.text, theme.text_faint, theme.raised, theme.on_accent], [colors.background, colors.text, colors.disabled, colors.button, colors.selected_text], "{name}");
assert_eq!(theme.accent, colors.selected, "{name}");
assert!(contrast(theme.selected, theme.bg) > contrast(theme.hover, theme.bg) && contrast(theme.hover, theme.bg) > 1.1, "{name}");
}
for picked in PICKED.map(Color::rgb) {
for (name, theme) in [("dark", Theme::dark()), ("light", Theme::light())] {
let accented = theme.accented(picked);
assert_eq!(unreadable(&accented, false), Vec::<String>::new(), "{name} around {picked}");
}
}
}
#[test]
fn a_theme_is_made_around_one_colour() {
let (dark, blue) = (Theme::dark(), Color::rgb(0x0078d4));
let around = dark.accented(blue);
assert!((around.accent.to_hsv()[0] - blue.to_hsv()[0]).abs() < 0.005);
assert_eq!(around.accent, blue.lerp(Color::WHITE, 0.1));
assert!(contrast(around.accent, dark.bg) >= 4.5 && contrast(blue.lerp(Color::WHITE, 0.05), dark.bg) < 4.5);
assert_eq!(Theme::light().accented(blue).accent, blue.lerp(Color::BLACK, 0.05));
assert_eq!(Theme { bg: Color::WHITE, ..Theme::light() }.accented(blue).accent, blue);
let same = |t: &Theme| (t.bg, t.panel, t.raised, t.line, t.text, t.text_dim, t.red, t.font, t.control_height);
assert_eq!(same(&around), same(&dark));
assert!(around.hover != dark.hover && around.selected != dark.selected && around.selected != around.hover);
for theme in [Theme::dark(), Theme::light()] {
let again = theme.accented(theme.accent);
assert_eq!((again.accent, again.on_accent), (theme.accent, theme.on_accent));
let near = |one: Color, other: Color| (0..3).all(|at| ((one.0 >> (8 * at)) & 0xff).abs_diff((other.0 >> (8 * at)) & 0xff) <= 12);
assert!(near(again.selected, theme.selected) && near(again.hover, theme.hover), "{again:?}");
}
for color in PICKED.map(Color::rgb) {
assert_eq!((dark.accented(color).on_accent, Theme::light().accented(color).on_accent), (dark.bg, Color::WHITE), "{color}");
}
}
#[test]
fn spacing_and_text_grow_and_shrink() {
let t = Theme::dark();
assert_eq!((t.dense().control_height, t.dense().gap, t.dense().radius), (24.0, 4.0, 4.0));
assert_eq!((t.roomy().control_height, t.roomy().gap, t.roomy().radius), (32.0, 12.0, 8.0));
assert_eq!((t.dense().font, t.roomy().accent), (t.font, t.accent));
let large = t.text_scaled(2.0);
assert_eq!((large.font.size, large.small_font.size, large.gap, large.radius), (26.0, 23.0, t.gap, t.radius));
assert!((large.control_height - (28.0 + 13.0 * 1.35)).abs() < 1e-4);
assert_eq!(t.text_scaled(1.0), t);
assert_eq!(t.dense().text_scaled(1.5).font, t.text_scaled(1.5).dense().font);
assert_eq!([f32::NAN, f32::INFINITY].map(|by| t.text_scaled(by)), [t, t]);
assert_eq!(t.text_scaled(0.0).font.size, 6.5);
}
#[test]
fn the_theme_follows_what_the_person_asked_for() {
let wished = |change: fn(&mut Preferences)| {
let mut wishes = Preferences::default();
change(&mut wishes);
Theme::following(&wishes)
};
assert_eq!(wished(|_| {}), Theme::light());
assert_eq!(wished(|w| w.dark = true), Theme::dark());
let red = Color::rgb(0xd13438);
assert_eq!(wished(|w| (w.dark, w.accent) = (true, Some(Color::rgb(0xd13438)))), Theme::dark().accented(red));
let night = WINDOWS[3].1;
assert_eq!(wished(|w| (w.dark, w.high_contrast, w.accent) = (false, Some(WINDOWS[3].1), Some(Color::rgb(0xd13438)))), Theme::high_contrast(night));
assert_eq!(wished(|w| (w.text_scale, w.high_contrast) = (1.5, Some(ContrastColors::LIGHT))), Theme::high_contrast(ContrastColors::LIGHT).text_scaled(1.5));
}
}