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 with alpha , and the colour already on the canvas as (its alpha is ). 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 (, the case in most drawing) as
The fraction is how much of the new layer shows. The fraction is how much of the old pixel shows through. The output alpha follows the same pattern:
If , the canvas is opaque and . When the canvas is itself translucent, the straight-alpha colour is , which is why systems prefer the premultiplied form below.
Premultiplied colour
Many systems store the colour already multiplied by its alpha, . This is called premultiplied alpha. The over operator then becomes, for any canvas with premultiplied colour ,
with the same rule for every channel and for alpha, and no division. For an opaque canvas . 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
Take a red source with , colour , and an opaque blue canvas , .
- Red channel: .
- Green channel: .
- Blue channel: .
- Result: , a dark magenta, with .
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.
Start with an opaque white background, . Put a red layer with on it, then a green layer with on top.
- Red over white: .
- Green over that: .
- Red channel: . Green channel: . Blue channel: .
- Result: , a mostly green pixel with a little red.
Now reverse the order. Green over white first: . Then red with over that: .
Interpretation: the two orders give and . The layers are the same, but the result depends on which one is on top. The renderer must sort the layers back to front.
A two-pixel image: pixel 1 is opaque red, , . Pixel 2 is fully transparent black, , . You average them to make one pixel, the usual way to shrink an image.
- Naive average without premultiplying: colour , alpha .
- Composite over white: .
- Premultiplied average: premultiplied values are and , average , alpha .
- Composite over white with the premultiplied form: .
Interpretation: the correct answer is a half-covered red pixel over white, which is in step 4. The naive average gives , 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
| Situation | Problem | Better choice |
|---|---|---|
| Layers drawn in the wrong order | Output depends on order, so colours look wrong | Sort back to front, or use a depth buffer for opaque geometry |
| Resizing or filtering straight-alpha images | Dark or colour fringes at transparent edges | Premultiply before filtering |
| Many opaque layers | Compositing each one is wasted work | Draw opaque objects first with depth testing and skip blending |
| Many translucent layers, unordered | Sorting is costly and can be ambiguous | Weighted blended order-independent transparency, which accepts an approximation |
| Colour in linear light vs gamma | Blends look too dark when done on gamma-encoded values | Convert 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 . 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
- Red with alpha 1 over white: . The red layer hides the white.
- Green with alpha 0.8 over that: .
- Result: . The background no longer shows through the red, so the blue channel is zero.
QuestionWhy is the over operator's output alpha and not just ?Show answer
The output alpha says how much of the pixel is covered by something. The new layer covers . The part not covered by the new layer, , lets the canvas's coverage show through, and that coverage is . So the total coverage is the new layer plus the uncovered share of the old coverage. If the canvas is empty (), the output alpha is just .
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
- Alpha compositing (Wikipedia)
- Porter and Duff, Compositing Digital Images, SIGGRAPH 1984.