extract static method

Future<ColorPalette> extract(
  1. List<int> argbPixels, {
  2. int maxColors = 64,
  3. int desiredColors = 16,
  4. PaletteWeights weights = const PaletteWeights(),
})

Quantizes argbPixels and produces a ColorPalette.

maxColors controls how many distinct colors the quantizer keeps (higher values produce richer but slower palettes). desiredColors controls how many ranked colors are returned in ColorPalette.colors.

Implementation

static Future<ColorPalette> extract(
  List<int> argbPixels, {
  int maxColors = 64,
  int desiredColors = 16,
  PaletteWeights weights = const PaletteWeights(),
}) async {
  if (argbPixels.isEmpty) {
    throw ArgumentError.value(argbPixels, 'argbPixels', 'must not be empty');
  }

  final quantizer = QuantizerCelebi();
  final result = await quantizer.quantize(argbPixels, maxColors);

  // Score ranks the quantized colors by suitability; put them first so
  // `dominantColor` matches the recommended color.
  final ranked = Score.score(
    result.colorToCount,
    desired: desiredColors,
  ).toSet();

  final candidates = <int, int>{
    for (final argb in ranked) argb: result.colorToCount[argb] ?? 0,
  };
  for (final entry in result.colorToCount.entries) {
    candidates.putIfAbsent(entry.key, () => entry.value);
  }

  return ColorPalette.fromPopulations(
    candidates,
    maxColors: candidates.length.clamp(1, 128),
    weights: weights,
  );
}