Merge branch 'bb/rgb-12-bit-colors'

The color parsing code learned to handle 12-bit RGB colors, spelled
as "#RGB" (in addition to "#RRGGBB" that is already supported).

* bb/rgb-12-bit-colors:
  color: add support for 12-bit RGB colors
  t/t4026-color: add test coverage for invalid RGB colors
  t/t4026-color: remove an extra double quote character
This commit is contained in:
Junio C Hamano 2024-05-08 10:18:47 -07:00
commit 20ceead5c3
4 changed files with 41 additions and 12 deletions

View file

@ -316,7 +316,8 @@ terminals, this is usually not the same as setting to "white black".
Colors may also be given as numbers between 0 and 255; these use ANSI Colors may also be given as numbers between 0 and 255; these use ANSI
256-color mode (but note that not all terminals may support this). If 256-color mode (but note that not all terminals may support this). If
your terminal supports it, you may also specify 24-bit RGB values as your terminal supports it, you may also specify 24-bit RGB values as
hex, like `#ff0ab3`. hex, like `#ff0ab3`, or 12-bit RGB values like `#f1b`, which is
equivalent to the 24-bit color `#ff11bb`.
+ +
The accepted attributes are `bold`, `dim`, `ul`, `blink`, `reverse`, The accepted attributes are `bold`, `dim`, `ul`, `blink`, `reverse`,
`italic`, and `strike` (for crossed-out or "strikethrough" letters). `italic`, and `strike` (for crossed-out or "strikethrough" letters).

21
color.c
View file

@ -64,12 +64,16 @@ static int match_word(const char *word, int len, const char *match)
return !strncasecmp(word, match, len) && !match[len]; return !strncasecmp(word, match, len) && !match[len];
} }
static int get_hex_color(const char *in, unsigned char *out) static int get_hex_color(const char **inp, int width, unsigned char *out)
{ {
const char *in = *inp;
unsigned int val; unsigned int val;
val = (hexval(in[0]) << 4) | hexval(in[1]);
assert(width == 1 || width == 2);
val = (hexval(in[0]) << 4) | hexval(in[width - 1]);
if (val & ~0xff) if (val & ~0xff)
return -1; return -1;
*inp += width;
*out = val; *out = val;
return 0; return 0;
} }
@ -135,11 +139,14 @@ static int parse_color(struct color *out, const char *name, int len)
return 0; return 0;
} }
/* Try a 24-bit RGB value */ /* Try a 24- or 12-bit RGB value prefixed with '#' */
if (len == 7 && name[0] == '#') { if ((len == 7 || len == 4) && name[0] == '#') {
if (!get_hex_color(name + 1, &out->red) && int width_per_color = (len == 7) ? 2 : 1;
!get_hex_color(name + 3, &out->green) && const char *color = name + 1;
!get_hex_color(name + 5, &out->blue)) {
if (!get_hex_color(&color, width_per_color, &out->red) &&
!get_hex_color(&color, width_per_color, &out->green) &&
!get_hex_color(&color, width_per_color, &out->blue)) {
out->type = COLOR_RGB; out->type = COLOR_RGB;
return 0; return 0;
} }

View file

@ -112,7 +112,8 @@ int want_color_fd(int fd, int var);
* Translate a Git color from 'value' into a string that the terminal can * Translate a Git color from 'value' into a string that the terminal can
* interpret and store it into 'dst'. The Git color values are of the form * interpret and store it into 'dst'. The Git color values are of the form
* "foreground [background] [attr]" where fore- and background can be a color * "foreground [background] [attr]" where fore- and background can be a color
* name ("red"), a RGB code (#0xFF0000) or a 256-color-mode from the terminal. * name ("red"), a RGB code (#FF0000 or #F00) or a 256-color-mode from the
* terminal.
*/ */
int color_parse(const char *value, char *dst); int color_parse(const char *value, char *dst);
int color_parse_mem(const char *value, int len, char *dst); int color_parse_mem(const char *value, int len, char *dst);

View file

@ -96,8 +96,8 @@ test_expect_success '256 colors' '
color "254 bold 255" "[1;38;5;254;48;5;255m" color "254 bold 255" "[1;38;5;254;48;5;255m"
' '
test_expect_success '24-bit colors' ' test_expect_success 'RGB colors' '
color "#ff00ff black" "[38;2;255;0;255;40m" color "#ff00ff #0f0" "[38;2;255;0;255;48;2;0;255;0m"
' '
test_expect_success '"default" foreground' ' test_expect_success '"default" foreground' '
@ -112,7 +112,7 @@ test_expect_success '"default" can be combined with attributes' '
color "default default no-reverse bold" "[1;27;39;49m" color "default default no-reverse bold" "[1;27;39;49m"
' '
test_expect_success '"normal" yields no color at all"' ' test_expect_success '"normal" yields no color at all' '
color "normal black" "[40m" color "normal black" "[40m"
' '
@ -140,6 +140,26 @@ test_expect_success 'extra character after attribute' '
invalid_color "dimX" invalid_color "dimX"
' '
test_expect_success 'non-hex character in RGB color' '
invalid_color "#x23456" &&
invalid_color "#1x3456" &&
invalid_color "#12x456" &&
invalid_color "#123x56" &&
invalid_color "#1234x6" &&
invalid_color "#12345x" &&
invalid_color "#x23" &&
invalid_color "#1x3" &&
invalid_color "#12x"
'
test_expect_success 'wrong number of letters in RGB color' '
invalid_color "#1" &&
invalid_color "#23" &&
invalid_color "#789a" &&
invalid_color "#bcdef" &&
invalid_color "#1234567"
'
test_expect_success 'unknown color slots are ignored (diff)' ' test_expect_success 'unknown color slots are ignored (diff)' '
git config color.diff.nosuchslotwilleverbedefined white && git config color.diff.nosuchslotwilleverbedefined white &&
git diff --color git diff --color