Computer colour

By Martin McBride, 2016-12-01
Tags: colour rgb hexadecimal css named colours
Categories: data representation imaging

Computers represent colours by mixing different amounts of red, green and blue.

RGB colour values

Computers represent colours using three number - the red, green, and blue (RGB) values. Usually each value is a number between 0 and 255 (0 indicates none of the colour is present, 255 indicates that the colour is turned up to the maximum). Here are some examples of (R, G, B) values:

colour chart

  • (255, 0, 0) represents pure red - red is turned on full, green and blue are turned off. Similarly (0, 255, 0) gives pure green, (0, 0, 255) gives pure blue.
  • (0, 255, 255) gives a mixture of green and blue with no red. This turquoisey colour is usually called cyan. Similarly (255, 0, 255) mixes red and blue, with no green, to give magenta, while red and green mix to give yellow.
  • (0, 0, 0), all three colours set to zero, gives black. (255, 255, 255) mixes all three colours to give white. Any colour which combines equal amounts of red, green and blue, such as (128, 128, 128) will give grey. The bigger the number, the lighter the grey.

The reason the colour values have the range 0 to 255 is because that is the range of values which will fit into a byte. An RGB colour can be stored in 3 bytes of memory.

Hex colour values

Sometimes it is useful to write colours as hexadecimal values rather than 3 denary values. We use this notation:

#ff3300

The first 2 digits give the red value (0xFF, or 255). The next 2 digits give the green value (0x33, or 51). The final 2 digits give the blue value (0x00, or 0). Of course, this is exactly the same colour as (255, 51, 0), just written in a different way. Hex notation is used in Cascading Style Sheets (CSS) to specify colours on web pages.

This chart shows a selection of the available colours, with their hex values:

colour chart

In total, there are 256x256x256 possible RGB values, which is about 16 million colours! But the human eye can't see quite that many colours, so colours which have very similar RGB values will look identical.

Named colours

CSS named colours contain 140 colour names which can be used in web pages (HTML or CSS). They are also used by the SVG format (for vector images). Some software graphics libraries also allow you to use named colours.

Named colours include all the standard colours (red, green, blue, yellow, white...) plus lots of other colours with whimsical names like powder blue or mint cream. Each colour has an RGB equivalent.

See also

Sign up to the Creative Coding Newletter

Join my newsletter to receive occasional emails when new content is added, using the form below:

Popular tags

555 timer abstract data type abstraction addition algorithm and gate array ascii ascii85 base32 base64 battery binary binary encoding binary search bit block cipher block padding byte canvas colour coming soon computer music condition cryptographic attacks cryptography decomposition decryption deduplication dictionary attack encryption file server flash memory hard drive hashing hexadecimal hmac html image insertion sort ip address key derivation lamp linear search list mac mac address mesh network message authentication code music nand gate network storage none nor gate not gate op-amp or gate pixel private key python quantisation queue raid ram relational operator resources rgb rom search sort sound synthesis ssd star network supercollider svg switch symmetric encryption truth table turtle graphics yenc