Search

Search papers and lessons

Alpha Compositing

Level 2 · 8 min read

Alpha compositing layers semi-transparent pixels on top of each other with one weighted-average rule, the over operator, so a stack of images or glyphs renders correctly.

On this page

TL;DR

A pixel can be partly transparent: it has a colour and an alpha value between 0 (see-through) and 1 (solid). To draw one semi-transparent layer over another, mix the two colours in proportion to alpha. The rule that does this, the over operator, is applied once per layer, so any stack of layers can be drawn one after another from the back.

Where you'll see it: the image editor's layer panel, anti-aliased text and icons drawn over a background, UI windows with translucent backgrounds, and the blending step of every 2D graphics library.

Intuition

Think of a pane of tinted glass held in front of a painted wall. The glass lets some light through and adds its own tint. If the glass is 50 percent opaque, the colour you see is half the glass's tint and half the wall's colour. Stack a second, more opaque pane and the same rule applies again, using what you see through the first pane as the new "wall".

Why does a graphics system need this? Edges of shapes are rarely pixel-aligned, so the pixel on the edge is partly covered. Text is drawn with soft edges. Photos have transparent regions. Without a single rule for combining partial coverage, each drawing routine would need its own blending code, and stacked layers would come out wrong. The over operator gives one rule that works for every layer, so drawing code only has to say how much each layer covers each pixel.

The math

Write each source colour as CAC_A with alpha αA\alpha_A, and the colour already on the canvas as CBC_B (its alpha is αB\alpha_B). Both colours are per channel: red, green and blue each get the same formula. The over operator computes the result colour for an opaque canvas (αB=1\alpha_B = 1, the case in most drawing) as

Cout=αA CA+(1−αA) CB.C_{\text{out}} = \alpha_A\, C_A + (1 - \alpha_A)\, C_B.

The fraction αA\alpha_A is how much of the new layer shows. The fraction 1−αA1 - \alpha_A is how much of the old pixel shows through. The output alpha follows the same pattern:

αout=αA+(1−αA) αB.\alpha_{\text{out}} = \alpha_A + (1 - \alpha_A)\, \alpha_B.

If αB=1\alpha_B = 1, the canvas is opaque and αout=1\alpha_{\text{out}} = 1. When the canvas is itself translucent, the straight-alpha colour is Cout=(αACA+(1−αA)αBCB)/αoutC_{\text{out}} = \big(\alpha_A C_A + (1 - \alpha_A)\alpha_B C_B\big)/\alpha_{\text{out}}, which is why systems prefer the premultiplied form below.

Premultiplied colour

Many systems store the colour already multiplied by its alpha, PA=αACAP_A = \alpha_A C_A. This is called premultiplied alpha. The over operator then becomes, for any canvas with premultiplied colour PB=αBCBP_B = \alpha_B C_B,

Pout=PA+(1−αA) PB,P_{\text{out}} = P_A + (1 - \alpha_A)\, P_B,

with the same rule for every channel and for alpha, and no division. For an opaque canvas PB=CBP_B = C_B. It also makes filtering, such as resizing an image, correct.

Order matters

The over operator is associative, so you can combine layers in any grouping, but it is not commutative. Putting layer A over layer B is not the same as putting B over A. You must composite in a fixed back-to-front order.

Worked examples

Example
One semi-transparent red layer over blue

Take a red source with αA=0.5\alpha_A = 0.5, colour CA=(1,0,0)C_A = (1, 0, 0), and an opaque blue canvas CB=(0,0,1)C_B = (0, 0, 1), αB=1\alpha_B = 1.

  1. Red channel: 0.5×1+0.5×0=0.50.5 \times 1 + 0.5 \times 0 = 0.5.
  2. Green channel: 0.5×0+0.5×0=00.5 \times 0 + 0.5 \times 0 = 0.
  3. Blue channel: 0.5×0+0.5×1=0.50.5 \times 0 + 0.5 \times 1 = 0.5.
  4. Result: Cout=(0.5,0,0.5)C_{\text{out}} = (0.5, 0, 0.5), a dark magenta, with αout=0.5+0.5×1=1\alpha_{\text{out}} = 0.5 + 0.5 \times 1 = 1.

Interpretation: half the pixel is red and half is blue, so the pixel reads as a purple-magenta. The canvas stays solid because the blue behind is opaque.

Example
Stacking two layers, and why order changes the answer

Start with an opaque white background, C=(1,1,1)C = (1, 1, 1). Put a red layer with α=0.5\alpha = 0.5 on it, then a green layer with α=0.8\alpha = 0.8 on top.

  1. Red over white: 0.5×(1,0,0)+0.5×(1,1,1)=(1,0.5,0.5)0.5 \times (1, 0, 0) + 0.5 \times (1, 1, 1) = (1, 0.5, 0.5).
  2. Green over that: 0.8×(0,1,0)+0.2×(1,0.5,0.5)0.8 \times (0, 1, 0) + 0.2 \times (1, 0.5, 0.5).
  3. Red channel: 0+0.2=0.20 + 0.2 = 0.2. Green channel: 0.8+0.1=0.90.8 + 0.1 = 0.9. Blue channel: 0+0.1=0.10 + 0.1 = 0.1.
  4. Result: (0.2,0.9,0.1)(0.2, 0.9, 0.1), a mostly green pixel with a little red.

Now reverse the order. Green over white first: 0.8×(0,1,0)+0.2×(1,1,1)=(0.2,1,0.2)0.8 \times (0, 1, 0) + 0.2 \times (1, 1, 1) = (0.2, 1, 0.2). Then red with α=0.5\alpha = 0.5 over that: 0.5×(1,0,0)+0.5×(0.2,1,0.2)=(0.6,0.5,0.1)0.5 \times (1, 0, 0) + 0.5 \times (0.2, 1, 0.2) = (0.6, 0.5, 0.1).

Interpretation: the two orders give (0.2,0.9,0.1)(0.2, 0.9, 0.1) and (0.6,0.5,0.1)(0.6, 0.5, 0.1). The layers are the same, but the result depends on which one is on top. The renderer must sort the layers back to front.

Example
Why premultiplied alpha matters when shrinking an image

A two-pixel image: pixel 1 is opaque red, C=(1,0,0)C = (1, 0, 0), α=1\alpha = 1. Pixel 2 is fully transparent black, C=(0,0,0)C = (0, 0, 0), α=0\alpha = 0. You average them to make one pixel, the usual way to shrink an image.

  1. Naive average without premultiplying: colour (0.5,0,0)(0.5, 0, 0), alpha 0.50.5.
  2. Composite over white: 0.5×(0.5,0,0)+0.5×(1,1,1)=(0.75,0.5,0.5)0.5 \times (0.5, 0, 0) + 0.5 \times (1, 1, 1) = (0.75, 0.5, 0.5).
  3. Premultiplied average: premultiplied values are (1,0,0)(1, 0, 0) and (0,0,0)(0, 0, 0), average (0.5,0,0)(0.5, 0, 0), alpha 0.50.5.
  4. Composite over white with the premultiplied form: (0.5,0,0)+0.5×(1,1,1)=(1,0.5,0.5)(0.5, 0, 0) + 0.5 \times (1, 1, 1) = (1, 0.5, 0.5).

Interpretation: the correct answer is a half-covered red pixel over white, which is (1,0.5,0.5)(1, 0.5, 0.5) in step 4. The naive average gives (0.75,0.5,0.5)(0.75, 0.5, 0.5), because it lets the transparent pixel's black colour leak in. The premultiplied average does not, which is why most image pipelines use it.

Tradeoffs and failure modes

SituationProblemBetter choice
Layers drawn in the wrong orderOutput depends on order, so colours look wrongSort back to front, or use a depth buffer for opaque geometry
Resizing or filtering straight-alpha imagesDark or colour fringes at transparent edgesPremultiply before filtering
Many opaque layersCompositing each one is wasted workDraw opaque objects first with depth testing and skip blending
Many translucent layers, unorderedSorting is costly and can be ambiguousWeighted blended order-independent transparency, which accepts an approximation
Colour in linear light vs gammaBlends look too dark when done on gamma-encoded valuesConvert to linear light before blending

The over operator is simple and fast, which is why it is the default. Its main cost is the order dependence. Real renderers hide that cost with depth sorting or approximate order-independent methods.

History

Thomas Porter and Tom Duff published the operator family in 1984, in a paper on compositing digital images (SIGGRAPH). They defined the over operator and others, such as in and out, and showed how they combine. Premultiplied alpha is the form that makes the operators uniform and makes filtering correct, which is why it is common in image pipelines.

Check yourself

QuestionA pixel has alpha 0.25 and colour (1, 1, 1) over a black canvas (0, 0, 0) with alpha 1. What is the output colour?Show answer

Over gives Cout=0.25×(1,1,1)+0.75×(0,0,0)=(0.25,0.25,0.25)C_{\text{out}} = 0.25 \times (1, 1, 1) + 0.75 \times (0, 0, 0) = (0.25, 0.25, 0.25). The white layer shows a quarter of the way, so the pixel is a dark grey.

QuestionUsing the two-layer example, what would the result be if the red layer had alpha 1 instead of 0.5, still over white first and then green with alpha 0.8?Show answer
  1. Red with alpha 1 over white: 1×(1,0,0)+0×(1,1,1)=(1,0,0)1 \times (1, 0, 0) + 0 \times (1, 1, 1) = (1, 0, 0). The red layer hides the white.
  2. Green with alpha 0.8 over that: 0.8×(0,1,0)+0.2×(1,0,0)=(0.2,0.8,0)0.8 \times (0, 1, 0) + 0.2 \times (1, 0, 0) = (0.2, 0.8, 0).
  3. Result: (0.2,0.8,0)(0.2, 0.8, 0). The background no longer shows through the red, so the blue channel is zero.
QuestionWhy is the over operator's output alpha αA+(1−αA)αB\alpha_A + (1 - \alpha_A)\alpha_B and not just αA\alpha_A?Show answer

The output alpha says how much of the pixel is covered by something. The new layer covers αA\alpha_A. The part not covered by the new layer, 1−αA1 - \alpha_A, lets the canvas's coverage show through, and that coverage is αB\alpha_B. So the total coverage is the new layer plus the uncovered share of the old coverage. If the canvas is empty (αB=0\alpha_B = 0), the output alpha is just αA\alpha_A.

QuestionApplied: a sprite sheet with straight (not premultiplied) alpha is scaled down by 50 percent with a bilinear filter. The edges look dark. What is happening and what fix would you apply?Show answer

The filter averages colour values across transparent pixels that hold dark colours, so the dark colours bleed into the semi-transparent edge, as in the shrinking example. The fix is to premultiply the sprite before filtering and un-premultiply afterwards if the engine needs straight alpha. Most engines can load premultiplied textures directly.

Sources