Color Codes Explained — HEX vs RGB vs HSL and How to Convert

When specifying colors on the web or in design, you often see three notations: HEX like #3366cc, RGB like rgb(51,102,204), and HSL like hsl(220,60%,50%). All of these are simply the same color written in a different way. In this article we explain how each works, when to use it, and the steps to convert between them, with a verified example throughout.

To state the conclusion first: #3366cc, rgb(51, 102, 204) and hsl(220, 60%, 50%) are all the same color. We use this color as a consistent example throughout the article and trace how to move between the notations.

1. The digital representation of color (RGB basics)

Colors on a display are made by mixing red (R), green (G) and blue (B) light. The more light you mix, the brighter it gets, and turning all three to maximum produces white. This is called additive color mixing (the opposite of the subtractive mixing of paint).

The strength of each channel is usually expressed in 8 bits = 256 steps from 0 to 255. With three channels, the number of expressible colors is 256 × 256 × 256 ≈ 16.77 million. For example, R=51, G=102, B=204 is a blue-ish color made by mixing a little red, a medium amount of green, and a strong amount of blue.

2. HEX (hexadecimal color codes)

HEX writes each RGB value as two hexadecimal digits, prefixed with #. The form is #rrggbb, where rr is red, gg is green and bb is blue. In hexadecimal, 0–255 corresponds to 00ff.

The 3-digit shorthand (#rgb)

When each channel's two digits are an identical repeated pair (such as 33, 66, cc), you can use the 3-digit form with each pair collapsed to a single character. #36c means the same as #3366cc (each digit doubled). However, a color like #1a2b3c, which is not a repeated pair, cannot be shortened.

With alpha (#rrggbbaa / #rgba)

Adding one more channel at the end expresses opacity (alpha). In #rrggbbaa, aa ranges from 00 (fully transparent) to ff (fully opaque). For example, #3366cc80 is #3366cc at roughly 50% opacity (80 is 128 in decimal, about 50% of 255). The 4-digit shorthand #36c8 works the same way.

3. RGB / RGBA

In CSS, the rgb() function writes the same color in decimal: rgb(51, 102, 204). To add transparency, use rgba(), or the modern syntax rgb(51 102 204 / 50%), where the final value sets the opacity.

Each channel can be written as an integer from 0 to 255, or as a relative value (percentage) from 0% to 100%. rgb(20%, 40%, 80%) works out to 20% × 255 ≈ 51, 40% × 255 ≈ 102 and 80% × 255 = 204, giving essentially the same color as rgb(51, 102, 204). Because the numbers map directly to the RGB channels, this notation is convenient when computing colors in code.

4. HSL / HSLA

HSL expresses a color through three components: hue, saturation and lightness. You write it like hsl(220, 60%, 50%).

The reason HSL is favored is that it is closer to human perception and easier to adjust. If you want to "make it a bit brighter while keeping the hue", raise L; if you want to "mute it", lower S. In other words, you get the adjustment you want by moving just one axis. Doing the same thing in RGB/HEX requires changing all three values at once, which is not intuitive. The hsla() function and the hsl(220 60% 50% / 50%) syntax are available for transparency.

5. Conversion procedures

RGB → HEX

Simply convert each channel to two hexadecimal digits and line them up, padding with a leading 0 if a value is a single digit.

Lining them up gives #3366cc.

HEX → RGB

Conversely, take two digits at a time and convert back to decimal. cc is 12×16 + 12 = 204. For a 3-digit HEX, double each digit before converting (#36c → #3366cc).

RGB → HSL

The standard algorithm follows these steps. We compute it for #3366cc = rgb(51, 102, 204).

  1. Divide each value by 255 to get the range 0–1: R=0.2, G=0.4, B=0.8.
  2. Find the maximum max=0.8, the minimum min=0.2, and the difference d=0.6.
  3. Lightness L = (max + min) / 2 = (0.8 + 0.2) / 2 = 0.550%.
  4. Saturation S = d / (1 − |2L − 1|) = 0.6 / 1 = 0.660% (when L=0.5 the denominator is 1).
  5. Hue H: since the maximum is B, H = 60 × ((R − G) / d + 4) = 60 × ((0.2 − 0.4) / 0.6 + 4) = 60 × 3.667 ≈ 220 degrees.

The result is hsl(220, 60%, 50%), matching the original color. (For the hue formula: if the max is R use ((G−B)/d) mod 6, if G use (B−R)/d + 2, if B use (R−G)/d + 4, then multiply by 60.)

HSL → RGB

The reverse conversion is also defined. From lightness and saturation, compute the chroma C = (1 − |2L − 1|) × S, split the hue into 60-degree segments to assign R, G and B, then add (L − C/2) to every channel. Converting hsl(220, 60%, 50%) back gives rgb(51, 102, 204). Since the manual calculation is tedious, in practice the most reliable approach is a one-shot conversion with the tool below.

The same color in three notations (examples). In each row below, every value from left to right is the same color.
NameHEX3-digit HEXRGBHSL
Black#000000#000rgb(0, 0, 0)hsl(0, 0%, 0%)
White#ffffff#fffrgb(255, 255, 255)hsl(0, 0%, 100%)
Red#ff0000#f00rgb(255, 0, 0)hsl(0, 100%, 50%)
Green (pure)#00ff00#0f0rgb(0, 255, 0)hsl(120, 100%, 50%)
Blue#0000ff#00frgb(0, 0, 255)hsl(240, 100%, 50%)
The article's example#3366cc#36crgb(51, 102, 204)hsl(220, 60%, 50%)

6. Contrast and accessibility

When choosing colors, what matters is not only how they look but also the contrast between text and background. Low contrast makes text hard to read for people with low vision or in bright outdoor light.

The web accessibility guidelines WCAG (Web Content Accessibility Guidelines) recommend a contrast ratio of at least 4.5:1 for normal text and at least 3:1 for large text. The contrast ratio is calculated from the relative luminance of the text and background colors. Being mindful of lightness (L) in HSL makes it easier to control the light/dark difference, and as a result, easier to tune contrast. Once you have settled on a palette, checking the contrast ratio as well will give you peace of mind.

Free Tool Convert with the Color Converter Enter a HEX, RGB or HSL value and it converts to the other notations on the spot. It also supports alpha values and a preview, making color work go smoothly.

Frequently Asked Questions (FAQ)

What is the difference between HEX and RGB?

HEX and RGB are the same color written two different ways. Both are combinations of the red, green and blue channels (each 0-255); HEX writes them in hexadecimal as "#rrggbb", while RGB writes them in decimal as "rgb(51,102,204)". For example, #3366cc and rgb(51,102,204) are exactly the same color.

What are the advantages of using HSL?

HSL expresses color as hue (H: 0-360 degrees), saturation (S: %) and lightness (L: %), which is closer to how humans perceive color and makes it easier to adjust. To make a color brighter while keeping its hue, raise L; to mute it, lower S. These adjustments are intuitive, whereas in RGB/HEX you would have to change all three values at once.

What is a 3-digit HEX (#rgb)?

It is a shorthand you can use when each channel's two hex characters are an identical repeated pair. It means the same as the 6-digit form with each digit doubled, so #36c is the same color as #3366cc. Not every color can be shortened to three digits: a color such as #1a2b3c, which is not a repeated pair, must be written in six digits.

← Back to the Tech Blog list