ColorRamp constructor

ColorRamp(
  1. List<Color> colors, {
  2. List<double>? stops,
})

Creates a ramp that interpolates through colors over the particle's lifetime.

stops optionally positions each color on the 0...1 life progress axis; when omitted the colors are spaced evenly.

Implementation

ColorRamp(List<Color> colors, {List<double>? stops})
  : assert(colors.isNotEmpty, 'colors must not be empty'),
    assert(
      stops == null || stops.length == colors.length,
      'stops must have the same length as colors',
    ) {
  if (colors.length == 1) {
    _lut.fillRange(0, _resolution, colors.first.toARGB32());
    return;
  }
  final positions =
      stops ?? List.generate(colors.length, (i) => i / (colors.length - 1));
  var segment = 0;
  for (var i = 0; i < _resolution; i++) {
    final t = i / (_resolution - 1);
    while (segment < colors.length - 2 && t > positions[segment + 1]) {
      segment++;
    }
    final start = positions[segment];
    final end = positions[segment + 1];
    final local = end > start ? ((t - start) / (end - start)) : 1.0;
    final color = Color.lerp(
      colors[segment],
      colors[segment + 1],
      local.clamp(0.0, 1.0),
    )!;
    _lut[i] = color.toARGB32();
  }
}