From patchwork Mon Apr 29 16:48:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beat Bolli X-Patchwork-Id: 13647373 Received: from mail-gateway-shared13.cyon.net (mail-gateway-shared13.cyon.net [194.126.200.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B52D38612E for ; Mon, 29 Apr 2024 16:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.126.200.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409559; cv=none; b=ddJYn4QkYCPTMJNebf8Ou5cV2c8wLT0uaaPUb7JaDTVSB8WF/sYuSjGiJKYYuGLS16GLuzyn3G4/Wt/SlIP+ApTm0Z72hkz5dHlvZ5AHLZEi3rH2NXzjO516zftOM39PoU425J094UHMdDPeBqfyMO+W0mqrr8RSMg+Dt2QD8+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409559; c=relaxed/simple; bh=4nK/E7RABLdHXRTrdyxm0UlGWc5UrzrvngOw4ZvHUsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D5hyfCxDy11sCgrHHVb/yPt/NZpmw2Yygcdkw0xqkKWFt7QFFbQ4BmNM5/C6qc2X6rRRVBIhnS3kuSUliDXL2Q80ludv/WDJWQFXfs/RQ+4X8JFImVtV5j6iIXzdzUdFqAAX6XPF/99rrJrS+nAEvGRHkkIGJ9WsDMMMhRxr+y0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li; spf=pass smtp.mailfrom=drbeat.li; arc=none smtp.client-ip=194.126.200.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=drbeat.li Received: from s019.cyon.net ([149.126.4.28]) by mail-gateway-shared13.cyon.net with esmtpsa (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim) (envelope-from ) id 1s1UEu-000000007tb-0wFT for git@vger.kernel.org; Mon, 29 Apr 2024 18:52:28 +0200 Received: from [10.20.10.233] (port=58698 helo=mail.cyon.ch) by s019.cyon.net with esmtpa (Exim 4.96.2) (envelope-from ) id 1s1UEt-0069XH-0M; Mon, 29 Apr 2024 18:52:27 +0200 Received: from minibeat.bolli (minibeat.bolli [192.168.11.3]) by drbeat.li (Postfix) with SMTP id D3C371801C4; Mon, 29 Apr 2024 18:52:25 +0200 (CEST) Received: by minibeat.bolli (sSMTP sendmail emulation); Mon, 29 Apr 2024 18:52:25 +0200 From: "Beat Bolli" To: git@vger.kernel.org Cc: Jeff King , Beat Bolli Subject: [PATCH 1/3] t/t4026-color: remove an extra double quote character Date: Mon, 29 Apr 2024 18:48:47 +0200 Message-ID: <20240429164849.78509-2-dev+git@drbeat.li> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240429164849.78509-1-dev+git@drbeat.li> References: <20240429164849.78509-1-dev+git@drbeat.li> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - s019.cyon.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - drbeat.li X-Get-Message-Sender-Via: s019.cyon.net: authenticated_id: ig@drbeat.li X-Authenticated-Sender: s019.cyon.net: ig@drbeat.li This is most probably just an editing left-over from cb357221a4 (t4026: test "normal" color, 2014-11-20) which added this test. Signed-off-by: Beat Bolli --- t/t4026-color.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t4026-color.sh b/t/t4026-color.sh index cc3f60d468f4..37622451fc23 100755 --- a/t/t4026-color.sh +++ b/t/t4026-color.sh @@ -112,7 +112,7 @@ test_expect_success '"default" can be combined with attributes' ' 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" ' From patchwork Mon Apr 29 16:48:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beat Bolli X-Patchwork-Id: 13647374 Received: from mail-gateway-shared13.cyon.net (mail-gateway-shared13.cyon.net [194.126.200.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69A3233C7 for ; Mon, 29 Apr 2024 16:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.126.200.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409560; cv=none; b=h0bVQzoI0Kg3AJVCZzsNZKLyKDwoRWoYOzLtVnFjpCKHyPs8dCF/fHfeL/1y0NU9sryWgjH3WQOm1PxrHqbtKr78J2q8fCPjdaMXdYpsTHGn08Kphui6YstUE1HlJAlPQqkfBgCc5d4cZoZ2BrmZL/lQAyiC/wWfGMlwugJNSKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409560; c=relaxed/simple; bh=1djxXvxEB9KaD4L5pyk5dv8PE0vXkOiYcn5yWDSyKEs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dr/23wHoYooJPxoAWA0fSY7Pk9MbWI42WnfTxJSKc14kNQYYt9nz09VS8ycPPBPdxah4Fa69HfaweGdZhfPK7Rd1p4q3URTziNW/kP8ZbatXHkqJr4E2c4mU34rMeMU72aqYOQ+ujM7PG7/BQ4ewXHsmPZnQhlQ9NlMcNZVGMzY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li; spf=pass smtp.mailfrom=drbeat.li; arc=none smtp.client-ip=194.126.200.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=drbeat.li Received: from s019.cyon.net ([149.126.4.28]) by mail-gateway-shared13.cyon.net with esmtpsa (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim) (envelope-from ) id 1s1UEv-000000007tn-1aji for git@vger.kernel.org; Mon, 29 Apr 2024 18:52:29 +0200 Received: from [10.20.10.231] (port=33256 helo=mail.cyon.ch) by s019.cyon.net with esmtpa (Exim 4.96.2) (envelope-from ) id 1s1UEu-0069Y6-0p; Mon, 29 Apr 2024 18:52:28 +0200 Received: from minibeat.bolli (minibeat.bolli [192.168.11.3]) by drbeat.li (Postfix) with SMTP id E9D7D180197; Mon, 29 Apr 2024 18:52:26 +0200 (CEST) Received: by minibeat.bolli (sSMTP sendmail emulation); Mon, 29 Apr 2024 18:52:26 +0200 From: "Beat Bolli" To: git@vger.kernel.org Cc: Jeff King , Beat Bolli Subject: [PATCH 2/3] t/t4026-color: add test coverage for invalid RGB colors Date: Mon, 29 Apr 2024 18:48:48 +0200 Message-ID: <20240429164849.78509-3-dev+git@drbeat.li> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240429164849.78509-1-dev+git@drbeat.li> References: <20240429164849.78509-1-dev+git@drbeat.li> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - s019.cyon.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - drbeat.li X-Get-Message-Sender-Via: s019.cyon.net: authenticated_id: ig@drbeat.li X-Authenticated-Sender: s019.cyon.net: ig@drbeat.li Make sure that the RGB color parser rejects invalid characters. Signed-off-by: Beat Bolli --- t/t4026-color.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/t/t4026-color.sh b/t/t4026-color.sh index 37622451fc23..0c39bd74a613 100755 --- a/t/t4026-color.sh +++ b/t/t4026-color.sh @@ -140,6 +140,15 @@ test_expect_success 'extra character after attribute' ' 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" +' + test_expect_success 'unknown color slots are ignored (diff)' ' git config color.diff.nosuchslotwilleverbedefined white && git diff --color From patchwork Mon Apr 29 16:48:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Beat Bolli X-Patchwork-Id: 13647375 Received: from mail-gateway-shared13.cyon.net (mail-gateway-shared13.cyon.net [194.126.200.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33F378614C for ; Mon, 29 Apr 2024 16:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.126.200.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409561; cv=none; b=RPT3syXmVif2jjMA7LvSkpK5E0hkgi23VwsGIiZy+oXw3iFnH0E6qargZVZYV7oJ7J60PChByoG9bMQVmkTi60zXBTUXpUMQ9mWvWrha8uvEWMlCfyUlcplw+pB+wjUMrK5yY/RQMI53UqIihcsKzByYQ/KVfJN/2wVv2qZoN/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714409561; c=relaxed/simple; bh=EcTQzrZg8jOGG8P/qp9dkYtVi22MjECS0aSiqfu12Ys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jXn1/bQj9l9xwXZ2+TOICx85meLag5YsKTsULIgeJu75phDawI5hrYmHMOp9XnO/tyLUM8rey94IwLSAV9blIbQSKaOmp+swNKpnNuyxUzTE8CLHdBwXNeM5HvE/wYrx4BpzyyX0RbTvz6nBybKGzwtJEWAQztTgv/51na1XWEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li; spf=pass smtp.mailfrom=drbeat.li; arc=none smtp.client-ip=194.126.200.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=drbeat.li Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=drbeat.li Received: from s019.cyon.net ([149.126.4.28]) by mail-gateway-shared13.cyon.net with esmtpsa (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim) (envelope-from ) id 1s1UEw-000000007wy-20Kc for git@vger.kernel.org; Mon, 29 Apr 2024 18:52:31 +0200 Received: from [10.20.10.232] (port=34734 helo=mail.cyon.ch) by s019.cyon.net with esmtpa (Exim 4.96.2) (envelope-from ) id 1s1UEv-0069Yu-18; Mon, 29 Apr 2024 18:52:29 +0200 Received: from minibeat.bolli (minibeat.bolli [192.168.11.3]) by drbeat.li (Postfix) with SMTP id 0AB111801C4; Mon, 29 Apr 2024 18:52:28 +0200 (CEST) Received: by minibeat.bolli (sSMTP sendmail emulation); Mon, 29 Apr 2024 18:52:27 +0200 From: "Beat Bolli" To: git@vger.kernel.org Cc: Jeff King , Beat Bolli Subject: [PATCH 3/3] color: add support for 12-bit RGB colors Date: Mon, 29 Apr 2024 18:48:49 +0200 Message-ID: <20240429164849.78509-4-dev+git@drbeat.li> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240429164849.78509-1-dev+git@drbeat.li> References: <20240429164849.78509-1-dev+git@drbeat.li> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - s019.cyon.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - drbeat.li X-Get-Message-Sender-Via: s019.cyon.net: authenticated_id: ig@drbeat.li X-Authenticated-Sender: s019.cyon.net: ig@drbeat.li RGB color parsing currently supports 24-bit values in the form #RRGGBB. As in Cascading Style Sheets (CSS [1]), also allow to specify an RGB color using only three digits with #RGB. In this shortened form, each of the digits is – again, as in CSS – duplicated to convert the color to 24 bits, e.g. #f1b specifies the same color as #ff11bb. In color.h, remove the '0x' prefix in the example to match the actual syntax. [1] https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color Signed-off-by: Beat Bolli --- Documentation/config.txt | 3 ++- color.c | 21 ++++++++++++++------- color.h | 3 ++- t/t4026-color.sh | 9 ++++++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 70b448b13262..6f649c997c0f 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -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 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 -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`, `italic`, and `strike` (for crossed-out or "strikethrough" letters). diff --git a/color.c b/color.c index f663c06ac4ed..227a5ab2f42e 100644 --- a/color.c +++ b/color.c @@ -64,12 +64,16 @@ static int match_word(const char *word, int len, const char *match) 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; - 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) return -1; + *inp += width; *out = val; return 0; } @@ -135,11 +139,14 @@ static int parse_color(struct color *out, const char *name, int len) return 0; } - /* Try a 24-bit RGB value */ - if (len == 7 && name[0] == '#') { - if (!get_hex_color(name + 1, &out->red) && - !get_hex_color(name + 3, &out->green) && - !get_hex_color(name + 5, &out->blue)) { + /* Try a 24- or 12-bit RGB value prefixed with '#' */ + if ((len == 7 || len == 4) && name[0] == '#') { + int width_per_color = (len == 7) ? 2 : 1; + const char *color = name + 1; + + 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; return 0; } diff --git a/color.h b/color.h index bb28343be210..7ed259a35bb4 100644 --- a/color.h +++ b/color.h @@ -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 * 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 - * 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_mem(const char *value, int len, char *dst); diff --git a/t/t4026-color.sh b/t/t4026-color.sh index 0c39bd74a613..9a6f8a4bc5bf 100755 --- a/t/t4026-color.sh +++ b/t/t4026-color.sh @@ -96,8 +96,8 @@ test_expect_success '256 colors' ' color "254 bold 255" "[1;38;5;254;48;5;255m" ' -test_expect_success '24-bit colors' ' - color "#ff00ff black" "[38;2;255;0;255;40m" +test_expect_success 'RGB colors' ' + color "#ff00ff #0f0" "[38;2;255;0;255;48;2;0;255;0m" ' test_expect_success '"default" foreground' ' @@ -146,7 +146,10 @@ test_expect_success 'non-hex character in RGB color' ' invalid_color "#12x456" && invalid_color "#123x56" && invalid_color "#1234x6" && - invalid_color "#12345x" + invalid_color "#12345x" && + invalid_color "#x23" && + invalid_color "#1x3" && + invalid_color "#12x" ' test_expect_success 'unknown color slots are ignored (diff)' '