vcommon

posts - 36, comments - 30, trackbacks - 0, articles - 0

The over operator is, in effect, the normal painting operation (see Painter's algorithm). The in operator is the alpha compositing equivalent of clipping.

As an example, the over operator can be accomplished by applying the following formula to each pixel value:

where Co is the result of the operation, Ca is the color of the pixel in element A, Cb is the color of the pixel in element B, and αa and αb are the alpha of the pixels in elements A and B respectively. If it is assumed that all color values are premultiplied by their alpha values (ci = αiCi), we can rewrite this as:

where

However, this operation may not be appropriate for all applications, since it is not associative ( i.e. it matters whether you first add object A and then object B or first B and then A. For example when building a picture from 3 color-channels it should not take effect in which order you add them, but with this non-associative version it would.). The associative version of this operation is very similar; simply take the newly computed color value and divide it by its new alpha value, as follows:

(转贴csdn zyl910（编程的乐趣在于编程控制硬件，与用图形学实现绚丽效果）

32位色下的颜色混合

R   =   R1   *   Alpha1   +   R2   *   Alpha2   *   (1-Alpha1)
G   =   G1   *   Alpha1   +   G2   *   Alpha2   *   (1-Alpha1)
B   =   B1   *   Alpha1   +   B2   *   Alpha2   *   (1-Alpha1)
Alpha   =   1   -   (1   -   Alpha1)   *   (   1   -   Alpha2)
R   =   R   /   Alpha
G   =   G   /   Alpha
B   =   B   /   Alpha

R1、G1、B1、Alpha1指上层的颜色值
R2、G2、B2、Alpha2指下层的颜色值
R、G、B、Alpha指合并后的颜色