Lesson 47: Hex colours
Colours as numbers
- A web page writes a colour as six hexadecimal digits, for example
#FF8000(orange). - The six digits are three pairs: red, green and blue. Each pair is 00 to FF, which is 0 to 255.
- An RGB LED has a red, a green and a blue LED inside. With
analogWriteeach one gets a value from 0 to 255.
Words for this lesson
| Word | Meaning |
|---|---|
| hexadecimal | base 16: digits 0–9, then A–F for 10–15 |
| denary | base 10, our normal numbers |
| RGB | red, green, blue |
| pair | two hex digits: one byte, 8 bits |
From a pair to a number
| Pair | Working | Denary | Binary |
|---|---|---|---|
| FF | 15 × 16 + 15 | 255 | 11111111 |
| 80 | 8 × 16 + 0 | 128 | 10000000 |
| 2F | 2 × 16 + 15 | 47 | 00101111 |
In the exam
- 0478/12/M/J/22 Q2: the HTML colour code #2F15D6 is stored as three 8-bit values. (a) Give the binary for each pair. Mark scheme: 2F is 0010 1111, 15 is 0001 0101, D6 is 1101 0110, the same as your second program prints. Each hex digit is one group of 4 bits. (b) Two other uses of hexadecimal: the mark scheme gives IP addresses and error codes.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A web colour code like FF8000 is three hexadecimal pairs: red, green and blue, each from 00 to FF (0 to 255). Type a code into the Serial Monitor and show the colour on the RGB LED: red on pin 9, green on pin 10, blue on pin 11, with analogWrite. Finish hexDigit(c), which turns one hex character into 0 to 15.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Each pair is stored in the computer as 8 bits. Type a colour code. For each of the three pairs print the pair, its denary value and its 8 bits, for example 2F = 47 = 00101111.
Nhấn Chạy để xem kết quả ở đây.