diff options
author | Pierre-Loup A. Griffais <git@plagman.net> | 2020-06-06 15:19:10 -0700 |
---|---|---|
committer | Pierre-Loup A. Griffais <git@plagman.net> | 2020-11-01 13:45:27 -0800 |
commit | d41b28bfa8f5bf5155082167fb004e3c709b52ae (patch) | |
tree | ed92694f4147fbe89db372b692559c01ada23c44 | |
parent | e25ae1d9b03a84c3ca825745156af2a7045f04e5 (diff) |
Support true color output for icons if the terminal advertises it.
-rw-r--r-- | xprop.c | 31 |
1 files changed, 29 insertions, 2 deletions
@@ -751,6 +751,17 @@ is_utf8_locale (void) #endif } +static int +is_truecolor_term (void) +{ + char *colorterm = getenv( "COLORTERM" ); + + if (colorterm && !strcmp(colorterm,"truecolor")) + return 1; + + return 0; +} + static const char * Format_Icons (const unsigned long *icon, int len) { @@ -779,13 +790,15 @@ Format_Icons (const unsigned long *icon, int len) icon_pixel_bytes = 1; if (is_utf8_locale()) icon_pixel_bytes = 3; /* Up to 3 bytes per character in that mode. */ + if (is_utf8_locale()) + icon_pixel_bytes = 25; /* 16 control characters, and up to 9 chars of RGB. */ /* Initial tab, pixels, and newline. */ icon_line_bytes = 8 + display_width * icon_pixel_bytes + 1; offset = (tail - result); - alloced += 80; /* For the header */ + alloced += 80; /* For the header, final newline, color reset */ alloced += icon_line_bytes * height; /* For the rows */ result = realloc (result, alloced); @@ -825,7 +838,17 @@ Format_Icons (const unsigned long *icon, int len) (587 * (g / 255.0)) + (114 * (b / 255.0)))); - if (is_utf8_locale()) + if (is_truecolor_term()) + { + float opacity = a / 255.0; + + r = r * opacity; + g = g * opacity; + b = b * opacity; + + tail += sprintf (tail, "\033[38;2;%d;%d;%dm\342\226\210\342\226\210", r, g, b ); + } + else if (is_utf8_locale()) { static const char palette[][4] = { @@ -857,6 +880,10 @@ Format_Icons (const unsigned long *icon, int len) tail += sprintf (tail, "\n"); } + /* Reset colors. */ + if (is_truecolor_term()) + tail += sprintf (tail, "\033[0m"); + tail += sprintf (tail, "\n"); } |