fromImage static method

Future<PaletteGenerator> fromImage(
  1. Image image, {
  2. Rect? region,
  3. int maximumColorCount = _defaultCalculateNumberColors,
  4. List<PaletteFilter> filters = const <PaletteFilter>[avoidRedBlackWhitePaletteFilter],
  5. List<PaletteTarget> targets = const <PaletteTarget>[],
})

Create a PaletteGenerator from an dart:ui.Image asynchronously.

The region specifies the part of the image to inspect for color candidates. By default it uses the entire image. Must not be equal to Rect.zero, and must not be larger than the image dimensions.

The maximumColorCount sets the maximum number of colors that will be returned in the PaletteGenerator. The default is 16 colors.

The filters specify a lost of PaletteFilter instances that can be used to include certain colors in the list of colors. The default filter is an instance of AvoidRedBlackWhitePaletteFilter, which stays away from whites, blacks, and low-saturation reds.

The targets are a list of target color types, specified by creating custom PaletteTargets. By default, this is the list of targets in PaletteTarget.baseTargets.

Implementation

static Future<PaletteGenerator> fromImage(
  ui.Image image, {
  Rect? region,
  int maximumColorCount = _defaultCalculateNumberColors,
  List<PaletteFilter> filters = const <PaletteFilter>[
    avoidRedBlackWhitePaletteFilter
  ],
  List<PaletteTarget> targets = const <PaletteTarget>[],
}) async {
  final ByteData? imageData = await image.toByteData();
  if (imageData == null) {
    throw StateError('Failed to encode the image.');
  }

  return PaletteGenerator.fromByteData(
    EncodedImage(
      imageData,
      width: image.width,
      height: image.height,
    ),
    region: region,
    maximumColorCount: maximumColorCount,
    filters: filters,
    targets: targets,
  );
}