What is a hex color code?
A hex color code writes a screen color as three two-digit hexadecimal numbers after a hash sign: #RRGGBB. The first pair is the amount of red, the second green and the third blue, each from 00 (none) to ff (full). In decimal that is 0 to 255 per channel, exactly the numbers an rgb() color uses. Hex and RGB are two spellings of the same color, not two different color systems.
Hexadecimal is base 16. It needs sixteen digits, so after 0 to 9 it borrows the letters a to f. Case does not matter: #FF5733 and #ff5733 are the same color.
| Hex digit | 0–9 | a | b | c | d | e | f |
|---|---|---|---|---|---|---|---|
| Decimal value | 0–9 | 10 | 11 | 12 | 13 | 14 | 15 |
With 256 levels for each of three channels, a six-digit code can name 256 × 256 × 256 = 16,777,216 colors. The notation is defined in CSS Color Module Level 4, the W3C specification for colors on the web, which also defines rgb(), hsl(), oklch() and the other formats this converter outputs. All of these name colors in the sRGB space that ordinary screens use, except oklch(), lab() and color(), which can also reach the wider gamut of newer displays.
How to convert hex to RGB by hand
The converter above does this instantly, but the math is short enough to do in your head once you have seen it:
- Remove the # and, for a three-digit code, write each digit twice.
- Split the six digits into three pairs: red, green and blue.
- Convert each pair from base 16 to base 10: 16 times the first digit, plus the second digit.
- Write the three numbers as rgb(red, green, blue).
Here is #ff5733, a warm orange-red, worked through:
| Pair | Channel | Math | Decimal |
|---|---|---|---|
ff | Red | 15 × 16 + 15 | 255 |
57 | Green | 5 × 16 + 7 | 87 |
33 | Blue | 3 × 16 + 3 | 51 |
So #ff5733 is rgb(255, 87, 51), or rgb(255 87 51) in the newer CSS syntax without commas. A few landmarks make quick estimates easy: 00 is 0, 80 is 128 (just over half), cc is 204 and ff is 255.
Three-digit, four-digit and eight-digit hex codes
CSS accepts hex codes with 3, 4, 6 or 8 digits. The short forms double every digit, so #f53 means #ff5533, which is rgb(255, 85, 51), not the same as #ff5733. Only 4,096 colors have a three-digit form, which is why most design tools give you six digits.
The extra pair in an eight-digit code is alpha, the opacity: 00 is fully transparent and ff fully opaque. To get the CSS alpha value, divide the pair by 255. In #ff573380 the alpha pair 80 is 128, and 128 ÷ 255 ≈ 0.502, so the color is about rgba(255, 87, 51, 0.5). The converter shows 0.5 because it uses the fewest decimals that turn back into the same alpha byte. A four-digit code is the shorthand version: #f538 is #ff553388, and 88 is 136, so its alpha is 136 ÷ 255 ≈ 0.533.
Going the other way, multiply the opacity by 255, round, and write the result in hex. These are the pairs you will use most:
| Opacity | 100% | 90% | 80% | 75% | 70% | 60% | 50% | 40% | 30% | 25% | 20% | 10% | 0% |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex pair | ff | e6 | cc | bf | b3 | 99 | 80 | 66 | 4d | 40 | 33 | 1a | 00 |
How to convert RGB to hex
Divide each channel by 16. The whole-number result is the first hex digit and the remainder is the second, with 10 to 15 written as a to f. For rgb(255, 87, 51): 255 ÷ 16 = 15 remainder 15, which is ff; 87 ÷ 16 = 5 remainder 7, which is 57; and 51 ÷ 16 = 3 remainder 3, which is 33. Put them together and you have #ff5733.
Values under 16 need a leading zero, or the code comes out too short: 10 is 0a, so rgb(10, 0, 255) is #0a00ff. For RGBA to hex, also convert the alpha: multiply it by 255 and round, so 0.5 × 255 = 127.5 rounds to 128, which is 80, giving #ff573380. In the converter, type the numbers into the Red, Green, Blue and Alpha boxes, or drag the sliders, and the hex code updates as you go.
HEX, RGB, HSL or OKLCH: which CSS format to use
Every format below describes the same color, #ff5733. Browsers treat them identically, so the choice is about what is easiest to read and edit.
| Format | #ff5733 written this way | Good for |
|---|---|---|
| HEX | #ff5733 | The shortest form. Design handoff, tokens, anywhere a color is pasted. |
| rgb(), modern syntax | rgb(255 87 51) | Readable channels; alpha goes after a slash, as in rgb(255 87 51 / 50%). |
| rgb() and rgba(), comma syntax | rgb(255, 87, 51) | Older stylesheets and libraries. Same color. |
| hsl() | hsl(10.6 100% 60%) | Hand-tuning saturation and lightness. |
| hwb() | hwb(10.6 20% 0%) | Mixing a hue with white and black, like paint. |
| oklch() | oklch(0.68 0.21 33.7) | Palettes, tints and shades with even lightness steps, and wide-gamut colors. |
| lab() | lab(61.03 63.55 55.96) | Color science and print workflows that already use CIE Lab. |
| color(display-p3 …) | color(display-p3 0.926 0.389 0.258) | Writing colors for wide-gamut screens directly. |
CSS Color Level 4 made the commas optional and put alpha after a slash, so rgb(255 87 51 / 50%) and rgba(255, 87, 51, 0.5) are the same color. The two function names are now aliases: rgb() takes alpha too, and rgba() accepts the new syntax. Every current browser supports both, so use whichever style your codebase already has.
HSL is handy for quick tweaks, but its lightness is not what your eye sees. Pure blue, hsl(240 100% 50%), and pure yellow, hsl(60 100% 50%), both have 50% lightness, yet yellow looks far brighter. OKLCH fixes that: the same two colors have lightness 0.452 and 0.968. That is why OKLCH is the better choice for building palettes, tints and shades. oklch(), lab() and color() have worked in all major browsers since May 2023.
The formulas behind each conversion
Every formula here comes from CSS Color Module Level 4 (HSL, HWB, Lab and OKLCH) or is the standard textbook definition (HSV and CMYK). Channels are scaled to 0–1 first, so for #ff5733 R = 1, G = 87 ÷ 255 ≈ 0.341 and B = 51 ÷ 255 = 0.2.
HSL
max = largest channel, min = smallest channel
L = (max + min) / 2
S = (max − L) / min(L, 1 − L) 0 when max = min
H = 60 × (G − B) / (max − min) when red is largest
(add 360° if that is negative; when green is largest use
(B − R) and add 120°, when blue is use (R − G) and add 240°)
For #ff5733: L = (1 + 0.2) ÷ 2 = 0.6, S = 0.4 ÷ 0.4 = 100%, and H = 60 × 36 ÷ 204 ≈ 10.6°, giving hsl(10.6 100% 60%). Rounding the hue to 11° would give back #ff5833, a slightly different color, which is why the converter keeps one decimal here.
HSV (also called HSB)
HSV uses the same hue as HSL, with V = max and S = (max − min) ÷ max. For #ff5733 that is V = 100% and S = 0.8 ÷ 1 = 80%. HSV is common in color pickers, but CSS has no hsv() function.
CMYK (a naive approximation)
K = 1 − max(R, G, B)
C = (1 − R − K) / (1 − K)
M = (1 − G − K) / (1 − K)
Y = (1 − B − K) / (1 − K) all 0 when K = 1 (black)
For #ff5733 this gives C 0%, M 66%, Y 80%, K 0%.
Not for print. This formula knows nothing about inks, paper or presses, so it cannot tell you what a printer will produce. Real print conversion uses an ICC color profile for the specific printing condition. Treat the CMYK numbers as a rough starting point and ask your printer for proofs.
OKLCH, OKLab, Lab and LCH
These start by undoing the sRGB gamma curve: a channel up to 0.04045 is divided by 12.92, anything larger becomes ((v + 0.055) ÷ 1.055)2.4. The linear values are multiplied by a 3 × 3 matrix to get CIE XYZ. For OKLab, a second matrix turns XYZ into cone responses, each is cube-rooted, and a third matrix gives L, a and b. OKLCH is the same color in polar form: C = √(a² + b²) and h = atan2(b, a). CIE Lab works the same way with a D50 white point, so it adapts XYZ from D65 to D50 first. The converter uses the exact matrices published in the CSS specification.
When you type an oklch(), lab() or display-p3 color that ordinary screens cannot show, the converter maps it into sRGB the way CSS does: it lowers the chroma at the same lightness and hue until the color fits, and tells you it did.
Contrast ratio and WCAG 2.2
WCAG, the Web Content Accessibility Guidelines, measures contrast between two colors from their relative luminance:
each channel c (0–1): c ≤ 0.04045 → c / 12.92
otherwise → ((c + 0.055) / 1.055)^2.4
L = 0.2126 R + 0.7152 G + 0.0722 B
contrast ratio = (L1 + 0.05) / (L2 + 0.05) L1 = the lighter color
For #ff5733 the linear channels are 1, 0.0953 and 0.0331, so L = 0.2126 + 0.7152 × 0.0953 + 0.0722 × 0.0331 ≈ 0.2832. Against white (L = 1) the ratio is 1.05 ÷ 0.3332 ≈ 3.15:1; against black (L = 0) it is 0.3332 ÷ 0.05 ≈ 6.66:1. So white text on this orange passes only for large text, and black text passes level AA but not AAA.
| What | Level AA | Level AAA |
|---|---|---|
| Normal text | 4.5:1 (SC 1.4.3) | 7:1 (SC 1.4.6) |
| Large text: 24 px, or about 18.5 px bold | 3:1 (SC 1.4.3) | 4.5:1 (SC 1.4.6) |
| Icons, form controls, focus rings, chart lines | 3:1 (SC 1.4.11) | No higher level |
The thresholds are hard limits. WCAG's own guidance says not to round, so a ratio of 4.499:1 fails 4.5:1. This tool compares the unrounded value and shows the ratio cut off at two decimals rather than rounded, so a fail never displays as 4.50:1. That is why #777777 on white shows 4.47:1 (the exact value is 4.478:1, a fail), while #767676, at 4.54:1, is the lightest gray that passes AA on white.
WCAG does not say how to handle transparency. The Contrast tab blends a translucent text color over the background, and a translucent background over white, which is WCAG's default background, before it measures.
Common colors in HEX, RGB and HSL
The 16 basic CSS color names, plus orange and rebeccapurple, with their values and their contrast ratio against white. CSS defines 148 color names in all, and the converter tells you when your color matches one, or which named color is closest.
| Name | HEX | RGB | HSL | On white |
|---|---|---|---|---|
| black | #000000 | rgb(0, 0, 0) | hsl(0 0% 0%) | 21.00:1 |
| white | #ffffff | rgb(255, 255, 255) | hsl(0 0% 100%) | 1.00:1 |
| red | #ff0000 | rgb(255, 0, 0) | hsl(0 100% 50%) | 3.99:1 |
| lime | #00ff00 | rgb(0, 255, 0) | hsl(120 100% 50%) | 1.37:1 |
| blue | #0000ff | rgb(0, 0, 255) | hsl(240 100% 50%) | 8.59:1 |
| yellow | #ffff00 | rgb(255, 255, 0) | hsl(60 100% 50%) | 1.07:1 |
| aqua | #00ffff | rgb(0, 255, 255) | hsl(180 100% 50%) | 1.25:1 |
| fuchsia | #ff00ff | rgb(255, 0, 255) | hsl(300 100% 50%) | 3.13:1 |
| silver | #c0c0c0 | rgb(192, 192, 192) | hsl(0 0% 75.3%) | 1.81:1 |
| gray | #808080 | rgb(128, 128, 128) | hsl(0 0% 50%) | 3.94:1 |
| maroon | #800000 | rgb(128, 0, 0) | hsl(0 100% 25%) | 10.94:1 |
| olive | #808000 | rgb(128, 128, 0) | hsl(60 100% 25%) | 4.19:1 |
| green | #008000 | rgb(0, 128, 0) | hsl(120 100% 25%) | 5.13:1 |
| purple | #800080 | rgb(128, 0, 128) | hsl(300 100% 25%) | 9.41:1 |
| teal | #008080 | rgb(0, 128, 128) | hsl(180 100% 25%) | 4.77:1 |
| navy | #000080 | rgb(0, 0, 128) | hsl(240 100% 25%) | 16.00:1 |
| orange | #ffa500 | rgb(255, 165, 0) | hsl(38.8 100% 50%) | 1.97:1 |
| rebeccapurple | #663399 | rgb(102, 51, 153) | hsl(270 50% 40%) | 8.40:1 |
The names are a historical mix: 16 came from early computer palettes and most of the rest from an old Unix color list. They are not evenly spread or even consistent, and darkgray is lighter than gray. Use them for quick experiments, and hex or OKLCH values in a design system.
Convert hex to RGB in code
If you need the conversion in your own project, it is a few lines in any language. These handle the 3, 4, 6 and 8-digit forms.
function hexToRgb(hex) {
let h = hex.replace(/^#/, '')
if (h.length === 3 || h.length === 4) h = [...h].map((d) => d + d).join('')
const n = parseInt(h, 16)
if (h.length === 8) return { r: n >>> 24, g: (n >>> 16) & 255, b: (n >>> 8) & 255, a: (n & 255) / 255 }
return { r: n >> 16, g: (n >> 8) & 255, b: n & 255, a: 1 }
}
hexToRgb('#ff5733') // { r: 255, g: 87, b: 51, a: 1 }
const rgbToHex = (r, g, b) => '#' + [r, g, b].map((v) => v.toString(16).padStart(2, '0')).join('')
rgbToHex(255, 87, 51) // '#ff5733'
def hex_to_rgb(h):
h = h.lstrip('#')
if len(h) in (3, 4):
h = ''.join(c * 2 for c in h)
return tuple(int(h[i:i + 2], 16) for i in range(0, len(h), 2))
hex_to_rgb('#ff5733') # (255, 87, 51)
tuple(bytes.fromhex('ff5733')) # (255, 87, 51)
'#{:02x}{:02x}{:02x}'.format(255, 87, 51) # '#ff5733'
.button {
/* the same color, three ways */
background: #ff5733;
background: rgb(255 87 51);
background: oklch(0.68 0.21 33.7);
/* about 50% opacity: an 8-digit hex, or a relative color made from the hex */
background: #ff573380;
background: rgb(from #ff5733 r g b / 50%);
}
The relative color syntax on the last line takes the channels of an existing color and changes only the alpha. It has worked in all major browsers since September 2024; on older browsers, use the eight-digit hex.
How this converter works, and why nothing leaves your device
Every conversion on this page is done by JavaScript running in your own browser tab, with no library and no server. The color math follows the sample code published with CSS Color Module Level 4, and the contrast math follows the WCAG 2.2 definitions.
- Nothing is uploaded. The page's Content-Security-Policy only allows connections back to this site, and the converter makes none. Open your browser's Network panel and convert a few hundred colors: no request appears.
- Works offline. Once the page has loaded you can disconnect and keep converting.
- No account, no cookies. Your last color, tab and options are kept in your browser's local storage, on your device only.
- Pick from screen uses the browser's built-in eyedropper where one exists (Chromium-based browsers today). The browser asks before it reads a pixel, and only that color reaches the page.
Frequently asked questions
How do I convert HEX to RGB?
Split the six digits into three pairs for red, green and blue, and convert each pair from base 16: 16 times the first digit plus the second. #ff5733 gives ff = 255, 57 = 87 and 33 = 51, so rgb(255, 87, 51). For a three-digit code such as #f53, double each digit first, to #ff5533. Paste any code into the converter above to skip the math.
How do I convert RGB to HEX?
Divide each channel by 16: the whole number is the first hex digit and the remainder is the second, with 10 to 15 written as a to f. 87 ÷ 16 = 5 remainder 7, so 87 is 57, and rgb(255, 87, 51) becomes #ff5733. A channel under 16 needs a leading zero: 10 is 0a. Type the numbers into the Red, Green and Blue boxes above to get the code.
What is an 8-digit hex color?
It is #RRGGBBAA: the usual six digits plus an alpha (opacity) pair, where 00 is fully transparent and ff is fully opaque. Divide the alpha pair by 255 for the CSS value: in #ff573380 the pair 80 is 128, and 128 ÷ 255 ≈ 0.502, so it is about rgba(255, 87, 51, 0.5). The four-digit form, such as #f538, is its shorthand. All current browsers support both.
What is the difference between RGB and RGBA?
RGBA adds an alpha channel for transparency, from 0 (invisible) to 1 (solid). In current CSS the two functions are the same: rgba() is an alias of rgb(), and both accept alpha, either as rgba(255, 87, 51, 0.5) or in the newer form rgb(255 87 51 / 50%). Use whichever style your codebase already uses.
How many colors can a hex code represent?
A six-digit code has 16,777,216 combinations, which is 256 levels each of red, green and blue. The three-digit shorthand covers only 4,096 of them. An eight-digit code adds 256 levels of alpha, for 4,294,967,296 combinations in all.
Is HEX or RGB better for CSS?
Neither is more accurate: #ff5733 and rgb(255 87 51) are the same sRGB color, and browsers treat them identically. HEX is shorter and common in design files, while rgb() is easier to read and to adjust by hand. For building palettes or changing lightness evenly, oklch() is the better choice, because its lightness matches what people see.
Is the CMYK value accurate for printing?
No. It uses the simple formula K = 1 − max(R, G, B), with no printer profile, so it ignores the inks, paper and press that decide real print color. Use it as a rough starting point, and get print values from your printer’s color profile or a printed proof.
What contrast ratio does text need?
WCAG 2.2 level AA asks for at least 4.5:1 for normal text, and 3:1 for large text (24 px, or about 18.5 px bold) and for icons and form controls. Level AAA asks for 7:1 and 4.5:1. The Contrast tab checks any pair and never rounds up, so 4.499:1 counts as a fail.
What is OKLCH, and why are its numbers so different from HSL?
OKLCH describes a color by perceived lightness (0 to 1), chroma (how colorful it is) and hue angle, in the Oklab color space that CSS Color Level 4 adopted. HSL lightness is just the average of the largest and smallest channel, so pure blue and pure yellow both get 50%, while OKLCH gives them 0.452 and 0.968. That makes OKLCH better for palettes and for adjusting lightness predictably.
Is this hex to RGB converter free and private?
Yes. There is no sign-up, and every conversion runs in your browser: the page has no code that sends colors anywhere, and its security policy blocks connections to other sites. It remembers your last color and options in your browser’s local storage, on your device only.
Sources
- W3C, CSS Color Module Level 4, Candidate Recommendation Draft (9 October 2026): §5.1 rgb() and rgba(), §5.2 hex notation, §6.1 named colors, §7 HSL, §8 HWB, §9 Lab, LCH, Oklab and OkLCh, §14.2 gamut mapping, and the sample code for color conversions.
- W3C, Web Content Accessibility Guidelines (WCAG) 2.2, Recommendation (12 December 2024): the definitions of relative luminance, contrast ratio and large-scale text, and Success Criteria 1.4.3, 1.4.6 and 1.4.11.
- W3C WAI, Understanding Success Criterion 1.4.3: Contrast (Minimum), on thresholds without rounding and on point-to-pixel sizes.
- W3C WAI, Understanding Success Criterion 1.4.6: Contrast (Enhanced).
- W3C WAI, Understanding Success Criterion 1.4.11: Non-text Contrast.
- IEC 61966-2-1:1999, Multimedia systems and equipment: Colour measurement and management, Part 2-1: Default RGB colour space, sRGB, the source of the sRGB transfer function.
- Björn Ottosson, A perceptual color space for image processing (2020), the original description of Oklab.
- MDN Web Docs: <hex-color>, rgb(), oklch(), <named-color> and EyeDropper.
- Web Platform Status: Oklab and OkLCh, for browser support dates (Baseline since May 2023, widely available since November 2025).