[PATCH] xprop: Add colored icon support

Andy Spencer andy753421 at gmail.com
Thu Nov 12 02:46:56 PST 2009


* Supports both 16 and 265 color terminals
* 16 color output supports ascii and utf8 brightness

Signed-off-by: Andy Spencer <andy753421 at gmail.com>
---

Here's an updated patch that uses setupterm and tigetnum, it also adds a
check for curses and term.h in configure.ac..

When I test it in xterm, it says it only supports 8 colors, even though
running colortest works correctly with all 256 colors so I'm not sure
what's going on there.

>From what I can tell, there's a difference in the color tables for 8
color and 16 color terminals? Does anyone have any suggestions on how to
actually test these? (e.g. old terminals that only support 8/16 colors)

 configure.ac |    4 +-
 xprop.c      |  122 +++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 78 insertions(+), 48 deletions(-)

diff --git a/configure.ac b/configure.ac
index 58b49ee..f2b6380 100644
--- a/configure.ac
+++ b/configure.ac
@@ -37,7 +37,9 @@ AC_PROG_INSTALL
 
 XORG_DEFAULT_OPTIONS
 
-AC_CHECK_HEADERS([wchar.h wctype.h langinfo.h])
+AC_CHECK_HEADERS([wchar.h wctype.h langinfo.h term.h])
+AC_SEARCH_LIBS(setupterm, curses)
+AC_SEARCH_LIBS(tigetnum, curses)
 
 # Checks for pkg-config packages
 PKG_CHECK_MODULES(XPROP, x11)
diff --git a/xprop.c b/xprop.c
index ea65013..eebe947 100644
--- a/xprop.c
+++ b/xprop.c
@@ -49,6 +49,13 @@ from The Open Group.
 #include <langinfo.h>
 #endif
 
+#ifdef HAVE_TERM_H
+#include <term.h>
+#else
+#define setupterm(...)
+#define tigetnum(...) 0
+#endif
+
 #ifndef HAVE_WCTYPE_H
 #define iswprint(x) isprint(x)
 #endif
@@ -770,20 +777,21 @@ Format_Icons (const unsigned long *icon, int len)
 
     alloced = 0;
 
-    while (icon < end)
-    {
+    while (icon < end) {
 	unsigned long width, height;
 	int w, h;
 	int offset;
-	
+	int colors;
+
 	width = *icon++;
 	height = *icon++;
 
 	offset = (tail - result);
-	
+
 	alloced += 80;				/* For the header */
-	alloced += (width*4 + 8) * height;	/* For the rows (plus padding) */
-	
+	alloced += (width*14 + 8) * height;	/* For the rows (plus padding) */
+	alloced += 3;				/* For the footer */
+
 	result = Realloc (result, alloced);
 	tail = &result[offset];
 
@@ -792,58 +800,76 @@ Format_Icons (const unsigned long *icon, int len)
 
 	tail += sprintf (tail, "\tIcon (%lu x %lu):\n", width, height);
 
-	if (width > 144 || height > 144)
-	{
+	if (width > 144 || height > 144) {
 	    tail += sprintf (tail, "\t(not shown)");
 	    icon += width * height;
 	    continue;
 	}
-	
-	for (h = 0; h < height; ++h)
-	{
+
+	setupterm(NULL, 0, 0);
+	colors = tigetnum("colors");
+
+	for (h = 0; h < height; ++h) {
 	    tail += sprintf (tail, "\t");
-	    
-	    for (w = 0; w < width; ++w)
-	    {
+
+	    for (w = 0; w < width; ++w) {
 		unsigned char a, r, g, b;
 		unsigned long pixel = *icon++;
-		unsigned long brightness;
-		
+
 		a = (pixel & 0xff000000) >> 24;
 		r = (pixel & 0x00ff0000) >> 16;
 		g = (pixel & 0x0000ff00) >> 8;
 		b = (pixel & 0x000000ff);
-		
-		brightness =
-		    (a / 255.0) * (1000 - ((299 * (r / 255.0)) +
-					   (587 * (g / 255.0)) +
-					   (114 * (b / 255.0))));
-
-		if (is_utf8_locale())
-		{
-		    static const char palette[][4] =
-		    {
-			" ",
-			"\342\226\221",		/* 25% */
-			"\342\226\222",		/* 50% */
-			"\342\226\223",		/* 75% */
-			"\342\226\210",		/* 100% */
-		    };
-		    int idx;
-
-		    idx = (brightness * ((sizeof (palette)/sizeof(palette[0])) - 1)) / 1000;
-
-		    tail += sprintf (tail, "%s", palette[idx]);
-		}
-		else
-		{
-		    static const char palette[] =
-			" .'`,^:\";~-_+<>i!lI?/\\|()1{}[]rcvunxzjftLCJUYXZO0Qoahkbdpqwm*WMB8&%$#@";
-		    int idx;
-		    
-		    idx = (brightness * (sizeof(palette) - 2)) / 1000;
-		    
-		    *tail++ = palette[idx];
+
+		if (colors >= 256) {
+		    unsigned char color;
+
+		    color = 16 +
+			    ((r<48?0 : r<116?1 : (r-116)/40+2) * 36) +
+			    ((g<48?0 : g<116?1 : (g-116)/40+2) *  6) +
+			    ((b<48?0 : b<116?1 : (b-116)/40+2) *  1);
+		    tail += sprintf (tail, "\33[7;38;5;%dm", color);
+		    tail += sprintf (tail, " ");
+		} else {
+		    unsigned long brightness;
+
+		    brightness =
+			(a / 255.0) * (1000 - ((299 * (r / 255.0)) +
+					       (587 * (g / 255.0)) +
+					       (114 * (b / 255.0))));
+
+		    if (colors >= 16) {
+			unsigned char colors[] = {16, 12, 10, 14, 9, 13, 11, 15};
+			int cidx;
+
+			cidx = (r/128 << 2) |
+			       (g/128 << 1) |
+			       (b/128 << 0);
+			tail += sprintf (tail, "\33[38;5;%dm", colors[cidx]);
+		    }
+
+		    if (is_utf8_locale()) {
+			static const char palette[][4] = {
+			    " ",
+			    "\342\226\221",		/* 25% */
+			    "\342\226\222",		/* 50% */
+			    "\342\226\223",		/* 75% */
+			    "\342\226\210",		/* 100% */
+			};
+			int idx;
+
+			idx = (brightness * ((sizeof (palette)/sizeof(palette[0])) - 1)) / 1000;
+
+			tail += sprintf (tail, "%s", palette[idx]);
+		    } else {
+			static const char palette[] =
+			    " .'`,^:\";~-_+<>i!lI?/\\|()1{}[]rcvunxzjftLCJUYXZO0Qoahkbdpqwm*WMB8&%$#@";
+			int idx;
+
+			idx = (brightness * (sizeof(palette) - 2)) / 1000;
+
+			*tail++ = palette[idx];
+		    }
 		}
 	    }
 
@@ -851,6 +877,8 @@ Format_Icons (const unsigned long *icon, int len)
 	}
 
 	tail += sprintf (tail, "\n");
+	if (colors >= 16)
+	    tail += sprintf (tail, "\33[0m");
     }
 
     return result;
-- 
1.6.5.2




More information about the xorg mailing list