Alpha compositing 2026-11-05
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.[ref]
Write for source and background colour triples, with alpha values . In premultiplied alpha form, and . Then source-over compositing gives
When , the corresponding straight alpha colour is . The output can remain partly transparent, ready to be combined with another background.
For an original Linear-light RGB example, place red with over blue with . The result is
Reversing the layers instead gives and , with the same output alpha. Layer order changes the colour even when it leaves the output alpha unchanged.
If the background is opaque, and , so the rule reduces to . For a physical light-mixture interpretation, carry out that arithmetic in Linear-light RGB; encoded sRGB values first need their appropriate conversion.[ref] Separately specify whether stored colour samples already contain their alpha multiplier, so that the source contribution is not multiplied twice.
Premultiplied alpha 2026-11-05
With premultiplied alpha, a stored colour triple already includes its alpha channel multiplier: , where is the corresponding straight alpha colour. The alpha value itself is retained. This is also called associated alpha. For , recover ; at zero alpha the multiplication loses the original colour. PNG uses unassociated samples rather than this convention.[ref]
For an original Linear-light RGB example, red with becomes the stored quadruple . The source-over compositing contribution of this sample is already . Multiplying its first three entries by alpha again would make the red contribution instead of .
Premultiplied alpha also makes a useful linear interpolation example transparent. Let sample A be opaque red, , and sample B be fully transparent blue, . Their premultiplied quadruples are and . Averaging them gives
Placed over black, this contributes . Averaging the straight colour triples and alphas separately would instead give , , contributing over black. That blue contribution came from colour attached to a fully transparent sample. The calculation explains how an interpolation convention can create a coloured fringe.
The multiplication convention and the color space are separate choices. The example uses Linear-light RGB throughout. A file decoder, texture sampler and alpha compositing stage must agree on both the colour encoding and whether the samples use straight alpha or premultiplied alpha.
Source-over compositing 2026-11-05
Source-over compositing combines a source layer with a background by retaining the source contribution and the fraction of the background left by the source's alpha channel. For premultiplied alpha colour triples and , the rule is
The W3C compositing specification states this operator explicitly.[ref] The output is another colour-and-alpha pair, so it can be combined with another layer.
For an original Linear-light RGB example, take red layer A with alpha , green layer B with alpha , and blue layer C with alpha . Place A over B over C. Expanding source-over compositing gives
with . Its straight alpha colour is therefore . The colour triple alone does not describe the partly transparent output; the alpha must accompany it.
The expansion also proves associativity in exact arithmetic: grouping A over B first or grouping B over C first produces those same three terms. Reordering the layers changes the coefficients, so associativity does not permit exchanging front and back. Intermediate rounding can introduce small numerical differences even when the mathematical grouping is equivalent.
For fully transparent source alpha zero, source-over compositing retains the background; for opaque source alpha one, it retains only the source. To interpret the colour arithmetic as a mixture of light contributions, use a common Linear-light RGB space. Stored sRGB codes generally need decoding first.[ref] Apply the equation for the actual storage convention: treating premultiplied alpha samples as straight alpha samples multiplies the source contribution twice.
Unpremultiplication 2026-11-05
Unpremultiplication converts premultiplied alpha colour samples back to straight alpha by dividing out a nonzero alpha channel. If is the stored premultiplied colour triple, then
The alpha value is retained. PNG's encoder guidance specifies this division when converting associated input samples to its unassociated representation, followed by scaling to the output sample range and rounding.[ref]
For an original exact-arithmetic example in Linear-light RGB, let and . The recovered colour is . Multiplying that triple by the retained alpha restores . Dividing the alpha itself would instead destroy the transparency information.
At , all exact premultiplied colour triples arising from finite equal zero. The original colour cannot be recovered from that result: many inputs mapped to the same stored value. PNG's conversion guidance chooses black for this case; that is a defined output convention, not recovered hidden colour.[ref]
Finite precision adds another limitation. Suppose , , and the premultiplied channels are rounded to eight-bit normalized codes. Their integer codes become , while the alpha code is one. Unpremultiplication then yields normalized straight colour , far from the original triple. For fixed exact alpha, a stored component error becomes after division. Near-zero alpha can therefore magnify rounding error even when the conversion formula is correct.