[comp.windows.x] GXinvert and AllPlanes on color displays

whaley@spectre.pa.dec.com (Ken Whaley) (12/12/90)

Sorry if this is a repeat, but my news poster seems flaky today.

References: <17226@natinst.natinst.com>

Just on the off chance that you haven't received thousands of responses by now,
I'll try to give you a brief explanation as to why just using 
GXinvert + AllPlanes won't give you the correct results almost all of the time
on a color display.  Since I wish someone had explained something 
like this to me a long time ago, I'll post this to the net in the case that 
others might want to learn the pleasures of pixel bit twiddling.

First of all, I'm assuming that what you want to do is reverse the foreground
and background values to highlight the text string.  There are several ways
to combine boolean functions and plane masks to achieve this effect, but you 
basically want to take two arbitrary pixel values (your GC foreground and 
background colors) and swap them.  I assume you draw a rectangle over the 
region you want to highlight with function = GXinvert.

Assume foreground pixel (fg) = 	5	(00000101 binary)
Assume background pixel (bg) =  16	(00010000 binary)

(It doesn't matter what colors are assigned to these values--what's important
is the pixel values themselves.)

By applying the invert operation with plane mask AllPlanes, you invert
every bit in the fg and bg pixels

Inverted fg: 11111010 (binary), 0xFA
Inverted bg: 11101111 (binary), 0xEF

Now, chances are (unless you run applications that fill up the colormap) that 
you have "black" stored at pixel values 0xFA and 0xEF, which explains why you 
see black on the screen.

If you set the planemask to fg^bg ("^" is xor), then the planemask is 00010101

Inverted fg & planemask = 11111010
			& 00010101
			----------
			  00010000  = bg

Inverted bg & planemask = 11101111
			& 00010101
			----------
			  00000101  = fg

By setting planemask = fg^bg, you say that you only want to invert
the bits in fg/bg that are different from bg/fg, so that fg-->bg and bg-->fg,
which is exactly what you want (note that this works when fg & bg != 0, because
1^1 = 0, and so that bit won't change).

	Ken