toHSL property

HSLColor toHSL

Convert this to a HSL color space

Implementation

HSLColor get toHSL {
  /// Code copied from `HSLColor` in the flutter repository
  final rgb = toRGB;
  final r = rgb.red;
  final g = rgb.green;
  final b = rgb.blue;

  var cmin = math.min(math.min(r, g), b),
      cmax = math.max(math.max(r, g), b),
      delta = cmax - cmin;
  var h = 0.0, s = 0.0, l = 0.0;

  if (delta == 0) {
    h = 0;
  } else if (cmax == r) {
    h = ((g - b) / delta) % 6;
  } else if (cmax == g) {
    h = (b - r) / delta + 2;
  } else {
    h = (r - g) / delta + 4;
  }

  h = (h * 60).roundToDouble();

  if (h < 0) h += 360;

  l = (cmax + cmin) / 2;

  // Calculate saturation
  s = delta == 0 ? 0 : delta / (1 - (2 * l - 1).abs());

  // Multiply l and s by 100
  s += (s * 100).toFixed(1);
  l += (l * 100).toFixed(1);
  return HSLColor(
    alpha: toRGB.alpha.toDouble(),
    hue: h,
    saturation: s,
    lightness: l,
  );
}