Sub topic 5
Hex codes: writing RGB in base 16
This chapter assumes you know that a screen colour is three 8-bit channels (red, green, blue), each from 0 to 255. It also assumes you are comfortable with simple arithmetic. Some CSS or JavaScript helps for the code, but is not required.
What a hex code is
A hex code is an RGB colour written in base 16 (hexadecimal). It starts with # and is followed by six digits. Each pair of digits is one channel, in the order red, green, blue.
Base 16 uses sixteen symbols: 0–9 for zero to nine, then a–f for ten to fifteen. Two hex digits cover exactly 256 values (16 × 16), which matches one 8-bit channel. That fit is why hex became the standard short form for RGB.
| Hex pair | Calculation | Decimal |
|---|---|---|
| `00` | 0 × 16 + 0 | 0 |
| `0a` | 0 × 16 + 10 | 10 |
| `88` | 8 × 16 + 8 | 136 |
| `ff` | 15 × 16 + 15 | 255 |
Reading a hex code by hand
Take #ff8800 and split it into pairs:
- Red is
ff, which is 255. - Green is
88, which is 136. - Blue is
00, which is 0.
So #ff8800 is the same colour as rgb(255, 136, 0), a strong orange. The first digit of each pair is the "sixteens" place, so it has the larger effect. In 88, the first 8 contributes 128 and the second contributes 8.
Hex in CSS
CSS accepts hex and functional notation for the same colour. The shorthand #fff repeats each digit, so it expands to #ffffff.
.button {
background-color: #ff8800;
border-color: rgb(255 136 0);
color: #fff;
}Here background-color and border-color are the same orange. The text colour is white.
The shorthand only works when each pair has two identical digits. You can write #f80 for #ff8800, but you cannot shorten #ff8801.
Converting in code
Conversion is just parsing or formatting three base-16 numbers. The JavaScript below does both directions.
function hexToRgb(hex) {
const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex);
if (!m) throw new Error("Invalid hex colour: " + hex);
return m.slice(1).map((h) => parseInt(h, 16));
}
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("");
}
console.log(hexToRgb("#ff8800")); // [255, 136, 0]
console.log(rgbToHex(255, 136, 0)); // "#ff8800"In hexToRgb, the regular expression captures three pairs of hex digits. parseInt(h, 16) reads each pair as a base-16 number. In rgbToHex, toString(16) writes a number in base 16. padStart(2, "0") is essential, because without it the value 0 would become "0" instead of "00", and the code would be too short.
Common errors
- Dropping the leading zero. Writing blue 0 as
0instead of00shifts every pair and produces a different colour or an invalid code. - Mixing up shorthand.
#f80means#ff8800, not#f08000. Each digit is doubled. - Wrong length. Valid codes have 3 or 6 digits. CSS also accepts 4 or 8 digits, where the extra digit or pair is transparency (alpha). Five or seven digits are invalid.
- Case confusion.
#FF8800and#ff8800are identical. Hex is case-insensitive, so pick one style and keep it consistent in a project. - Assuming hex is a different colour space. A hex code carries no colour space of its own. On the web it is read as sRGB by default, so the same digits can look different if a tool interprets them in another space.
Choosing between hex and rgb()
Use hex when you want a compact value to copy between design tools and code. Use rgb() when you need to read or adjust individual channels, or when you want to write an alpha value in a readable way. Both are equivalent in the browser, so the choice is about readability for your team.
Exercise
What RGB values does the hex code #1a80ff represent? Show how you get each channel.
Show answer
Split into pairs: 1a, 80, ff. 1a is 1 × 16 + 10 = 26. 80 is 8 × 16 + 0 = 128. ff is 15 × 16 + 15 = 255. The colour is rgb(26, 128, 255).
Exercise
Why does rgbToHex(0, 8, 255) need padStart(2, "0") to return the correct result, and what would it return without it?
Show answer
(0).toString(16) gives "0" and (8).toString(16) gives "8", each only one digit. Without padding the result would be "#08ff", which is four digits and not a valid six-digit code. With padding it is "#0008ff".