diff --git a/crates/anstyle-git/src/lib.rs b/crates/anstyle-git/src/lib.rs index fb602557..7b7e2a81 100644 --- a/crates/anstyle-git/src/lib.rs +++ b/crates/anstyle-git/src/lib.rs @@ -110,11 +110,16 @@ fn parse_color(word: &str) -> Result, ()> { "cyan" => Some(anstyle::AnsiColor::Cyan.into()), "white" => Some(anstyle::AnsiColor::White.into()), _ => { - if word.starts_with('#') && word.len() == 7 { + if let Some(hex) = word.strip_prefix('#') { + let l = hex.len(); + if l != 3 && l != 6 { + return Err(()); + } + let l = l / 3; if let (Ok(r), Ok(g), Ok(b)) = ( - u8::from_str_radix(&word[1..3], 16), - u8::from_str_radix(&word[3..5], 16), - u8::from_str_radix(&word[5..7], 16), + u8::from_str_radix(&hex[0..l], 16), + u8::from_str_radix(&hex[l..(2 * l)], 16), + u8::from_str_radix(&hex[(2 * l)..(3 * l)], 16), ) { Some(anstyle::Color::from((r, g, b))) } else {