1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
Mixing two pixels (r1,g1,b1,a1) and (r2,g2,b2,a2) depends on the transparency of the
second pixel (which is painted over the first one). The new red value is a barycenter
of the original red values, using a2:
(255-a2)*r1 + a2*r2
r = ---------------------
255
Notice that if a2 = 255, the formula reduces to: r = r2. Similarly, if a2 = 0, the
formula reduces to: r = r1. The same method is used for green and blue. The new
opacity is given by:
(255-a1)*(255-a2) (255-a1)*(255-a2)
255-a = -------------------, hence a = 255 - -------------------
255 255
because (255-a1)/255 is the factor of transparency for the first pixel. For example, if
a1 = 0, 100 percent of the light passes through the pixel. On the contrary, when a1 =
255, 0 percent of the light passes through the pixel. The above is equivalent to:
a1*a2
a = a1 + a2 - -------
255
For example, if one pixel is opaque (say a1 = 255), the result is opaque. If one pixel
is completely transparent (a1 = 0), the result has the opacity of the other one. |
Partager