rgbToHsl function

(double, double, double) rgbToHsl(
  1. int r,
  2. int g,
  3. int b
)

Returns (h, s, l) where h is degrees [0, 360).

Implementation

(double h, double s, double l) rgbToHsl(int r, int g, int b) {
  final rNot = r / 255.0;
  final gNot = g / 255.0;
  final bNot = b / 255.0;

  final (cMax, cMin) = getMaxMin(rNot, gNot, bNot);
  final delta = cMax - cMin;
  final l = (cMax + cMin) / 2.0;

  double h;
  double s;

  if (delta == 0) {
    h = 0;
    s = 0;
  } else {
    if (cMax == rNot) {
      h = 60 * (((gNot - bNot) / delta) % 6);
    } else if (cMax == gNot) {
      h = 60 * (((bNot - rNot) / delta) + 2);
    } else {
      h = 60 * (((rNot - gNot) / delta) + 4);
    }
    if (h < 0) h += 360;

    s = delta / (1 - (2 * l - 1).abs());
  }

  return (h, _round3(s), _round3(l));
}