第 3 章 · 第 1 节Chapter 3 · Section 1 基础Core
3.1 颜色模型基础Color models
区分 RGB、HSL、Lab 与 CMYK,认识颜色数值与视觉感受的差异。Distinguish RGB, HSL, Lab and CMYK, and relate numerical color to perception.
本节要点Key points
- RGB、HSL、Lab、CMYK 是同一颜色的不同坐标表示。RGB, HSL, Lab and CMYK are different coordinate representations of the same color.
- HSL 便于调参但不感知均匀;Lab 近似人眼色差;CMYK 依赖印刷条件。HSL is convenient for adjustment but not perceptually uniform. Lab approximates perceptual difference. CMYK depends on printing conditions.
- 换模型不会改变屏幕上的颜色,只会改变数值和可操作方式。Changing the model does not change the on-screen color; it changes the numbers and the operations available.
01理解概念Understand the concept
为什么要学颜色模型?
设计 colormap 时,不能直接用 RGB 插值——因为 RGB 不是感知均匀的(255,0,0 与 0,255,0 "看上去"亮度差很多)。你需要感知均匀的颜色空间(Lab / HCL)来插值,才能得到平滑的渐变。
4 大颜色空间
- RGB:加色模型,设备相关。屏幕显示的默认。
- HSL:艺术家友好(色相/饱和度/亮度独立),但仍不感知均匀。
- CIE Lab:感知均匀,设计 colormap 的黄金标准。
- CMYK:减色(印刷),web 可视化用不到。
CVD(色觉缺陷)
约 8% 男性、0.5% 女性是色盲。常见三种:
- 红色盲(protanopia):约 1% 男性
- 绿色盲(deuteranopia):约 6% 男性(最常见)
- 蓝色盲(tritanopia):极罕见
Why learn color models?
When designing a colormap, do not interpolate directly in RGB—because RGB is not perceptually uniform (255,0,0 and 0,255,0 "look" very different in lightness). You need a perceptually uniform color space (Lab / HCL) to interpolate and get smooth gradients.
The 4 major color spaces
- RGB: additive model, device-dependent. The default for screen display.
- HSL: artist-friendly (independent hue / saturation / lightness), but still not perceptually uniform.
- CIE Lab: perceptually uniform, the gold standard for designing colormaps.
- CMYK: subtractive (print). Not used in web visualizations.
CVD (Color Vision Deficiency)
About 8% of men and 0.5% of women are color-blind. Three common types:
- Protanopia (red-blind): ~1% of men
- Deuteranopia (green-blind): ~6% of men (most common)
- Tritanopia (blue-blind): extremely rare
02动手实验Try the experiment
试一试:Try this: 拖动 RGB 滑块,观察同一颜色在不同模型中的数值,以及近似色觉缺陷模拟的变化。Move the RGB sliders and compare representations and approximate color-vision simulations.
实验将在进入此区域时加载,需要启用 JavaScript。The experiment loads when you reach this area. JavaScript is required.
色觉模拟为近似教学演示;配色仍需结合实际图形、背景与读者检查。Color-vision simulations are approximate teaching aids. Evaluate the actual chart, background and audience. Some experiment controls use Chinese labels.
做完后:你应该看到After the experiment: what to look for
03代码与讲解Code and explanation收起 / 展开Collapse / expand +
⭐ 关键代码
⭐ 关键点 1:RGB → HSL 手写
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) { h = s = 0; }
else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h * 360, s * 100, l * 100];
}
⭐ 自己写 RGB↔HSL 是为了理解每一步的物理含义——"最大通道决定色相、相减决定饱和度"——比用 d3 库更能理解色相为什么在 0-360° 循环。
⭐ 关键点 2:CVD 模拟矩阵
const CVD_MATRICES = {
deuteranopia: [
[0.625, 0.375, 0.000],
[0.700, 0.300, 0.000],
[0.000, 0.300, 0.700]
],
protanopia: [[0.567, 0.433, 0.000], [0.558, 0.442, 0.000], [0.000, 0.242, 0.758]],
tritanopia: [[0.950, 0.050, 0.000], [0.000, 0.433, 0.567], [0.000, 0.475, 0.525]]
};
function simulateCVD([r, g, b], type) {
const m = CVD_MATRICES[type];
return [
r * m[0][0] + g * m[0][1] + b * m[0][2],
r * m[1][0] + g * m[1][1] + b * m[1][2],
r * m[2][0] + g * m[2][1] + b * m[2][2]
].map(v => Math.round(Math.max(0, Math.min(255, v))));
}
⭐ 关键点 3:d3 内置多空间
const c = d3.color('rgb(100, 50, 200)');
c.rgb(); // RGB 对象
d3.hsl(c); // {h, s, l}
d3.lab(c); // {l, a, b}
d3.hcl(c); // Lab 的极坐标表达⭐ Key code
⭐ Key point 1: Hand-written RGB ↔ HSL
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) { h = s = 0; }
else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h * 360, s * 100, l * 100];
}
⭐ Writing RGB↔HSL yourself is to understand the physical meaning of each step—"the largest channel determines hue, the difference determines saturation"—which is more illuminating than using a d3 library for understanding why hue cycles between 0–360°.
⭐ Key point 2: CVD simulation matrix
const CVD_MATRICES = {
deuteranopia: [
[0.625, 0.375, 0.000],
[0.700, 0.300, 0.000],
[0.000, 0.300, 0.700]
],
protanopia: [[0.567, 0.433, 0.000], [0.558, 0.442, 0.000], [0.000, 0.242, 0.758]],
tritanopia: [[0.950, 0.050, 0.000], [0.000, 0.433, 0.567], [0.000, 0.475, 0.525]]
};
function simulateCVD([r, g, b], type) {
const m = CVD_MATRICES[type];
return [
r * m[0][0] + g * m[0][1] + b * m[0][2],
r * m[1][0] + g * m[1][1] + b * m[1][2],
r * m[2][0] + g * m[2][1] + b * m[2][2]
].map(v => Math.round(Math.max(0, Math.min(255, v))));
}
⭐ Key point 3: d3's built-in multi-space support
const c = d3.color('rgb(100, 50, 200)');
c.rgb();
d3.hsl(c); // {h, s, l}
d3.lab(c); // {l, a, b}
d3.hcl(c);04练习Quiz
学完后,可以标记进度。Mark this section when you finish.