use concat_string::concat_string; use itertools::Itertools; use tui::style::{Color, Style}; use unicode_segmentation::UnicodeSegmentation; use crate::utils::error; pub const FIRST_COLOUR: Color = Color::LightMagenta; pub const SECOND_COLOUR: Color = Color::LightYellow; pub const THIRD_COLOUR: Color = Color::LightCyan; pub const FOURTH_COLOUR: Color = Color::LightGreen; #[cfg(not(target_os = "windows"))] pub const FIFTH_COLOUR: Color = Color::LightRed; pub const HIGHLIGHT_COLOUR: Color = Color::LightBlue; pub const AVG_COLOUR: Color = Color::Red; pub const ALL_COLOUR: Color = Color::Green; /// Convert a hex string to a colour. fn convert_hex_to_color(hex: &str) -> error::Result { fn hex_component_to_int(hex: &str, first: &str, second: &str) -> error::Result { u8::from_str_radix(&concat_string!(first, second), 16).map_err(|_| { error::BottomError::ConfigError(format!( "\"{hex}\" is an invalid hex color, could not decode." )) }) } fn invalid_hex_format(hex: &str) -> error::BottomError { error::BottomError::ConfigError(format!( "\"{hex}\" is an invalid hex color. It must be either a 7 character hex string of the form \"#12ab3c\" or a 3 character hex string of the form \"#1a2\".", )) } if !hex.starts_with('#') { return Err(invalid_hex_format(hex)); } let components = hex.graphemes(true).collect_vec(); if components.len() == 7 { // A 6-long hex. let r = hex_component_to_int(hex, components[1], components[2])?; let g = hex_component_to_int(hex, components[3], components[4])?; let b = hex_component_to_int(hex, components[5], components[6])?; Ok(Color::Rgb(r, g, b)) } else if components.len() == 4 { // A 3-long hex. let r = hex_component_to_int(hex, components[1], components[1])?; let g = hex_component_to_int(hex, components[2], components[2])?; let b = hex_component_to_int(hex, components[3], components[3])?; Ok(Color::Rgb(r, g, b)) } else { Err(invalid_hex_format(hex)) } } pub fn str_to_fg(input_val: &str) -> error::Result