Alpha Blending
Question submitted by (13 August 1999)




Return to The Archives
 
  How do you do Alpha blending in C++ without using a library like DirectX or anything else?  
 

 
  There are lots of variations to alpha blending. Different blending-functions give radically different results. The most common is the multiplicative blending of two ARGB values, which I'll explain here.

Let's assume you have two input colors: RED and BLUE. The 'blend' of these two colors (using properties of light, not paint) results in purple. But alpha blending requires a little more information. We'll need to assign an alpha component to each: for Color1 (the RED color) we'll use 25% (or, 0.25) and for Color2 (the BLUE color) we'll use 75% (0.75). This means we're dimming our Color1 light to 25% of full power, and the Color2 light to 75% of full power. Which should result in a primarily bluish shade of purple. Here are the actual numbers:
Color1: 0.25, 1.0, 0.0, 0.0 (in A,R,G,B order)
Color2: 0.75, 0.0, 0.0, 1.0 (in A,R,G,B order)
To perform the alpha blending, you must first apply the alpha to each color. This is done by multiplying the R, G and B components by the A component. Next, we add the two colors.

Alpha blending of two colors


Finally, we'll need to saturate (i.e. any of those values that goes above 1.0 gets clamped to 1.0.) In our example, all three values are under 1.0, so there's nothing to be done.



Response provided by Paul Nettle
 
 

This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.


 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.