xyzInViewingConditions method

List<double> xyzInViewingConditions(
  1. ViewingConditions viewingConditions, {
  2. List<double>? array,
})

XYZ representation of CAM16 seen in viewingConditions.

Implementation

List<double> xyzInViewingConditions(ViewingConditions viewingConditions,
    {List<double>? array}) {
  final alpha =
      (chroma == 0.0 || j == 0.0) ? 0.0 : chroma / math.sqrt(j / 100.0);

  final t = math.pow(
      alpha /
          math.pow(
              1.64 -
                  math.pow(0.29, viewingConditions.backgroundYTowhitePointY),
              0.73),
      1.0 / 0.9);
  final hRad = hue * math.pi / 180.0;

  final eHue = 0.25 * (math.cos(hRad + 2.0) + 3.8);
  final ac = viewingConditions.aw *
      math.pow(j / 100.0, 1.0 / viewingConditions.c / viewingConditions.z);
  final p1 =
      eHue * (50000.0 / 13.0) * viewingConditions.nC * viewingConditions.ncb;

  final p2 = (ac / viewingConditions.nbb);

  final hSin = math.sin(hRad);
  final hCos = math.cos(hRad);

  final gamma = 23.0 *
      (p2 + 0.305) *
      t /
      (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
  final a = gamma * hCos;
  final b = gamma * hSin;
  final rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
  final gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
  final bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;

  final rCBase = math.max(0, (27.13 * rA.abs()) / (400.0 - rA.abs()));
  final rC = MathUtils.signum(rA) *
      (100.0 / viewingConditions.fl) *
      math.pow(rCBase, 1.0 / 0.42);
  final gCBase = math.max(0, (27.13 * gA.abs()) / (400.0 - gA.abs()));
  final gC = MathUtils.signum(gA) *
      (100.0 / viewingConditions.fl) *
      math.pow(gCBase, 1.0 / 0.42);
  final bCBase = math.max(0, (27.13 * bA.abs()) / (400.0 - bA.abs()));
  final bC = MathUtils.signum(bA) *
      (100.0 / viewingConditions.fl) *
      math.pow(bCBase, 1.0 / 0.42);
  final rF = rC / viewingConditions.rgbD[0];
  final gF = gC / viewingConditions.rgbD[1];
  final bF = bC / viewingConditions.rgbD[2];

  final x = 1.86206786 * rF - 1.01125463 * gF + 0.14918677 * bF;
  final y = 0.38752654 * rF + 0.62144744 * gF - 0.00897398 * bF;
  final z = -0.01584150 * rF - 0.03412294 * gF + 1.04996444 * bF;

  if (array != null) {
    array[0] = x;
    array[1] = y;
    array[2] = z;
    return array;
  } else {
    return [x, y, z];
  }
}