pixelToGray function

double pixelToGray(
  1. Image image,
  2. int x,
  3. int y
)

Implementation

double pixelToGray(img.Image image, int x, int y) {
  if (x < 0 || x >= image.width || y < 0 || y >= image.height) return 0.0;

  final pixel = image.getPixel(x, y);

  // Assuming the pixel object has r, g, b properties
  final red = pixel.r.toDouble();
  final green = pixel.g.toDouble();
  final blue = pixel.b.toDouble();

  // // Convert the pixel to grayscale using the luminosity method
  // return (0.3 * red + 0.59 * green + 0.11 * blue) / 255.0;

  // High-quality grayscale conversion using luminance method
  // This is a refined method that considers human eye sensitivity
  final gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

  // Normalize to the range [0, 1]
  return gray / 255.0;
}