Colors in Portable Network Graphics PNG

PNG files can encode colors in many ways that depend on color type, bit depth, palette. This leads to different maximum number of colors. See details here.

A word on PNG

PNG stands for Portable Network Graphics. This is an image file format described in the RFC 2083 document which this post is largely based on. Any software writing or reading PNG files must implement the mandatory aspects of this RFC. The PNG file is made of a signature (8 bytes identical for each PNG file) and chunks. Interpreting the chunks allows the software to render or otherwise use the image. Some chunks are more relevant than others when it comes to colors encoding.

Chunks relevant for the encoding of colors in Portable Network Graphics PNG

The chunks relevant to colors encoding in a PNG file are the following ones. They are all mandatory in implementation but not necessarily in file for encoding and decoding:

  • IHDR chunk with the color type and the bit depth
  • PLTE palette chunk
  • IDAT image data chunk

IHDR chunk with bit depth and color type

The IHDR chunk is mandatory in encoder and decoder implementation. Plus, any PNG file contains one. Among other important aspect of the file, it contains the bit depth and the color type.

The color type is the sum of 3 variables:

  • 1 if palette is used
  • 2 if color is used
  • 4 if alpha channel (opacity/transparency) is used

Note that palette use is only possible with color and without alpha channel. Therefore, color type can only take the values of 0, 2, 3, 4, and 6.

The bit depth is the number of bits per sample or per palette index. The differences between the sample and the pixel are that a pixel is represented by two samples in case of alpha channel and that the sample bit depth is always 8 in case of palette. Possible values for the bit depth are powers of 2 from 1 to 16: 1, 2, 4, 8, 16. Though, possible values depend on the color type:

Color typeAllowed bit depthsPixel description
0 (grayscale)1, 2, 4, 8, 16
(any)
grayscale sample
2 (color)8, 16
(>4)
RGB sample
3 (palette + color)1, 2, 4, 8
(<16)
Palette index
(requires PLTE chunk)
4 (grayscale + alpha)8, 16
(>4)
grayscale sample
+ alpha sample
6 (color + alpha)8, 16
(>4)
RGB sample
+ alpha sample

PLTE the palette chunk

The palette chunk contains 1 to 256 three bytes RGB colors. The number of colors in the palette must be within what bit depth allows (2^4 = 16 colors maximum for bit depth 4 for example). This chunk is necessary for color type 3 (palette + color) and optional for color types 2 and 6 (color and color + alpha). Each pixel value is the number of the palette entry encoding its color. This means that a palette encoded PNG can represent 1 to 256 colors chosen among 2 ^ (3 * 8) = 16,777,216 possible RGB colors where 2 is the binary possibilities of bits, 3 is the number of bytes (RGB) and 8 is the number of bits for each byte.

Chunks of lower importance for PNG color encoding

Here are some non-strictly color encoding related data not listed above. Yet, they are of interest when discussing colors encoding. None of those are mandatory:

  • bKGD Background color chunk
  • cHRM Primary chromaticities and white point chunk
  • gAMA Image gamma chunk
  • hIST Image histogram chunk
  • sBIT Significant bits chunk
  • tRNS Transparency chunk

As they are not directly useful to the color encoding, please refer to the other readings section below for details about them.

Maximum number of colors that can be encoded in a PNG file

The maximum number of colors that can be encoded in a PNG file depends on the color type and the bit depth. The alpha channel (opacity/transparency) is not considered. Here is a summary of the possibilities below:

Color typeBit
depth
Maximum number
of colors
(without
alpha)
0 (grayscale),
4 (grayscale + alpha)
12^1 = 2
22^2 = 4
42^4 = 16
82^8 = 256
162^16 = 65,536
2 (color),
6 (color + alpha)
82^(3*8) = 16,777,216
162^(3*16) =
281,474,976,710,656
3 (palette + color)1Palette of up to 2^1 = 2
colors among 2^(3*8) =
16,777,216 colors
2Palette of up to 2^2 = 4
colors among 2^(3*8) =
16,777,216 colors
4Palette of up to 2^4 = 16
colors among 2^(3*8) =
16,777,216 colors
8Palette of up to 2^8 = 256
colors among 2^(3*8) =
16,777,216 colors

Therefore, the global maximum number of colors that can be represented by a PNG file without considering transparency is 281,474,976,710,656.

The general formula for the calculation of maximum number of colors can be expressed as:

maximum_number_of_colors = bit_possibilities ^ ( number_of_channels * bit_depth )

Where:

  • bit_possibilities = 2
  • number_of_channels is 1 for grayscales and 3 for RGB
  • bit_depth is the bit depth as explained above

Other readings on PNG colors encoding

Information technology — Computer graphics and image processing — Portable Network Graphics (PNG): Functional specification. ISO/IEC 15948:2003 (E)

PNG (Portable Network Graphics) Specification Version 1.0

PNG The Definitive Guide Paperback

Leave a comment

Thanks for your visit. I would be thrilled to know what you think of this explanation on the color encoding in the PNG files through the comments section below.