computeAverageBrightness function

double computeAverageBrightness(
  1. Image image
)

Implementation

double computeAverageBrightness(img.Image image) {
  double totalBrightness = 0;
  final int width = image.width;
  final int height = image.height;

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      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 to grayscale using luminosity method
      final gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

      totalBrightness += gray;
    }
  }

  // Calculate the average brightness
  return totalBrightness / (width * height);
}