colorAtPosition method

Color colorAtPosition(
  1. double pos
)

Returns the color at a normalized (0.0 to 1.0) position in the color sequence. If a color stop isn't hit, the returned color will be an interpolation of a color between two color stops.

Implementation

Color colorAtPosition(double pos) {
  assert(pos >= 0.0 && pos <= 1.0);

  if (pos == 0.0) return colors[0];

  double lastStop = stops[0];
  Color lastColor = colors[0];

  for (int i = 0; i < colors.length; i++) {
    double currentStop = stops[i];
    Color currentColor = colors[i];

    if (pos <= currentStop) {
      double blend = (pos - lastStop) / (currentStop - lastStop);
      return _interpolateColor(lastColor, currentColor, blend);
    }
    lastStop = currentStop;
    lastColor = currentColor;
  }
  return colors[colors.length - 1];
}