randomPalette function

List<Color> randomPalette(
  1. int count, {
  2. NiceColorPreset? preset,
})

Generate random palette.

Implementation

List<Color> randomPalette(int count, {NiceColorPreset? preset}) {
  final rng = math.Random();
  double satRange(double lo, double hi) => lo + rng.nextDouble() * (hi - lo);
  double valRange(double lo, double hi) => lo + rng.nextDouble() * (hi - lo);

  final (sLo, sHi, vLo, vHi) = switch (preset) {
    NiceColorPreset.pastel => (0.25, 0.45, 0.85, 1.0),
    NiceColorPreset.vivid => (0.8, 1.0, 0.8, 1.0),
    NiceColorPreset.muted => (0.2, 0.4, 0.5, 0.7),
    NiceColorPreset.earth => (0.3, 0.6, 0.4, 0.7),
    NiceColorPreset.neon => (0.9, 1.0, 0.95, 1.0),
    NiceColorPreset.warm => (0.6, 0.9, 0.7, 1.0),
    NiceColorPreset.cool => (0.5, 0.8, 0.6, 0.9),
    null => (0.4, 1.0, 0.5, 1.0),
  };

  return List.generate(count, (_) {
    double hue;
    if (preset == NiceColorPreset.warm) {
      hue = rng.nextDouble() * 60; // 0-60
    } else if (preset == NiceColorPreset.cool) {
      hue = 180 + rng.nextDouble() * 120; // 180-300
    } else {
      hue = rng.nextDouble() * 360;
    }
    return HSVColor.fromAHSV(
            1, hue, satRange(sLo, sHi), valRange(vLo, vHi))
        .toColor();
  });
}