lerp method Null safety

HSLColor? lerp(
  1. HSLColor? a,
  2. HSLColor? b,
  3. double t
)

Linearly interpolate between two HSLColors.

The colors are interpolated by interpolating the alpha, hue, saturation, and lightness channels separately, which usually leads to a more pleasing effect than Color.lerp (which interpolates the red, green, and blue channels separately).

If either color is null, this function linearly interpolates from a transparent instance of the other color. This is usually preferable to interpolating from Colors.transparent (const Color(0x00000000)) since that will interpolate from a transparent red and cycle through the hues to match the target color, regardless of what that color's hue is.

The t argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning a (or something equivalent to a), 1.0 meaning that the interpolation has finished, returning b (or something equivalent to b), and values between them meaning that the interpolation is at the relevant point on the timeline between a and b. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid (and can easily be generated by curves such as Curves.elasticInOut).

Values outside of the valid range for each channel will be clamped.

Values for t are usually obtained from an Animation<double>, such as an AnimationController.

Implementation

static HSLColor? lerp(HSLColor? a, HSLColor? b, double t) {
  assert(t != null);
  if (a == null && b == null)
    return null;
  if (a == null)
    return b!._scaleAlpha(t);
  if (b == null)
    return a._scaleAlpha(1.0 - t);
  return HSLColor.fromAHSL(
    lerpDouble(a.alpha, b.alpha, t)!.clamp(0.0, 1.0),
    lerpDouble(a.hue, b.hue, t)! % 360.0,
    lerpDouble(a.saturation, b.saturation, t)!.clamp(0.0, 1.0),
    lerpDouble(a.lightness, b.lightness, t)!.clamp(0.0, 1.0),
  );
}