Skip to content

Commit 20ceead

Browse files
committed
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
2 parents db05f61 + 7b97dfe commit 20ceead

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

Documentation/config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ terminals, this is usually not the same as setting to "white black".
316316
Colors may also be given as numbers between 0 and 255; these use ANSI
317317
256-color mode (but note that not all terminals may support this). If
318318
your terminal supports it, you may also specify 24-bit RGB values as
319-
hex, like `#ff0ab3`.
319+
hex, like `#ff0ab3`, or 12-bit RGB values like `#f1b`, which is
320+
equivalent to the 24-bit color `#ff11bb`.
320321
+
321322
The accepted attributes are `bold`, `dim`, `ul`, `blink`, `reverse`,
322323
`italic`, and `strike` (for crossed-out or "strikethrough" letters).

color.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ static int match_word(const char *word, int len, const char *match)
6464
return !strncasecmp(word, match, len) && !match[len];
6565
}
6666

67-
static int get_hex_color(const char *in, unsigned char *out)
67+
static int get_hex_color(const char **inp, int width, unsigned char *out)
6868
{
69+
const char *in = *inp;
6970
unsigned int val;
70-
val = (hexval(in[0]) << 4) | hexval(in[1]);
71+
72+
assert(width == 1 || width == 2);
73+
val = (hexval(in[0]) << 4) | hexval(in[width - 1]);
7174
if (val & ~0xff)
7275
return -1;
76+
*inp += width;
7377
*out = val;
7478
return 0;
7579
}
@@ -135,11 +139,14 @@ static int parse_color(struct color *out, const char *name, int len)
135139
return 0;
136140
}
137141

138-
/* Try a 24-bit RGB value */
139-
if (len == 7 && name[0] == '#') {
140-
if (!get_hex_color(name + 1, &out->red) &&
141-
!get_hex_color(name + 3, &out->green) &&
142-
!get_hex_color(name + 5, &out->blue)) {
142+
/* Try a 24- or 12-bit RGB value prefixed with '#' */
143+
if ((len == 7 || len == 4) && name[0] == '#') {
144+
int width_per_color = (len == 7) ? 2 : 1;
145+
const char *color = name + 1;
146+
147+
if (!get_hex_color(&color, width_per_color, &out->red) &&
148+
!get_hex_color(&color, width_per_color, &out->green) &&
149+
!get_hex_color(&color, width_per_color, &out->blue)) {
143150
out->type = COLOR_RGB;
144151
return 0;
145152
}

color.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ int want_color_fd(int fd, int var);
112112
* Translate a Git color from 'value' into a string that the terminal can
113113
* interpret and store it into 'dst'. The Git color values are of the form
114114
* "foreground [background] [attr]" where fore- and background can be a color
115-
* name ("red"), a RGB code (#0xFF0000) or a 256-color-mode from the terminal.
115+
* name ("red"), a RGB code (#FF0000 or #F00) or a 256-color-mode from the
116+
* terminal.
116117
*/
117118
int color_parse(const char *value, char *dst);
118119
int color_parse_mem(const char *value, int len, char *dst);

t/t4026-color.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ test_expect_success '256 colors' '
9696
color "254 bold 255" "[1;38;5;254;48;5;255m"
9797
'
9898

99-
test_expect_success '24-bit colors' '
100-
color "#ff00ff black" "[38;2;255;0;255;40m"
99+
test_expect_success 'RGB colors' '
100+
color "#ff00ff #0f0" "[38;2;255;0;255;48;2;0;255;0m"
101101
'
102102

103103
test_expect_success '"default" foreground' '
@@ -112,7 +112,7 @@ test_expect_success '"default" can be combined with attributes' '
112112
color "default default no-reverse bold" "[1;27;39;49m"
113113
'
114114

115-
test_expect_success '"normal" yields no color at all"' '
115+
test_expect_success '"normal" yields no color at all' '
116116
color "normal black" "[40m"
117117
'
118118

@@ -140,6 +140,26 @@ test_expect_success 'extra character after attribute' '
140140
invalid_color "dimX"
141141
'
142142

143+
test_expect_success 'non-hex character in RGB color' '
144+
invalid_color "#x23456" &&
145+
invalid_color "#1x3456" &&
146+
invalid_color "#12x456" &&
147+
invalid_color "#123x56" &&
148+
invalid_color "#1234x6" &&
149+
invalid_color "#12345x" &&
150+
invalid_color "#x23" &&
151+
invalid_color "#1x3" &&
152+
invalid_color "#12x"
153+
'
154+
155+
test_expect_success 'wrong number of letters in RGB color' '
156+
invalid_color "#1" &&
157+
invalid_color "#23" &&
158+
invalid_color "#789a" &&
159+
invalid_color "#bcdef" &&
160+
invalid_color "#1234567"
161+
'
162+
143163
test_expect_success 'unknown color slots are ignored (diff)' '
144164
git config color.diff.nosuchslotwilleverbedefined white &&
145165
git diff --color

0 commit comments

Comments
 (0)