GIF Global Color Table Bit-Depth Range
The Global Color Table (GCT) in a GIF file relies on a specific bit-depth structure to determine both the total number of colors an image can display and the fidelity of those individual colors. In the GIF specification (both GIF87a and GIF89a), the palette index bit-depth ranges from 1 bit to 8 bits, allowing between 2 and 256 simultaneous colors per table. However, each individual color entry within that table is defined using a 24-bit RGB format, providing access to a true-color palette of over 16.7 million possible values.
Palette Index Bit-Depth (1 to 8 Bits)
The size of the Global Color Table is dictated by a 3-bit field located within the Logical Screen Descriptor of the GIF header. This value determines the bit-depth of the pixel data referencing the table:
- 1-bit depth: Supports up to \(2^1\) (2) colors.
- 2-bit depth: Supports up to \(2^2\) (4) colors.
- 3-bit depth: Supports up to \(2^3\) (8) colors.
- 4-bit depth: Supports up to \(2^4\) (16) colors.
- 5-bit depth: Supports up to \(2^5\) (32) colors.
- 6-bit depth: Supports up to \(2^6\) (64) colors.
- 7-bit depth: Supports up to \(2^7\) (128) colors.
- 8-bit depth: Supports up to \(2^8\) (256) colors.
Because the field in the header is 3 bits wide, the encoded value ranges from 0 to 7, which corresponds mathematically to \(2^{(N + 1)}\) entries. An encoder can optimize file size by lowering the bit-depth to match only the number of colors strictly required by the image.
Color Entry Bit-Depth (24-Bit RGB)
While the table can hold a maximum of 256 palette entries (8-bit index), the color definition of each entry is fixed:
- Each color entry consists of 3 sequential bytes (24 bits total).
- Red: 1 byte (8 bits, values 0–255)
- Green: 1 byte (8 bits, values 0–255)
- Blue: 1 byte (8 bits, values 0–255)
This 24-bit definition allows any selected palette color to be chosen from a master palette of 16,777,216 distinct colors.
The "Color Resolution" Field
The GIF Logical Screen Descriptor also contains a separate 3-bit "Color Resolution" field. Historically, this field represented the bit-depth of the original palette from which the image colors were sampled:
- It can indicate a range from 1 to 8 bits per primary color channel.
- This theoretical range represents 3 bits (1 bit per R, G, B channel) up to 24 bits (8 bits per channel) of color precision.
- In modern software implementations, this field is largely ignored during rendering, and standard 8-bit-per-channel (24-bit total) RGB values are used for all palette definitions.