Game Development Reference
In-Depth Information
Figure 3-27 illustrates the preceding equation. Our source color is a shade of pink, and the
destination color is a shade of green. Both colors contribute equally to the final output color,
resulting in a somewhat dirty shade of green or olive.
Figure 3-27. Blending two pixels
Two fine gentlemen named Porter and Duff came up with a slew of blending equations. We will
stick with the preceding equation, though, as it covers most of our use cases. Try experimenting
with it on paper or in the graphics software of your choice to get a feeling for what blending will
do to your composition.
Note Blending is a wide field. If you want to exploit it to its fullest potential, we suggest that you
search the Web for Porter and Duff's original work on the subject. For the games we will write,
though, the preceding equation is sufficient.
Notice that there are a lot of multiplications involved in the preceding equations (six, to be precise).
Multiplications are costly, and we should try to avoid them where possible. In the case of blending,
we can get rid of three of those multiplications by pre-multiplying the RGB values of the source
pixel color with the source alpha value. Most graphics software supports pre-multiplication of an
image's RGB values with the respective alphas. If that is not supported, you can do it at load time
in memory. However, when we use a graphics API to draw our image with blending, we have to
make sure that we use the correct blending equation. Our image will still contain the alpha values,
so the preceding equation would output incorrect results. The source alpha must not be multiplied
with the source color. Luckily, all Android graphics APIs allow us to specify fully how we want to
blend our images.
In Bob's case, we just set all the white pixels' alpha values to zero in our preferred graphics
software program, load the image in ARGB8888 or ARGB4444 format, maybe pre-multiply the
alpha, and use a drawing method that does the actual alpha blending with the correct blending
equation. The result would look like Figure 3-28 .
 
Search WWH ::




Custom Search