getLuminance method

Future<double> getLuminance ({Uint8List imageData, int skip: 1 })

This asynchronously calculates luminance for an image.

The function returns a double representing luminance from image data of Uint8List type. luminance with a brightness value between 0 for darkest and 1 for lightest. It represents the relative luminance of the color.

The function uses list of Color generated from PaletteGenerator for calculations.

Note:

  • The values this function returns is computationally very expensive to calculate. Consider using higher values for skip (which should not be more than the number of dominant colors in image) or calculating luminance of the most dominant color generated from PaletteGenerator for an image (use computeLuminance() of Color).
  • For better accuracy of brightness/luminance, use getBrightnessFrom orgetWallpaperBrightness.

Implementation

static Future<double> getLuminance(
    {Uint8List imageData, int skip = 1}) async {
  int index = 0, n = 0;
  double luminance;
  PaletteGenerator palette = await PaletteGenerator.fromUint8List(imageData);
  double totalLum = 0;
  while (index < palette.colors.length) {
    totalLum += palette.colors.toList()[index].computeLuminance();
    n += 1;
    index += skip;
  }
  print('[getLuminance] Colors counted for calculations: $n');
  luminance = totalLum / n;
  return luminance;
}