use anstyle::{Color, RgbColor};
use vtcode_config::constants::ui;
pub(super) const TEXT_HEADROOM_RATIO: f32 = 12.0;
use crate::theme::color_math::{aa_muted_text, contrast_ratio, filled_text, mix, relative_luminance};
use crate::theme::gpui_kit::{UiThemePalette, UiThemeSpec, theme_specs};
use crate::theme::registry::theme_definition;
use crate::theme::types::ColorAccessibilityConfig;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UiThemeColors {
pub background: RgbColor,
pub foreground: RgbColor,
pub accent: RgbColor,
pub secondary: RgbColor,
pub danger: RgbColor,
pub warning: RgbColor,
pub info: RgbColor,
pub success: RgbColor,
pub muted: RgbColor,
pub accent_contrast: RgbColor,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UiAnsiSlots {
pub red: RgbColor,
pub green: RgbColor,
pub yellow: RgbColor,
pub blue: RgbColor,
pub magenta: RgbColor,
pub cyan: RgbColor,
}
pub const fn rgb_u32(color: RgbColor) -> u32 {
(color.0 as u32) << 16 | (color.1 as u32) << 8 | color.2 as u32
}
pub fn is_gpui_kit_theme(theme_id: &str) -> bool {
find_spec(theme_id).is_some()
}
pub fn gpui_kit_theme_label(theme_id: &str) -> Option<&'static str> {
find_spec(theme_id).map(|spec| spec.label)
}
pub fn gpui_kit_theme_colors(theme_id: &str) -> Option<UiThemeColors> {
find_spec(theme_id).map(|spec| colors_for(&spec.palette))
}
pub fn gpui_kit_theme_colors_all() -> impl Iterator<Item = (&'static str, bool, UiThemeColors)> {
theme_specs().iter().map(|spec| (spec.id, spec.dark, colors_for(&spec.palette)))
}
pub fn builtin_theme_colors(theme_id: &str) -> Option<UiThemeColors> {
let palette = &theme_definition(theme_id)?.palette;
let styles = palette.build_styles_with_accessibility(&ColorAccessibilityConfig::default());
let rgb = |color: Option<Color>, fallback: RgbColor| match color {
Some(Color::Rgb(color)) => color,
_ => fallback,
};
let original_foreground = rgb(Some(styles.foreground), palette.foreground);
let black = RgbColor(0, 0, 0);
let white = RgbColor(255, 255, 255);
let target = if contrast_ratio(black, palette.background) > contrast_ratio(white, palette.background) {
black
} else {
white
};
let foreground = (0..=100)
.map(|step| mix(original_foreground, target, step as f32 / 100.0))
.find(|color| contrast_ratio(*color, palette.background) >= TEXT_HEADROOM_RATIO)
.unwrap_or(target);
let accent = rgb(styles.mcp.get_fg_color(), foreground);
Some(UiThemeColors {
background: palette.background,
foreground,
accent,
secondary: rgb(styles.secondary.get_fg_color(), foreground),
danger: rgb(styles.error.get_fg_color(), foreground),
warning: rgb(styles.warning.get_fg_color(), foreground),
info: rgb(styles.info.get_fg_color(), foreground),
success: if relative_luminance(palette.background) > 0.5 {
RgbColor(0x23, 0x7a, 0x45)
} else {
RgbColor(0x63, 0xc7, 0x79)
},
muted: aa_muted_text(palette.background, foreground),
accent_contrast: filled_text(accent),
})
}
fn find_spec(theme_id: &str) -> Option<&'static UiThemeSpec> {
let trimmed = theme_id.trim();
theme_specs().iter().find(|spec| spec.id.eq_ignore_ascii_case(trimmed))
}
fn colors_for(palette: &UiThemePalette) -> UiThemeColors {
let accent = pick_accent(&[palette.accent], palette);
UiThemeColors {
background: palette.background,
foreground: palette.foreground,
accent,
secondary: pick_accent(&[palette.magenta, palette.cyan, palette.blue], palette),
danger: pick_accent(&[palette.red], palette),
warning: warning_for(palette),
info: pick_accent(&[palette.blue, palette.cyan], palette),
success: pick_accent(&[palette.green], palette),
muted: aa_muted_text(palette.background, palette.foreground),
accent_contrast: filled_text(accent),
}
}
fn pick_accent(candidates: &[RgbColor], palette: &UiThemePalette) -> RgbColor {
let floor = ui::THEME_MIN_CONTRAST_RATIO;
if let Some(&readable) = candidates.iter().find(|&&c| contrast_ratio(c, palette.background) >= floor) {
return readable;
}
candidates
.iter()
.copied()
.reduce(|best, candidate| {
if contrast_ratio(candidate, palette.background) > contrast_ratio(best, palette.background) {
candidate
} else {
best
}
})
.unwrap_or(palette.foreground)
}
fn warning_for(palette: &UiThemePalette) -> RgbColor {
const AMBER: RgbColor = RgbColor(0xD9, 0x9A, 0x4E);
const DARK_AMBER: RgbColor = RgbColor(0x98, 0x63, 0x11);
let (candidate, alternate) = if relative_luminance(palette.background) < 0.5 {
(AMBER, DARK_AMBER)
} else {
(DARK_AMBER, AMBER)
};
pick_accent(&[candidate, alternate, palette.yellow], palette)
}