views:

362

answers:

2

In general, the "normal" blend mode equation looks like this:

D = Sa * S + D * (1.0 - Sa)

where D is destination color, Sa is source alpha and S is source color.

Now, this works fine with fully opaque destination but I'd like to know how you would handle that with semi and fully transparent destination.

When blending the source over a fully transparent destination, the source pixel (a pixel being color and alpha) will be unchanged and not blended like in the equation before, and if the destination background is fully opaque, the above equation should be applied, but I can't find a nice way to handle the situations where destination alpha is in between 0 and 1.

For example, if you blend a white pixel with 50% alpha on a transparent background, the color should not tend to that transparent color value (which is more or less in an undefined state), the destination color should be full white, and not 50% (after alpha multiplication), which is what you get after applying the above equation (if D is made the same color as S, which was something I thought of).

A: 

This equation is a simplification of the general blending equation. It assumes the destination color is opaque, and therefore drops the destination color's alpha term.

D = C1 * C1a + C2 * C2a * (1 - C1a)

where D is the resultant color, C1 is the color of the first element, C1a is the alpha of the first element, C2 is the second element color, C2a is the alpha of the second element. The destination alpha is calculated with:

Da = C1a + C2a * (1 - C1a)

The resultant color is premultiplied with the alpha. To restore the color to the unmultiplied values, just divide by Da, the resultant alpha.

Mr. Berna
This does not work. For example, if the destination color is black and the source color is white. Destination alpha is 0 and source alpha is 0.5, then you end up with D = 1.0 * 0.5 + 0.0 * 0.0 * (1.0 - 0.5) which is 1.0 * 0.5 with is alike multiplying the source color with it's alpha. This is not what I want, I'd like the source color to be untouched if the destination alpha is 0.
This is how this blending mode works. You'll want a different blending mode if you want different behavior. There are oodles of other blending modes. There's a good list at http://illusions.hu/effectwiki/doku.php?id=list_of_blendings
Mr. Berna
Also, the resulting color is premultiplied by the alpha. You can revert to the unmulitiplied colors by dividing the resulting colors by the resulting alpha. Which, as I think further about this, is what I think you want.
Mr. Berna
Ho yea, me stupid, you are right, the solution is simply to divide by the resulting alpha at the end. Thanks.
A: 

Most blending formulas are used on static images where the destination colors alpha is not stored. If the alpha is available then the final alpha is simply going to be the arithmetic mean of the source alpha and the destination alpha.

You can calculate the average alpha and then simply use that in the place of 'Sa' in your formula.

Mind telling us what this is for?

evolve
Calculating the destination alpha is not a problem, it's the color that pose a problem.It's to blend 1 semi transparent image over a second semi transparent image and save the result into a PNG file with transparency.In photoshop I can do that by loading the two images, merging the two layers and save to a PNG.
I still don't see why averaging the alpha channels wouldn't produce the proper color?
evolve