fromImage method
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.
The image
must not be null.
Implementation
static Future<PaletteGenerator> fromImage(
ui.Image image, {
Rect region,
int maximumColorCount,
List<PaletteFilter> filters,
List<PaletteTarget> targets,
}) async {
assert(image != null);
assert(region == null || region != Rect.zero);
assert(
region == null ||
(region.topLeft.dx >= 0.0 && region.topLeft.dy >= 0.0),
'Region $region is outside the image ${image.width}x${image.height}');
assert(
region == null ||
(region.bottomRight.dx <= image.width &&
region.bottomRight.dy <= image.height),
'Region $region is outside the image ${image.width}x${image.height}');
filters ??= <PaletteFilter>[avoidRedBlackWhitePaletteFilter];
maximumColorCount ??= _defaultCalculateNumberColors;
final _ColorCutQuantizer quantizer = _ColorCutQuantizer(
image,
maxColors: maximumColorCount,
filters: filters,
region: region,
);
final List<PaletteColor> colors = await quantizer.quantizedColors;
return PaletteGenerator.fromColors(
colors,
targets: targets,
);
}