fromByteData static method
- EncodedImage encodedImage, {
- Rect? region,
- int maximumColorCount = _defaultCalculateNumberColors,
- List<
PaletteFilter> filters = const <PaletteFilter>[avoidRedBlackWhitePaletteFilter], - List<
PaletteTarget> targets = const <PaletteTarget>[],
Create a PaletteGenerator
asynchronously from encoded image ByteData,
width, and height. These parameters are packed in EncodedImage.
The image encoding must be RGBA with 8 bits per channel, this corresponds to ImageByteFormat.rawRgba or ImageByteFormat.rawStraightRgba.
In contast with fromImage and fromImageProvider this method can be used
in non-root isolates, because it doesn't involve interaction with the
dart:ui
library, which is currently not supported, see https://github.com/flutter/flutter/issues/10647.
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> fromByteData(
EncodedImage encodedImage, {
Rect? region,
int maximumColorCount = _defaultCalculateNumberColors,
List<PaletteFilter> filters = const <PaletteFilter>[
avoidRedBlackWhitePaletteFilter
],
List<PaletteTarget> targets = const <PaletteTarget>[],
}) async {
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 ${encodedImage.width}x${encodedImage.height}');
assert(
region == null ||
(region.bottomRight.dx <= encodedImage.width &&
region.bottomRight.dy <= encodedImage.height),
'Region $region is outside the image ${encodedImage.width}x${encodedImage.height}');
assert(
encodedImage.byteData.lengthInBytes ~/ 4 ==
encodedImage.width * encodedImage.height,
"Image byte data doesn't match the image size, or has invalid encoding. "
'The encoding must be RGBA with 8 bits per channel.',
);
final _ColorCutQuantizer quantizer = _ColorCutQuantizer(
encodedImage,
maxColors: maximumColorCount,
filters: filters,
region: region,
);
final List<PaletteColor> colors = await quantizer.quantizedColors;
return PaletteGenerator.fromColors(
colors,
targets: targets,
);
}