stretchedTo method

PrimitiveGradient stretchedTo(
  1. int length
)

Force this PrimitiveGradient to have length number of colors/stops.

This method tries to return a gradient comprised of more colors and stops than the original but that still represents the original visually.

The returned PrimitiveGradient has first and last stops entries that have stayed the same but its intermittent entries have been remapped to accommodate extra colors/stops additions by extrapolateStops and sample.

Implementation

PrimitiveGradient stretchedTo(int length) {
  if (colors.length == length) return this;
  final stretchedStops = List<double>.generate(
    length,
    (int i) {
      if (i == 0) {
        return stops.first;
      } else if (i == length - 1) {
        return stops.last;
      } else {
        return extrapolateStop(stops, (i + 1) / length);
      }
    },
    growable: false,
  );
  final stretchedColors = stretchedStops
      .map<Color>((double stop) => sample(colors, stops, stop))
      .toList(growable: false);
  // print('input length: ${colors.length}, '
  // 'output length: ${stretchedColors.length} (desired: $length)\n\n');
  return PrimitiveGradient._(stretchedColors, stretchedStops);
}