fromPopulations static method
ColorPalette
fromPopulations(
- Map<
int, int> candidates, { - int maxColors = 16,
- PaletteWeights weights = const PaletteWeights(),
Builds a palette from quantized colors and their populations.
candidates maps an ARGB color to the number of source pixels it
represented. The maxColors most populated candidates are kept and
ranked; each of the six classic targets is then matched against them.
Implementation
static ColorPalette fromPopulations(
Map<int, int> candidates, {
int maxColors = 16,
PaletteWeights weights = const PaletteWeights(),
}) {
final swatches =
candidates.entries
.map((e) => PaletteSwatch(argb: e.key, population: e.value))
.toList()
..sort((a, b) => b.population.compareTo(a.population));
final trimmed = swatches.take(maxColors).toList(growable: false);
final maxPopulation = trimmed.isEmpty
? 0.0
: trimmed.first.population.toDouble();
final targets = <String, PaletteSwatch>{};
for (final target in PaletteTarget.all) {
PaletteSwatch? best;
var bestScore = -1.0;
for (final swatch in trimmed) {
final hct = Hct.fromInt(swatch.argb);
final score = scoreCandidate(
candidate: hct,
population: swatch.population.toDouble(),
maxPopulation: maxPopulation,
target: target,
weights: weights,
);
if (score > bestScore) {
bestScore = score;
best = swatch;
}
}
if (best != null) {
targets[target.name] = best;
}
}
return ColorPalette._(trimmed, targets);
}