labToXyz static method

XyzColor labToXyz(
  1. LabColor labColor
)

Converts a XYZ color to a LAB color.

Implementation

static XyzColor labToXyz(LabColor labColor) {
  final lightness = labColor.lightness;
  final a = labColor.a;
  final b = labColor.b;

  var y = (lightness + 16) / 116;
  var x = (a / 500) + y;
  var z = y - (b / 200);

  final x3 = x * x * x;
  final z3 = z * z * z;

  x = (x3 > 0.008856) ? x3 : (116 * x - 16) / 903.3;
  y = (lightness > 0.008856 * 903.3)
      ? math.pow((lightness + 16) / 116, 3).toDouble()
      : lightness / 903.3;
  z = (z3 > 0.008856) ? z3 : (116 * z - 16) / 903.3;

  // LAB colors with their `a` values beneath and `b` values above the
  // RGB color space's bounds, such as LabColor(60, -128, 127), will
  // calculate their `x` and/or `z` values beneath 0, by at most -0.082.
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  if (z < 0) z = 0;

  final alpha = labColor.alpha / 255;

  return XyzColor.extrapolate(<double>[x, y, z, alpha]);
}