Created
September 12, 2021 15:19
-
-
Save jtmuller5/f61d3f96a3f769c45f258b71eb5ea038 to your computer and use it in GitHub Desktop.
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
/// Get the color from a gradient at a specific position | |
/// Position should be between 0 and 1 | |
extension ColorGetter on Gradient { | |
Color? colorAtPosition({ | |
required double position, | |
}) { | |
List<double> _stops = stops ?? List.generate(colors.length, (index) => index * (1 / (colors.length-1))); | |
for (var stop = 0; stop < _stops.length - 1; stop++) { | |
final leftStop = _stops[stop], | |
rightStop = _stops[stop + 1]; | |
final leftColor = colors[stop], | |
rightColor = colors[stop + 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