ROP2 and Color

The drawing mode gets more interesting—and much more complex—when color is introduced. Let's assume a display capable of eight pure colors (such as the EGA and VGA in versions of Windows prior to version 3). The pen can be any of these eight pure colors, and for simplicity's sake, let's restrict the background to these colors also. The eight colors are combinations of the bits in the red, green, and blue color planes, as shown in the following table—a 1 means the color is illuminated, and a 0 means the color is off.

Red Green Blue Pure Color

0 0 0 Black
0 0 1 Blue
0 1 0 Green
0 1 1 Cyan
Red Green Blue Pure Color

1 0 0 Red
1 0 1 Magenta
1 1 0 Yellow
1 1 1 White

Each of the three color planes is affected separately by the raster operation. For example, say you have a cyan background color and a magenta pen color, and your drawing mode is R2_NOTMERGEPEN. What color will the pen actually draw? For red, the pen is 1 (has red), and the destination is 0 (no red). Looking at the ROP2 table on page 551, you see that the result is 0 (no red). For green, the pen is 0 (no green), and the destination is 1 (has green), so the result is 0 (no green). For blue, the pen is 1 (has blue), and the destination is 1 (has blue), so the result is 0 (no blue). Thus the line has no red, no green, and no blue. The color will be black.

Let's take the R2_XORPEN drawing mode, which performs a bitwise exclusive OR operation on each of the possible combinations in the three color planes. The following table shows the resultant color for all combinations of the eight destination colors and the eight pen colors.

        PEN COLOR        
Destination Black Blue Green Cyan Red Magenta Yellow White

Black Black Blue Green Cyan Red Magenta Yellow White
Blue Blue Black Cyan Green Magenta Red White Yellow
Green Green Cyan Black Blue Yellow White Red Magenta
Cyan Cyan Green Blue Black White Yellow Magenta Red
Red Red Magenta Yellow White Black Blue Green Cyan
Magenta Magenta Red White Yellow Blue Black Cyan Green
Yellow Yellow White Red Magenta Green Cyan Black Blue
White White Yellow Magenta Red Cyan Green Blue Black

On certain devices (particularly on 256-color video boards), the bits that define each pixel may not correspond to color in a consistent manner, and the results of using some drawing modes are not well defined.

At the beginning of Chapter 11 I mentioned that Windows GDI is strong in the area of raster operations. The drawing mode is one example of that. And if you think that you'll probably never ever use some of these ROP2 codes, just wait until you see the regular raster operation codes in Chapter 13—there are more than 200 raster operations that you'll probably never use. But it's nice to know that they're available.