Created
March 9, 2023 21:29
-
-
Save folaoluwafemi/a873a398ce024560f9d92c8254f9e8aa to your computer and use it in GitHub Desktop.
A simple function to interpolate between a list of colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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