Exact formula for alpha blending used in WPF

55 views Asked by At

I am currently trying to match alpha-blending as specified in DirectX with the blending mechanism there (all those D3D11_BLEND_ONE, D3D11_BLEND_SRC_COLOR stuff) with the default alpha blending mechanism used in WPF.

The goal is, that two rgba Pixels P1 and P2 come out the same by DirectX blending and by the inbuilt WPF blending.

I learned, that the two main principles used are "straight" and "premultiplied" alpha channel.

From experiment it seems to me that WPF uses for the color channel the formula from "straight" interpolation that is

C_out = C_1 a_1 + C_2 (1 - a_1)

where C is the value of a single color channel, C_1 is the color of the pixel atop, C_2 is the color of the pixel below and a_1, a_2 are the respective alpha values (I assume color and alpha values to be normalized to the [0,1] interval.)

What I could not find out: What is the correct formula for the alpha channel, that is: What is f(a_1, a_2) in

a_out = f(a_1, a_2)

From the wikipedia article

Alpha compositing

it seems that for premultiplied alpha the formula would be

a_out  = a_1 + (1 - a_1) a_2

There is also a formula for straight alpha given there, but I think I can not apply it, because it is accompanied by a formula for the color channel that includes a division and therefore deviates from the "straight" formula for C_out that I wrote down in the beginning, and that seems to be corroborated by experiment.

So my questions are:

  1. Is my formula for C_out above truly correct in WPF?
  2. What is the accompanying formula for a_out = f(a_1, a_2) ?

Clarification: What do I mean with "the default alpha blending mechanism used in WPF". If you consider the following xaml-code fragment:

<Grid>
<Grid Background="#FF0A0A0A" /><!--Background-->
<Grid Background="#4D00DCFF" /><!--Top-->
</Grid>

then, due to the usual rendering rules the second grid is drawn above the first. If I make a screenshot and draw out the rgb values of the displayed (mixed) color it is consistent with the formula I gave above. Concretely it is (7, 73, 84)_decimal for (r,g,b). The alpha value of the mixed color I do not know how to determine.

So I assume the formula for C_out from above is generally applied by WPF in color mixing situation like in the "Grid"-example. As it is the formula for "straight alpha blend" I worded it as saying "'straight' is the default alpha blending mechanism of WPF".

0

There are 0 answers