Function Signature
js
rgbToLab(r: number, g: number, b: number): { L: number, a: number, b: number }Parameters
r(number): Red channel value (0–255).g(number): Green channel value (0–255).b(number): Blue channel value (0–255).
Return Value
An object containing the Lab color components: L (lightness, 0–100), a (green–red axis), and b (blue–yellow axis).
js
rgbToLab(255, 102, 0);
// { L: ~62, a: ~58, b: ~70 }
rgbToLab(0, 170, 255);
// { L: ~66, a: ~-7, b: ~-41 }
rgbToLab(255, 255, 255);
// { L: 100, a: 0, b: 0 }
rgbToLab(0, 0, 0);
// { L: 0, a: 0, b: 0 }Conversion Details
- Internally, the function first converts RGB to CIE XYZ using
rgbToXyz. - Then, the XYZ values are transformed into Lab using
xyzToLab. - The Lab model is device-independent and standardized for perceptual uniformity.
Examples
js
// Bright red
console.log(rgbToLab(255, 0, 0));
// { L: ~53, a: ~80, b: ~67 }
// Forest green
console.log(rgbToLab(34, 139, 34));
// { L: ~50, a: ~-47, b: ~45 }Usage Notes
- The Lab space is useful for measuring perceptual differences between colors (ΔE calculations).
- L ranges from 0 (black) to 100 (white).
aaxis ranges from green (negative) to red (positive).baxis ranges from blue (negative) to yellow (positive).