Skip to content

Commit

Permalink
feat: Support short hex colors #RGB for compatibility with git 2.46
Browse files Browse the repository at this point in the history
git 2.46 adds support for 12-bit hex colors, written `#RGB` with three
hex digits, in addition to the existing 24-bit `#RRGGBB` support with
six. Add support for this.
  • Loading branch information
joshtriplett committed Jul 13, 2024
1 parent 76f6849 commit 4fa09f7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/anstyle-git/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ fn parse_color(word: &str) -> Result<Option<anstyle::Color>, ()> {
"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 {
Expand Down

0 comments on commit 4fa09f7

Please sign in to comment.