lerpGradient function
Lerps between a LinearGradient colors, based on t
Implementation
Color lerpGradient(List<Color> colors, List<double> stops, double t) {
if (colors.isEmpty) {
throw ArgumentError('"colors" is empty.');
} else if (colors.length == 1) {
return colors[0];
}
if (stops.length != colors.length) {
stops = [];
/// provided gradientColorStops is invalid and we calculate it here
colors.asMap().forEach((index, color) {
final percent = 1.0 / (colors.length - 1);
stops.add(percent * index);
});
}
for (var s = 0; s < stops.length - 1; s++) {
final leftStop = stops[s];
final rightStop = stops[s + 1];
final leftColor = colors[s];
final rightColor = colors[s + 1];
if (t <= leftStop) {
return leftColor;
} else if (t < rightStop) {
final sectionT = (t - leftStop) / (rightStop - leftStop);
return Color.lerp(leftColor, rightColor, sectionT)!;
}
}
return colors.last;
}