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]
Compositing and Blending Level 1 by World Wide Web Consortium. 2024Candidate Recommendation Draft, §5.1 and §9.1.4, Source Over
Write for source and background colour triples, with alpha values . In premultiplied alpha form, and . Then source-over compositing givesWhen , 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 isReversing 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]
Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others. §13.16, alpha channel processing and gamma correction
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]
Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others. §6.2, Alpha representation; §12.3, conversion from associated data
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 givesPlaced 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 isThe W3C compositing specification states this operator explicitly.[ref]
Compositing and Blending Level 1 by World Wide Web Consortium. 2024Candidate Recommendation Draft, §9.1.4, Source Over; §5.1, premultiplied output and output alpha
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 giveswith . 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]
Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others. §13.16, alpha channel processing and gamma correction
Apply the equation for the actual storage convention: treating premultiplied alpha samples as straight alpha samples multiplies the source contribution twice. Straight alpha 2026-11-05
With straight alpha, colour samples are stored independently of their alpha channel value. The same convention is called unassociated alpha. For a normalized colour triple and alpha , the stored pair is ; premultiplied alpha instead stores . The PNG specification requires the unassociated convention.[ref]
Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others. §6.2, Alpha representation
For an original Linear-light RGB colour and alpha , the straight-alpha quadruple is . The corresponding premultiplied quadruple is . Both describe the same source contribution when interpreted correctly. Raising alpha to can leave the straight colour triple unchanged, while its premultiplied counterpart must become .
For source-over compositing against an opaque black background, the first sample contributes . The stored straight colour is therefore not already its final contribution. At zero alpha, a straight-alpha representation can retain arbitrary colour components while the source contributes no colour to this operation. Multiplying by zero discards those components.
Unpremultiplication recovers the straight colour by dividing premultiplied colour by alpha when alpha is nonzero. It cannot recover a colour erased at zero alpha, and finite-precision errors become significant near zero. If a premultiplied component has absolute rounding error at most , exact division by a fixed alpha magnifies that bound to . At , a component error can therefore become . These are original error bounds, rather than a measured decoder defect.
The alpha convention and the color space remain separate specifications. For light-linear arithmetic, decode nonlinear colour samples before forming their light contributions. PNG documents both compositing and conversion from associated data, including handling zero alpha.[ref]
Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others. §12.3, conversion from associated data; §13.16, alpha channel processing