A transparent pixel needs a representation contract
A pixel can contain a color and an alpha value, yet those four numbers do not explain themselves. Does the stored RGB already include alpha? Is the color expressed in a linear-light space? Does alpha represent the scalar attenuation assumed by the compositing model, or a geometric coverage estimate with unresolved subpixel structure? Mixing these interpretations produces visible errors even when every multiplication in the program is correct.
We will specify one model, derive its forward operation, and then ask what can be recovered from the result. The last question is an inverse problem: compositing a known foreground is much easier than separating an unknown foreground from a photograph.
Straight color and premultiplied color store different quantities
Definition 13.1.1
(A linear-light scalar-alpha layer)
Let be a straight RGB color and its alpha. In the layer model used here, alpha weights the foreground and transmits the layer below it. Over a known opaque background color , the observed linear-light color is
The premultiplied color is . A premultiplied layer is stored as and satisfies, in each color channel, . A straight layer instead stores .
All color arithmetic below takes place in linear-light RGB. Encoded display RGB generally needs decoding before this particular light-mixing calculation and encoding afterwards. Alpha itself is the scalar weight; it is not a color channel to which a display transfer curve should be applied. The equations specify our mathematical model, not every possible graphics system's chosen blending space.
For , recover straight color by . At , a valid premultiplied layer has , and its straight color cannot be recovered: every possible gives the same zero premultiplied value. A fully transparent stored straight color is hidden data, not a visible color contribution. Never divide by zero to recover it.
Worked example 13.1.2
(Straight color at very small alpha)
Suppose alpha is stored as and each premultiplied channel in steps of , so a valid red component is . The recovered red is : a single quantization step moves the straight color from no red to full red. The pixel adds at most to any composite, so the unreliable color is also nearly invisible. At the layer is and no straight color can be recovered at all.
Derive source-over for two possibly transparent layers
Definition 13.1.3
(Source-over operator)
Let be the front layer and the layer behind it. The front contributes ; the fraction passing through it scales the contribution of B. The resulting transparency is the product of the two individual transparencies. Therefore define source-over by
This pair describes the combined layer before choosing any still lower background. If B is opaque, the resulting alpha is 1 and the color reduces to . For a transparent B, neither nor the result should be mistaken for straight color.
Proposition 13.1.4
(Source-over preserves valid layers)
Suppose and in every color channel . Then satisfies the same inequalities.
Proof
(Componentwise inequalities)
Write and , both in . The components are nonnegative as sums of nonnegative terms. Multiplying by and adding gives . The new alpha is , and as a product of two numbers in that interval, so the new alpha lies in .
Grouping preserves the result; reordering generally does not
Theorem 13.1.5
(Source-over is associative on premultiplied layers)
For any valid layers A, B and C in this scalar-alpha model,
The transparent layer is an identity on either side. The operation is generally not commutative.
Proof
(Expand contributions and multiply transparencies)
Write , and similarly for B and C. The combined transparency of A over B is . Both groupings therefore have premultiplied color
and alpha . This proves equality of both stored parts. Substituting in either input position proves the identity claim.
Commutativity would require the same color after exchanging front and back layers. The example below gives different colors, so no such general law holds. Associativity permits regrouping a fixed front-to-back order; it does not permit sorting the layers arbitrarily.
Concept lensStructural
Layers as affine maps
Each layer defines a map on stored pairs by . Then . With ,
because the transparency of is . Over-composition is therefore composition of maps, and associativity is the associativity of function composition. The layer is recovered from its map as , and the identity layer gives the identity map. A whole stack of layers is one affine map: its slope is the transparency of the stack, and its value at the zero pair is the stack's premultiplied pair.
These are exact real-arithmetic identities. Finite-precision evaluations can differ by rounding when the same layers are grouped differently.
Worked example 13.1.6
(Half-red over half-blue, then over white)
Let A be straight red with alpha , and B be straight blue with alpha . Their premultiplied pairs are and .
A over B has and . Its straight color is , obtained by dividing by . Over opaque white, the final color becomes .
Reverse the two colored layers. B over A instead has , with the same alpha but a different color. Over white it gives . Alpha alone cannot encode which colored layer was in front.
Interpolate contributions before recovering straight color
Images are resampled at boundaries, so representation matters even before compositing. For a parameter , convex interpolation of two premultiplied samples uses
This preserves . Recover a straight color only if the interpolated alpha is positive. These claims concern nonnegative weights summing to one; filters with negative weights need their own range and reconstruction analysis.
Counterexample mode
An invisible endpoint can contaminate a straight-color average
One endpoint is opaque red: , . The other is fully transparent but stores straight blue: , . Interpolating straight RGB and alpha separately at the midpoint gives and . Compositing this interpolated sample over black gives : the invisible blue has created a visible blue component.
Convert first to premultiplied data. The endpoints become and . Their midpoint is with alpha , so over black it gives . Its recovered straight color is red.
Premultiplication removes the arbitrary hidden RGB from the contribution before interpolation. It is not a rule to multiply an already premultiplied color by alpha again.
Common mistake
(Premultiplying a premultiplied color)
Let a layer be stored premultiplied as with , so its straight color is red. Over black its color is . Multiplying the stored by alpha again gives , which is the premultiplied color of red at alpha : a different layer. Convert from straight to premultiplied exactly once, and check which representation each function expects.
A scalar alpha does not record every subpixel relationship
The source-over equations were derived for the declared scalar attenuation model. If alpha is instead estimated by averaging a binary coverage mask over a pixel, two scalar averages need not describe their overlap. Let be masks at a uniformly chosen subpixel position. The covered fraction of their union is
The usual scalar-alpha formula replaces the last term by . For masks, this is correct under a matching overlap assumption, such as independent masks; it is not true for all correlations. A renderer that has discarded the subpixel overlap cannot recover it from the two alpha values alone. This limitation of the representation does not invalidate the associative algebra of the scalar model we have defined.
Common mistake
(Reading scalar source-over as union coverage)
Scalar source-over with gives alpha . Two masks that each cover half the pixel cover exactly if they coincide and if they are disjoint. The value is the answer of the scalar model, not a measurement of union coverage.
Matting reverses the equation, but it may have no unique answer
Compositing knows the layers and computes I. Matting tries to infer an unknown alpha and often unknown foreground from I. The same forward formula does not automatically supply an inverse.
Definition 13.1.7
(Matting problem)
Given an observed linear-light color and a background , matting asks for and with . The problem is identifiable when exactly one such pair exists.
Worked example 13.1.8
(One grayscale observation permits many mattes)
Let the observed grayscale value be , with known black background . Then . Both and explain the observation. In fact every with is valid.
The lower endpoint follows from , which requires . Knowing the background has therefore restricted the possibilities but has not identified a unique foreground or alpha. Extra observations or assumptions are needed to select among them.
There is a simpler identifiable case: suppose both F and B are known. Put and . The model becomes .
Proposition 13.1.9
(Alpha from known foreground and background colors)
Let be known, , , and
(a) If for some , then . (b) On the unique minimizer of is .
Proof
(Inner product and completing the square)
(a) Taking the inner product of with gives , and .
(b) Completing the square gives
The last two terms are independent of alpha, and the first is a strictly convex quadratic with its minimum at . On the minimizer is therefore the point of the interval nearest to , namely , and it is unique.
Proof X-ray
What each part of the argument does
Part (a) rests on one scalar equation, obtained by contracting the vector equation with . It is necessary but not sufficient: after is computed, the equation must still be checked in every channel. Part (b) is the non-routine step. The decomposition splits the loss into a term that depends on alpha only through and a constant, the squared length of the component of orthogonal to . That constant is zero exactly when , so an exact solution is the case of zero minimum, and a channel direction orthogonal to changes the loss but never , as the last exercise shows. The argument needs so that . It solves a chosen loss with known, and says nothing about unknown .
To verify an exact solution, also check that and in every channel. A scalar projection alone does not prove that the observed color lies on the segment joining B to F. If , the predicted image is B for all alpha: when , alpha is unidentifiable; when , no noiseless alpha can explain the observation.
Solved exercises
Quick check
(A foreground stores straight with alpha . Find its premultiplied pair and its color over opaque . What is the resulting alpha?)
Premultiply once, then apply source-over.
Solution · Answer
The premultiplied color is . Source-over gives . The resulting alpha is , so that final color is also the straight RGB of the opaque result. Multiplying p by again would count alpha twice and darken the foreground incorrectly.
Quick check
(Use the half-red A, half-blue B and opaque white C from the worked example. Compute B over C, then A over that result, and compare with (A over B) over C. What changes if A and B are exchanged?)
Group B and C first.
Solution · Answer
B over C is opaque with color . A over it gives , matching (A over B) over C. Exchanging A and B gives over white. Parenthesization preserves the result; front-to-back order generally changes it.
Quick check
(Two binary masks each cover half a pixel. Find the actual union coverage when they cover exactly the same half, and when they cover disjoint halves. Compare with scalar source-over using two alpha values of .)
Use union coverage equals the sum of the two coverages minus the intersection.
Solution · Answer
For coincident halves, the intersection has coverage , so the union is . For disjoint halves, the intersection is 0 and the union is 1. Scalar source-over instead gives . The two marginal alpha values do not determine their intersection. The scalar result uses its overlap/attenuation model; it is not an exact union calculation for both geometric configurations.
Quick check
(Known colors are and . Find alpha for . Then replace by . Does the projection change? Is the second observation an exact solution of the model? What happens instead when ?)
Compute and project it onto .
Solution · Answer
Here has squared norm 2. For the first observation, , so and : it is an exact solution.
The changed green channel is orthogonal to D, so the projection remains , already in the feasible interval. The residual is now , which is nonzero. Thus this is the least-squares estimate, while no exact alpha can create a nonzero green channel from those known foreground/background colors. If , all alphas predict that same color; observation equal to it leaves alpha unidentified, and observation different from it is inconsistent with the noiseless model.