[SDL] new to alpha

Xavier Wielemans willy at alterface.net
Mon Jul 1 01:21:01 PDT 2002

  At 11:17 Monday 1/07/2002 +0800, you wrote:
>What is alpha mean,how to use alpha channel?
>Any simple example?

Tu put it really simply, alpha channel is a concept used in a 2D graphics 
scene composed of several superposed layers.  Alpha gives you transparency 
information about each pixel in a graphic layer.
In the case of 8-bit alpha, for instance :
alpha = 0 means totally transparent
alpha = 255 means totally opaque
intermediate values correspond to semi-transparency levels.

Simple example: lets say a pixel p in the scene has a certain "background" 
color value Cb, and you come and apply a new layer (with alpha information 
in it) above the background.  At pixel p position, the color and alpha 
values of the new layer (foreground) are Cf and Af, respectively.  The new 
resulting color value Cr for pixel p then becomes :

Cr = (Af*Cf + (255-Af)*Cb)/255

[NB: if you several color components, the same formula is to be applied 
once for each component separately.  For RGB color, for instance, compute 
it once for red, once for green and once for blue.]

You can verify that, in special cases where Af is 0 or 255, the final color 
value becomes Cb or Cf, respectively, which is indeed coherent with the 
definition of "totally transparent/totally opaque".  In all other cases, 
the resulting color is what you could call a "wheighted mix" between 
background and foreground color.  The mixing operation itself is called 
blending, compositing,... (many other names, certainly).

As I said, this is really the basics, there are many variations available 
on the market : 1-bit alpha (like a visibility bit flag for each pixel), 
other blending formulas, very nice generalized blending formula for 
blending two layers having both an alpha channel onto each other (Cb, Ab, 
Cf, Af --> Cr, Ar - my favourite :) ), alpha in 3D textures (I suppose),...

Hope this helps...


>SDL mailing list
>SDL at libsdl.org

Xavier Wielemans - alterface
phone +32 10 47 41 05
fax +32 10 47 20 89

More information about the SDL mailing list