RGB ↔ CIELAB Conversion Guide
This explains the full mathematical conversion pipeline between sRGB and CIELAB (Lab) color spaces.
1. Conversion Pipeline Overview
RGB CIELAB
- sRGB Linear RGB
- Linear RGB XYZ
- XYZ CIELAB
CIELAB RGB
- CIELAB XYZ
- XYZ Linear RGB
- Linear RGB sRGB
2. sRGB to Linear RGB
sRGB values are gamma‑compressed. Convert them to linear light:
Clin=⎩⎨⎧12.92Csrgb,(1.055Csrgb+0.055)2.4,Csrgb≤0.04045Csrgb>0.04045
This is applied independently to (R), (G), and (B).
3. Linear RGB to XYZ
Using the sRGB color space matrix with a D65 white point:
XYZ=0.41245640.21267290.01933390.35757610.71515220.11919200.18043750.07217500.9503041RlinGlinBlin
4. XYZ to CIELAB
Normalize XYZ by the D65 reference white:
Xn=0.95047,Yn=1.00000,Zn=1.08883
x=XnX,y=YnY,z=ZnZ
Define the nonlinear function:
f(t)=⎩⎨⎧t1/3,3(296)2t+294,t>(296)3t≤(296)3
Then compute Lab:
L∗=116f(y)−16
a∗=500[f(x)−f(y)]
b∗=200[f(y)−f(z)]
5. CIELAB to XYZ
The inverse of (f(t)):
f−1(t)={t3,3(296)2(t−294),t>296t≤296
Compute:
fy=116L+16,fx=fy+500a,fz=fy−200b
X=Xnf−1(fx),Y=Ynf−1(fy),Z=Znf−1(fz)
6. XYZ to Linear RGB
RlinGlinBlin=3.240970−0.9692440.055630−1.5373831.875968−0.203977−0.4986110.0415551.056972XYZ
7. Linear RGB to sRGB
Csrgb={12.92Clin,1.055Clin1/2.4−0.055,Clin≤0.0031308Clin>0.0031308
Clamp results to ([0,1]) and scaled by 255 before converting to 8‑bit.
8. Summary
RGB Lab
- Remove gamma (sRGB linear)
- Convert to XYZ
- Normalize by D65
- Apply nonlinear transform
- Produce L*, a*, b*
Lab RGB
- Convert Lab XYZ via inverse nonlinear transform
- XYZ linear RGB
- Linear RGB sRGB (gamma)
- Clamp to valid output
9. References
- CIE 1976 L*a*b* Specification
- IEC 61966‑2‑1 sRGB Standard