Where Does a PNG Keep Its Transparent Edges?
On a screen, the edge of a green leaf cut-out can look pale against cream and dark against charcoal. A Portable Network Graphics (PNG) file can store each edge pixel’s colour and its opacity, the degree to which that pixel hides what lies behind it. The viewing software supplies the background for the blend. The pale and dark rims can therefore come from the same file.

The leaf here is hypothetical. It has a solid green interior, a softened outline and an invisible area outside the shape. The World Wide Web Consortium (W3C) describes the storage options in its second edition of the PNG specification: "Indexed-color, grayscale, and truecolor images are supported, plus an optional alpha channel." An alpha channel is the collection of opacity values for the pixels in an image. The distinction between colour and alpha is easiest to follow at a single point on the outline.
What is stored at a softened leaf edge?
Suppose the leaf is stored as truecolour with alpha. Truecolour represents colour directly with red, green and blue values. Each pixel in this kind of PNG carries four samples, or individual values: red, green, blue and alpha. The colour values describe the foreground at that point; alpha tells the viewer how much of that foreground will contribute when it is blended with a background.
The W3C specification keeps those values independent at storage. An edge pixel can contain the same green as a pixel inside the leaf even when the edge pixel has lower opacity. The file does not first darken that green by multiplying it by alpha. The term unassociated alpha describes this storage convention: the effect of opacity is applied when the viewer uses the colour in a blend.
Inside the hypothetical shape, maximum alpha leaves the pixel fully opaque. Outside the shape, zero alpha makes the pixel fully transparent. The rectangular image can still contain colour values at that outside position; at zero opacity the blend shows only the backdrop. Between those extremes an edge pixel can keep enough of the stored green to carry the outline while leaving some of the background visible.
- Colour samplesRed, green and blue describe the foreground colour.
- Alpha sampleOpacity is stored separately from the foreground colour.
The stored green need not darken when its alpha goes down.
A useful way to locate the stored signal is to follow a line from the leaf through its outline and into the surrounding area. In this example the colour could stay green along that line as the opacity falls away. If the edge owes its softness to those intermediate opacity values, looking only at the colour samples would miss the change that creates it. Looking only at alpha would show where the leaf fades, without identifying the shade of green that fades there.
What changes when the backdrop changes?
The W3C calls the merge of foreground and background compositing. Its second edition describes a blend in which each foreground contribution is weighted by alpha and the background contribution is weighted by the fraction left over. The W3C calculation blends red, green and blue separately using values proportional to light intensity. It does not blend the gamma-encoded values directly, since gamma encoding represents those intensities on a non-linear scale.
With the example leaf on cream, the backdrop pulls a partly opaque pixel towards cream. With the same leaf on charcoal, the backdrop pulls that pixel towards charcoal. In both previews the viewer starts from the same foreground colour and uses the same opacity. The difference in the rim comes from the background term in the calculation, so there is no need for a cream-edged copy of the PNG and a charcoal-edged copy.
- What the leaf file carries
- Opaque interior in the leaf
- Partly opaque rim in the leaf
- Transparent area outside the shape
- What the viewer displays
- The foreground without a contribution from the backdrop
- A rim blended with cream or charcoal
- The backdrop at that position
Under the same compositing conditions the opaque interior stays the same when the backdrop changes. At a transparent position beyond the silhouette the opposite happens: the switch from cream to charcoal is visible in full. The softened rim falls between them, with the extent of the change depending on how much of the backdrop each pixel lets through. The two previews make a distinction that is hard to see on a single flat background: the dark rim in one preview does not establish that the stored foreground colour at the rim is dark.
When can a PNG give transparency to a colour instead?
A different encoding works if the leaf has a hard boundary. Suppose a particular grey value is reserved for the area outside the leaf, the leaf itself is fully opaque, and that grey occurs nowhere inside it. For a greyscale PNG without a separate alpha channel, the W3C second edition allows a transparency chunk, a named section of the file called tRNS, to identify the exact grey that disappears. Its truecolour counterpart identifies a particular red, green and blue combination.
This is an exact match. Every pixel with the designated value is fully transparent; everything else is fully opaque. A pixel that is almost the designated grey still shows in full. If a detail inside the leaf has exactly the same value as the transparent area outside it, the viewer treats the detail as transparent too. A colour key of this kind cannot give separate opacities to identical colour values in different places or produce the intermediate opacity of the original softened leaf.
For 16-bit greyscale or truecolour, the W3C second edition says the decoder must test for an exact match before rescaling the sample values. Suppose the grey inside the leaf is so close to the designated grey that a reduced display scale would round them to the same value. At the precision stored in the file they are still different. The outside disappears, while the inside remains opaque. The viewer cannot decide which to hide from the rounded grey alone.
The comparison is different for indexed-colour PNG. Each pixel in that kind of file points to an entry in a palette, a list of colours kept with the image. The W3C specification allows tRNS to carry opacity values for palette entries, including partial opacity. Pixels using a given entry share its opacity, so a file of this kind can retain degrees of transparency without writing out an alpha sample alongside each pixel.
The opacity table can also end before the palette does. The W3C treats the palette entries beyond the end of the table as fully opaque. A palette for the leaf could put its translucent greens early in the list and leave the opaque greens at the end, where those colours still have palette entries but need no entries of their own in the opacity table.
Scroll sideways to see all columns.
| Storage case | What carries transparency | What the rule applies to |
|---|---|---|
| Truecolour or greyscale with tRNS | A designated colour or grey value | Exact matches disappear; other pixels are opaque |
| Indexed-colour with tRNS | Opacity values attached to palette entries | Pixels that refer to the same entry share its opacity |
Suppose the leaf silhouette has green at its centre and the same green with partial opacity at its rim. A palette could represent the distinction with separate entries: one for the opaque green and one for the partly opaque green. Both entries can describe the same foreground colour, because their opacity values come from the accompanying table. A greyscale or truecolour image that specifies only a transparent colour has no equivalent way to make that distinction.
The storage route matters when interpreting the file; it can be invisible in the preview. In the leaf example, an edge encoded with per-pixel alpha and an indexed edge with matching colour and opacity can show the same rim. Neither needs to change what it stores when the display moves from cream to charcoal. On screen it is the rim that changes, while the fully opaque interior keeps its green.
Frequently Asked Questions
What does lossless storage mean for PNG?
In the W3C second edition, a PNG decoder can recover the reference image exactly. The reference image is the pixel array used as the specification’s starting point for encoding, which may differ from the original input to the encoder. Information discarded before that reference image is established cannot be recovered from the file.
Can a PNG contain a suggested background colour?
A PNG can include a bKGD chunk containing a suggested background colour. The W3C second edition allows a viewer to ignore the suggestion when a more suitable background is available. The suggestion is separate from the image’s transparency information.
Can a PNG preview sharpen while the file is still arriving?
The W3C second edition allows an interlaced PNG, which sends groups of pixels in an order that supports progressive display. A viewer can build a coarse view of the whole image and refine it as the later groups arrive. The last group completes the image.
Does a PNG have to specify its intended pixel shape?
A PNG can carry physical pixel information in a pHYs chunk. The chunk can specify pixel dimensions or only the pixel aspect ratio, the proportion of a pixel’s width to its height. Without pHYs, the W3C second edition assumes square pixels and leaves their physical size unspecified.
Sources
- World Wide Web Consortium (W3C): Portable Network Graphics (PNG) Specification (Second Edition)