rgb2xy static method

List<double> rgb2xy(
  1. int r,
  2. int g,
  3. int b
)

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

Returns a list of doubles representing the xy values. x, y, brightness

Implementation

static List<double> rgb2xy(int r, int g, int b) {
  if (r == 0 && g == 0 && b == 0) return [0.0, 0.0, 0.0];

  // Convert to a linear RGB color space.
  double R = r / 255;
  double G = g / 255;
  double B = b / 255;

  // Apply gamma correction.
  R = _gammaCorrection(R);
  G = _gammaCorrection(G);
  B = _gammaCorrection(B);

  // Convert to XYZ using the Wide RGB D65 conversion formula.
  final double X = R * 0.4124 + G * 0.3576 + B * 0.1805;
  final double Y = R * 0.2126 + G * 0.7152 + B * 0.0722;
  final double Z = R * 0.0193 + G * 0.1192 + B * 0.9505;

  // Calculate the xy values from the XYZ values.
  final double x = X / (X + Y + Z);
  final double y = Y / (X + Y + Z);

  return [x, y, Y];
}