HEX, RGB and HSL Color Codes Explained (With Easy Conversions)

HEX, RGB and HSL Color Codes Explained (With Easy Conversions)

Designers hand you #3B5BDB, a design tool shows rgb(59, 91, 219), and a CSS tutorial uses hsl(228, 69%, 55%). All three describe exactly the same blue. They are simply different ways of writing a color down, each with its own strengths.

This guide explains how each format works, shows how to convert between them, and gives practical advice for choosing one in your CSS, design files and brand guidelines. You can check any conversion with the free Color Toolkit & Photo Picker.

How screens make color

Every pixel on a screen is made of tiny red, green and blue lights. By mixing different brightness levels of these three, a screen can show millions of colors. Full brightness of all three gives white; all three off gives black. Equal amounts of each give a shade of grey.

In standard web color, each of the three channels has 256 possible levels, from 0 (off) to 255 (full). That gives 256 × 256 × 256 = 16,777,216 possible colors. Both HEX and RGB describe these same three numbers; HSL describes them in a way that is easier for people to reason about.

RGB: three numbers from 0 to 255

RGB writes the red, green and blue levels directly:

  • rgb(255, 0, 0) is pure red.
  • rgb(0, 0, 0) is black; rgb(255, 255, 255) is white.
  • rgb(128, 128, 128) is a medium grey.
  • rgb(59, 91, 219) is a strong blue with a little red and some green.

RGB is easy for code to work with because each value is a plain number. Adding a fourth value gives transparency, called alpha: rgb(59 91 219 / 50%) in modern CSS, or the older rgba(59, 91, 219, 0.5).

HEX: the same three numbers in base 16

A HEX code is RGB written in hexadecimal, a number system with sixteen digits: 0 to 9 and then A to F (A = 10, F = 15). Two hex digits can count from 00 to FF, which is 0 to 255, exactly one color channel.

So #3B5BDB splits into three pairs:

Pair Channel Calculation Decimal
3B Red 3 × 16 + 11 59
5B Green 5 × 16 + 11 91
DB Blue 13 × 16 + 11 219

That is why #3B5BDB and rgb(59, 91, 219) are identical.

Short HEX and HEX with transparency

  • Three-digit HEX doubles each digit: #F80 means #FF8800. It only works when both digits of every pair are the same.
  • Eight-digit HEX adds an alpha pair at the end: #3B5BDB80 is the same blue at about 50% opacity (80 in hex is 128, roughly half of 255).
  • HEX codes are not case sensitive: #3b5bdb equals #3B5BDB.

HSL: hue, saturation and lightness

RGB and HEX are precise, but it is hard to look at #3B5BDB and know how to make it “a bit lighter” or “less intense”. HSL solves this by describing color the way people think about it:

  • Hue is the position on the color wheel, in degrees from 0 to 360. Roughly: 0 is red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue and 300 is magenta.
  • Saturation is how intense the color is, from 0% (grey) to 100% (fully vivid).
  • Lightness runs from 0% (black) through 50% (the pure color) to 100% (white).

Our blue is about hsl(228, 69%, 55%). To make a lighter tint for a background, keep the hue and saturation and raise the lightness: hsl(228, 69%, 92%). To make a darker shade for a hover state, lower it: hsl(228, 69%, 42%). This is why HSL is popular for building color scales and themes.

Converting between formats

HEX to RGB

  1. Remove the # and split the code into three pairs.
  2. For each pair, multiply the first digit by 16 and add the second (using A=10 to F=15).
  3. Write the three results as rgb(r, g, b).

RGB to HEX

  1. Divide each value by 16. The whole number is the first hex digit; the remainder is the second.
  2. Example: 219 ÷ 16 = 13 remainder 11, so D and B, giving DB.
  3. Join the three pairs after a #.

RGB to HSL

This conversion involves more arithmetic: you scale each channel to 0–1, find the largest and smallest values, and calculate lightness as their average, saturation from their difference and hue from which channel is largest. It is easy to make a slip by hand, so use the Color Toolkit or your design tool, which convert instantly in both directions.

Which format should you use?

Situation Good choice Why
Brand guidelines and sharing colors HEX Short, easy to copy, understood by every tool
CSS with transparency RGB or HSL with alpha Readable opacity values
Building tints, shades and themes HSL Change one number to adjust lightness or intensity
Calculations in code RGB Plain numbers per channel
Print design CMYK (not covered here) Printers mix inks, not light

Contrast and accessibility

A color format does not tell you whether text will be readable. For that, check the contrast ratio between text and background. The Web Content Accessibility Guidelines (WCAG) recommend at least 4.5:1 for normal body text and 3:1 for large text. Light grey text on white, a common design trend, often fails this. When in doubt, darken the text color by lowering HSL lightness until it passes.

Picking colors from a photo

Want to match a color from a logo, product photo or screenshot? Open the Color Toolkit & Photo Picker, load the image and click any point to get its HEX, RGB and HSL values. The image is processed in your browser and is not uploaded.

Frequently asked questions

Why does the same HEX color look different on two screens?
Screens differ in calibration, brightness and color gamut. The code is identical; the display is not. Calibrated monitors give the most consistent results.

Are HSL and HSB/HSV the same?
No. Many design apps use HSB (also called HSV), where 100% brightness gives the pure color, not white. Numbers from one do not transfer directly to the other, so convert through HEX or RGB.

How many colors should a website palette have?
Most sites work well with one primary brand color, one accent, a few neutral greys and colors for success, warning and error states, each with a few lighter and darker steps.