vtcode_commons/
color256_theme.rs1use std::sync::atomic::{AtomicU8, Ordering};
8
9const HARMONIOUS_FALSE: u8 = 0;
10const HARMONIOUS_TRUE: u8 = 1;
11const HARMONIOUS_UNSET: u8 = 2;
12
13static RUNTIME_HARMONIOUS_HINT: AtomicU8 = AtomicU8::new(HARMONIOUS_UNSET);
14
15fn resolve_harmony(
16 is_light_theme: bool,
17 env_override: Option<bool>,
18 runtime_hint: Option<bool>,
19) -> bool {
20 env_override.or(runtime_hint).unwrap_or(!is_light_theme)
21}
22
23fn is_harmonious_for_theme(is_light_theme: bool) -> bool {
30 resolve_harmony(
31 is_light_theme,
32 harmonious_override(),
33 harmonious_runtime_hint(),
34 )
35}
36
37fn harmonious_runtime_hint() -> Option<bool> {
38 match RUNTIME_HARMONIOUS_HINT.load(Ordering::Relaxed) {
39 HARMONIOUS_TRUE => Some(true),
40 HARMONIOUS_FALSE => Some(false),
41 _ => None,
42 }
43}
44
45pub fn set_harmonious_runtime_hint(value: Option<bool>) {
50 let encoded = match value {
51 Some(true) => HARMONIOUS_TRUE,
52 Some(false) => HARMONIOUS_FALSE,
53 None => HARMONIOUS_UNSET,
54 };
55 RUNTIME_HARMONIOUS_HINT.store(encoded, Ordering::Relaxed);
56}
57
58fn gray_index(level: u8) -> u8 {
60 232 + (23 - level.min(23))
61}
62
63fn cube_index(r: u8, g: u8, b: u8) -> u8 {
65 let r = r.min(5);
66 let g = g.min(5);
67 let b = b.min(5);
68
69 let max = r.max(g).max(b) as i16;
70 let min = r.min(g).min(b) as i16;
71 let offset = 5 - max - min;
72
73 let r = ((r as i16 + offset).clamp(0, 5)) as u8;
74 let g = ((g as i16 + offset).clamp(0, 5)) as u8;
75 let b = ((b as i16 + offset).clamp(0, 5)) as u8;
76
77 16 + 36 * r + 6 * g + b
78}
79
80fn adjust_index(index: u8, is_harmonious: bool) -> u8 {
86 if is_harmonious {
87 return index;
88 }
89
90 match index {
91 16..=231 => {
92 let adjusted = index - 16;
93 let r = adjusted / 36;
94 let g = (adjusted % 36) / 6;
95 let b = adjusted % 6;
96 cube_index(r, g, b)
97 }
98 232..=255 => gray_index(index - 232),
99 _ => index,
100 }
101}
102
103pub fn adjust_index_for_theme(index: u8, is_light_theme: bool) -> u8 {
105 adjust_index(index, is_harmonious_for_theme(is_light_theme))
106}
107
108pub fn rgb_to_ansi256_for_theme(r: u8, g: u8, b: u8, is_light_theme: bool) -> u8 {
110 let base_index = if r == g && g == b {
111 if r < 8 {
112 16
113 } else if r > 248 {
114 231
115 } else {
116 ((r as u16 - 8) / 10) as u8 + 232
117 }
118 } else {
119 let r_index = ((r as u16 * 5) / 255) as u8;
120 let g_index = ((g as u16 * 5) / 255) as u8;
121 let b_index = ((b as u16 * 5) / 255) as u8;
122 16 + 36 * r_index + 6 * g_index + b_index
123 };
124
125 adjust_index_for_theme(base_index, is_light_theme)
126}
127
128fn parse_bool(value: &str) -> Option<bool> {
129 match value.trim().to_ascii_lowercase().as_str() {
130 "1" | "true" | "yes" | "on" => Some(true),
131 "0" | "false" | "no" | "off" => Some(false),
132 _ => None,
133 }
134}
135
136fn harmonious_override() -> Option<bool> {
137 std::env::var("VTCODE_256_HARMONIOUS")
138 .ok()
139 .and_then(|value| parse_bool(&value))
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn harmonious_indices_are_identity() {
148 assert_eq!(adjust_index(16, true), 16);
149 assert_eq!(adjust_index(231, true), 231);
150 assert_eq!(adjust_index(232, true), 232);
151 assert_eq!(adjust_index(255, true), 255);
152 assert_eq!(adjust_index(194, true), 194);
153 }
154
155 #[test]
156 fn non_harmonious_gray_and_cube_reflect() {
157 assert_eq!(gray_index(0), 255);
158 assert_eq!(gray_index(23), 232);
159 assert_eq!(cube_index(0, 0, 0), 231);
160 assert_eq!(cube_index(5, 5, 5), 16);
161 }
162
163 #[test]
164 fn non_harmonious_adjusts_existing_indices() {
165 assert_eq!(adjust_index(194, false), 22);
166 assert_eq!(adjust_index(224, false), 52);
167 assert_eq!(adjust_index(233, false), 254);
168 assert_eq!(adjust_index(14, false), 14);
169 }
170
171 #[test]
172 fn rgb_to_ansi256_applies_theme_adjustment() {
173 assert_eq!(rgb_to_ansi256_for_theme(0, 0, 0, false), 16);
174 assert_eq!(rgb_to_ansi256_for_theme(0, 0, 0, true), 231);
175 assert_eq!(rgb_to_ansi256_for_theme(255, 255, 255, false), 231);
176 assert_eq!(rgb_to_ansi256_for_theme(255, 255, 255, true), 16);
177 }
178
179 #[test]
180 fn resolve_harmony_precedence_is_env_then_runtime_then_default() {
181 assert!(resolve_harmony(true, Some(true), Some(false)));
182 assert!(!resolve_harmony(false, Some(false), Some(true)));
183 assert!(resolve_harmony(true, None, Some(true)));
184 assert!(!resolve_harmony(true, None, None));
185 assert!(resolve_harmony(false, None, None));
186 }
187}