Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ColorUtils.lerp(List<Color> colors, double t) function #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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