[hackers] [st][PATCH] Fix truecolor being slightly inaccurate

From: Yutao Yuan <yyt16384_AT_gmail.com>
Date: Sun, 6 Mar 2022 21:07:41 +0800

The displayed color in truecolor mode is sometimes darker than
requested. The inaccuracy can be verified with the following example,
which should paint a white block but instead produces color #fefefe.

    printf '\e[38;2;255;255;255m\u2588\u2588\u2588\n'

The truecolor code uses 8-bit color values (0 to 0xff), while X uses
16-bit color values (0 to 0xffff). Existing code multiplies the value by
256 to make the conversion, but this is incorrect, as it maps 0xff to
0xff00, not 0xffff. This patch multiplies the value by 257 instead,
which is the correct formula (0xff * 257 == 0xffff).
---
 x.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/x.c b/x.c
index cd96575..f012539 100644
--- a/x.c
+++ b/x.c
_AT_@ -69,9 +69,9 @@ static void ttysend(const Arg *);
 /* macros */
 #define IS_SET(flag) ((win.mode & (flag)) != 0)
-#define TRUERED(x) (((x) & 0xff0000) >> 8)
-#define TRUEGREEN(x) (((x) & 0xff00))
-#define TRUEBLUE(x) (((x) & 0xff) << 8)
+#define TRUERED(x) ((((x) & 0xff0000) >> 16) * 257)
+#define TRUEGREEN(x) ((((x) & 0xff00) >> 8) * 257)
+#define TRUEBLUE(x) ((((x) & 0xff)) * 257)
 typedef XftDraw *Draw;
 typedef XftColor Color;
-- 
2.35.0
Received on Sun Mar 06 2022 - 14:07:41 CET

This archive was generated by hypermail 2.3.0 : Sun Mar 06 2022 - 14:12:36 CET