HEX vs RGB vs HSL: What's the Difference and Which Should You Use?

Quick Answer
HEX, RGB and HSL are three ways to write the same screen color. HEX and RGB both store the amounts of red, green and blue light, just in base 16 and base 10. HSL stores hue, saturation and lightness instead, which is easier for humans to adjust. Pick HEX for handoff, RGB for opacity math, and HSL when you need to tweak or theme a color.
On this page
Key Takeaways
- HEX and RGB are the same data: `#6D46D3` is exactly `rgb(109 70 211)`, only written in base 16 versus base 10.
- HSL describes a color as a hue angle plus saturation and lightness, so "make it a bit darker" means changing one number.
- Modern CSS accepts all three with transparency: `rgb(109 70 211 / 50%)`, `hsl(256.6 61.6% 55.1% / 50%)` or an 8-digit hex that adds an alpha pair such as `80`.
- HSL lightness is not perceptual lightness. Pure blue and pure green share `50%` in HSL but look very different in brightness, which is why OKLCH exists.
What does each color format actually store?
Every screen color you meet on the web is, by default, an sRGB color: a mix of red, green and blue light. The CSS Color Module Level 4 says colors specified in HTML and untagged images are treated as sRGB, and lists hex colors, rgb() and hsl() as ways to write one [1]. The three formats are different notations for the same small set of colors. (If you are searching with the British spelling, "colour", you are looking for exactly the same thing.)
RGB stores three numbers from 0 to 255: red, green and blue. Each channel has 256 steps, so 256 × 256 × 256 gives 16,777,216 possible colors. rgb(109 70 211) means 109 of red, 70 of green and 211 of blue.
HEX is RGB written in hexadecimal (base 16), where digits run 0 to 9 and then A to F. Each channel becomes a two-digit pair: 6D is 6 × 16 + 13 = 109, 46 is 4 × 16 + 6 = 70, and D3 is 13 × 16 + 3 = 211. So #6D46D3 and rgb(109 70 211) are identical, down to the last digit.
HSL stores hue, saturation and lightness. Hue is an angle on the color wheel from 0 to 360 degrees (in HSL, sRGB green sits at 120 degrees). Saturation is how intense the color is, from gray at 0% to fully vivid at 100%. Lightness runs from black at 0% through "normal" at 50% to white at 100% [3].
How does one color look in HEX, RGB, HSL and OKLCH?
Here is the SwatchNest violet in every format. All values come from our own color converter, so you can paste any row into the color converter and get the same result back.
| Format | CSS value | What it tells you |
|---|---|---|
| HEX | #6D46D3 | Compact, six digits, easy to paste |
| RGB | rgb(109 70 211) | Red 109, green 70, blue 211 |
| HSL | hsl(256.6 61.6% 55.1%) | Hue 256.6 degrees, fairly vivid, a little lighter than mid |
| OKLCH | oklch(0.519 0.205 290.1) | Perceived lightness 0.519, chroma 0.205, hue 290.1 |
Notice the hue number differs between HSL (256.6) and OKLCH (290.1). MDN notes that hue angles differ across the sRGB, CIELAB and Oklab color spaces, so the numbers are not interchangeable [4]. HSL values are shown with one decimal here because rounding to whole numbers can shift a color by a step or two. With one decimal, hsl(256.6 61.6% 55.1%) converts back to exactly #6D46D3.
Two more colors from our palette show how the same trio of numbers looks for different hues:
| Color | HEX | RGB | HSL |
|---|---|---|---|
| Pink | #EC4899 | rgb(236 72 153) | hsl(330.4 81.2% 60.4%) |
| Amber | #F59E0B | rgb(245 158 11) | hsl(37.7 92.1% 50.2%) |
| Navy ink | #17172F | rgb(23 23 47) | hsl(240 34.3% 13.7%) |
If you want to convert by hand in a spreadsheet or a script, the dedicated tools do it for you: HEX to RGB, RGB to HEX, HEX to HSL and HSL to HEX. When you need named references such as violet, our violet color page lists the shades and codes.
When should you use HEX, RGB or HSL?
There is no single winner. Each format is best at a different job, and most real projects use more than one.
Use HEX for handoff and compact tokens
HEX is the common currency of design tools, brand guidelines and style guides. It is short, copies cleanly, and lets you shorten some codes: a pair of repeated digits collapses, so #336699 can be written #369. HEX is a good default for a design token you store once and reference everywhere. See our list of HTML color codes for ready-to-use values.
Use RGB when you need opacity or channel math
RGB is the natural choice in code that reads or blends channels: canvas drawing, image processing, or mixing two colors by averaging their numbers. In CSS it is also the most familiar way to add transparency. The channels are plain integers, so there is little room for confusion.
Use HSL when you tweak, theme or build scales
HSL shines when a human is adjusting a color. Keep hue and saturation fixed and change only lightness, and you get a family of related shades. This is how most light and dark themes and button hover states are built. Here is a violet ramp at hue 257 and 62% saturation, with each hex code computed from the HSL value:
| HSL | HEX | Typical use |
|---|---|---|
hsl(257 62% 25%) | #2F1867 | Dark text, deep backgrounds |
hsl(257 62% 45%) | #542CBA | Pressed or hover state |
hsl(257 62% 55%) | #6D45D3 | Base brand color |
hsl(257 62% 65%) | #8E6EDD | Light hover on dark mode |
hsl(257 62% 90%) | #DFD6F5 | Tinted backgrounds |
Notice that hsl(257 62% 55%) gives #6D45D3, one step away from our exact #6D46D3. That is normal: whole-number HSL cannot hit every HEX value. If you want an exact brand color, store the HEX and use HSL only for the variations. Need more shades? Our palette generator builds full sets from one base.
How do you write transparency and modern CSS syntax?
Modern CSS lets you write rgb() and hsl() with spaces instead of commas, and put transparency after a slash. MDN's examples include rgb(255 0 153 / 80%) and hsl(120deg 75% 25% / 60%), and the alpha value can be a number or a percentage [2] [3]. The old comma form such as rgba(109, 70, 211, 0.5) still works, but the space-and-slash form is the current style and works the same in both functions.
Here is SwatchNest violet at half opacity in three spellings:
| Format | 50% opacity |
|---|---|
| RGB | rgb(109 70 211 / 50%) |
| HSL | hsl(256.6 61.6% 55.1% / 50%) |
| 8-digit HEX | #6D46D3 plus 80 (eight digits in total) |
The last pair of digits in the 8-digit hex is the alpha, on the same 00 to FF scale as the other channels. 80 is 128 out of 255, which is about 50.2%, so six digits plus 80 is a hair above half opaque. CSS Color 4 defines the hex forms as 3, 4, 6 and 8 digits [1]. If you want a precise percentage, rgb(109 70 211 / 50%) is easier to read than hex. Our HEX to RGBA tool converts between the two.
One thing to check when adding transparency: text and borders on a semi-transparent color need a contrast check against whatever sits behind them, not against the color alone. Use the contrast checker with the final blended result.
What is OKLCH and why does it matter?
OKLCH is a newer way to write colors, defined in CSS Color 4 as an Oklab color given by lightness, chroma and hue [1]. It is a different color model, not a different spelling of the same HSL numbers. MDN describes its lightness as the color's "perceived lightness", unlike HSL, where lightness is measured against other colors [4]. That is why designers like it: its lightness number tracks how bright a color looks far better than HSL lightness does.
You can see the problem in HSL with three pure colors, each at 100% saturation and 50% lightness. HSL treats them as equally light. Their OKLCH lightness says otherwise:
| Color | HSL | HEX | OKLCH lightness |
|---|---|---|---|
| Blue | hsl(240 100% 50%) | #0000FF | 0.452 |
| Red | hsl(0 100% 50%) | #FF0000 | 0.628 |
| Green | hsl(120 100% 50%) | #00FF00 | 0.866 |
Same HSL lightness, but green is almost twice as light as blue by OKLCH. If you build a palette by holding HSL lightness constant across hues, some swatches will look glaringly bright and others muddy. Holding OKLCH lightness constant gives a more even result, which helps when choosing text and background pairs.
OKLCH is also well supported: MDN lists it as Baseline widely available, in browsers since May 2023 [4]. A practical workflow is to keep HEX as your source of truth, use HSL for quick edits, and try OKLCH when you need evenly bright steps. Not every OKLCH value fits on an ordinary screen, so check the result, and keep a HEX fallback if you support older browsers.
How do the conversion formulas work?
You rarely need to convert by hand, but knowing the steps makes the numbers feel less magical. Here is #6D46D3 going to HSL, using the standard method (divide each channel by 255, then compare the largest and smallest):
- Channels: R = 109, G = 70, B = 211.
- Lightness: the average of the largest and smallest channel. (211 + 70) ÷ 2 = 140.5, and 140.5 ÷ 255 is about 55.1%.
- Saturation: the spread between largest and smallest (141) relative to how far lightness is from the extremes, which works out to about 61.6%.
- Hue: blue is the largest channel, so hue lands between 180 and 300 degrees. The red-green difference sets where: 60 × ((109 − 70) ÷ 141 + 4) is about 256.6 degrees.
Going back from HSL to RGB reverses those steps. From HEX to RGB it is just reading pairs, as in the first diagram. The W3C spec includes sample code for the HSL conversion in CSS Color 4, if you want the full algorithm [1]. Or let our color converter handle all four formats at once.
Frequently Asked Questions
Is HEX or RGB better for CSS?
Neither is better; they describe identical colors. HEX is shorter, so it is popular for tokens and brand guides. RGB is easier when you need to see or script individual channels, or write a readable percentage alpha like rgb(109 70 211 / 50%).
Is HSL more accurate than RGB?
No. HSL is a different arrangement of the same sRGB colors, not a more precise one. Its main benefit is that hue, saturation and lightness map to how people think about adjusting a color. Rounding in HSL can move a color one step away from its HEX value.
Why does my HSL color not match my HEX code exactly?
Whole-number HSL values cannot represent every RGB color. Using one decimal, as in hsl(256.6 61.6% 55.1%), converts back to #6D46D3 exactly. If an exact brand match matters, keep the HEX code as the master value.
Should I switch my site to OKLCH?
You do not have to. OKLCH is helpful for building evenly spaced palettes and checking perceived lightness. HEX, RGB and HSL remain fully valid in CSS, so adopt OKLCH gradually where it solves a real problem, and check browser support first.
Our Take
We think most teams are better off treating HEX as the format of record and HSL as the working tool. Store brand colors as HEX so every designer and developer pastes the same string, then reach for HSL when you need hover states, dark-mode variants or a quick tint. Use RGB with a slash alpha whenever transparency is involved, because it reads more clearly than an 8-digit hex. And we would experiment with OKLCH for palette scales, since the HSL lightness trap with blue and green is real and easy to hit. None of this is a rule, and a consistent team habit matters more than the specific format.
Sources
- CSS Color Module Level 4: Named Colors. W3C.
- <color>. MDN Web Docs.
- hsl(). MDN Web Docs.
- oklch(). MDN Web Docs.
Color values are shown in sRGB. Contrast results follow WCAG 2.x and do not cover every accessibility need. Spotted an error? Tell us.



