You can get billions of results if you search Alpha blending on the web. I'm not going to talk about any new or better algorithm here. If you read those articles, you will found what most of them mention are all about a specific application: blending a semi-transparent color on a solid background color. However, what OpenPainter needs is a more general case, which is to blend two semi-transparent colors. That is to say that the background color is also semi-transparent.

I did a lot of experiments on PS first to try to get the formula of alpha blending. Actually the algorithm was figured out by some simple experiments as well as logic analysis. For conveniency of calculation, the values of all color channels are from 0.0 to 1.0 (representing the percentage).

The first step is to calculate the alpha value. Color is actually produced by light. Everything is actually so simple if we consider transparency the light through rate of glass. For example, a color in alpha of 0.2, that can be considered as a piece of colorful glass in light through rate of 80%. When we look through it, 80% of the light goes through and 20% of the color remains. That is what alpha of 0.2 stands for. Let me do a blending to mix colors of alpha of 0.2 and 0.6. We have two pieces of glass now. One has the through rate of 80%, and the other is of 40%. After the light goes through the glass in through rate of 80%, 80% of the light remains. After travels through the other glass in through rate of 40%, more light is filtered, and only 80% * 40% = 32% of the light remains. It means the transparency of 32%, or alpha = 0.68.

As a conclusion, we have now:



Next, to calculate the component value of each color channel. It is actually easier by using the mathematics way. We've already known how to blend semi-transparent color on solid background color:



Thus, we can do the alpha blending in two different ways. The first one is to blend the second color after the first color is already blended to the solid background; while the second way is to blend the two colors first and then blend the result to the solid background. So we get:



Because these two way should cause exactly the same results, we have:



Arranged:



That is the alpha blending formula in RGB color space, and similar to CMYK and other color sacs.

Last edited Jun 28, 2010 at 2:15 AM by richardbao2000, version 1

Comments

No comments yet.