Tuttilo
Guide

RGB vs HEX vs HSL: Complete Guide to Color Formats in CSS

Master RGB, HEX, and HSL color formats. Learn how each works, converting between them, and when to use which format for web design and CSS.

Daniele Lo Re7 min read

Understanding Color Models

Color formats represent how we define colors in digital design and code. RGB (Red, Green, Blue), HEX (Hexadecimal), and HSL (Hue, Saturation, Lightness) all describe the same colors but use different mathematical models. Understanding these models helps you work more efficiently and communicate color choices clearly.

RGB is an additive color model based on how screens produce light by combining red, green, and blue. HEX is simply a different notation for RGB using hexadecimal numbers. HSL is a cylindrical color model that describes colors in terms more intuitive to humans. All three are fully supported in modern CSS and can represent the same 16.7 million colors (24-bit color depth). The choice between them affects code readability, ease of manipulation, and workflow efficiency.

RGB: The Screen Color Model

RGB expresses colors using three values representing red, green, and blue light intensity, each ranging from 0 to 255. Pure red is rgb(255, 0, 0), pure blue is rgb(0, 0, 255), and white is rgb(255, 255, 255) where all channels are at maximum. Black is rgb(0, 0, 0) with all channels off.

RGB values directly correspond to how screens work, mixing light to produce colors. This makes RGB intuitive when thinking about screen display technology. CSS supports rgb() and rgba() functions, where the 'a' adds an alpha channel for transparency (0 to 1). For example, rgba(255, 0, 0, 0.5) is semi-transparent red. RGB is easy to understand for programmers comfortable with numerical values and works well when you need to manipulate individual color channels programmatically. However, creating color variations like "a lighter version of this blue" requires mental math or tools.

HEX: The Developer's Shorthand

HEX color codes represent RGB values in hexadecimal notation, using base-16 numbers (0-9 and A-F). A HEX color starts with # followed by six characters: two for red, two for green, two for blue. #FF0000 is pure red (FF=255 in decimal), #0000FF is pure blue, #FFFFFF is white, and #000000 is black.

HEX codes can be shortened to three characters when each color channel has duplicate digits: #F00 equals #FF0000. This shorthand is common for colors like #FFF (white) or #000 (black). HEX is the most compact color notation and has been the traditional standard in web development. It's easy to copy and paste, and tools like color pickers typically output HEX codes. However, HEX doesn't support transparency in CSS (though some design tools use eight-character HEX codes with alpha channel). HEX values are harder to read and mentally adjust than RGB or HSL, making them less intuitive for creating color variations.

HSL: The Designer's Choice

HSL describes colors using Hue, Saturation, and Lightness. Hue is the color type (0-360 degrees on a color wheel: 0/360 is red, 120 is green, 240 is blue). Saturation is color intensity (0% is gray, 100% is full color). Lightness is brightness (0% is black, 50% is pure color, 100% is white).

HSL matches how designers think about color. To make a color lighter, you increase the lightness value. To make it more vibrant, you increase saturation. To shift the hue, you adjust the hue degree. This makes HSL incredibly powerful for creating color schemes and variations. For example, hsl(240, 100%, 50%) is pure blue; hsl(240, 100%, 75%) is a lighter blue; hsl(240, 50%, 50%) is a less saturated blue. CSS supports hsla() for transparency. HSL is perfect for programmatically generating color palettes, creating hover effects by adjusting lightness, and maintaining consistent color relationships across a design system.

Converting Between Color Formats

Converting between these formats is straightforward with tools but useful to understand conceptually. HEX to RGB is simple: convert each pair of hexadecimal digits to decimal. #FF5733 becomes rgb(255, 87, 51) where FF=255, 57=87, 33=51. RGB to HEX reverses this process, converting decimal to hexadecimal.

RGB to HSL is more complex, involving finding the maximum and minimum RGB values, calculating lightness, then saturation, then hue based on which channel is dominant. HSL to RGB requires different formulas depending on the hue range. Rather than doing these calculations manually, use tools like Tuttilo's color converter to switch between formats instantly. Design tools like Figma and Adobe XD display all three formats simultaneously. Browser developer tools let you click color values to cycle between RGB, HEX, and HSL formats. Modern CSS also supports color-mix() and relative color syntax for creating variations without manual conversion.

When to Use Each Format

Use HEX when you want concise, portable color codes that copy and paste easily. HEX is standard in design handoffs and style guides. It's perfect for static colors that won't be manipulated programmatically. Stick with HEX for brand colors, exact color specifications, and when working with designers who prefer this format.

Use RGB when working with JavaScript or needing transparency. The rgba() function is more widely supported historically than HEX with alpha. RGB is also clearer when manipulating colors programmatically, as you can directly adjust channel values. Use RGB for calculations, image processing, or when interfacing with graphics libraries.

Use HSL when creating color systems, themes, or dynamic color variations. HSL makes it trivial to generate lighter/darker versions, create complementary colors, or adjust saturation across a palette. Use HSL for CSS variables in design systems, where you might define base hue and saturation while varying lightness for different UI states. HSL is ideal for accessible color systems, as you can ensure sufficient lightness contrast mathematically. For modern web development with dynamic theming or color manipulation, HSL is often the most maintainable choice.

Related Tools