Skip to content

lab / lch ​

Explicit CSS APIs ​

Legacy data methods, string methods and parsers keep the configured white point (D65 by default). toCssLabString() / toCssLchString() and format keys cssLab / cssLch always use CSS D50, independently of plugin reconfiguration.

ts
import { parseCssLab } from '@xpyjs/color/plugins/lab'
xcolor('#f00').toCssLabString() // D50 CSS lab(); requires the lab plugin
const rgba = parseCssLab('lab(54.29% 80.81 69.89 / .5)') // No registration required; sRGB RGBA or null

parseCssLab() parses CSS Lab/LCH with D50, preserves alpha, maps black/white endpoints and clips to sRGB. Use colorSpace records to retain out-of-gamut coordinates. Legacy lab / lch format keys keep their previous meaning.

CIE Lab and CIE LCH perceptual color space support. Lab is designed based on human visual perception and is commonly used for color difference calculations.

The plugin retains its D65 reference white for compatibility. CSS Color 4 Lab and LCH use D50, so equivalent coordinates may render differently.

Install ​

ts
import xcolor from '@xpyjs/color'
import labPlugin from '@xpyjs/color/plugins/lab'

xcolor.use(labPlugin)

Lab Methods ​

toLab() ​

Returns a Lab object.

  • l: Lightness (0–100)
  • a: Green-red axis (approximately -128 to 127)
  • b: Blue-yellow axis (approximately -128 to 127)
ts
xcolor('#ff0000').toLab()
// { l: 53.24, a: 80.09, b: 67.2 }

toLabString() ​

Returns a lab() format string.

ts
xcolor('#ff0000').toLabString()
// 'lab(53.24% 80.09 67.2)'

LCH Methods ​

toLch() ​

Returns an LCH object. LCH is the polar coordinate form of Lab.

  • l: Lightness (0–100)
  • c: Chroma
  • h: Hue (0–360)
ts
xcolor('#ff0000').toLch()
// { l: 53.24, c: 104.55, h: 40 }

toLchString() ​

Returns an lch() format string.

ts
xcolor('#ff0000').toLchString()
// 'lch(53.24% 104.55 40)'

Color Difference ​

deltaE(other) ​

Calculates the CIE76 color difference (Euclidean distance) between the current color and another color. Smaller values indicate closer colors.

ParameterTypeDescription
otherXColorInputTarget color for comparison
ts
xcolor('#ff0000').deltaE('#ff0100')  // 0.07 (nearly identical)
xcolor('#ff0000').deltaE('#0000ff')  // ≈ 176 (vastly different)

Color Difference Reference

ΔE RangePerceived Difference
0–1Nearly imperceptible
1–2Distinguishable upon close observation
2–10Noticeable at a glance
11–49Similar colors
50+Completely different

Input Parsing ​

ts
xcolor('lab(53.24 80.09 67.2)').toHex()   // '#ff0000'
xcolor('lch(53.24 104.55 40)').toHex()   // '#ff0000'

Formatting ​

ts
xcolor('#ff0000').toString('lab')  // 'lab(53.24% 80.09 67.2)'
xcolor('#ff0000').toString('lch')  // 'lch(53.24% 104.55 40)'

Released under the MIT License.