设计师必知的颜色理论:从HEX、RGB到HSL的完全指南

发布于:2026年5月9日 | 阅读时间:约8分钟

在屏幕上,每一种颜色都是由数字精确控制的。无论是网页设计、UI开发还是数字绘画,你一定会遇到三种最主流的颜色表示方式:HEX(十六进制)RGB(红绿蓝)HSL(色相‑饱和度‑亮度)。它们本质上描述的都是同一个东西——颜色,但各自的视角和使用场景完全不同。理解它们背后的数学原理,能让你在设计时更精准地选择、调整和转换颜色。

核心公式:HEX是RGB的十六进制简写,RGB是显示器的原生加法模型,HSL则是对RGB进行了一次极坐标变换,让颜色调整更符合人类的直觉。本文将从数学原理出发,带你彻底理清三者的关系,并使用我们的 色值转换器 进行实操。

一、RGB:显示器原生色彩模型

RGB(Red‑Green‑Blue)是最底层的颜色表示方法。每个像素点都由红、绿、蓝三种发光体组成,通过控制每种光的强度(0 到 255),可以混合出约 1677 万种颜色。这是加法混色——三原色越强,最终颜色越接近白色(255, 255, 255)。

在CSS中,RGB表示为 rgb(59, 130, 246)。这个数值直接对应屏幕硬件的电压控制,因此处理图像、视频和动画时,RGB是最直接的模型。

二、HEX:RGB的紧凑十六进制写法

HEX(Hexadecimal)只是RGB的另一种写法,纯粹是为了节省空间和方便复制。它将每个通道的十进制数值(0‑255)转换为两位十六进制数(00‑FF),然后拼接成一个以 # 开头的六位字符串。

HEX 编码规则 R(十进制) → 两位十六进制
G(十进制) → 两位十六进制
B(十进制) → 两位十六进制
最终 HEX = #RRGGBB

示例:rgb(59, 130, 246) → 59(十进制) = 3b, 130 = 82, 246 = f6 → #3b82f6。这种形式书写更短,因此在CSS和设计软件中被广泛使用。

三、HSL:最符合人类直觉的颜色模型

RGB和HEX的问题在于:如果你想把一个颜色调得更暗一些,应该同时减少R、G、B多少?这并不直观。HSL(Hue‑Saturation‑Lightness)正是为了解决这个问题而设计的。

HSL将颜色分解为三个独立的维度:

HSL本质上是将RGB的立方体空间变换为一个圆柱体空间,使得颜色的“明暗”和“鲜艳度”可以独立调节。

四、RGB到HSL的数学转换

这是计算机科学中经典的色彩空间变换。首先将RGB归一化到0~1范围,然后计算亮度、饱和度,最后通过分段函数计算色相。

RGB → HSL 算法 1. 归一化:R' = R/255, G' = G/255, B' = B/255
2. 找到最大值 Cmax 和最小值 Cmin,差值 Δ = Cmax - Cmin
3. 亮度 L = (Cmax + Cmin) / 2
4. 饱和度 S:如果 Δ=0 则 S=0;否则 S = Δ / (1 - |2L - 1|)
5. 色相 H:根据 Cmax 所在通道分六种情况计算角度,结果乘以60°得到0°~360°

演算示例:将 rgb(59, 130, 246) 转换为 HSL。

R'=0.231, G'=0.510, B'=0.965。Cmax=0.965, Cmin=0.231, Δ=0.734。
L = (0.965+0.231)/2 = 0.598 → 60%。
S = 0.734 / (1 - |2×0.598 - 1|) = 0.734 / (1 - 0.196) ≈ 0.913 → 91%。
Cmax=蓝色,所以 H = 60 × (4 + (0.231-0.510)/0.734) ≈ 217°。
最终结果:hsl(217, 91%, 60%)。这正是我们最初的颜色。

你可以使用我们的 色值转换器 来验证任意颜色的转换过程。

五、HSL到RGB的逆转换

从HSL还原为RGB同样遵循一套明确的数学规则。给定 H, S, L,首先计算中间量色度 C、二次色 X 和亮度偏移 m,然后根据色相所在扇区分配到 R、G、B 通道。

HSL → RGB 算法 1. C = (1 - |2L - 1|) × S
2. X = C × (1 - |(H / 60°) mod 2 - 1|)
3. m = L - C/2
4. 根据 H 的区段,将 (R', G', B') 设置为 (C, X, 0) 的排列组合
5. 最终 R = round((R' + m) × 255),G、B 同理

这套算法被嵌入到所有主流浏览器和设计软件中,你在CSS里写 `hsl(217, 91%, 60%)`,浏览器会自动完成上述计算并驱动屏幕显示正确的颜色。

六、什么时候用哪种格式?

场景推荐格式原因
复制粘贴设计稿色值HEX简洁、一致,所有设计软件都支持
需要动态调整透明度RGB可以写成 rgba(r, g, b, a)
创建调色板或变体HSL修改亮度或饱和度即可生成浅色/深色版本
CSS变量和主题切换HSL只需修改变量的 L 值即可实现暗色模式

现代的CSS甚至支持在HEX格式中直接写透明度(如 `#3b82f680`),但HSL仍然是生成色彩系统时最灵活的选择。例如,通过保持色相不变,只递减亮度,你可以生成一套从深到浅的蓝色色阶——这对按钮悬停状态、卡片背景等设计元素至关重要。

常见问题

为什么我的HEX颜色输入#3b82f6后,在CSS中看起来不一样?

HEX色值不受显示器校准影响,但不同屏幕的色域覆盖和亮度设置不同,可能导致视觉差异。代码本身是正确的,差异来自硬件。

如何快速在不改变色相的情况下调暗一个颜色?

使用HSL格式,只需降低亮度(L)值即可。例如 `hsl(217, 91%, 40%)` 会比 `60%` 更暗,但色相和饱和度保持不变。这是HEX/RGB做不到的简单操作。

HEX、RGB和HSL哪个更适合用于动画?

HSL更适合在动画中创建渐变和色彩循环。通过递增色相(Hue),你可以让颜色在色轮上平滑旋转,生成彩虹般的动画效果。

Color Theory for Designers: A Complete Guide to HEX, RGB, and HSL

Published: May 9, 2026 | Reading time: ~8 min

Every color on a screen is controlled by numbers. In web design, UI development, or digital painting, you inevitably encounter three major color formats: HEX, RGB, and HSL. They all describe the same thing — color — but from different perspectives. Understanding the math behind them helps you choose, adjust, and convert colors with precision.

Key concept: HEX is a compact hexadecimal shorthand for RGB. RGB is the native additive model of displays. HSL transforms RGB into a cylindrical coordinate system, making color adjustments far more intuitive. Use our Color Converter to experiment with these conversions.

1. RGB: The Display's Native Color Model

RGB (Red‑Green‑Blue) is the lowest‑level color representation. Each pixel is formed by three light emitters whose intensity ranges from 0 to 255, mixing approximately 16.77 million colors. This is additive mixing — the stronger the primaries, the closer the result is to white (255, 255, 255).

In CSS, RGB is written as rgb(59, 130, 246). Because it directly corresponds to the hardware driving the screen, it's the most direct model for images, video, and animations.

2. HEX: A Compact Shorthand for RGB

HEX (Hexadecimal) is simply an alternative notation for RGB, designed for compactness and easy copying. It converts each channel's decimal value (0‑255) into a two‑digit hex number (00‑FF) and concatenates them with a # prefix.

HEX Encoding R (decimal) → two‑digit hex
G (decimal) → two‑digit hex
B (decimal) → two‑digit hex
Result: #RRGGBB

Example: rgb(59, 130, 246) → 59 = 3b, 130 = 82, 246 = f6 → #3b82f6. This shorter form is widely used in CSS and design tools.

3. HSL: The Most Human‑Intuitive Color Model

The problem with RGB and HEX is that if you want to make a color darker, how much should you reduce R, G, and B? It's not intuitive. HSL (Hue‑Saturation‑Lightness) was designed to solve this.

HSL separates color into three independent dimensions:

HSL is essentially a transformation of the RGB cube into a cylindrical space, allowing independent control over a color's lightness and vividness.

4. Converting RGB to HSL Mathematically

This is a classic color space transformation. First, normalize RGB to the 0–1 range, then compute lightness, saturation, and finally hue through a piecewise function.

RGB → HSL Algorithm 1. Normalize: R' = R/255, G' = G/255, B' = B/255
2. Find max (Cmax), min (Cmin), and delta Δ = Cmax - Cmin
3. Lightness L = (Cmax + Cmin) / 2
4. Saturation S = 0 if Δ=0, else S = Δ / (1 - |2L - 1|)
5. Hue H: determine which channel is Cmax, then compute the angle and multiply by 60° to obtain 0°–360°

Worked example: convert rgb(59, 130, 246) to HSL.

R'=0.231, G'=0.510, B'=0.965. Cmax=0.965, Cmin=0.231, Δ=0.734.
L = (0.965+0.231)/2 = 0.598 → 60%.
S = 0.734 / (1 - |2×0.598 - 1|) = 0.734 / (1 - 0.196) ≈ 0.913 → 91%.
Cmax = blue, so H = 60 × (4 + (0.231-0.510)/0.734) ≈ 217°.
Final result: hsl(217, 91%, 60%). This is our original color. Try the Color Converter to verify any conversion.

5. Converting HSL Back to RGB

Given H, S, L, compute intermediate values: chroma C, secondary component X, and lightness offset m, then assign to R, G, B based on the hue sector.

HSL → RGB Algorithm 1. C = (1 - |2L - 1|) × S
2. X = C × (1 - |(H / 60°) mod 2 - 1|)
3. m = L - C/2
4. Based on H sector, assign (R', G', B') as permutations of (C, X, 0)
5. R = round((R' + m) × 255), same for G and B

This algorithm is embedded in all major browsers and design software. When you write hsl(217, 91%, 60%) in CSS, the browser performs this computation in real time.

6. When to Use Which Format?

ScenarioRecommended FormatReason
Copy‑pasting from design filesHEXCompact, universal across tools
Adding transparencyRGBUse rgba(r, g, b, a)
Creating color palettes or variantsHSLAdjust lightness or saturation independently
CSS variables & theme switchingHSLChange only the L value for dark mode

Modern CSS even supports alpha in HEX (e.g., `#3b82f680`), but HSL remains the most flexible for generating systematic color scales. Keeping the hue constant and decreasing lightness creates a smooth gradient of shades — essential for button hover states, card backgrounds, and more.

FAQ

Why does my HEX color #3b82f6 look different in CSS?

HEX values are screen‑independent, but different displays have varying color gamuts and brightness settings, causing visual discrepancies. The code is correct; the difference is in the hardware.

How can I quickly darken a color without changing its hue?

Use HSL and reduce the Lightness value. For example, hsl(217, 91%, 40%) is darker than 60% while keeping the same hue and saturation — something HEX/RGB cannot easily achieve.

Which format is best for animations?

HSL excels in animations. By incrementing Hue, you can smoothly rotate through the color wheel, creating rainbow‑like effects that are cumbersome with RGB or HEX.