HEX to RGB and HSL: How Digital Color Codes Work Under the Hood with Conversion Formulas
Understand digital color models. Learn how HEX hexadecimal notation maps to decimal RGB, how HSL cylindrical coordinates work, and exact mathematical conversion formulas.

In web development and graphic design, color is expressed in multiple formats: #0F766E, rgb(15, 118, 110), or hsl(175, 77%, 26%). To a computer monitor, all three notations represent the exact same teal hue. But to a developer or designer, each format serves a distinct purpose.
HEX notation is compact and universal. RGB maps directly to the physical red, green, and blue sub-pixels of LED displays. HSL organizes colors intuitively according to human visual perception, making it the preferred choice for building dynamic CSS design systems and dark-mode themes.
This guide explains the underlying binary mathematics of HEX codes, details the conversion formulas between RGB and HSL, and provides CSS best practices for modern design systems. You can convert color values and inspect accessibility contrast in real time using Synctoolo's free Color Converter and Color Picker.
The Hexadecimal System: Decoding HEX Color Codes
A standard 6-character HEX color code (e.g. #2A6496) is simply a compact base-16 representation of 3 byte values (Red, Green, Blue). In hexadecimal, counting runs from 0 through 15 using numerals 0-9 followed by letters A through F (where A=10, B=11, C=12, D=13, E=14, and F=15).
A 6-digit HEX code splits into three two-character pairs:
# R R G G B B
# 2 A 6 4 9 6
Red Channel: 2A (Hex) = (2 * 16^1) + (10 * 16^0) = 32 + 10 = 42 (Decimal)
Green Channel: 64 (Hex) = (6 * 16^1) + (4 * 16^0) = 96 + 4 = 100 (Decimal)
Blue Channel: 96 (Hex) = (9 * 16^1) + (6 * 16^0) = 144 + 6 = 150 (Decimal)
Resulting Decimal RGB: rgb(42, 100, 150)
Because each two-digit hexadecimal channel represents 8 bits of color depth (2^8 = 256 possible shades per channel from 0 to 255), standard 24-bit Truecolor yields 16,777,216 distinct color permutations (256 * 256 * 256).
Why HSL Is Superior for UI Design Systems
While RGB and HEX are ideal for machine hardware, they are unintuitive for human designers. If you have the color rgb(42, 100, 150) and want to create a hover state that is 15% lighter, which numbers should you change? In RGB, you must modify all three channels simultaneously, risking an accidental hue shift.
HSL (Hue, Saturation, Lightness) solves this by mapping colors onto cylindrical coordinates:
- Hue (0 to 360 degrees): The base color angle on the color wheel (0 = Red, 120 = Green, 240 = Blue, 360 = Red).
- Saturation (0% to 100%): The intensity or purity of the color (0% = completely gray, 100% = pure vibrant pigment).
- Lightness (0% to 100%): The amount of white or black mixed in (0% = pure black, 50% = normal color, 100% = pure white).
| Design State | HSL Representation | Why It Works |
|---|---|---|
| Base Button Color | hsl(210, 80%, 50%) |
Balanced brand blue hue. |
| Hover State (Lighter) | hsl(210, 80%, 58%) |
Adjust lightness only (+8%). Hue and saturation stay locked. |
| Active / Click State (Darker) | hsl(210, 80%, 42%) |
Adjust lightness only (-8%). Zero risk of color distortion. |
| Subtle Background Tint | hsl(210, 80%, 96%) |
Ultra-light tint for card backgrounds while keeping brand harmony. |
The RGB to HSL Mathematical Algorithm
To convert normalized RGB values (r, g, b between 0 and 1) to HSL coordinates:
- Find the maximum and minimum values:
max = max(r, g, b),min = min(r, g, b). - Calculate Lightness:
L = (max + min) / 2. - If
max == min, Saturation is 0% (achromatic gray). - Otherwise, Saturation is:
S = (max - min) / (1 - |2L - 1|). - Calculate Hue based on which channel was the maximum value:
- If Red is max:
H = 60 * [((g - b) / (max - min)) % 6] - If Green is max:
H = 60 * [((b - r) / (max - min)) + 2] - If Blue is max:
H = 60 * [((r - g) / (max - min)) + 4]
- If Red is max:
Convert and inspect colors across HEX, RGB, HSL, and OKLCH in real time using Synctoolo's free Color Converter and pick palette shades with our Color Picker.
Tools mentioned in this article
FAQ
What does an 8-character HEX code represent?+
An 8-character HEX code includes a two-character Alpha (opacity) channel at the end: #RRGGBBAA. For example, #00000080 represents pure black at 50% opacity (0x80 is 128 in decimal, representing 128/255 = ~50%).
What is the difference between HSL and modern OKLCH?+
HSL is not perceptually uniform: yellow at 50% lightness appears dramatically brighter to human eyes than blue at 50% lightness. Modern OKLCH organizes colors so that identical lightness values look equally bright across every hue, preventing visual artifacts in dark-mode themes.
Can I convert color codes without installing software?+
Yes. Synctoolo's Color Converter runs entirely in your web browser. You can paste HEX, RGB, or HSL strings and copy CSS variables instantly with zero latency.
We build and review free, privacy-first tools at Synctoolo.
Keep reading

Learn how to build accessible dark-mode web interfaces. Understand WCAG 2.2 contrast ratios, avoiding pure black backgrounds, and desaturating accent colors.

Build accessible, consistent color systems in CSS. Understand why OKLCH fixes HSL brightness distortions and enables wide-gamut Display P3 web palettes.