1use 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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct UiThemeColors {
22 pub background: RgbColor,
23 pub foreground: RgbColor,
24 pub accent: RgbColor,
26 pub secondary: RgbColor,
28 pub danger: RgbColor,
29 pub warning: RgbColor,
30 pub info: RgbColor,
31 pub success: RgbColor,
32 pub muted: RgbColor,
34 pub accent_contrast: RgbColor,
36}
37
38#[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
49pub const fn rgb_u32(color: RgbColor) -> u32 {
51 (color.0 as u32) << 16 | (color.1 as u32) << 8 | color.2 as u32
52}
53
54pub fn is_gpui_kit_theme(theme_id: &str) -> bool {
56 find_spec(theme_id).is_some()
57}
58
59pub fn gpui_kit_theme_label(theme_id: &str) -> Option<&'static str> {
61 find_spec(theme_id).map(|spec| spec.label)
62}
63
64pub fn gpui_kit_theme_colors(theme_id: &str) -> Option<UiThemeColors> {
68 find_spec(theme_id).map(|spec| colors_for(&spec.palette))
69}
70
71pub 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
77pub 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 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
141fn 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
162fn 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}