CCalclabhub

Color

How To Convert Colors

A colour can be written in several ways, and each notation is convenient for a different job. Hex is compact for stylesheets, RGB maps directly to how a screen mixes light, and HSL is the one that lets you reason about a colour by eye.

Quick Answer

Hex RRGGBB = (R x 65536) + (G x 256) + B

R
Red channel, 0 to 255
G
Green channel, 0 to 255
B
Blue channel, 0 to 255
H, S, L
Hue in degrees, saturation and lightness as percentages

The colour #3B82F6 is rgb(59, 130, 246), which is hsl(217, 91%, 60%). Two hex digits per channel, in red, green, blue order, and each pair converts to a number from 0 to 255.

What Is How To Convert Colors?

A digital colour is three numbers describing how much red, green and blue light to emit. Screens mix those three primaries additively, which is why every notation eventually resolves to an RGB triple.

The hex notation packs those three numbers into six hexadecimal digits. Each channel takes two digits, running from 00 for none to FF for full, so a hex code is exactly three bytes written in the base sixteen shorthand common in computing.

Hex is written in red, green and blue order, so the first pair is red. A value like #3B82F6 means red 3B, green 82 and blue F6, which is 59, 130 and 246 in decimal.

A three-digit hex code is a shorthand where each digit is doubled. #F0A is the same as #FF00AA. The shorthand only works when both digits of every pair are identical, so it cannot express most colours.

The RGB notation writes the same three numbers directly, either as integers from 0 to 255 or as percentages. It is easier to read and to generate in code, which is why it is common in scripts and inline styles.

HSL takes a different approach. Hue is an angle from 0 to 360 degrees around a colour wheel, saturation is how far the colour is from grey, and lightness is how close it is to white or black.

HSL is the notation that matches how people describe colour. Making a colour lighter means raising the lightness, and making it less vivid means lowering the saturation, both of which are single-number changes rather than three.

The conversion from RGB to HSL is a geometric operation on the RGB cube. The maximum and minimum channel values give the lightness and the saturation, and which channel is the maximum determines the hue sector.

Saturation in HSL is not the same as perceived vividness. Two colours with the same saturation value can look quite different because the eye is far more sensitive to green than to blue, which is what the luminance calculation accounts for.

Relative luminance is a weighted sum of the three channels after a gamma correction, using weights of about 0.2126 for red, 0.7152 for green and 0.0722 for blue. It is the basis of every contrast calculation in accessibility guidance.

The contrast ratio compares the luminance of two colours, running from 1 to 1 for identical colours up to 21 to 1 for black against white. The WCAG guidelines require 4.5 to 1 for normal body text and 3 to 1 for large text.

Contrast matters more than colour choice for readability. A stylish low-contrast palette can make text unreadable for someone with reduced vision or a screen in bright sunlight, and the ratio is the objective measure of that risk.

The calculator takes a hex code, RGB sliders or a colour picker, keeps them in step, and shows the hex, RGB and HSL values together with the contrast ratio against white and black.

Formula

R = 0xRR, G = 0xGG, B = 0xBB

Each pair of hex digits is a base-sixteen number from 0 to 255, in red, green and blue order.

SymbolMeaning
h1High nibble
h2Low nibble

L = 0.2126 R + 0.7152 G + 0.0722 B (gamma corrected)

Each channel is linearised and then weighted by the eye's sensitivity, which is highest for green.

SymbolMeaning
R'Linear red
G'Linear green
B'Linear blue

How To Calculate How To Convert Colors

  1. 1

    Split the hex code into three pairs

    Take the six digits two at a time. #3B82F6 becomes 3B, 82 and F6.

  2. 2

    Convert each pair from base sixteen

    Multiply the first digit by sixteen and add the second. 3B is 3 times 16 plus 11, which is 59.

  3. 3

    Write the RGB triple

    The three results are the red, green and blue channels. 3B82F6 is rgb(59, 130, 246).

  4. 4

    Convert to HSL if you need to reason about it

    Find the largest and smallest channel to get the lightness, then derive the saturation and hue from the spread.

  5. 5

    Check the contrast

    Compute the relative luminance of the colour and of the background, then divide the lighter plus 0.05 by the darker plus 0.05. Aim for 4.5 to 1 for body text.

Examples

Example 1: Hex #3B82F6

Hex code
#3B82F6
Red
59
Green
130
Blue
246
StepCalculationResult
Red3 x 16 + 1159
Green8 x 16 + 2130
Blue15 x 16 + 6246
Hueblue sector of the colour wheel217
Saturationspread relative to the lightest channel91
Lightness(246 + 59) / 2 / 25560

Result: #3B82F6 is rgb(59, 130, 246), which is hsl(217, 91%, 60%).

Example 2: Hex #FFFFFF

Hex code
#FFFFFF
Red
255
Green
255
Blue
255
StepCalculationResult
Red15 x 16 + 15255
Green15 x 16 + 15255
Blue15 x 16 + 15255
Hueno colour present, so hue falls back to zero0
Saturationgrey, so no saturation0
Lightnessmaximum lightness100

Result: White is rgb(255, 255, 255) and hsl(0, 0%, 100%), with a contrast ratio of 21 to 1 against black.

Calculator

Prefer a full-width tool? Open the How To Convert Colors calculator page.

Common Mistakes

  • Swapping the channel order

    Hex is red, green, blue in that order. Writing it as blue, green, red produces a plausible but wrong colour.

  • Treating hex letters as text

    A to F stand for the values ten to fifteen. Comparing them as letters rather than numbers breaks the conversion.

  • Using three-digit shorthand when it does not apply

    #3B8 is not a short form of #3B82F6, because the digits are not paired. Shorthand only works when each pair repeats a single digit.

  • Assuming HSL saturation equals perceived vividness

    Saturation is a geometric measure. Green looks far brighter than blue at the same saturation and lightness, which is why luminance weighting exists.

  • Judging contrast by eye

    Perceived contrast is unreliable, especially for thin text and for people with reduced colour vision. The luminance ratio is the measure that accessibility guidance uses.

  • Ignoring the background in a contrast check

    A colour has no contrast on its own. The ratio is always between a foreground and a background, and both matter.

  • Forgetting that hex is case insensitive

    Upper and lower case mean the same thing, so a mismatch in case is never the cause of a colour that renders differently.

FAQ

What is the difference between RGB and HSL?

They describe the same colours in different coordinates. RGB matches how a screen emits light, while HSL matches how people think about hue, vividness and brightness, which makes it easier to adjust a colour by hand.

Why do hex codes have six digits?

Two digits for each of the three channels. Each pair is a base-sixteen number from 0 to 255, so the six digits carry exactly the same information as an RGB triple.

What does the contrast ratio mean?

It compares the relative luminance of two colours, from 1 to 1 for identical colours up to 21 to 1 for black on white. WCAG asks for 4.5 to 1 for normal text and 3 to 1 for large text.

Is HSL the same as HSV or HSB?

No. HSV, also called HSB, uses brightness where HSL uses lightness, so the two give different values for the same colour. Design tools often use HSV while CSS uses HSL.

Can every colour be written in three-digit hex?

No. Shorthand only works when both digits in every pair are identical, which is a small subset of the sixteen million colours a six-digit code can express.

How many colours can hex represent?

Sixteen million seven hundred and seventy-seven thousand two hundred and sixteen, which is 256 cubed. That covers every combination of the three eight-bit channels.

References

  1. [1]World Wide Web Consortium, CSS Color Module Level 4 — https://www.w3.org/TR/css-color-4/
  2. [2]World Wide Web Consortium, Understanding Success Criterion 1.4.3: Contrast (Minimum) — https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html
  3. [3]Wikipedia, HSL and HSV — https://en.wikipedia.org/wiki/HSL_and_HSV