gitfetch_rs/display/
colors.rs

1use anyhow::Result;
2
3pub fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
4  let hex = hex.trim_start_matches('#');
5
6  let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0);
7  let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0);
8  let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0);
9
10  (r, g, b)
11}
12
13pub fn get_ansi_color(hex: &str) -> Result<String> {
14  let (r, g, b) = hex_to_rgb(hex);
15  Ok(format!("\x1b[38;2;{};{};{}m", r, g, b))
16}