Allow ANSI colors in themes (#5119)

pull/7382/head
tomleb 1 year ago committed by GitHub
parent d5af6031f6
commit 29638babea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -390,8 +390,23 @@ impl ThemePalette {
Self { palette: default } Self { palette: default }
} }
pub fn hex_string_to_rgb(s: &str) -> Result<Color, String> { pub fn string_to_rgb(s: &str) -> Result<Color, String> {
if s.starts_with('#') && s.len() >= 7 { if s.starts_with('#') {
Self::hex_string_to_rgb(s)
} else {
Self::ansi_string_to_rgb(s)
}
}
fn ansi_string_to_rgb(s: &str) -> Result<Color, String> {
if let Ok(index) = s.parse::<u8>() {
return Ok(Color::Indexed(index));
}
Err(format!("Theme: malformed ANSI: {}", s))
}
fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
if s.len() >= 7 {
if let (Ok(red), Ok(green), Ok(blue)) = ( if let (Ok(red), Ok(green), Ok(blue)) = (
u8::from_str_radix(&s[1..3], 16), u8::from_str_radix(&s[1..3], 16),
u8::from_str_radix(&s[3..5], 16), u8::from_str_radix(&s[3..5], 16),
@ -417,7 +432,7 @@ impl ThemePalette {
.get(value) .get(value)
.copied() .copied()
.ok_or("") .ok_or("")
.or_else(|_| Self::hex_string_to_rgb(value)) .or_else(|_| Self::string_to_rgb(value))
} }
pub fn parse_modifier(value: &Value) -> Result<Modifier, String> { pub fn parse_modifier(value: &Value) -> Result<Modifier, String> {
@ -493,7 +508,7 @@ impl TryFrom<Value> for ThemePalette {
let mut palette = HashMap::with_capacity(map.len()); let mut palette = HashMap::with_capacity(map.len());
for (name, value) in map { for (name, value) in map {
let value = Self::parse_value_as_str(&value)?; let value = Self::parse_value_as_str(&value)?;
let color = Self::hex_string_to_rgb(value)?; let color = Self::string_to_rgb(value)?;
palette.insert(name, color); palette.insert(name, color);
} }

Loading…
Cancel
Save