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.0193339β0.35757610.71515220.1191920β0.18043750.07217500.9503041βββRlinβGlinβBlinβββ
4. XYZ to CIELAB
Normalize XYZ by the D65 reference white:
Xnβ=0.95047,Ynβ=1.00000,Znβ=1.08883
x=XnβXβ,y=YnβYβ,z=ZnβZβ
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>296βtβ€296ββ
Compute:
fyβ=116L+16β,fxβ=fyβ+500aβ,fzβ=fyββ200bβ
X=Xnβfβ1(fxβ),Y=Ynβfβ1(fyβ),Z=Znβfβ1(fzβ)
6. XYZ to Linear RGB
βRlinβGlinβBlinβββ=β3.240970β0.9692440.055630ββ1.5373831.875968β0.203977ββ0.4986110.0415551.056972βββXYZββ
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