xyz2rgb static method

List<int> xyz2rgb(
  1. num x,
  2. num y,
  3. num z
)

Implementation

static List<int> xyz2rgb(num x, num y, num z) {
  if (x <= 1 && y <= 1 && z <= 1) {
    // if both <=1 we assume that are 0-1 range, NOTE that means if
    // you REALLY want (0.01 then you need to pass it like that and
    // not as 1 in to 100 scale)
    x *= 100;
    y *= 100;
    z *= 100;
  }
  x = x / 100;
  y = y / 100;
  z = z / 100;
  num r;
  num g;
  num b;

  r = (x * 3.2404542) + (y * -1.5371385) + (z * -0.4985314);
  g = (x * -0.969266) + (y * 1.8760108) + (z * 0.041556);
  b = (x * 0.0556434) + (y * -0.2040259) + (z * 1.0572252);

  // Assume sRGB
  r = (r > 0.0031308) ? ((1.055 * pow(r, (1.0 / 2.4))) - 0.055) : r * 12.92;

  g = g > 0.0031308 ? ((1.055 * pow(g, (1.0 / 2.4))) - 0.055) : g * 12.92;

  b = b > 0.0031308 ? ((1.055 * pow(b, (1.0 / 2.4))) - 0.055) : b * 12.92;

  r = min(max(0, r), 1);
  g = min(max(0, g), 1);
  b = min(max(0, b), 1);

  return [(r * 255).round(), (g * 255).round(), (b * 255).round()];
}