scoreCandidate function

double scoreCandidate({
  1. required Hct candidate,
  2. required double population,
  3. required double maxPopulation,
  4. required PaletteTarget target,
  5. PaletteWeights weights = const PaletteWeights(),
})

Scores a single candidate against a PaletteTarget.

Returns a value in the range 0..1, where higher is a better match.

Implementation

double scoreCandidate({
  required Hct candidate,
  required double population,
  required double maxPopulation,
  required PaletteTarget target,
  PaletteWeights weights = const PaletteWeights(),
}) {
  final chromaScore =
      1.0 - (candidate.chroma - target.targetChroma).abs() / 100.0;
  final toneScore = 1.0 - (candidate.tone - target.targetTone).abs() / 100.0;
  final populationScore = maxPopulation <= 0 ? 0.0 : population / maxPopulation;

  double clamp01(double v) => v < 0 ? 0 : (v > 1 ? 1 : v);

  return clamp01(chromaScore) * weights.chroma +
      clamp01(toneScore) * weights.tone +
      clamp01(populationScore) * weights.population;
}