Skip to content

Instantly share code, notes, and snippets.

@jtmuller5
Last active September 12, 2021 14:55
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/62f802e59987621176221b4c0bfe8f0f to your computer and use it in GitHub Desktop.
Save jtmuller5/62f802e59987621176221b4c0bfe8f0f to your computer and use it in GitHub Desktop.
/// 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