farbeAusHsv function

Farbe farbeAusHsv(
  1. double ton,
  2. double saettigung,
  3. double helligkeit
)

Aus Farbton, Sättigung und Helligkeit (HSV) eine Farbe.

ton in Grad (0–360), saettigung und helligkeit von 0 bis 1. Gebraucht für eine freie Farbwahl: ein Regler je Größe ist begreiflicher als sechs Hexzeichen.

Implementation

Farbe farbeAusHsv(double ton, double saettigung, double helligkeit) {
  final h = (ton % 360 + 360) % 360;
  final s = saettigung.clamp(0.0, 1.0);
  final v = helligkeit.clamp(0.0, 1.0);
  final c = v * s;
  final x = c * (1 - ((h / 60) % 2 - 1).abs());
  final m = v - c;
  final (r, g, b) = switch (h ~/ 60) {
    0 => (c, x, 0.0),
    1 => (x, c, 0.0),
    2 => (0.0, c, x),
    3 => (0.0, x, c),
    4 => (x, 0.0, c),
    _ => (c, 0.0, x),
  };
  int acht(double f) => ((f + m) * 255).round().clamp(0, 255);
  return Farbe(acht(r), acht(g), acht(b));
}