lerpTo method

List<ColorModel> lerpTo(
  1. ColorModel color,
  2. int steps, {
  3. ColorSpace? colorSpace,
  4. bool excludeOriginalColors = false,
})

Returns the interpolated steps between this color and color.

The returned ColorModel's values will be interpolated in the color space defined by colorSpace, or in the color space of this color if colorSpace is null.

If excludeOriginalColors is false, this color and color will not be included in the list. If color is in a different color space, it will be converted to this color's color space.

Implementation

List<ColorModel> lerpTo(
  ColorModel color,
  int steps, {
  ColorSpace? colorSpace,
  bool excludeOriginalColors = false,
}) {
  assert(steps > 0);

  final colorA = colorSpace != null ? colorSpace.from(this) : this;
  final valuesA = colorA is RgbColor
      ? colorA.toPreciseListWithAlpha()
      : colorA.toListWithAlpha();

  final colorB = colorSpace != null ? colorSpace.from(color) : convert(color);
  final valuesB = colorB is RgbColor
      ? colorB.toPreciseListWithAlpha()
      : colorB.toListWithAlpha();

  final colors = <ColorModel>[];
  final slice = 1 / (steps + 1);
  for (var i = 1; i <= steps; i++) {
    final values = <num>[];
    final step = slice * i;
    for (var j = 0; j < valuesA.length; j++) {
      values.add(_interpolateValue(valuesA[j], valuesB[j], step));
    }
    colors.add(colorA.withValues(values));
  }

  if (!excludeOriginalColors) {
    colors.insert(0, colorA);
    colors.add(colorB);
  }

  return colors.map<ColorModel>((color) => convert(color)).toList();
}