Turning three decimal numbers into hex pairs
Going from RGB to hex is the mirror of hex to RGB: instead of splitting a string into pairs and
reading each as base-16, you take three decimal numbers and write each one as a two-digit
hexadecimal number, then concatenate the results. Every step is per-channel — red becomes its own
pair, green becomes its own pair, blue becomes its own pair — and the three pairs are simply
placed next to each other with a # in front.
Two worked examples
Take rgb(30, 144, 255). Convert 30: it is 1 × 16 + 14, and 14 in hex is e, so 30 is 1e.
Convert 144: it is 9 × 16 + 0, so 144 is 90. Convert 255: it is 15 × 16 + 15, the maximum for
a byte, so 255 is ff. Joined, that is #1e90ff — dodger blue.
A second example, rgb(46, 204, 113): 46 is 2 × 16 + 14, which is 2e; 204 is 12 × 16 + 12,
which is cc; 113 is 7 × 16 + 1, which is 71. The result is #2ecc71, a clean emerald green.
Both examples are the reverse of a hex-to-RGB conversion, and running either one back through the
other direction returns the original numbers exactly, with no rounding drift in either direction.
Why 255 is a ceiling, not a suggestion
An 8-bit colour channel has exactly 256 possible values, 0 through 255, because a byte holds
256 distinct states. rgb(255, 99, 300) is not a very bright, very saturated red — it is a
number that has no hex pair to become, since two hex digits stop at ff. Tools that silently
clamp 300 down to 255 hide the fact that a value came from somewhere else entirely: a 0–100%
scale mistyped as 0–255, a value doubled by a unit-conversion bug, or a paste that grabbed one
digit too many. Flagging the input instead of guessing keeps that bug visible. The same logic
applies at the bottom end: a negative channel, like rgb(-10, 99, 71), is rejected for the same
reason — there is no hex pair below 00, so a negative number is not a darker shade, it is a
value that should never have reached this step.
Decimal to hex, quick reference
A handful of round numbers are worth recognising on sight, since they come up constantly in design tools and default palettes.
| Decimal | Hex pair |
|---|---|
| 0 | 00 |
| 16 | 10 |
| 32 | 20 |
| 64 | 40 |
| 128 | 80 |
| 255 | ff |
When hex is the better format to hand off
RGB is what a colour picker or a canvas API tends to give you; hex is what a stylesheet, a design handoff document, or a Slack message asking "what colour is this" tends to want. Converting RGB to hex is the step that turns three numbers scattered across a UI into one string a designer or a CSS rule can use directly, which is why the two conversions get used about equally often in practice — the direction just depends on which end of the pipeline you're standing at. Keep both this page and its Hex to RGB counterpart bookmarked and you cover the pipeline in either direction, whichever end a colour happens to arrive from.