mod palette;
mod studio;
use crate::color::Rgba;
pub use palette::ThemeId;
#[cfg(feature = "serde")]
pub use studio::ThemeImportError;
pub const ANIMATION_TIME: f32 = 0.10;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ThemeMode {
#[default]
Light,
Dark,
}
impl ThemeMode {
pub fn from_setting(value: &str) -> Self {
if value == "dark" { Self::Dark } else { Self::Light }
}
pub fn as_setting(self) -> &'static str {
match self {
Self::Light => "light",
Self::Dark => "dark",
}
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Theme {
pub background: Rgba,
pub surface: Rgba,
pub surface_raised: Rgba,
pub sunken: Rgba,
pub text: Rgba,
pub text_muted: Rgba,
pub accent: Rgba,
pub accent_text: Rgba,
pub border: Rgba,
pub selection: Rgba,
pub hover: Rgba,
pub danger: Rgba,
pub warning: Rgba,
pub success: Rgba,
pub done: Rgba,
pub link: Rgba,
pub scrollbar: Rgba,
pub focus: Rgba,
}
impl Default for Theme {
fn default() -> Self {
Self::light()
}
}
impl Theme {
pub fn light() -> Self {
palette::theme(ThemeId::GithubLight)
}
pub fn dark() -> Self {
palette::theme(ThemeId::GithubDark)
}
pub fn for_id(id: ThemeId) -> Self {
palette::theme(id)
}
pub fn is_dark(&self) -> bool {
relative_luminance(self.background) < 0.5
}
pub fn with_accent(mut self, accent: Rgba) -> Self {
let towards = if self.is_dark() { Rgba::WHITE } else { Rgba::BLACK };
self.accent = accent;
self.focus = until_contrast(accent, self.background, towards, 3.0);
self.link = until_contrast(accent, self.background, towards, 4.5);
self.selection = mix(self.background, accent, if self.is_dark() { 0.35 } else { 0.28 });
self.accent_text = if contrast_ratio(Rgba::WHITE, accent) >= contrast_ratio(Rgba::BLACK, accent) {
Rgba::WHITE
} else {
Rgba::BLACK
};
if contrast_ratio(self.accent_text, self.accent) < 4.5 {
let away = if self.accent_text == Rgba::WHITE {
Rgba::BLACK
} else {
Rgba::WHITE
};
self.accent = until_contrast(self.accent, self.accent_text, away, 4.5);
}
self
}
pub fn accessibility_issues(&self) -> Vec<(&'static str, f32)> {
let mut issues = Vec::new();
for (label, foreground, background, minimum) in [
("text on background", self.text, self.background, 4.5),
("text on surface", self.text, self.surface, 4.5),
("muted text on background", self.text_muted, self.background, 4.5),
("link on background", self.link, self.background, 4.5),
("accent text on accent", self.accent_text, self.accent, 4.5),
("danger on background", self.danger, self.background, 4.5),
("focus on background", self.focus, self.background, 3.0),
] {
let ratio = contrast_ratio(foreground, background);
if ratio < minimum {
issues.push((label, ratio));
}
}
issues
}
pub fn tokens(&self) -> [(&'static str, Rgba); 18] {
[
("background", self.background),
("surface", self.surface),
("surface_raised", self.surface_raised),
("sunken", self.sunken),
("text", self.text),
("text_muted", self.text_muted),
("accent", self.accent),
("accent_text", self.accent_text),
("border", self.border),
("selection", self.selection),
("hover", self.hover),
("danger", self.danger),
("warning", self.warning),
("success", self.success),
("done", self.done),
("link", self.link),
("scrollbar", self.scrollbar),
("focus", self.focus),
]
}
}
pub fn effective_id(id: ThemeId, follow_system: bool, system_dark: bool) -> ThemeId {
if follow_system {
id.follow_system(system_dark)
} else {
id
}
}
pub fn mix(a: Rgba, b: Rgba, t: f32) -> Rgba {
a.mix(b, t)
}
pub(crate) fn until_contrast(color: Rgba, background: Rgba, towards: Rgba, minimum: f32) -> Rgba {
let mut candidate = color;
for step in 0..=20 {
candidate = mix(color, towards, step as f32 / 20.0);
if contrast_ratio(candidate, background) >= minimum {
break;
}
}
candidate
}
pub fn relative_luminance(color: Rgba) -> f32 {
color.relative_luminance()
}
pub fn readable_on(fill: Rgba, dark: Rgba) -> Rgba {
if contrast_ratio(Rgba::WHITE, fill) >= contrast_ratio(dark, fill) {
Rgba::WHITE
} else {
dark
}
}
pub fn contrast_ratio(a: Rgba, b: Rgba) -> f32 {
let (first, second) = (relative_luminance(a), relative_luminance(b));
let (light, dark) = if first > second {
(first, second)
} else {
(second, first)
};
(light + 0.05) / (dark + 0.05)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_theme_has_consistent_darkness() {
for id in ThemeId::ALL {
assert_eq!(Theme::for_id(id).is_dark(), id.mode() == ThemeMode::Dark, "{id:?}");
}
}
#[test]
fn contrast_meets_aa_for_primary_pairs() {
for id in ThemeId::ALL {
let theme = Theme::for_id(id);
for (foreground, background, label) in [
(theme.text, theme.background, "text/background"),
(theme.text, theme.surface, "text/surface"),
(theme.text_muted, theme.background, "muted/background"),
(theme.link, theme.background, "link/background"),
(theme.accent_text, theme.accent, "accent text/accent"),
(theme.danger, theme.background, "danger/background"),
] {
let ratio = contrast_ratio(foreground, background);
assert!(ratio >= 4.5, "{id:?} {label}: {ratio:.2}");
}
assert!(contrast_ratio(theme.focus, theme.background) >= 3.0, "{id:?} focus");
}
}
#[test]
fn destructive_text_meets_aa_in_every_theme() {
for id in ThemeId::ALL {
let theme = Theme::for_id(id);
let text = readable_on(theme.danger, theme.background);
let ratio = contrast_ratio(text, theme.danger);
assert!(ratio >= 3.0, "{id:?} destructive: {ratio:.2}");
}
}
#[test]
fn built_in_themes_have_no_accessibility_issues() {
for id in ThemeId::ALL {
assert_eq!(Theme::for_id(id).accessibility_issues(), Vec::new(), "{id:?}");
}
}
#[test]
fn accents_keep_aa_contrast_on_every_base() {
let accents = [
Rgba::from_rgb(0xff, 0xd7, 0x00),
Rgba::from_rgb(0x00, 0x7a, 0xcc),
Rgba::from_rgb(0xe0, 0x6c, 0x2b),
Rgba::from_rgb(0x8a, 0x2b, 0xe2),
Rgba::from_rgb(0x20, 0xc0, 0x60),
];
for id in ThemeId::ALL {
for accent in accents {
let theme = Theme::for_id(id).with_accent(accent);
assert_eq!(theme.accessibility_issues(), Vec::new(), "{id:?} {accent:?}");
}
}
}
#[cfg(feature = "serde")]
#[test]
fn vscode_themes_import_and_export() {
let text = r##"{
"colors": {
"editor.background": "#1e1e1e",
"foreground": "#d4d4d4",
"focusBorder": "#007acc",
"list.activeSelectionBackground": "#264f78",
"descriptionForeground": "#9d9d9d"
}
}"##;
let theme = Theme::from_vscode_json(text).expect("import");
assert_eq!(theme.background, Rgba::from_rgb(0x1e, 0x1e, 0x1e));
assert_eq!(theme.text, Rgba::from_rgb(0xd4, 0xd4, 0xd4));
assert!(theme.accessibility_issues().is_empty());
let css = theme.to_css_variables();
assert!(css.contains("--wb-background: #1e1e1e;"));
assert_eq!(theme.to_json()["background"], "#1e1e1e");
assert!(Theme::from_vscode_json("{\"colors\":{}}").is_err());
}
#[test]
fn high_contrast_keeps_aa() {
for theme in [Theme::dark().high_contrast(), Theme::light().high_contrast()] {
assert!(theme.accessibility_issues().is_empty());
assert_eq!(
theme.background,
if theme.is_dark() { Rgba::BLACK } else { Rgba::WHITE }
);
}
}
#[test]
fn black_and_white_are_twenty_one_to_one() {
assert!((contrast_ratio(Rgba::BLACK, Rgba::WHITE) - 21.0).abs() < 0.01);
}
#[test]
fn modes_round_trip() {
for mode in [ThemeMode::Light, ThemeMode::Dark] {
assert_eq!(ThemeMode::from_setting(mode.as_setting()), mode);
}
}
#[test]
fn tokens_cover_every_field() {
let theme = Theme::light();
assert_eq!(theme.tokens().len(), 18);
assert_eq!(theme.tokens()[0], ("background", theme.background));
assert_eq!(theme.tokens()[17], ("focus", theme.focus));
}
}