= Alpha compositing
\b[Alpha compositing] combines image contributions using their colour values and <alpha channels>. The <source-over compositing> rule places a source layer over a destination while retaining the destination's contribution where the source is partly transparent. Other compositing rules select different contributions; the alpha values alone do not identify the chosen rule.<Compositing and Blending Level 1 by World Wide Web Consortium>[2024Candidate Recommendation Draft, §5.1 and §9.1.4, Source Over]{ref}
Write $C_s,C_b$ for source and background colour triples, with alpha values $\alpha_s,\alpha_b$. In <premultiplied alpha> form, $c_s=\alpha_sC_s$ and $c_b=\alpha_bC_b$. Then <source-over compositing> gives
$$
c_o=c_s+(1-\alpha_s)c_b,\qquad
\alpha_o=\alpha_s+(1-\alpha_s)\alpha_b.
$$
When $\alpha_o>0$, the corresponding <straight alpha> colour is $C_o=c_o/\alpha_o$. The output can remain partly transparent, ready to be combined with another background.
For an original <Linear-light RGB> example, place red $C_s=(1,0,0)$ with $\alpha_s=1/2$ over blue $C_b=(0,0,1)$ with $\alpha_b=1/4$. The result is
$$
c_o=(1/2,0,1/8),\qquad \alpha_o=5/8,\qquad C_o=(4/5,0,1/5).
$$
Reversing the layers instead gives $c_o=(3/8,0,1/4)$ and $C_o=(3/5,0,2/5)$, with the same output alpha. \b[Layer order changes the colour even when it leaves the output alpha unchanged.]
If the background is opaque, $\alpha_b=1$ and $\alpha_o=1$, so the rule reduces to $C_o=\alpha_sC_s+(1-\alpha_s)C_b$. For a physical light-mixture interpretation, carry out that arithmetic in <Linear-light RGB>; encoded <sRGB> values first need their appropriate conversion.<Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others>[§13.16, alpha channel processing and gamma correction]{ref} Separately specify whether stored colour samples already contain their alpha multiplier, so that the source contribution is not multiplied twice.
Back to article page