[PATCH] xprop: Add colored icon support

Alan Coopersmith Alan.Coopersmith at Sun.COM
Wed Nov 11 22:11:38 PST 2009


[Patches usually go to xorg-devel@, not xorg@, for review/submission.]

The greybeard in me wants to suggest that hardcoding the escape codes
is wrong and that termcap/terminfo should be used instead to find the
right ones for $TERM, but the realist in me wonders if there are any
$TERM values in widespread use these days other than xterm, screen,
and various vt100-variants.

	-alan-

Andy Spencer wrote:
> * Supports both 16 and 265 color terminals
> * 8 color output supports ascii and utf8 brightness
> 
> Signed-off-by: Andy Spencer <andy753421 at gmail.com>
> ---
> 
> I'm not sure how to test for 16 and 256 color so I left them set to 0.
> If someone can point out the proper way to do this I'll update it.
> 
> I also modified the bracing style in Format_Icons function to match the
> rest of xprop.c.
> 
> And an obligatory screenshot: :)
>   http://andy753421.ath.cx/temp/pidgin.png
> 
>  xprop.c |  114 +++++++++++++++++++++++++++++++++++++--------------------------
>  1 files changed, 67 insertions(+), 47 deletions(-)
> 
> diff --git a/xprop.c b/xprop.c
> index ea65013..2267e7d 100644
> --- a/xprop.c
> +++ b/xprop.c
> @@ -770,20 +770,20 @@ 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;
> -	
> +
>  	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 +792,75 @@ 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)
> -	{
> +
> +	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];
> +
> +		/* TODO: test for 256 color */
> +		if (0) {
> +		    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))));
> +
> +		    /* TODO: test for 16 color */
> +		    if (0) {
> +			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 +868,9 @@ Format_Icons (const unsigned long *icon, int len)
>  	}
>  
>  	tail += sprintf (tail, "\n");
> +	/* TODO: test for color */
> +	if (0 || 0)
> +	    tail += sprintf (tail, "\33[0m");
>      }
>  
>      return result;

-- 
	-Alan Coopersmith-           alan.coopersmith at sun.com
	 Sun Microsystems, Inc. - X Window System Engineering




More information about the xorg mailing list