getAverageColor static method

Color getAverageColor(
  1. Uint8List bytes
)

Implementation

static Color getAverageColor(Uint8List bytes) {
  final image = img.decodeImage(bytes);
  if (image == null) return Colors.black;

  int r = 0, g = 0, b = 0, count = 0;

  for (int y = 0; y < image.height; y += 10) {
    for (int x = 0; x < image.width; x += 10) {
      final pixel = image.getPixel(x, y);
      r += pixel.r.toInt();
      g += pixel.g.toInt();
      b += pixel.b.toInt();
      count++;
    }
  }

  return Color.fromARGB(255, r ~/ count, g ~/ count, b ~/ count);
}