labToRgb function

List<int> labToRgb(
  1. num l,
  2. num a,
  3. num b
)

Convert a CIE-L*ab color to RGB.

Implementation

List<int> labToRgb(num l, num a, num b) {
  const refX = 95.047;
  const refY = 100.000;
  const refZ = 108.883;

  num y = (l + 16.0) / 116.0;
  num x = a / 500.0 + y;
  num z = y - b / 200.0;

  final y3 = pow(y, 3);
  if (y3 > 0.008856) {
    y = y3;
  } else {
    y = (y - 16 / 116) / 7.787;
  }

  final x3 = pow(x, 3);
  if (x3 > 0.008856) {
    x = x3;
  } else {
    x = (x - 16 / 116) / 7.787;
  }

  final z3 = pow(z, 3);
  if (z3 > 0.008856) {
    z = z3;
  } else {
    z = (z - 16 / 116) / 7.787;
  }

  x *= refX;
  y *= refY;
  z *= refZ;

  x /= 100.0;
  y /= 100.0;
  z /= 100.0;

  // xyz to rgb
  num R = x * 3.2406 + y * (-1.5372) + z * (-0.4986);
  num G = x * (-0.9689) + y * 1.8758 + z * 0.0415;
  num B = x * 0.0557 + y * (-0.2040) + z * 1.0570;

  if (R > 0.0031308) {
    R = 1.055 * (pow(R, 1.0 / 2.4)) - 0.055;
  } else {
    R = 12.92 * R;
  }

  if (G > 0.0031308) {
    G = 1.055 * (pow(G, 1.0 / 2.4)) - 0.055;
  } else {
    G = 12.92 * G;
  }

  if (B > 0.0031308) {
    B = 1.055 * (pow(B, 1.0 / 2.4)) - 0.055;
  } else {
    B = 12.92 * B;
  }

  return [
    (R * 255.0).clamp(0, 255).toInt(),
    (G * 255.0).clamp(0, 255).toInt(),
    (B * 255.0).clamp(0, 255).toInt()
  ];
}