Last active
September 12, 2021 14:55
-
-
Save jtmuller5/62f802e59987621176221b4c0bfe8f0f to your computer and use it in GitHub Desktop.
This file contains 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
/// Get the color from a gradient at a specific position | |
Color? colorAlongGradient({ | |
required List<Color> colors, | |
List<double>? stops, | |
required double position, | |
}) { | |
stops ??= List.generate(colors.length, (index) => index * (1 / (colors.length-1))); | |
for (var s = 0; s < stops.length - 1; s++) { | |
final leftStop = stops[s], rightStop = stops[s + 1]; | |
final leftColor = colors[s], rightColor = colors[s + 1]; | |
if (position <= leftStop) { | |
return leftColor; | |
} else if (position < rightStop) { | |
final sectionT = (position - leftStop) / (rightStop - leftStop); | |
return Color.lerp(leftColor, rightColor, sectionT); | |
} | |
} | |
return colors.last; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment