Skip to main content

vtcode_ui/theme/
ui_palette.rs

1//! Shared UI palette tokens derived from the CLI theme registry.
2//!
3//! [`builtin_theme_colors`] is the consumer API: the desktop app (and any
4//! other UI consumer) renders these colors, which run through the same
5//! accessible style pipeline as the TUI. The [`gpui_kit_theme_colors`] family
6//! instead exposes the raw upstream spec rows and exists to verify the port
7//! (scheme agreement, upstream hue pins); for curated themes (Catppuccin,
8//! Gruvbox, Solarized) the registry intentionally overrides the spec rows, so
9//! the two answers differ there by design.
10
11use anstyle::{Color, RgbColor};
12use vtcode_config::constants::ui;
13
14use crate::theme::color_math::{aa_muted_text, contrast_ratio, filled_text, mix, relative_luminance};
15use crate::theme::gpui_kit::{UiThemePalette, UiThemeSpec, theme_specs};
16use crate::theme::registry::theme_definition;
17use crate::theme::types::ColorAccessibilityConfig;
18
19/// Semantic palette colors for one theme, ready for RGB/HSLA consumers.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct UiThemeColors {
22    pub background: RgbColor,
23    pub foreground: RgbColor,
24    /// Brand accent: primary buttons, badges, focus rings.
25    pub accent: RgbColor,
26    /// Quieter companion accent for secondary highlights.
27    pub secondary: RgbColor,
28    pub danger: RgbColor,
29    pub warning: RgbColor,
30    pub info: RgbColor,
31    pub success: RgbColor,
32    /// Muted text that still clears the WCAG AA floor.
33    pub muted: RgbColor,
34    /// Nearest readable text color on a filled accent surface.
35    pub accent_contrast: RgbColor,
36}
37
38/// The six ANSI hue slots carried by every VT Code theme.
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub struct UiAnsiSlots {
41    pub red: RgbColor,
42    pub green: RgbColor,
43    pub yellow: RgbColor,
44    pub blue: RgbColor,
45    pub magenta: RgbColor,
46    pub cyan: RgbColor,
47}
48
49/// Pack an RGB color as `0xRRGGBB`, the form GPUI/theme pickers consume.
50pub const fn rgb_u32(color: RgbColor) -> u32 {
51    (color.0 as u32) << 16 | (color.1 as u32) << 8 | color.2 as u32
52}
53
54/// Whether a theme id belongs to the upstream gpui-kit theme sets.
55pub fn is_gpui_kit_theme(theme_id: &str) -> bool {
56    find_spec(theme_id).is_some()
57}
58
59/// Display label of a gpui-kit theme id, for pickers and docs.
60pub fn gpui_kit_theme_label(theme_id: &str) -> Option<&'static str> {
61    find_spec(theme_id).map(|spec| spec.label)
62}
63
64/// Derive the raw upstream palette tokens for a gpui-kit theme id.
65/// Port-verification oracle only; UI consumers must use
66/// [`builtin_theme_colors`], which reflects the curated registry entries.
67pub fn gpui_kit_theme_colors(theme_id: &str) -> Option<UiThemeColors> {
68    find_spec(theme_id).map(|spec| colors_for(&spec.palette))
69}
70
71/// Every gpui-kit theme as `(id, dark, colors)`, in upstream order.
72/// Port-verification oracle only; see [`gpui_kit_theme_colors`].
73pub fn gpui_kit_theme_colors_all() -> impl Iterator<Item = (&'static str, bool, UiThemeColors)> {
74    theme_specs().iter().map(|spec| (spec.id, spec.dark, colors_for(&spec.palette)))
75}
76
77/// Shared desktop-ready colors for any built-in CLI theme.
78/// Uses the CLI registry and the same accessible text and semantic styles as the TUI.
79pub fn builtin_theme_colors(theme_id: &str) -> Option<UiThemeColors> {
80    let palette = &theme_definition(theme_id)?.palette;
81    let styles = palette.build_styles_with_accessibility(&ColorAccessibilityConfig::default());
82    let rgb = |color: Option<Color>, fallback: RgbColor| match color {
83        Some(Color::Rgb(color)) => color,
84        _ => fallback,
85    };
86    let original_foreground = rgb(Some(styles.foreground), palette.foreground);
87    // Desktop needs enough contrast headroom for raised surfaces as well as
88    // the canvas. Move toward the best neutral only when the CLI text color
89    // cannot support those surfaces, preserving its hue as far as possible.
90    let black = RgbColor(0, 0, 0);
91    let white = RgbColor(255, 255, 255);
92    let target = if contrast_ratio(black, palette.background) > contrast_ratio(white, palette.background) {
93        black
94    } else {
95        white
96    };
97    let foreground = (0..=100)
98        .map(|step| mix(original_foreground, target, step as f32 / 100.0))
99        .find(|color| contrast_ratio(*color, palette.background) >= 7.0)
100        .unwrap_or(target);
101    let accent = rgb(styles.mcp.get_fg_color(), foreground);
102    Some(UiThemeColors {
103        background: palette.background,
104        foreground,
105        accent,
106        secondary: rgb(styles.secondary.get_fg_color(), foreground),
107        danger: rgb(styles.error.get_fg_color(), foreground),
108        warning: rgb(styles.warning.get_fg_color(), foreground),
109        info: rgb(styles.info.get_fg_color(), foreground),
110        success: if relative_luminance(palette.background) > 0.5 {
111            RgbColor(0x23, 0x7a, 0x45)
112        } else {
113            RgbColor(0x63, 0xc7, 0x79)
114        },
115        muted: aa_muted_text(palette.background, foreground),
116        accent_contrast: filled_text(accent),
117    })
118}
119
120fn find_spec(theme_id: &str) -> Option<&'static UiThemeSpec> {
121    let trimmed = theme_id.trim();
122    theme_specs().iter().find(|spec| spec.id.eq_ignore_ascii_case(trimmed))
123}
124
125fn colors_for(palette: &UiThemePalette) -> UiThemeColors {
126    let accent = pick_accent(&[palette.accent], palette);
127    UiThemeColors {
128        background: palette.background,
129        foreground: palette.foreground,
130        accent,
131        secondary: pick_accent(&[palette.magenta, palette.cyan, palette.blue], palette),
132        danger: pick_accent(&[palette.red], palette),
133        warning: warning_for(palette),
134        info: pick_accent(&[palette.blue, palette.cyan], palette),
135        success: pick_accent(&[palette.green], palette),
136        muted: aa_muted_text(palette.background, palette.foreground),
137        accent_contrast: filled_text(accent),
138    }
139}
140
141/// Keep the intended hue: first candidate that clears the configured contrast
142/// floor wins; otherwise the most readable candidate (the shared style
143/// pipeline then re-applies the floor with the foreground as fallback).
144fn pick_accent(candidates: &[RgbColor], palette: &UiThemePalette) -> RgbColor {
145    let floor = ui::THEME_MIN_CONTRAST_RATIO;
146    if let Some(&readable) = candidates.iter().find(|&&c| contrast_ratio(c, palette.background) >= floor) {
147        return readable;
148    }
149    candidates
150        .iter()
151        .copied()
152        .reduce(|best, candidate| {
153            if contrast_ratio(candidate, palette.background) > contrast_ratio(best, palette.background) {
154                candidate
155            } else {
156                best
157            }
158        })
159        .unwrap_or(palette.foreground)
160}
161
162/// Warning keeps the dedicated amber semantics the CLI token uses, falling
163/// back to the theme yellow and then the default amber before readability.
164fn warning_for(palette: &UiThemePalette) -> RgbColor {
165    const AMBER: RgbColor = RgbColor(0xD9, 0x9A, 0x4E);
166    const DARK_AMBER: RgbColor = RgbColor(0x98, 0x63, 0x11);
167    let (candidate, alternate) = if relative_luminance(palette.background) < 0.5 {
168        (AMBER, DARK_AMBER)
169    } else {
170        (DARK_AMBER, AMBER)
171    };
172    pick_accent(&[candidate, alternate, palette.yellow], palette)
173}