eolas/neuron/78bb25d9-ca3c-4cbb-a2c4-e9c73950d1ca/Binary_colour_encoding.md

54 lines
1.7 KiB
Markdown
Raw Normal View History

2024-10-19 11:00:03 +01:00
---
tags:
- binary
---
# Binary encoding of colours
The approach to encoding binary representations of colour is very similar to the
approach we explored when looking at the encoding of
[alphanumeric values](Text_encoding.md).
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.
### 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.
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):
2024-10-20 19:50:20 +01:00
![](static/greyscale-encoding.svg)
2024-10-19 11:00:03 +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.
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.
2024-10-20 19:50:20 +01:00
![](static/red-encoding.svg)
2024-10-19 11:00:03 +01:00
#### Yellow
Yellow is represented in RGB with both red and blue set to 1 and the remaining 8
green bits set to ):
2024-10-20 19:50:20 +01:00
![](static/yellow-encoding.svg)
2024-10-19 11:00:03 +01:00
## Binary encoding of images