eolas/zk/Binary_colour_encoding.md

55 lines
1.7 KiB
Markdown
Raw Normal View History

2022-10-10 11:00:05 +01:00
---
categories:
- Computer Architecture
tags: [binary, binary-encoding]
---
2022-10-10 13:00:05 +01:00
2022-12-10 11:30:05 +00:00
# Binary encoding of colours
2022-10-10 13:00:05 +01:00
The approach to encoding binary representations of colour is very similar to the
approach we explored when looking at the encoding of
[alphanumeric values](/Electronics_and_Hardware/Binary/Text_encoding.md).
2022-10-10 13:00:05 +01:00
We begin by determining the total number of colours and colour shades we want to
represent. With this value established we then decide on the bit-length required
that will accomodate this number of variations. Finally, we assign a binary
number to each representation.
2022-10-10 13:00:05 +01:00
### Greyscale
We can start with a limited palette: greyscale. Here there is black and white
and number of interim shades of grey. The convention is to use an 8-bit number
which, given $2^8 = 256$ provides 256 shade variations.
2022-10-10 13:00:05 +01:00
In decimal, 0 is equal to black (zero light intensity) and 255 is equal to white
(full light intensity). Some examples of this (including binary and hex
representations are below):
2022-10-10 13:00:05 +01:00
![](/_img/greyscale-encoding.svg)
2022-10-10 13:00:05 +01:00
### Colour encoding
A single 8-bit number is sufficient for 256 greyscale combinations but with
colour, the convention is to use three 8-bit numbers where each byte corresponds
to a value for red, green and blue (RGB). Thus the overall bit-length is 24
($2^3 + 2^3 + 2^3$). Each value corresponds to a light intensity for the given
colour. Combined they are capable of representing all colours.
2022-10-10 13:00:05 +01:00
Some examples below
#### Red
Red is represented in RGB with all 8 red bits to set to 1 and the remaining 16
bits for the other two colours set to 0.
2022-10-10 13:00:05 +01:00
![](/_img/red-encoding.svg)
2022-10-10 13:00:05 +01:00
#### Yellow
Yellow is represented in RGB with both red and blue set to 1 and the remaining 8
green bits set to ):
2022-10-10 13:00:05 +01:00
![](/_img/yellow-encoding.svg)
2022-10-10 13:00:05 +01:00
## Binary encoding of images