Computing, IT, Networking & Security Digital Media, Display & Color sRGB (IEC 61966-2-1) and CSS Color Module Level 4

HEX, RGB, HSL & CMYK Color Converter Calculator

Give this converter a color in HEX, RGB or HSL and it returns every other notation your tools ask for: six- and eight-digit hex, CSS rgb() and rgba(), hsl() and hsla(), HSV/HSB for design apps, a naive CMYK separation, and the sRGB relative luminance that accessibility checks are built on. Each intermediate value is shown, so you can follow the max/min arithmetic that turns three channels into a hue angle rather than trusting a black box. Shorthand hex (#0f0) and alpha channels are handled.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Notation you are starting fromPick the notation you have in front of you; the other fields are hidden until you need them.HEX
Hex colorThree, four, six or eight hex digits, with or without a leading hash. Four and eight digits carry alpha.#3B82F6
RedRed channel on the 0–255 scale used by CSS and every image editor.59
GreenGreen channel, 0–255.130
BlueBlue channel, 0–255.246
HuePosition on the colour wheel: 0° red, 120° green, 240° blue.217 °
Saturation0% is grey, 100% is fully saturated at the current lightness.91 %
Lightness0% is black, 50% is the pure hue, 100% is white.60 %
Alpha (opacity)1 is fully opaque. An eight-digit hex input overrides whatever you type here.1

It returns

  • HEX — Eight digits when alpha is below 1, six digits otherwise.
  • RGB / RGBA (CSS-ready)
  • HSL / HSLA (CSS-ready)
  • HSV / HSB (H, S, V)
  • CMYK (naive conversion)
  • sRGB relative luminance — 0 for black, 1 for white. This is the quantity WCAG contrast ratios are built from.

The formula

S=maxmin1|2L1|
Y=0.2126Rlin+0.7152Glin+0.0722Blin
C=1RK1K

In plain text: L = (max + min) / 2 ; S = (max − min) / (1 − |2L − 1|) ; H = 60° × sector(max)

  • R, G, BChannel values normalised to 0–1 by dividing the 0–255 value by 255 (fraction)
  • max, minLargest and smallest of the three normalised channels (fraction)
  • LHSL lightness (fraction)
  • SHSL saturation (fraction)
  • HHue angle on the colour wheel (degrees)
  • KCMYK key (black), 1 − max (fraction)
  • YsRGB relative luminance, 0 for black and 1 for white (fraction)

Hue is undefined when max equals min (a neutral grey) and is reported as 0° by convention. The luminance line linearises each channel first: c ≤ 0.03928 ? c/12.92 : ((c+0.055)/1.055)^2.4.

Updated Category Digital Media, Display & Color Verified against published test cases Reading time 12 min

One colour, five notations, and why they exist

Every notation on this page describes the same physical thing: how much red, green and blue light a display emits. They differ only in how they index that space, and each was designed for a different job.

Hex is RGB written in base 16, two digits per channel, because a byte fits in exactly two hex digits. #3B82F6 means red 0x3B = 59, green 0x82 = 130, blue 0xF6 = 246. Shorthand triples the way you would expect but not the way most people guess: #0f0 expands to #00FF00 by repeating each digit, so f becomes ff = 255, not f0 = 240.

RGB is the same three numbers on a 0–255 scale, which is what image editors and CSS both accept directly.

HSL re-indexes the cube as a cylinder: a hue angle, a saturation, and a lightness. It exists because “make this 10% darker but the same colour” is one subtraction in HSL and three coupled changes in RGB. That is why design systems store palettes as HSL ramps.

HSV (also called HSB) uses the same hue but replaces lightness with value, the largest channel. In HSV, full saturation at full value gives the pure hue; in HSL the pure hue sits at 50% lightness. The two saturations are not interchangeable, which is the single most common source of “the colour picker disagrees with my code”.

CMYK is subtractive, meant for ink on paper rather than light from a panel. The conversion here is the naive one — pull out the common black, express what remains as ink percentages — and it is genuinely useful for rough proofs and nothing else.

Underneath all of them is the sRGB colour space defined in IEC 61966-2-1, which fixes what “255 red” actually means in terms of light. The relative luminance figure is where that definition becomes visible, and it is the number the CSS Color Module and the accessibility guidelines both build on.

The arithmetic, step by step

Start by normalising: divide each channel by 255 so R, G and B all live in 0–1. Then take max and min of the three and their difference Δ. Almost everything else falls out of those three numbers.

Lightness is the midpoint of the extremes: L = (max + min) / 2. A colour with channels 59, 130, 246 has max 0.9647 and min 0.2314, so L = 0.5980, or 59.8%.

Saturation asks how wide the spread is relative to the widest spread possible at that lightness. The widest possible Δ is 1 − |2L − 1|, which peaks at 1 when L = 0.5 and shrinks to 0 at pure black or pure white. So S = Δ / (1 − |2L − 1|). When Δ is zero the colour is grey and S is defined as 0 — the division is skipped, not attempted, because the denominator also collapses at the extremes.

Hue is a sector formula keyed on which channel is largest. If red is the maximum, H = 60 × ((G − B) / Δ), wrapped into 0–360 by adding 360 if it comes out negative. If green is the maximum, H = 60 × ((B − R) / Δ + 2). If blue is the maximum, H = 60 × ((R − G) / Δ + 4). The +2 and +4 rotate you onto the right 120° arc of the wheel; the inner fraction places you inside it.

HSV shares that hue exactly, but sets V = max and S_hsv = Δ / max. Because the denominator changed, the two saturation numbers differ for the same colour — 59/130/246 is 91% saturated in HSL and 76% in HSV.

CMYK takes the black out first: K = 1 − max. Whatever is left is scaled back up, C = (1 − R − K) / (1 − K) and likewise for M and Y. Pure black has K = 1 and the division is skipped, since there is no remaining colour to express.

Relative luminance is the one calculation that is not a re-indexing. Screen values are gamma-encoded, so you must linearise each channel before weighting it: c_lin = c/12.92 when c ≤ 0.03928, otherwise ((c + 0.055)/1.055)^2.4. Then Y = 0.2126·R + 0.7152·G + 0.0722·B. Those weights reflect how sensitive human vision is to each primary, which is why green carries over seven times the weight of blue.

Worked example: converting #3B82F6 by hand

Take the hex value #3B82F6 and derive every other notation.

  1. Hex to decimal. 0x3B = 3×16 + 11 = 59. 0x82 = 8×16 + 2 = 130. 0xF6 = 15×16 + 6 = 246. So the colour is rgb(59, 130, 246).
  2. Normalise. R = 59/255 = 0.2314, G = 130/255 = 0.5098, B = 246/255 = 0.9647.
  3. Max, min and Δ. max = 0.9647 (blue), min = 0.2314 (red), Δ = 0.7333.
  4. Lightness. L = (0.9647 + 0.2314) / 2 = 0.5980 → 60%.
  5. Saturation (HSL). 1 − |2(0.5980) − 1| = 1 − 0.1961 = 0.8039. S = 0.7333 / 0.8039 = 0.9122 → 91%.
  6. Hue. Blue is the maximum, so H = 60 × ((R − G)/Δ + 4) = 60 × ((0.2314 − 0.5098)/0.7333 + 4) = 60 × (−0.3797 + 4) = 60 × 3.6203 = 217.2° → 217°. The CSS value is hsl(217, 91%, 60%).
  7. HSV. V = max = 0.9647 → 96%. S_hsv = 0.7333 / 0.9647 = 0.7602 → 76%. So 217°, 76%, 96% — note the saturation differs from HSL's 91%.
  8. CMYK. K = 1 − 0.9647 = 0.0353 → 4%. C = (1 − 0.2314 − 0.0353) / 0.9647 = 0.9333 / 0.9647 = 0.9675 → 97%. M = (1 − 0.5098 − 0.0353) / 0.9647 = 0.4549 / 0.9647 = 0.4715 → 47%. Y = (1 − 0.9647 − 0.0353) / 0.9647 = 0%. So 97%, 47%, 0%, 4%.
  9. Relative luminance. Linearise: R_lin = ((0.2314 + 0.055)/1.055)^2.4 = 0.0437; G_lin = ((0.5098 + 0.055)/1.055)^2.4 = 0.2232; B_lin = ((0.9647 + 0.055)/1.055)^2.4 = 0.9216. Then Y = 0.2126×0.0437 + 0.7152×0.2232 + 0.0722×0.9216 = 0.0093 + 0.1596 + 0.0665 = 0.2355.

That last figure is the one you carry into an accessibility check: against white (Y = 1.0) the contrast ratio is (1.0 + 0.05) / (0.2355 + 0.05) = 3.68:1, which clears the 3:1 bar for large text but not the 4.5:1 bar for body copy.

How to read the numbers you get back

Use hue to judge whether two colours belong to the same family. Hues within roughly 15° of each other read as the same colour at a glance; a 180° difference is the complement. Because hue is an angle, 5° and 355° are neighbours, not opposites — a fact that breaks naive “sort the palette by hue” code.

Use lightness rather than value when you are building a ramp. An HSL ramp at fixed hue and saturation with lightness stepping 90%, 80%, 70% and so on gives predictable tints and shades. An HSV ramp does not, because dropping value darkens while dropping saturation washes out, and the two axes are not symmetric about a midpoint the way HSL's lightness is.

Treat the luminance figure as the only perceptually meaningful brightness number on the page. HSL lightness of 50% does not mean “half as bright as white”: hsl(60, 100%, 50%) (yellow) has a relative luminance of 0.9278 while hsl(240, 100%, 50%) (blue) has 0.0722, and both claim 50% lightness. Yellow really is over twelve times as luminous as blue at the same nominal lightness. If you are choosing text and background colours, work from luminance, and let the WCAG contrast ratio calculator apply the thresholds.

Treat the CMYK figures as indicative only. Real separations depend on the paper, the press, the total ink limit and an ICC profile; the naive formula knows none of that. It is fine for “roughly how much cyan is in this”, and wrong for anything that will be printed and paid for.

Reference colours in every notation

Each row is the same colour expressed five ways, with the sRGB relative luminance in the final column.
ColourHEXRGBHSLCMYKLuminance
White#FFFFFF255, 255, 2550°, 0%, 100%0, 0, 0, 01.0000
Black#0000000, 0, 00°, 0%, 0%0, 0, 0, 1000.0000
Mid grey#808080128, 128, 1280°, 0%, 50%0, 0, 0, 500.2159
Red#FF0000255, 0, 00°, 100%, 50%0, 100, 100, 00.2126
Green#00FF000, 255, 0120°, 100%, 50%100, 0, 100, 00.7152
Blue#0000FF0, 0, 255240°, 100%, 50%100, 100, 0, 00.0722
Yellow#FFFF00255, 255, 060°, 100%, 50%0, 0, 100, 00.9278
Cyan#00FFFF0, 255, 255180°, 100%, 50%100, 0, 0, 00.7874
Magenta#FF00FF255, 0, 255300°, 100%, 50%0, 100, 0, 00.2848

Luminance values are the weighted sums of the linearised channels: a primary at full strength contributes exactly its coefficient, so yellow is 0.2126 + 0.7152 = 0.9278 and magenta is 0.2126 + 0.0722 = 0.2848.

Traps in colour conversion

  • Expanding shorthand hex by padding with zeros. #0f0 is #00FF00, not #00F000. Each digit is repeated, which keeps 0 at 0 and f at 255 so the endpoints of the scale stay put.
  • Swapping HSL and HSV saturation. They use different denominators and disagree for every colour that is not fully saturated or fully grey. Design apps often show HSB while CSS speaks HSL.
  • Treating HSL lightness as brightness. Yellow and blue both sit at 50% lightness and differ by a factor of nearly thirteen in actual luminance.
  • Rounding HSL and expecting to get the same hex back. Only 16.7 million colours exist in 8-bit RGB, and the HSL cylinder is continuous, so a rounded hsl() string usually maps to a neighbouring hex value.
  • Sending naive CMYK to a printer. Without an ICC profile the numbers ignore ink limits, paper white and dot gain. Ask the printer for their profile and separate through it.
  • Assuming hex means sRGB everywhere. On a wide-gamut display, #FF0000 is whatever the display's red primary is unless the content is colour-managed. IEC 61966-2-1 defines sRGB specifically so that assumption is safe on standard hardware.
  • Forgetting alpha is not a colour. Compositing rgba(0,0,0,0.5) over white gives a different final luminance than #808080, because alpha blending happens in the encoded space that browsers use, not in your source value.

Colour conversion is the first step in most front-end colour work and the last step in most design-handoff work. Once you have a hex value you trust, the accessibility question follows immediately, and the contrast ratio calculator takes two of these values and reports pass or fail against the WCAG thresholds. If the colour is going into an image asset, the image file size calculator shows what bit depth and palette choices cost you in bytes, and the aspect ratio calculator handles the dimensions.

Use a different tool when perceptual uniformity matters. HSL and HSV are cheap cylindrical re-indexings of the RGB cube and are not perceptually uniform: a 10° hue step near green is a much smaller visual change than the same step near blue. For palette generation, gradient interpolation or measuring colour difference, work in CIELAB, OKLab or OKLCH instead — the CSS Color Module Level 4 exposes all three, and they are designed so that equal numeric steps look like equal visual steps.

Use a colour-managed pipeline when the destination is print or a wide-gamut display. Naive CMYK and unqualified hex both assume sRGB; the moment you leave that assumption, only an ICC profile gets you the right answer. And when you are embedding colour data in a payload rather than a stylesheet, the Base64 size calculator tells you what encoding an inline image costs in transfer bytes.

Frequently asked questions

How do I convert hex to RGB by hand?

Split the six digits into three pairs and read each pair as a base-16 number: the first digit is the sixteens place, the second the units. For #3B82F6, 3B = 3×16 + 11 = 59, 82 = 8×16 + 2 = 130, and F6 = 15×16 + 6 = 246, giving rgb(59, 130, 246). Letters A through F stand for 10 through 15.

Why is my HSL saturation different from the one my design app shows?

Because most design applications display HSB (also written HSV), not HSL. Both share the same hue angle, but HSL divides the channel spread by 1 − |2L − 1| while HSV divides it by max. For #3B82F6 that gives 91% in HSL and 76% in HSV. CSS hsl() expects the HSL number, so use the row labelled HSL when copying into a stylesheet.

What does the eight-digit hex form mean?

The last two digits are alpha on the same 0–255 scale, so #3B82F680 is the colour #3B82F6 at 0x80 = 128, which is 128 ÷ 255 = 0.50 opacity. Four-digit shorthand works the same way, with the fourth digit repeated. All current browsers accept both forms.

Is the CMYK output good enough to send to a printer?

No. It is the naive arithmetic conversion, which pulls out the common black and scales the remainder. It knows nothing about your paper, ink limit, dot gain or press. Use it to sanity-check that a colour is mostly cyan or mostly magenta, then have the file separated through the printer's own ICC profile before anything goes on press.

What is relative luminance and why does the calculator report it?

It is the perceived brightness of a colour on a 0-to-1 scale, computed by linearising the gamma-encoded channels and weighting them 0.2126 red, 0.7152 green and 0.0722 blue. It is the input to every WCAG contrast calculation, so having it here saves a step. Black is 0, white is 1, and mid-grey #808080 is 0.2159 rather than 0.5 — which is exactly why you cannot judge contrast from lightness.

Why does converting HSL to hex and back change my numbers?

Because 8-bit RGB has only 256 steps per channel while the HSL cylinder is continuous. Your HSL values are rounded onto the nearest representable colour, and reading that colour back gives the nearest HSL description of it, which can differ by a degree of hue or a percentage point. The calculator flags this when it happens.

What is a normal saturation for a UI colour?

There is no code-mandated number, but the working convention in design systems is that body text and surfaces sit low — often under 20% saturation — while accent and action colours sit high, frequently above 70%. The reason is practical rather than aesthetic: highly saturated large areas make text on top of them harder to read, because saturation eats into the luminance range available for contrast.

Can I enter a CSS colour name like chartreuse?

No. The hex field parses hex digits only, so a name is rejected with an error rather than guessed at. Look the name up in the CSS Color Module Level 4 named-colour table, then paste its hex value here. Named colours are exact hex aliases, so nothing is lost in the round trip.

References