Cutting out an object and placing it on a different background seems trivial, until dark edges, light halos or gradients that turn opaque appear. These problems almost always stem from a misunderstanding of how the alpha channel represents transparency and how programs combine layers. The subject is called compositing, and it underlies photo editing, graphical interfaces, video effects and game engines.
What alpha represents
The alpha channel is a fourth value attached to each pixel, alongside red, green and blue. It indicates coverage: how much of the pixel is occupied by the object.
- alpha = 1 (or 255 at 8 bits): the pixel is fully covered, opaque;
- alpha = 0: the pixel is empty, completely transparent;
- intermediate values: the pixel is only partly covered, as happens at antialiased edges, in hair, glass and smoke.
Alpha is not a color, and a pixel with zero alpha does not, strictly speaking, have a meaningful color. This seemingly philosophical point is the key to many practical defects.
The "over" operator
The most common case is placing a foreground layer (F) over a background (B). With values normalised between 0 and 1, and an opaque background, the formula is:
result = F.color × F.alpha + B.color × (1 − F.alpha)
An edge pixel with alpha 0.3 takes 30% of the object's color and 70% of the background's. This operation, together with others such as in, out, atop and xor, was formalised by Thomas Porter and Tom Duff in a 1984 paper on compositing digital images, and it is still the basis of graphics systems today.
Straight and premultiplied alpha
There are two ways to store a transparent pixel:
| Representation | What the color contains | Example: red at 50% |
|---|---|---|
| Straight alpha | the object's "pure" color | (1, 0, 0, 0.5) |
| Premultiplied alpha | color already multiplied by alpha | (0.5, 0, 0, 0.5) |
With premultiplied alpha the "over" formula becomes simpler:
result = F + B × (1 − F.alpha)
The benefits are not only about speed:
- Correct filtering: when you resize, blur or rotate a transparent image, you average pixels. With straight alpha, the average of an opaque red pixel and a transparent "black" pixel (0, 0, 0, 0) produces a darkened color. With premultiplied alpha, the transparent pixel is zero in every channel and does not contaminate the result.
- Associative compositing: several layers can be merged into intermediate groups without changing the final result.
- Additive lights: a pixel with non-zero color and zero alpha represents light that adds without covering, useful for glows and fire.
That is why many graphics engines and compositing programs work internally with premultiplied alpha, even though PNG files, by specification, store straight alpha.
Fringes: diagnosis and remedies
Dark edge around a cut-out. Typical of a straight-alpha image filtered or resized without premultiplying: the transparent pixels had a black color and "bled" into the edges. Remedy: premultiply before filtering, or fill transparent pixels with the color of nearby edges (a technique called dilation or bleeding), widely used for video game textures.
Light edge around an object cut from a white background. The edge pixels still contain part of the original background's color. Remedy: the "color decontamination" or defringe functions in retouching programs, which estimate and subtract the background's contribution.
Double multiplication. If an already premultiplied image is treated as straight, edges become too dark and transparent gradients too faint. It happens when two components in a chain make different assumptions: you need to establish which format each one expects.
Blend modes
Beyond simple layering, programs offer blend modes that combine colors in different ways. The most used are:
- Multiply: F × B; always darkens, white is neutral. Useful for shadows and applying textures.
- Screen: 1 − (1 − F) × (1 − B); always lightens, black is neutral. Useful for lights and glows.
- Overlay: multiplies in the dark areas of the background and screens in the light ones; increases contrast.
- Difference: |F − B|; useful for comparing two versions of an image, because whatever is identical turns black.
The Difference mode is a tool I often use in my work to spot changes between two versions of an image: even variations of a single value level become visible when the contrast of the result is boosted.
Bear in mind that blend modes are also affected by gamma: the same mode applied in sRGB space or in linear space gives different results.
Formats that support transparency
- PNG: full alpha at 8 or 16 bits, or simple transparency through the tRNS chunk.
- GIF: a single transparent color, with no intermediate levels: that is why edges look jagged.
- WebP and AVIF: full alpha, even with lossy compression.
- TIFF and OpenEXR: full alpha, used in professional workflows.
- JPEG: no alpha channel.
In short
- The alpha channel represents each pixel's coverage, from 0 (empty) to 1 (opaque).
- The "over" operator mixes foreground and background in proportion to alpha.
- Premultiplied alpha makes filtering and resizing correct and avoids dark edges.
- Light halos come from the original background and are removed with color decontamination.
- Blend modes such as multiply, screen and difference combine colors in specific ways.