Skip to content

Instantly share code, notes, and snippets.

@jtmuller5
Created September 12, 2021 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtmuller5/f61d3f96a3f769c45f258b71eb5ea038 to your computer and use it in GitHub Desktop.
Save jtmuller5/f61d3f96a3f769c45f258b71eb5ea038 to your computer and use it in GitHub Desktop.
/// 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