Skip to main content

vtcode_ui/theme/
scheme.rs

1use vtcode_commons::ansi_capabilities::{ColorScheme, detect_color_scheme};
2
3use crate::theme::color_math::{contrast_ratio, relative_luminance};
4use crate::theme::registry::{available_theme_suites, theme_definition, theme_suite_id};
5use crate::theme::types::{DEFAULT_THEME_ID, ThemeSuite};
6
7/// Report whether a theme matches the detected terminal light/dark scheme.
8pub fn theme_matches_terminal_scheme(theme_id: &str) -> bool {
9    let scheme = detect_color_scheme();
10    let theme_is_light = is_light_theme(theme_id);
11
12    match scheme {
13        ColorScheme::Light => theme_is_light,
14        ColorScheme::Dark | ColorScheme::Unknown => !theme_is_light,
15    }
16}
17
18/// Report whether a built-in theme should be treated as a light theme.
19pub fn is_light_theme(theme_id: &str) -> bool {
20    theme_definition(theme_id)
21        .map(|theme| relative_luminance(theme.palette.background) > 0.5)
22        .unwrap_or(false)
23}
24
25/// Suggest a built-in theme that matches the current terminal scheme.
26pub fn suggest_theme_for_terminal() -> &'static str {
27    suggest_theme_for_scheme(!matches!(detect_color_scheme(), ColorScheme::Light))
28}
29
30/// Suggest a built-in theme for an explicit light/dark scheme.
31fn suggest_theme_for_scheme(dark: bool) -> &'static str {
32    if dark { DEFAULT_THEME_ID } else { "vitesse-light" }
33}
34
35/// Pick the theme to apply after a terminal light/dark scheme change.
36///
37/// Prefers a light/dark twin from the same suite as `current_theme_id` so
38/// suite-aware themes stay within their family; falls back to the default
39/// suggestion for the requested scheme for single-theme or custom selections.
40pub fn theme_for_terminal_scheme_change(current_theme_id: &str, dark: bool) -> &'static str {
41    if let Some(suite_id) = theme_suite_id(current_theme_id)
42        && let Some(suite) = available_theme_suites().into_iter().find(|suite| suite.id == suite_id)
43    {
44        if let Some(theme_id) = suite_twin(&suite, current_theme_id, dark) {
45            return theme_id;
46        }
47    }
48    suggest_theme_for_scheme(dark)
49}
50
51/// The suite variant for the requested scheme.
52///
53/// Prefers the twin that shares the current id's qualifier (`-hard`,
54/// `-material`, ...), so `gruvbox-light` ⇄ `gruvbox-dark` and
55/// `gruvbox-material` ⇄ `gruvbox-material-light`. When the suite carries that
56/// qualifier on one side only, or the plain ids exist, the plain twin wins
57/// before readability ranking decides.
58fn suite_twin(suite: &ThemeSuite, current: &str, dark: bool) -> Option<&'static str> {
59    let qualifier = suite_qualifier(suite.id, current);
60    let candidates: Vec<&'static str> = suite
61        .theme_ids
62        .iter()
63        .copied()
64        .filter(|theme_id| is_light_theme(theme_id) != dark)
65        .collect();
66    if let Some(&exact) = candidates
67        .iter()
68        .find(|theme_id| suite_qualifier(suite.id, theme_id) == qualifier)
69    {
70        return Some(exact);
71    }
72    if let Some(&plain) = candidates
73        .iter()
74        .find(|theme_id| suite_qualifier(suite.id, theme_id).is_empty())
75    {
76        return Some(plain);
77    }
78    candidates
79        .into_iter()
80        .max_by(|left, right| variant_readability(left).total_cmp(&variant_readability(right)))
81}
82
83/// The qualifier a theme id adds after its suite id and scheme word, if any:
84/// `gruvbox-dark-hard` → `hard`, `gruvbox-material-dark` → `material`,
85/// `gruvbox` → empty.
86fn suite_qualifier<'a>(suite_id: &str, theme_id: &'a str) -> &'a str {
87    let qualifier = theme_id.strip_prefix(suite_id).unwrap_or_default().trim_start_matches('-');
88    if qualifier == "light" || qualifier == "dark" {
89        return "";
90    }
91    qualifier
92        .strip_prefix("light-")
93        .or_else(|| qualifier.strip_prefix("dark-"))
94        .unwrap_or(qualifier)
95}
96
97/// Contrast of a theme's text against its own background; used only to rank
98/// variants inside one suite.
99fn variant_readability(theme_id: &str) -> f32 {
100    theme_definition(theme_id)
101        .map(|theme| contrast_ratio(theme.palette.foreground, theme.palette.background))
102        .unwrap_or(0.0)
103}