hslToRgb static method

RgbColor hslToRgb(
  1. HslColor hslColor
)

Converts a HSL color to a RGB color.

Implementation

static RgbColor hslToRgb(HslColor hslColor) {
  final hsl = hslColor.toFactoredList();

  final hue = hsl[0];
  final saturation = hsl[1];
  final lightness = hsl[2];

  double red, green, blue;

  if (saturation == 0) {
    red = green = blue = lightness;
  } else {
    final q = (lightness < 0.5)
        ? lightness * (1 + saturation)
        : lightness + saturation - (lightness * saturation);

    final p = (2 * lightness) - q;

    double hueToRgb(t) {
      if (t < 0) {
        t += 1;
      } else if (t > 1) {
        t -= 1;
      }

      if (t < 1 / 6) return p + ((q - p) * 6 * t);
      if (t < 1 / 2) return q;
      if (t < 2 / 3) return p + ((q - p) * ((2 / 3) - t) * 6);

      return p;
    }

    red = hueToRgb(hue + (1 / 3));
    green = hueToRgb(hue);
    blue = hueToRgb(hue - (1 / 3));
  }

  final alpha = hslColor.alpha / 255;

  return RgbColor.extrapolate(<double>[red, green, blue, alpha]);
}