feat: allow hex color

This commit is contained in:
Pihkaal
2024-01-18 23:22:51 +01:00
parent e31f16d9a8
commit 2be41dd4d4
2 changed files with 35 additions and 7 deletions

10
config
View File

@@ -13,18 +13,16 @@ fps=30
[styling]
# Which color mode to use
# Value: "term", "rgb" or "ansi"
color_mode=term
# Value: "term", "hex" or "ansi"
color_mode=hex
# Loaded if color_mode is set to "term"
# Value: 0-15
color_term=7
# Loaded if color_mode is set to "rgb"
# Loaded if color_mode is set to "hex"
# Value: 0-255
color_r=255
color_g=0
color_b=0
color_hex=e6ecfe
# Loaded if color_mode is set to "ansi"
# Value: 0-255

View File

@@ -30,7 +30,10 @@ fn load_color(ini: &Ini) -> Color {
let color = ini.getint("styling", "color_term").unwrap().unwrap();
return load_term_color(color);
}
"rgb" => todo!(),
"hex" => {
let color = ini.get("styling", "color_hex").unwrap();
return load_hex_color(&color);
}
"ansi" => todo!(),
_ => panic!("ERROR: Invalid color mode: {}", color_mode),
}
@@ -57,3 +60,30 @@ fn load_term_color(value: i64) -> Color {
_ => panic!("ERROR: Invalid terminal color: {}", value),
}
}
fn load_hex_color(value: &str) -> Color {
// Expand #XXX colors
let value = if value.len() == 3 {
format!(
"{}{}{}{}{}{}",
&value[0..1],
&value[0..1],
&value[1..2],
&value[1..2],
&value[2..3],
&value[2..3]
)
} else {
value.to_owned()
};
if value.len() != 6 {
panic!("ERROR: Invalid hex color: {}", value);
}
let r = u8::from_str_radix(&value[0..2], 16).unwrap();
let g = u8::from_str_radix(&value[2..4], 16).unwrap();
let b = u8::from_str_radix(&value[4..6], 16).unwrap();
return Color::Rgb { r, g, b };
}