Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/src/ColorUtils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ class ColorUtils {
static const String BASIC_COLOR_BLUE = 'blue';
static const String HEX_BLACK = '#000000';
static const String HEX_WHITE = '#FFFFFF';

///
/// Lerps multiple colors according to [t]
///
/// Similar to Color.lerp, but with support for more than two colors instead
///
static Color lerp(List<Color> colors, double t) {
final double newT = math.min(1.0, math.max(0.0, t));
if (newT == 1.0) {
return colors.last;
}
final double x = newT * (colors.length - 1);
final Color oldColor = colors[x.floor()];
final Color newColor = colors[(x + 1).floor()];
return Color.lerp(oldColor, newColor, x - x.floor())!;
}

///
/// Converts the given [hex] color string to the corresponding int.
Expand Down