Color Space Conversions - Quick Reference
Fast lookup for color space conversion formulas and workflows.
Conversion Workflow
Quick Conversion Table
| From | To | Steps | Key Formula |
|---|---|---|---|
| sRGB | LAB | 1. Linearize 2. RGB→XYZ 3. XYZ→LAB | See RGB & Gamma, CIE XYZ, CIE LAB |
| sRGB | HSV | 1. Normalize 2. RGB→HSV | See HSV/HSL |
| LAB | sRGB | 1. LAB→XYZ 2. XYZ→RGB 3. Gamma correct | Reverse of above |
| HSV | sRGB | 1. HSV→RGB 2. Denormalize | See HSV/HSL |
Common Conversions
sRGB (8-bit) to LAB
1def srgb8_to_lab(r, g, b):
2 # 1. Normalize to [0, 1]
3 r, g, b = r/255, g/255, b/255
4
5 # 2. Linearize (gamma decode)
6 r_lin = srgb_to_linear(r)
7 g_lin = srgb_to_linear(g)
8 b_lin = srgb_to_linear(b)
9
10 # 3. RGB to XYZ
11 x, y, z = rgb_to_xyz(r_lin, g_lin, b_lin)
12
13 # 4. XYZ to LAB
14 L, a, b = xyz_to_lab(x, y, z)
15
16 return L, a, b
LAB to sRGB (8-bit)
1def lab_to_srgb8(L, a, b):
2 # 1. LAB to XYZ
3 x, y, z = lab_to_xyz(L, a, b)
4
5 # 2. XYZ to linear RGB
6 r_lin, g_lin, b_lin = xyz_to_rgb(x, y, z)
7
8 # 3. Gamma encode
9 r = linear_to_srgb(r_lin)
10 g = linear_to_srgb(g_lin)
11 b = linear_to_srgb(b_lin)
12
13 # 4. Denormalize and clamp
14 r = int(np.clip(r * 255, 0, 255))
15 g = int(np.clip(g * 255, 0, 255))
16 b = int(np.clip(b * 255, 0, 255))
17
18 return r, g, b
sRGB to HSV
1def srgb8_to_hsv(r, g, b):
2 # Normalize
3 r, g, b = r/255, g/255, b/255
4
5 # Convert
6 h, s, v = rgb_to_hsv(r, g, b)
7
8 return h, s, v # h in [0, 360), s, v in [0, 1]
Transformation Matrices
sRGB to XYZ (D65)
$$ \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} = \begin{bmatrix} 0.4124 & 0.3576 & 0.1805 \\ 0.2126 & 0.7152 & 0.0722 \\ 0.0193 & 0.1192 & 0.9505 \end{bmatrix} \begin{bmatrix} R_{linear} \\ G_{linear} \\ B_{linear} \end{bmatrix} $$
XYZ to sRGB (D65)
$$ \begin{bmatrix} R_{linear} \\ G_{linear} \\ B_{linear} \end{bmatrix} = \begin{bmatrix} 3.2406 & -1.5372 & -0.4986 \\ -0.9689 & 1.8758 & 0.0415 \\ 0.0557 & -0.2040 & 1.0570 \end{bmatrix} \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} $$
Gamma Functions
sRGB Gamma Decode (sRGB → Linear)
$$ C_{linear} = \begin{cases} \frac{C_{sRGB}}{12.92} & \text{if } C_{sRGB} \leq 0.04045 \ \left(\frac{C_{sRGB} + 0.055}{1.055}\right)^{2.4} & \text{otherwise} \end{cases} $$
sRGB Gamma Encode (Linear → sRGB)
$$ C_{sRGB} = \begin{cases} 12.92 \times C_{linear} & \text{if } C_{linear} \leq 0.0031308 \ 1.055 \times (C_{linear})^{1/2.4} - 0.055 & \text{otherwise} \end{cases} $$
LAB Cylindrical (LCH)
Convert LAB to polar coordinates for intuitive hue/chroma manipulation:
$$ C^* = \sqrt{(a^)^2 + (b^)^2} $$
$$ H^* = \text{atan2}(b^, a^) \times \frac{180}{\pi} $$
Reverse:
$$ a^* = C^* \times \cos(H^* \times \frac{\pi}{180}) $$
$$ b^* = C^* \times \sin(H^* \times \frac{\pi}{180}) $$
Common Pitfalls Checklist
- Always linearize sRGB before converting to XYZ
- Apply gamma correction after converting from XYZ to RGB
- Clamp RGB values to [0, 1] or [0, 255]
- Use correct illuminant (D65 for sRGB)
- Handle out-of-gamut colors appropriately
- Normalize RGB to [0, 1] before HSV/HSL conversion
- Check for division by zero in HSV/HSL calculations
Performance Tips
- Vectorize operations: Use NumPy for batch conversions
- Pre-compute matrices: Store transformation matrices as constants
- Use lookup tables: For gamma correction in real-time applications
- Avoid unnecessary conversions: Stay in one space as long as possible
- Use integer math: For 8-bit operations when precision allows
Complete Conversion Example
1import numpy as np
2
3# Complete pipeline: sRGB → LAB → Modify → sRGB
4def adjust_lightness(r, g, b, lightness_delta):
5 """Adjust lightness in perceptually uniform LAB space"""
6 # Forward: sRGB → LAB
7 r_norm, g_norm, b_norm = r/255, g/255, b/255
8 r_lin = srgb_to_linear(r_norm)
9 g_lin = srgb_to_linear(g_norm)
10 b_lin = srgb_to_linear(b_norm)
11 x, y, z = rgb_to_xyz(r_lin, g_lin, b_lin)
12 L, a, b_lab = xyz_to_lab(x, y, z)
13
14 # Modify
15 L = np.clip(L + lightness_delta, 0, 100)
16
17 # Reverse: LAB → sRGB
18 x, y, z = lab_to_xyz(L, a, b_lab)
19 r_lin, g_lin, b_lin = xyz_to_rgb(x, y, z)
20 r_norm = linear_to_srgb(r_lin)
21 g_norm = linear_to_srgb(g_lin)
22 b_norm = linear_to_srgb(b_lin)
23 r_out = int(np.clip(r_norm * 255, 0, 255))
24 g_out = int(np.clip(g_norm * 255, 0, 255))
25 b_out = int(np.clip(b_norm * 255, 0, 255))
26
27 return r_out, g_out, b_out
Reference Links
- RGB & Gamma Correction
- CIE XYZ Color Space
- CIE LAB Color Space
- HSV/HSL Conversions
- Standard Illuminants
- Color Difference Formulas
External Resources
Related Snippets
- CIE LAB Color Space
Perceptually uniform color space for color difference calculations - CIE XYZ Color Space
Device-independent color representation based on human vision - Color Difference Formulas
Delta E and perceptual color distance calculations - HSV/HSL Color Spaces
Intuitive color representations for user interfaces and color pickers - RGB Color Space & Gamma Correction
sRGB color model and gamma encoding/decoding formulas - Standard Illuminants & Chromatic Adaptation
Reference white points, color temperature, and chromatic adaptation transforms