⚡Utilvy
2026-11-23

How to Convert Hex Colors to RGB and HSL (With Examples)

A hex color code like #FF5733 is just three numbers written in base 16 — one pair each for red, green, and blue. Converting it to RGB means turning each pair into decimal. Converting to HSL adds one short formula. This guide walks through both, step by step, with a fully worked example.

What a Hex Code Actually Encodes

A six-digit hex code packs three color channels — red, green, and blue — into one compact string. Each pair of digits represents a single channel on a scale from 00 (decimal 0) to FF (decimal 255). Because hexadecimal is base 16, a single digit covers 0–15, and two digits cover the full 0–255 range.

Split #FF5733 into its three pairs and convert each one:

| Pair | Decimal | Channel | |------|---------|---------| | FF | 255 | Red | | 57 | 87 | Green | | 33 | 51 | Blue |

That makes #FF5733 equal to rgb(255, 87, 51) — a warm orange-red. Reading hex pairs gets easy once you remember that the first digit counts in sixteens: in 57, the 5 means 5 × 16 = 80, and the trailing 7 adds 7 more, landing on 87.

Converting Hex to RGB

There is no formula to memorize here — it is pure base conversion:

  1. Split the six digits into three pairs: FF, 57, 33.
  2. For each pair, multiply the first digit by 16 and add the second digit.
  3. FF → 15 × 16 + 15 = 255. Then 57 → 5 × 16 + 7 = 87. And 33 → 3 × 16 + 3 = 51.
  4. Write the result as rgb(255, 87, 51).

Three-digit shorthand follows the same rule after expansion: #F53 means FF5533, so each digit is doubled, never padded with zeros.

Converting RGB to HSL: The Worked Example

HSL describes the same color using hue (its position on the color wheel, 0–360°), saturation (how intense it is, 0–100%), and lightness (how close it sits to black or white, 0–100%). Designers prefer it because you can adjust one dimension — say, darken a button — without touching the other two.

Starting from rgb(255, 87, 51):

1. Normalize each channel. Divide by 255: R = 1.000, G = 0.341, B = 0.200.

2. Find the max, min, and delta. Max = 1.000 (red), min = 0.200 (blue), delta = 1.000 − 0.200 = 0.800.

3. Compute lightness. L = (max + min) ÷ 2 = (1.000 + 0.200) ÷ 2 = 0.600 → 60%.

4. Compute saturation. S = delta ÷ (1 − |2L − 1|) = 0.800 ÷ (1 − 0.200) = 0.800 ÷ 0.800 = 1.000 → 100%.

5. Compute hue. The formula depends on which channel is the max:

| Max channel | Hue formula | |-------------|-------------| | Red | 60 × ((G − B) ÷ delta mod 6) | | Green | 60 × ((B − R) ÷ delta + 2) | | Blue | 60 × ((R − G) ÷ delta + 4) |

Red is the max here, so H = 60 × ((0.341 − 0.200) ÷ 0.800) = 60 × 0.176 ≈ 11°.

The final result: #FF5733 = rgb(255, 87, 51) = hsl(11, 100%, 60%). All three notations render the identical color in CSS, so you can paste each into a stylesheet and compare side by side.

Step-by-Step: Convert Any Hex Code by Hand

  1. Split the hex string into three two-digit pairs.
  2. Convert each pair to decimal (first digit × 16 + second digit). You now have RGB.
  3. To continue to HSL, divide each channel by 255.
  4. Record the max, min, and delta values.
  5. Compute L = (max + min) ÷ 2, then S = delta ÷ (1 − |2L − 1|).
  6. Pick the hue formula based on the max channel and solve for H.
  7. Multiply L and S by 100 for percentages, then sanity-check: neutral grays (all channels equal) must give S = 0%, and pure black or white has no meaningful hue at all.

One caution: rounding. If you round the normalized channels to two decimals before computing, the final hue can drift by a degree or two. Keep three decimals during the math and round only the published result.

When to Use Each Format

| Format | Best for | Example | |--------|----------|---------| | Hex | Design handoff, CSS variables, copy-paste tokens | #FF5733 | | RGB / RGBA | Adding transparency or building colors in JavaScript | rgba(255, 87, 51, 0.5) | | HSL | Generating shade and tint scales, adjusting by feel | hsl(11, 100%, 40%) |

HSL earns its keep in design systems. Keep hue and saturation fixed and step only the lightness — hsl(11, 100%, 70%), hsl(11, 100%, 60%), hsl(11, 100%, 50%) — and you get a guaranteed harmonious ramp of the same color at different weights. Doing that in hex means a fresh conversion every single time.

Is Hex Better Than RGB?

Neither is more accurate — they encode identical information, and browsers convert between them internally. Hex wins on compactness and familiarity in design tools; RGB(A) wins when you need alpha transparency or compute channel values dynamically in code. Pick one convention per project and stay consistent.

What Is the Difference Between HSL and HSV (HSB)?

They share hue, but the third dimension differs: HSL's lightness runs from black (0%) through the pure hue (50%) to white (100%), while HSV's "value" runs from black to full brightness, with the pure hue at 100%. CSS supports hsl() natively but not hsv(), so HSL is the practical choice on the web.

How Do I Convert Back From HSL to Hex?

Run the math in reverse: use L and S to recover the chroma (C = (1 − |2L − 1|) × S), then the max channel (L + C ÷ 2) and min channel (L − C ÷ 2), reconstruct the middle channel from the hue sector, multiply each channel by 255, and finally convert every value to a two-digit hex pair. It is doable, but tedious — most people convert back with a tool rather than by hand.

Skip the Arithmetic

Hand conversion is great for building intuition, but nobody wants to compute hue on deadline. The free Color Converter on Utilvy turns any hex, RGB, or HSL value into all three formats instantly, rounded correctly — handy when you are building a shade scale or matching one brand color across formats.

Try these tools