How to Convert CIELAB to HEX
Converting a CIELAB (Lab) color to a HEX code requires translating a color defined by human perception into a format designed for digital screens. Lab describes how a color looks, while HEX tells a monitor exactly how much red, green, and blue light to emit.
The Gamut Problem
Lab is a device-independent color space that encompasses every color the human eye can see. HEX codes are tied to the sRGB color space, which is much smaller. Because of this, many valid Lab colors simply cannot be displayed on a standard monitor.
When converting from Lab to HEX, if the color falls outside the sRGB gamut, the conversion algorithm must clamp or map the color to the nearest available sRGB value. This means converting a highly saturated Lab color to HEX and back to Lab might result in a different set of numbers.
The Conversion Pipeline
There is no direct formula to go from Lab to HEX. The color must pass through several intermediate color spaces:
- Lab to XYZ: The
L,a, andbvalues are transformed into the CIE XYZ reference space using a D50 white point. - XYZ to Linear RGB: The XYZ coordinates are multiplied by a transformation matrix to get linear red, green, and blue light values.
- Gamma Correction: The linear RGB values are passed through the sRGB gamma curve to match how monitors display light.
- RGB to HEX: The final RGB decimals (0-255) are converted into base-16 hexadecimal strings and combined.