xy2rgb static method

List<int> xy2rgb(
  1. double x,
  2. double y, [
  3. double brightness = 1.0
])

Converts xy coordinates in the CIE 1931 color space to RGB.

Returns a list of doubles representing the RGB values. r, g, b

Implementation

static List<int> xy2rgb(double x, double y, [double brightness = 1.0]) {
  assert(x > 0.0 && x <= 1.0,
      "x must be greater than 0 and less than or equal to 1");
  assert(y > 0.0 && y <= 1.0,
      "y must be greater than 0 and less than or equal to 1");

  final double z = 1.0 - x - y;
  final double Y = brightness;
  final double X = (Y / y) * x;
  final double Z = (Y / y) * z;

  // sRGB D65 conversion
  double r = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
  double g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
  double b = X * 0.051713 - Y * 0.121364 + Z * 1.011530;

  // Apply reverse gamma correction.
  r = _reverseGammaCorrection(r);
  g = _reverseGammaCorrection(g);
  b = _reverseGammaCorrection(b);

  // If one of the values is greater than 1, the color is too saturated and
  // needs to be brought back down.
  if (r > b && r > g && r > 1.0) {
    g = g / r;
    b = b / r;
    r = 1.0;
  } else if (g > b && g > r && g > 1.0) {
    r = r / g;
    b = b / g;
    g = 1.0;
  } else if (b > r && b > g && b > 1.0) {
    r = r / b;
    g = g / b;
    b = 1.0;
  }

  // Make sure none of the values are less than 0.
  if (r < 0.0) r = 0.0;
  if (g < 0.0) g = 0.0;
  if (b < 0.0) b = 0.0;

  // Convert to 0-255 range.
  r = r * 255;
  g = g * 255;
  b = b * 255;

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