Skip to main content

RGB ↔ CIELAB Conversion Guide

This explains the full mathematical conversion pipeline between sRGB and CIELAB (Lab) color spaces.

1. Conversion Pipeline Overview

RGB β†’ CIELAB​

  1. sRGB β†’ Linear RGB
  2. Linear RGB β†’ XYZ
  3. XYZ β†’ CIELAB

CIELAB β†’ RGB​

  1. CIELAB β†’ XYZ
  2. XYZ β†’ Linear RGB
  3. Linear RGB β†’ sRGB

2. sRGB to Linear RGB

sRGB values are gamma‑compressed. Convert them to linear light:

Clin={Csrgb12.92,Csrgb≀0.04045(Csrgb+0.0551.055)2.4,Csrgb>0.04045 C_\text{lin} = \begin{cases} \frac{C_\text{srgb}}{12.92}, & C_\text{srgb} \le 0.04045 \\ \left(\frac{C_\text{srgb} + 0.055}{1.055}\right)^{2.4}, & C_\text{srgb} > 0.04045 \end{cases}

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.35757610.18043750.21267290.71515220.07217500.01933390.11919200.9503041][RlinGlinBlin] \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} = \begin{bmatrix} 0.4124564 & 0.3575761 & 0.1804375 \\ 0.2126729 & 0.7151522 & 0.0721750 \\ 0.0193339 & 0.1191920 & 0.9503041 \end{bmatrix} \begin{bmatrix} R_\text{lin} \\ G_\text{lin} \\ B_\text{lin} \end{bmatrix}

4. XYZ to CIELAB

Normalize XYZ by the D65 reference white:

Xn=0.95047,Yn=1.00000,Zn=1.08883X_n = 0.95047,\quad Y_n = 1.00000,\quad Z_n = 1.08883 x=XXn,y=YYn,z=ZZnx = \frac{X}{X_n},\quad y = \frac{Y}{Y_n},\quad z = \frac{Z}{Z_n}

Define the nonlinear function:

f(t)={t1/3,t>(629)3t3(629)2+429,t≀(629)3f(t) = \begin{cases} t^{1/3}, & t > \left(\frac{6}{29}\right)^3 \\ \frac{t}{3\left(\frac{6}{29}\right)^2} + \frac{4}{29}, & t \le \left(\frac{6}{29}\right)^3 \end{cases}

Then compute Lab:

Lβˆ—=116f(y)βˆ’16L^* = 116 f(y) - 16 aβˆ—=500[f(x)βˆ’f(y)]a^* = 500 \left[f(x) - f(y)\right] bβˆ—=200[f(y)βˆ’f(z)]b^* = 200 \left[f(y) - f(z)\right]

5. CIELAB to XYZ

The inverse of (f(t)):

fβˆ’1(t)={t3,t>6293(629)2(tβˆ’429),t≀629f^{-1}(t) = \begin{cases} t^3, & t > \frac{6}{29} \\ 3\left(\frac{6}{29}\right)^2 \left(t - \frac{4}{29}\right), & t \le \frac{6}{29} \end{cases}

Compute:

fy=L+16116,fx=fy+a500,fz=fyβˆ’b200f_y = \frac{L + 16}{116}, \quad f_x = f_y + \frac{a}{500}, \quad f_z = f_y - \frac{b}{200} X=Xnfβˆ’1(fx),Y=Ynfβˆ’1(fy),Z=Znfβˆ’1(fz)X = X_n f^{-1}(f_x),\quad Y = Y_n f^{-1}(f_y),\quad Z = Z_n f^{-1}(f_z)

6. XYZ to Linear RGB

[RlinGlinBlin]=[3.240970βˆ’1.537383βˆ’0.498611βˆ’0.9692441.8759680.0415550.055630βˆ’0.2039771.056972][XYZ]\begin{bmatrix} R_\text{lin} \\ G_\text{lin} \\ B_\text{lin} \end{bmatrix} = \begin{bmatrix} 3.240970 & -1.537383 & -0.498611 \\ -0.969244 & 1.875968 & 0.041555 \\ 0.055630 & -0.203977 & 1.056972 \end{bmatrix} \begin{bmatrix} X \\ Y \\ Z \end{bmatrix}

7. Linear RGB to sRGB

Csrgb={12.92 Clin,Clin≀0.00313081.055 Clin1/2.4βˆ’0.055,Clin>0.0031308C_\text{srgb} = \begin{cases} 12.92\, C_\text{lin}, & C_\text{lin} \le 0.0031308 \\ 1.055\, C_\text{lin}^{1/2.4} - 0.055, & C_\text{lin} > 0.0031308 \end{cases}

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