Skip to content

Instantly share code, notes, and snippets.

@folaoluwafemi
Created March 9, 2023 21:29
Show Gist options
  • Save folaoluwafemi/a873a398ce024560f9d92c8254f9e8aa to your computer and use it in GitHub Desktop.
Save folaoluwafemi/a873a398ce024560f9d92c8254f9e8aa to your computer and use it in GitHub Desktop.
A simple function to interpolate between a list of colors
Color interpolateColors(double value, List<Color> colors) {
assert(value >= 0 || value <= 1, 'value must be between 0 and 1');
final int colorListLength = colors.length - 1;
final int maxExpectedIndex = (colorListLength * value).ceil();
final int minExpectedIndex = (colorListLength * value).floor();
final Color minColor = colors[minExpectedIndex];
final Color maxColor = colors[maxExpectedIndex];
return Color.lerp(minColor, maxColor, value)!;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment