Skip to content

HEX to RGB Converter (and HSL, HWB, HSV, CMYK)

Convert colour codes between HEX, RGB(A), HSL, HWB, HSV and CMYK, with WCAG luminance and contrast against white and black.

Your details

3, 4, 6 or 8 digits, with or without #. The last two digits of #rrggbbaa are the opacity.

Results update as you type

RGB

rgb(42, 157, 143)

HEX
#2A9D8F
HSL
hsl(172.7, 57.8%, 39%)
HWB
hwb(172.7 16.5% 38.4%)
HSV / HSB
hsv(172.7°, 73.2%, 61.6%)
CMYK (naive formula)
cmyk(73.2%, 0%, 8.9%, 38.4%)
WCAG relative luminance
0.2659
Contrast with white
3.32 :1
Contrast with black
6.31 :1
Best text colour on it
Black text reads better (6.31:1)

Against white: 3.32:1 → passes AA for large text and UI components only (3:1)

Against black: 6.31:1 → passes AA (4.5:1) for normal text

CMYK here is the simple device-independent formula. Real print values depend on the press, paper and ICC profile, so ask your printer for a profile-based conversion.

Step-by-step solution
  1. 2A = 2 × 16 + A = 42
  2. 9D = 9 × 16 + D = 157
  3. 8F = 8 × 16 + F = 143
  4. The 0–255 channels are divided by 255 before the HSL, HSV and CMYK formulas.
  5. Relative luminance L = 0.2126 R + 0.7152 G + 0.0722 B (linearised channels) = 0.2659

Designers, developers and printers all describe colour differently. CSS files use hex codes like #2A9D8F, image editors show RGB sliders, colour pickers think in HSL or HSV, and print shops ask for CMYK. This converter takes a colour in any of those notations and gives you all the others at once, ready to paste into a stylesheet or a design tool.

It also answers the question that usually comes next: can I put text on this colour? The calculator computes the relative luminance defined by WCAG 2.2 and the contrast ratio against white and against black, tells you which of the two reads better, and says which accessibility level (AA or AAA) that contrast reaches.

The conversions follow the algorithms published in the W3C CSS Color Module Level 4, so the HSL and HWB values match what browsers compute. Transparent colours are supported: 4- and 8-digit hex codes (#rgba, #rrggbbaa) and an opacity field for RGB, HSL, HWB and HSV.

How to use this calculator

  1. Choose the notation you already have in Convert from: HEX, RGB, HSL, HWB, HSV or CMYK.
  2. Type the code or the channel values. Hex codes work with or without "#", in 3, 4, 6 or 8 digits, in upper or lower case.
  3. For RGB, HSL, HWB and HSV you can also set an opacity; the hex result then gets two extra digits and RGB becomes rgba().
  4. Read every other notation in the results, plus the WCAG luminance and the two contrast ratios.
  5. Open the steps to see how each hex pair becomes a decimal channel.

The formula

Hex to RGB. Each pair of hex digits is one byte: RR=16×d1+d2\text{RR} = 16 \times d_1 + d_2, where A–F stand for 10–15. Short codes double each digit (#3a7 = #33aa77).

RGB to HSL (CSS Color 4). With r,g,br, g, b scaled to 0–1, M=max⁡M = \max, m=min⁡m = \min:

L=M+m2S=M−Lmin⁡(L, 1−L)H=60∘×{g−bM−m mod 6M=rb−rM−m+2M=gr−gM−m+4M=bL = \frac{M + m}{2} \qquad S = \frac{M - L}{\min(L,\ 1 - L)} \qquad H = 60^\circ \times \begin{cases} \frac{g-b}{M-m} \bmod 6 & M = r \\ \frac{b-r}{M-m} + 2 & M = g \\ \frac{r-g}{M-m} + 4 & M = b \end{cases}

HSV and HWB share the same hue: V=MV = M, SV=(M−m)/MS_V = (M - m)/M; whiteness W=mW = m, blackness B=1−MB = 1 - M.

Naive CMYK: K=1−MK = 1 - M, C=1−r−K1−KC = \frac{1 - r - K}{1 - K} (same for M and Y).

WCAG relative luminance: each channel is linearised, clin=c/12.92c_{lin} = c/12.92 if c≤0.04045c \le 0.04045, otherwise (c+0.0551.055)2.4\left(\frac{c + 0.055}{1.055}\right)^{2.4}, then

L=0.2126 R+0.7152 G+0.0722 Bcontrast=L1+0.05L2+0.05L = 0.2126\,R + 0.7152\,G + 0.0722\,B \qquad \text{contrast} = \frac{L_1 + 0.05}{L_2 + 0.05}

Worked example

Take the teal #2A9D8F.

  • Hex pairs: 2A = 2 × 16 + 10 = 42, 9D = 9 × 16 + 13 = 157, 8F = 8 × 16 + 15 = 143, so rgb(42, 157, 143).
  • Scaled: r = 0.165, g = 0.616, b = 0.561. Max is g, min is r, so L=39%L = 39\%, S=57.8%S = 57.8\% and H=172.7∘H = 172.7^\circ: hsl(172.7, 57.8%, 39%).
  • The same colour is hwb(172.7 16.5% 38.4%) in HWB, hsv(172.7°, 73.2%, 61.6%) in HSV and cmyk(73.2%, 0%, 8.9%, 38.4%) with the naive CMYK formula.
  • Its relative luminance is 0.2659, which gives a contrast of 3.32:1 with white and 6.31:1 with black.

Black text is therefore the better choice on this teal and meets AA for normal text, while white text only reaches the 3:1 large-text level. A darker navy such as #1D3557 reaches 12.35:1 against white, enough for white body text at AAA.

Which notation should you use?

Notation Typical use Range
HEX CSS, HTML, brand guidelines, design hand-off 00–FF per channel
RGB / RGBA CSS, image editors, code (0–255 integers) 0–255, alpha 0–1
HSL CSS themes: change lightness to build tints and shades H 0–360°, S and L 0–100 %
HWB CSS Color 4: mix a pure hue with white and black H 0–360°, W and B 0–100 %
HSV / HSB Colour pickers in Photoshop, Figma and most apps H 0–360°, S and V 0–100 %
CMYK Printing (inks): needs a press profile for accuracy 0–100 % per ink

HSL and HSV look similar but are not interchangeable: 100 % lightness in HSL is always white, whereas 100 % value in HSV is the brightest version of the hue.

Why the CMYK result is only a starting point

Screens add light (RGB) while ink subtracts it (CMYK), and every press, paper and ink set has its own gamut. The formula used here is the textbook, device-independent conversion: it ignores dot gain and ink limits and always uses as much black as possible. Use it to get a rough idea or to fill a quick mock-up, but for brand colours, ask the printer to convert with the ICC profile of the job (for example FOGRA39 or GRACoL), or pick a Pantone reference.

Frequently asked questions

How do I convert a hex code to RGB by hand?

Split the six digits into three pairs and convert each pair from base 16: the first digit times 16 plus the second, with A = 10 up to F = 15. For example FF = 15 × 16 + 15 = 255.

What do 8-digit hex codes mean?

The last two digits are the alpha channel: 00 is fully transparent and FF fully opaque. #2A9D8F80 is the same teal at 128/255 ≈ 50 % opacity, which CSS writes as rgba(42, 157, 143, 0.502).

What contrast ratio do I need for accessible text?

WCAG 2.2 asks for at least 4.5:1 for normal text and 3:1 for large text (about 24 px, or 18.66 px bold) at level AA, and 7:1 and 4.5:1 at level AAA. Icons and form borders need 3:1. The calculator truncates the ratio instead of rounding it up, so 4.499 never shows as a pass.

Why does my converted colour differ by one unit from another tool?

HSL, HSV and CMYK values are usually rounded to whole numbers, and converting a rounded value back to RGB can land one step away. This tool keeps the exact channel values internally and rounds only the displayed result.

Is RGB to CMYK conversion accurate for printing?

Not exactly. The simple formula gives a reasonable approximation, but real printing depends on the press profile. Use the result as a guide and confirm with a proof or a profile-based conversion.

Sources

Last updated:

More Conversion calculators

People also use