How to Convert Between HEX, RGB, and HSL Color Codes

How to Convert Between HEX, RGB, and HSL Color Codes

Colors in web development come in more flavors than a paint store. HEX, RGB, HSL โ€” they all describe the same thing, but each format serves a different purpose. Knowing when to use which format, and how to convert between them, saves you from copying values into Google and hoping for the best.

HEX, RGB, and HSL are different formats for the same color โ€” each suited for different tasks. HEX is compact and universal, RGB is precise for programming, and HSL matches how humans think about color. Master the manual conversion math and understand HSL's perceptual trap to build better design systems and accessible interfaces.

HEX: the web native format

HEX codes like #2563eb are the most common color format on the web. Each pair of characters represents red, green, and blue values in hexadecimal. HEX is compact, universally supported, and easy to copy-paste. But it is terrible for making adjustments โ€” if you want "the same blue but 20 percent lighter," you cannot do that math in your head.

RGB: the programmer format

RGB uses decimal numbers from 0 to 255 for each channel: rgb(37, 99, 235). CSS also supports rgba() with an alpha channel for transparency. The ToolStand Color Code Converter instantly translates between HEX and RGB โ€” enter one, get the other.

HSL: the designer format

HSL stands for Hue, Saturation, Lightness. You pick a hue (0-360 degrees on the color wheel), adjust saturation (how vivid), and set lightness (black to white). HSL is intuitive for design work because it maps to how humans think about color. Want a lighter version? Increase lightness. Want a more muted version? Decrease saturation.

Building color schemes

Once you have a base color, the Color Palette Generator creates harmonious schemes using color theory: complementary, analogous, triadic, and monochromatic. Pick your base color in any format, and the generator produces a full palette with HEX codes ready to copy.

Practical workflow

You find a brand color in HEX: #7c3aed. You want a lighter variant for hover states. Step 1: Paste it into the Color Converter. Note the HSL values. Step 2: Increase Lightness for your hover variant. The converter shows the new HEX. Step 3: Open the Color Palette Generator for a complementary color. You have built a design system in under a minute.

Manual conversion math โ€” from HEX to RGB and back without a converter

The conversion between HEX and RGB is straightforward arithmetic. A HEX code like #2563eb has three pairs: 25 (red), 63 (green), eb (blue). Convert each pair from base-16 (hexadecimal) to base-10: 0x25 = 2ร—16 + 5 = 37, 0x63 = 6ร—16 + 3 = 99, 0xeb = 14ร—16 + 11 = 235. So #2563eb converts to rgb(37, 99, 235). Going backwards, take each decimal value, divide by 16: 37 รท 16 = 2 remainder 5 โ†’ hex "25"; 99 รท 16 = 6 remainder 3 โ†’ hex "63"; 235 รท 16 = 14 remainder 11 โ†’ hex "eb" (where 10=a, 11=b, 12=c, 13=d, 14=e, 15=f).

The conversion from RGB to HSL is more involved โ€” it requires finding the maximum and minimum of the three RGB values, calculating lightness (the midpoint between max and min), saturation (the range relative to lightness), and hue (which primary color the value is closest to, mapped to a 0โ€“360 degree angle). The Color Code Converter does all of this instantly, but understanding the math helps you predict how colors change when you adjust each parameter.

The HSL perceptual trap โ€” why lightness is not brightness

A common trap in HSL is assuming that equal lightness values produce colors that look equally bright. Pure blue at lightness 50 percent (hsl(240, 100%, 50%)) looks noticeably darker than pure yellow at the same lightness (hsl(60, 100%, 50%)). This is because the human eye's sensitivity varies by wavelength โ€” we perceive green and yellow as brighter than blue and purple at the same measured luminance.

The HSL model treats hue channels uniformly, which makes it mathematically elegant but perceptually misleading. For accessible design, rely on luminance contrast rather than HSL lightness alone. The WCAG contrast ratio formula uses relative luminance calculated from the sRGB color space โ€” not HSL values. Practical rule: if you raise lightness to 50% and the text is still hard to read on a white background, it is not a failure of HSL but a perceptual mismatch. Use the contrast-checking features of the Color Converter to verify actual readability.

WCAG accessibility and color โ€” contrast ratios, APCA, and simulation tools

Accessible color design starts with sufficient contrast. The WCAG 2.1 AA standard requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (18px bold or 24px regular). AAA level requires 7:1 and 4.5:1 respectively. These ratios are calculated from the relative luminance of foreground and background colors โ€” not from their perceived brightness.

The upcoming WCAG 3.0 introduces APCA (Accessible Perceptual Contrast Algorithm), which replaces the simple ratio with a perceptually uniform contrast metric. APCA accounts for font size, font weight, and color context more accurately than the current formula. To future-proof your designs, target APCA values of 90+ for body text and 75+ for large text. Use the Color Converter to test both current WCAG ratios and APCA values. Additionally, simulate color blindness (deuteranopia, protanopia, tritanopia) to ensure your color scheme communicates information through contrast, not just hue โ€” the Color Blindness Simulator helps with this.

CSS variable management โ€” building a maintainable color system with HSL

HSL is the superior format for CSS custom properties because you can derive an entire palette from a few base variables. Define primary hue as a custom property: --primary-h: 240; then generate variants: --primary-500: hsl(var(--primary-h), 80%, 50%); --primary-300: hsl(var(--primary-h), 70%, 70%); --primary-700: hsl(var(--primary-h), 90%, 30%). Changing one number โ€” the hue โ€” shifts your entire brand color system.

This technique enables programmatic theming. A dark mode toggle can invert lightness with a single CSS rule: [data-theme='dark'] { --bg-lightness: 15%; --text-lightness: 85%; }. Component backgrounds use calc() against these variables, adapting automatically without redeclaring every color. The Color Palette Generator outputs HSL values ready for CSS variable use, with consistent saturation across all shades.

Explore all 109 free tools at toolstand.io. Free, forever. No sign-up. No download. Just tools that work.