Hex to RGB Converter Guide: Color Notations Decoded (2026)
Every color you ship has at least three addresses: a hex code, an RGB triplet, and an HSL triple. This guide is the technical deep-dive into those notations — the base-16 math behind every hex digit, what the alpha channel really stores, the relative-luminance formula behind WCAG contrast ratios, and the converter workflows that keep design tokens and code in sync. Every conversion and ratio in this article was computed and machine-verified, not eyeballed.
Why Color Notations Exist at All
Color notations are not competing opinions about the same thing — they are addressing schemes for the same hardware reality. A modern display paints every pixel by driving three sub-pixels: red, green, and blue. Each channel is controlled by a number, and for most of computing history that number has been one byte: an integer from 0 to 255. Everything else about CSS color syntax — the hashes, the commas, the percentages — is ergonomics layered on top of that byte-level truth.
Hexadecimal won for design handoff because it is the most compact way to write three bytes readably: six characters, two per channel. RGB functional notation won in programming because the decimal numbers are the values you actually manipulate in code. HSL won for humans tuning colors because its three coordinates (hue angle, saturation, lightness) map to how people describe color in words. None of them change the pixels; they change how conveniently you can talk about the pixels.
That is why a converter never "changes" a color. Run any value through the Hex to RGB Converter and you get the same three bytes re-expressed — the same address in the same color space, written for a different audience. Here is the single most useful fact in this guide:
The same sRGB color, three addresses:
#1A2B3C rgb(26, 43, 60) hsl(210, 39.5%, 16.9%)
hexadecimal three decimal bytes hue / saturation / lightness
(design tokens) (JavaScript, canvas) (tints, shades, harmonies)
Deep palette strategy — building harmonies, scales, and brand systems from a base color — is covered in our Color Picker Ultimate Guide. This article stays focused on what the notations are and how to convert between them correctly. The gradient mechanics built on top of color interpolation live in the Gradient Generator Design Guide.
Hexadecimal Anatomy: Base 16 and the Two-Digit Byte
Hexadecimal is base 16. It needs sixteen digit symbols, so after 9 it borrows letters: A=10, B=11, C=12, D=13, E=14, F=15. A two-digit hex pair encodes exactly one byte, because the place values are 16 and 1:
value = first digit × 16 + second digit. The largest pair, FF, is 15 × 16 + 15 = 255 — which is precisely why a hex color channel runs from 00 to FF and why 255 (not 256) is the maximum RGB value: 256 would need a third digit.
Worked conversion: #1A2B3C → rgb(26, 43, 60)
Take the six digits as three byte pairs and expand each with the place-value rule. This example (a deep navy you will meet again in the contrast section) was verified with an independent base-16 conversion:
#1A2B3C -> rgb(26, 43, 60)
Red pair "1A": 1 × 16 + 10 = 16 + 10 = 26
Green pair "2B": 2 × 16 + 11 = 32 + 11 = 43
Blue pair "3C": 3 × 16 + 12 = 48 + 12 = 60
Hex digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal values: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Three structural facts fall out of this model, and each one prevents a real bug:
- Pairs are indivisible. #1A2B3C is three numbers, not six. You cannot reason about "the red part" of a hex code without taking two digits together.
- Every pair is a byte. There are 256 levels per channel and 256³ = 16,777,216 possible 6-digit colors — the "16.7 million colors" figure, derived here rather than asserted.
- Case is cosmetic. 1a2b3c and 1A2B3C parse to the same bytes in CSS. Case only matters when your own code compares hex strings as text (more on that in the mistakes section).
Because hex pairs are just bytes, hex is equally a base-16 way of writing any byte — which is why the same skill decodes hex to decimal for byte-level work far beyond color, from memory dumps to protocol headers.
Three-Digit Shorthand: The Doubling Rule
A 3-digit hex code is not truncated — it is a promise that every digit is doubled. #F0A expands to #FF00AA: the F becomes FF, the 0 becomes 00, the A becomes AA. Doubling a digit is the same as converting it to a byte whose two nibbles are equal, which is why only 4,096 colors have a shorthand form (16³ of the 16,777,216).
#F0A -> #FF00AA -> rgb(255, 0, 170)
F -> FF 0 -> 00 A -> AA
15→255 0→0 10→170
When is shorthand safe? Whenever the doubled form is the color you mean: pure primaries (#F00), grays on the nibble grid (#333, #EEE), and the handful of web-standards colors that happen to be doubling-friendly. It is unsafe the moment you copy a 6-digit design token and start shaving digits to save bytes — #1A2B3C has no valid 3-digit form, and rounding #1A2B3C to #1A3 changes the color outright. Compression is the wrong tool for that job; a minifier or a token rename saves more without touching values.
CSS also accepts 4-digit shorthand #RGBA, which doubles each digit including alpha — #F0A8 means the same color as #FF00AA88. It is far less common in the wild than the 3-digit form, so treat it as a reading skill more than a writing habit.
Alpha Channels: 8-Digit Hex and rgba()
Append one more byte pair and you are no longer describing a color — you are describing a color plus coverage. The fourth pair in 8-digit hex (#RRGGBBAA) is the alpha channel: 00 fully transparent, FF fully opaque, and everything between a fractional mask. The functional twin is rgba(26, 43, 60, 0.5), where alpha is a float from 0 to 1 instead of a byte from 0 to 255.
The two scales describe the same thing, but the mapping between them has a jagged edge worth knowing: alpha floats are byte/255, and byte = float × 255 rounded. Half opacity is the canonical example:
#1A2B3C80 -> rgba(26, 43, 60, 0.502) /* half-cover ink */
0x80 = 128 128 / 255 = 0.50196... ≈ 0.502
0.5 × 255 = 127.5 -> rounds to 128 = 0x80 (some tools truncate to 127 = 0x7F)
Common alpha bytes:
0x00 = 0 -> fully transparent
0x33 = 51 -> 0.20 exactly
0x80 = 128 -> 0.50196... (the "half" byte)
0xCC = 204 -> 0.80 exactly
0xFF = 255 -> fully opaque
Two design-system consequences follow. First, alpha stacks multiplicatively: two overlapping layers at 0.5 do not produce 0.5 coverage but 1 − (1 − 0.5) × (1 − 0.5) = 0.75 (three such layers reach 0.875). Overlays, focus rings, and hover washes compound invisibly unless you budget alpha per layer. Second, not every format carries alpha at all: GIF supports only binary transparency (a pixel is either fully in or fully out), and classic JPEG has none — our GIF Maker Animation Guide covers what that means for flat graphics, and Image Formats Explained maps which file types preserve which transparency model.
For handoff, pick one alpha dialect per document. Mixing #RRGGBBAA tokens with rgba() calls is readable; mixing in ARGB (alpha-first, common in some native platforms) is where copy-paste accidents happen.
HSL: The Notation Built for Humans
HSL describes a color as hue (an angle in degrees around the color wheel), saturation (a percentage from gray to fully chromatic), and lightness (a percentage from black through the pure hue to white). It is the notation that answers questions like "same color, but 20% lighter?" with arithmetic you can do in your head.
Convert our running example and check the numbers:
#3498DB -> rgb(52, 152, 219) -> hsl(204, 70%, 53%)
Same hue, same saturation, lightness ± 20 points:
hsl(204, 70%, 73%) -> #8AC4EA tint (softer, airy)
hsl(204, 70%, 53%) -> #3398DB base (rounds one step off #3498DB)
hsl(204, 70%, 33%) -> #19608F shade (deeper, grounded)
Rotate the hue +40°, keep everything else:
hsl(244, 70%, 53%) -> #3E33DB instant accent color
That last line is the entire productivity argument for HSL: a hover state is lightness −10, a disabled tint is lightness +30, an analogous accent is hue ±30 or ±40. In RGB or hex, those same operations are non-linear channel surgery. This is also why color-manipulation code (darken, lighten, mix, complement) is almost universally written in HSL or a similar cylindrical space internally.
One honesty note visible in the block above: the conventional rounded address hsl(204, 70%, 53%) converts back to #3398DB, one step away from the original #3498DB, because the exact conversion is 204.07°, 69.87%, 53.14% and rounding is lossy. That is normal and visually irrelevant — but if a pipeline demands byte-exact round trips, convert from the RGB bytes, never from rounded HSL. Generating full systems of tints and shades, then validating them for accessible contrast, is the territory of the Color Picker Ultimate Guide and the Color Palette Generator; the notation mechanics above are all you need to follow along there.
Color Math That Matters: Relative Luminance and WCAG Contrast
Most color math is convenience. One piece is compliance: the WCAG contrast ratio, which decides whether text is readable enough to ship. The formula is short, and it runs entirely on the RGB bytes you now know how to read.
Step one converts each sRGB channel from a "video-encoded" value to physical light. Channels are stored nonlinearly (that is what gamma buys you), so you undo the curve: divide by 255, then linearize — divide by 12.92 for tiny values (≤ 0.03928), otherwise raise the adjusted value to the power 2.4. Step two weights the linear channels by how strongly human vision responds to each: green dominates, red is second, blue barely counts.
| Channel | WCAG weight | Why |
|---|---|---|
| Red | 0.2126 | Moderate perceptual contribution |
| Green | 0.7152 | Human vision is most sensitive to green |
| Blue | 0.0722 | Weak luminous contribution |
Step three is the ratio itself: (L_lighter + 0.05) / (L_darker + 0.05), giving 1.0 for identical colors and 21.0 for black on white. Here is the full worked example, computed and verified, for our blue on white:
Contrast of #3498DB text on a #FFFFFF page:
1. Linearize each sRGB channel (c/255, then the sRGB curve):
R 52/255 = 0.2039 -> 0.0343
G 152/255 = 0.5961 -> 0.3140
B 219/255 = 0.8588 -> 0.7084
2. Weighted relative luminance:
L = 0.2126(0.0343) + 0.7152(0.3140) + 0.0722(0.7084) = 0.2830
3. Ratio against white (L = 1.0):
(1.0 + 0.05) / (0.2830 + 0.05) = 1.05 / 0.3330 = 3.15 : 1
Verdict: fails 4.5:1 body-text AA, passes 3:1 for large text
and UI components. Darken to about #2471A3 (5.30:1) for body copy.
Contrast the verdicts across our running examples — all ratios verified with the same computation:
- #1A2B3C on #FFFFFF: 14.44:1 — comfortably AAA (7:1) for all text.
- #767676 on #FFFFFF: 4.54:1 — the classic "darkest plain gray that clears body-text AA," barely.
- #3498DB on #FFFFFF: 3.15:1 — fine for large headlines and UI boundaries, not for paragraphs.
Thresholds to remember: 4.5:1 for normal text, 3:1 for large text (roughly 24 px, or 19 px bold) and for meaningful UI components, 7:1 for AAA. Because luminance weighting is green-heavy, note the practical shortcut: blue-dominant colors are luminance-poor, which is exactly why "blue links on white" fail contrast so often while yellow-on-white fails for the opposite reason.
Converting Programmatically: A Snippet That Survives Real Input
Converters are simple; correct converters are careful. The JavaScript below handles the inputs real traffic actually contains — pasted 3-digit codes, values with or without the hash, 8-digit alpha, uppercase and lowercase — and it deliberately avoids the bugs catalogued underneath it.
// Hex <-> RGB that survives real-world input
function hexToRgb(hex) {
let h = hex.replace(/^#/, '');
if (h.length === 3 || h.length === 4) {
h = [...h].map(c => c + c).join(''); // expand #F0A / #F0A8
}
if (h.length !== 6 && h.length !== 8) {
throw new Error('Unsupported hex length: ' + hex);
}
const num = parseInt(h, 16); // radix 16 is not optional
const hasAlpha = h.length === 8;
return {
r: (num >>> (hasAlpha ? 24 : 16)) & 255,
g: (num >>> (hasAlpha ? 16 : 8)) & 255,
b: (num >>> (hasAlpha ? 8 : 0)) & 255,
a: hasAlpha ? (num & 255) / 255 : 1
};
}
function rgbToHex(r, g, b, a = 1) {
const to255 = v => Math.round(Math.min(255, Math.max(0, v)));
const hex = ((1 << 24) + (to255(r) << 16) + (to255(g) << 8) + to255(b))
.toString(16).slice(1); // (1 << 24) pads to 6 digits
const aHex = a < 1
? Math.round(to255(a * 255)).toString(16).padStart(2, '0')
: '';
return ('#' + hex + aHex).toUpperCase(); // one canonical case
}
Five bugs that bite home-grown converters
- parseInt without a radix.
parseInt('1A')does not fail — it silently returns 1, stopping at the first non-decimal digit (likewiseparseInt('2B')→ 2). Always pass the 16:parseInt(h, 16). The0xprefix happens to auto-detect, but explicit beats accidental. - Missing zero-pad on the way back. Channel 5 becomes
'5', not'05', so naive concatenation builds#5...— a five-digit, invalid code. Either pad each byte (padStart(2, '0')) or use the(1 << 24)offset trick shown above. - Case drift.
toString(16)emits lowercase while most design tokens are uppercase. CSS does not care; string-keyed lookups do. Normalize to one case at the boundary, as the snippet does. - Alpha rounding asymmetry. 0.5 × 255 = 127.5, which rounds to 128 (0x80) but truncates to 127 (0x7F). Tools disagree by one byte, so #1A2B3C80 and #1A2B3C7F are both "half alpha" — pick Math.round and stay consistent.
- 8-digit values exceed 2³¹. A code like #FF808080 parses to an integer larger than the 32-bit signed range; bitwise coercion flips the sign. Unsigned shift (
>>>) handles it, which is why the snippet uses it throughout.
The same byte discipline transfers to other encodings: color values travel through CSS-in-JS, JSON tokens, and base64-encoded SVG data URIs (see Base64 Encoder & Decoder Explained and the Image to Base64 tool) — and if you inline an SVG swatch as a data URI, the # of the hex color must be URL-encoded, which is a job for the URL Encoder/Decoder. If you are assembling a personal toolbox of utilities like these, the Developer Tools Guide maps the rest of the free toolkit worth keeping in reach.
sRGB, Wide Gamut, and Why Screens Disagree
Hex and RGB values are coordinates in the sRGB color space — a specific, decades-old definition of what "full red" means physically. They are not absolute light recipes. On a wide-gamut display, the same #FF0000 coordinate is usually rendered more vividly than on an older panel, because the hardware can and browsers apply color management. This is why the same approved brand hex can look slightly different between your laptop and a newer phone, and why two people can argue about a color both are seeing "correctly."
Modern CSS acknowledges this honestly. The color() function lets you address other gamuts explicitly — color(display-p3 1 0.5 0) is a color that sRGB hex simply cannot express — and where a wide-gamut color must be shown on an sRGB screen, the browser maps it (gamut mapping), which can shift what you see. Photography pipelines already live in this world: modern phones capture Display-P3 by default, and ICC profiles embedded in image files tell the browser how to interpret every pixel — our iPhone Photo Management guide covers P3 capture, and Image Metadata Explained shows where those profiles live in the file.
The practical rules for web work are modest. Author UI colors in sRGB (hex/rgb) and they will render predictably everywhere. Reach for color(display-p3 …) only with an sRGB fallback declared first, so older browsers keep a coherent palette. And when a color looks "off" across devices, suspect the environment (panel, brightness, profile) before suspecting your math — and verify the asset side with Optimizing Images for Web and JPG Quality Settings Explained, since lossy artifacts shift apparent color too.
Which Notation When: A Decision Table
The notations are interchangeable in value and specialized in ergonomics. Mature codebases use several at once, each where it earns its keep:
| Job | Reach for | Why |
|---|---|---|
| Design tokens & brand values | 6-digit hex | Compact, diffable, universally pasted between tools |
| Overlays, washes, focus rings | 8-digit hex or rgba() | Alpha is first-class; stack budgets stay auditable |
| Programmatic tint/shade/rotate | HSL | Lightness and hue are single coordinates, not channel surgery |
| Debugging what the devtools show | Whatever it shows | Inspectors default to rgb/hsl; match it to copy values losslessly |
| Canvas, pixel loops, serialization | RGB bytes | The 0-255 integers are the bytes on the wire |
| Wide-gamut accents | color(display-p3 …) | Only notation that addresses outside sRGB (with a fallback) |
A useful discipline: tokens store the canonical hex; derived states (hover, active, disabled) are generated in HSL at build time and emitted back as hex or rgb. Humans author, machines convert — the opposite of hand-converting between notations in comments.
Workflow: Converting Colors in Real Projects
From browser devtools
The inspector is already a converter: the color swatch picker cycles the same value through hex, rgb, hsl, and (in current browsers) wide-gamut forms. Copy from it when exploring. When you need a value as a persistent token — or you are working outside the browser — paste into the Hex to RGB Converter, which returns the full address set at once. If a devtools value arrives as rgb(26 43 60 / 0.5), note the space-slash modern syntax; every tool above accepts the equivalent comma forms too.
From design files
Design tools default to sRGB and export hex, so tokens flow cleanly; but screenshots and flattened exports do not carry notation at all. Sample the pixel with the Color Picker (or pick straight from an image with the Image Color Picker), then convert the sampled value. Watch for two silent value-changers when importing from image editors: an embedded ICC profile that is not sRGB (see the gamut section) and anti-aliased edge pixels that are blends, not the true brand color — sample centers, not edges.
Batch and adjacent conversions
For palette-level work, the Color Converter handles the full notation matrix including HSL, and the Gradient Generator consumes those values directly when you move from tokens to interpolation — the mechanics of which are the Gradient Generator Design Guide's subject. Byte-level neighbors belong in the same toolbox session: Hex to Decimal Converter for the raw bytes, and once a brand system is settled, the Favicon Generator and Logo Maker take those same hex values as inputs. CSS-side cleanup after token refactors pairs naturally with the CSS Minifier.
7 Common Color-Notation Mistakes
- Wrong digit counts parsed "successfully." CSS accepts exactly 3, 4, 6, or 8 hex digits. A 5-digit or 7-digit value (#1A2B3, usually a copy-paste amputation) invalidates the whole declaration, so the element silently inherits a different color instead of erroring. When a color "doesn't apply," count digits first.
- Opacity stacking surprises. Alpha composites multiplicatively: two 0.5 layers yield 0.75 coverage; three yield 0.875. Teams budget per-element alpha, ship the stack, and discover the combined wash is far stronger than intended. Compute the stack value (1 − ∏(1 − α)) before shipping layered translucency.
- Mixing alpha notations. #RRGGBBAA puts alpha last; ARGB (some native platforms) puts it first. A pasted ARGB value silently becomes a strange color with 26/255 alpha instead of erroring. Also remember the modern
rgb(r g b / a)slash syntax when reading devtools output. - Assuming hex and rgb diverge. They never do — same bytes. "Fixes" that convert a failing hex token to rgb() change nothing about rendering and cost a sync point. If two notations of the same color render differently, the cause is environment (gamut, profile), not notation.
- Case-sensitive parsing in custom code. CSS treats #1a2b3c and #1A2B3C identically; your token map that keys on raw strings does not. Normalize case (and the leading #) at every ingestion point — most "missing token" bugs in theme code are case drift.
- Forgetting the gamut when debugging. A P3 photo next to sRGB swatches will not match no matter how many times you convert the hex. Before re-converting values, check the rendering environment: display capability, embedded profiles, and browser color management (see the gamut section).
- Hand-rolling luminance wrong. The classic errors: applying the 2.4-power curve to non-linear sRGB values (skipping linearization), using TV-style weights (0.299/0.587/0.114) instead of WCAG's 0.2126/0.7152/0.0722, or forgetting the +0.05 on both sides of the ratio. Each shifts a passing color to failing or vice versa. Copy the formula from the worked example above, not from memory.
Frequently Asked Questions
Split the six digits into three pairs and convert each pair from base 16 to decimal. For #1A2B3C: 1A is 1×16+10=26, 2B is 2×16+11=43, and 3C is 3×16+12=60, giving rgb(26, 43, 60). Each pair is one byte, so every pair lands between 0 and 255.
Yes. Hex and RGB are two encodings of the same three bytes: #1A2B3C, rgb(26, 43, 60), and its HSL conversion hsl(210, 39.5%, 16.9%) all address the identical sRGB color. Pick the notation that fits the task — tokens, math, or tuning — not the color.
The last pair is the alpha channel, on the same 0-255 byte scale as the color channels. #1A2B3C80 keeps the color at roughly half opacity because 0x80 is 128, and 128÷255 is about 0.502. The equivalent functional form is rgba(26, 43, 60, 0.5).
Hex and RGB values describe sRGB coordinates, not physical light output. Screens with different panels, brightness, or color management reproduce those coordinates differently, and wide-gamut displays may present them with extra saturation. The numbers are exact; the rendering is environment-dependent.
Hex is the common choice for design tokens because it is compact, universally supported, and easy to diff. Keep alpha as a separate token or an 8-digit hex pair, and switch to HSL in code when you need to generate tints, shades, or hue rotations programmatically.
HSL-to-RGB conversion runs through floating-point math, and tools round at different steps or to different precision. Converting hsl(204, 70%, 53%) back to hex yields #3398DB or #3498DB depending on where rounding happens — one step apart, both defensible. If you need byte-exact round trips, convert from RGB, not from rounded HSL.
Conclusion: One Color, Many Addresses
Hex, RGB, and HSL are not dialects of taste — they are byte views, integer views, and geometry views of the same sRGB coordinates. Once you can expand a hex pair by hand, read an alpha byte as a fraction of 255, compute a contrast ratio from three weighted channels, and name the one honest caveat about screens (gamut and color management), converter tools stop being magic and start being time-savers.
Keep the canonical values in hex tokens, do the manipulation in HSL, ship alpha deliberately, and let verified tools do the arithmetic: start with the Hex to RGB Converter and the Color Converter, and keep the Color Picker Ultimate Guide nearby for everything that happens after conversion — palette, harmony, and accessibility at system scale.