Screen Colour Formats
Every pixel on the screen is represented by numbers that define its colour. Normally these are split into red, green and blue channels.
Indexed (palette) format
The indexed colour format was common in the past but has pretty much died out nowadays. It uses just a small amount of data to represent a pixel with the value being an index into a colour look up table (palette).
- 1-bit colour, gives 2 colours, monochrome
- 2-bit colour, gives 4 colours, known as CGA
- 4-bit colour, gives 16 colours, known as EGA
- 8-bit colour, gives 256 colours, known as VGA or Super VGA at high resolutions
15 bit format (555)
5 bits each for red, green and blue. If held in 16 bit data one bit is not used. This gives 32*32*32 (32,768) different colours.
Tip: to convert a 32 bit colour to a 15 bit 555 one you must reduce the data and shift the bits into position like this:
WORD c= (((red & 0xF8) << 7) | ((green & 0xF8) << 2) | (blue >> 3))
16 bit format (565)
With this format 5 bits are given to the Red channel, 6 to the Green channel and 5 to the Blue channel. Apparently the reason for the extra bit going to the green channel is that the human eye can see more shades of green than any other colour. This gives 32*64*32 (65,536) different colours.
Tip: to convert a 32 bit colour to a 16 bit 565 one you must reduce the data and shift the bits into position like this:
WORD c= (((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3));
24 bit format
With this format there are 8 bits (one byte) per colour channel. This gives 256 levels of colour for red, green and blue hence 256*256*256 (16,777,216) different colours.
32 bit format
The same as 24 bit format but with 8 extra bits unused, for padding purposes. It is common for processors to deal with data internally in 32 bits and so padding the colour to 32 bits provides an optimisation. Note: 32 bit textures can use the extra 8 bits for an alpha channel. The alpha channel varies from 0 (fully transparent) to 255 (fully opaque). Screen formats do not use the alpha channel and so can show the same number of colours as 24 bit.
The 32 bit colour format is by far the easiest to work with and gives the best range of colours. The only down side to it is that it will use the most memory. A 640 by 480 screen in 32 bit colour would require 921.6 KB of memory.