GradientStops
Mojo struct 🡭
GradientStops
@memory_only
struct GradientStopsA one-dimensional color ramp: stops sorted by offset, and the interpolated color at any position in [0, 1]. The half of every gradient that does not depend on geometry, and on its own the color scale a data value maps through once it is normalized.
Index, measure and iterate it like a list: len(stops),
stops[i] and for stop in stops.
Implemented traits
AnyType, Copyable, Deinitable, Movable, Sized
Methods
__init__
fn def __init__(out self)A ramp with no stops yet; color_at returns transparent black until one is added.
Args:
- self (
Self)
Returns:
Self
__getitem__
fn def __getitem__(self, i: Int) -> GradientStopArgs:
- self (
Self) - i (
Int)
Returns:
GradientStop
set_color_space
fn def set_color_space(mut self, space: ColorSpace)Set the space color_at interpolates in: SRGB, the default, mixes the stops’ channel bytes directly; LINEAR mixes them in linear light (see ColorSpace), which keeps a ramp between two saturated colors from sagging through a dark middle. Alpha interpolates the same way in both. SvgCanvas writes a LINEAR ramp with color-interpolation="linearRGB".
Args:
- self (
Self) - space (
ColorSpace): The space the ramp interpolates in.
color_space
fn def color_space(self) -> ColorSpaceThe space color_at interpolates in.
Args:
- self (
Self)
Returns:
ColorSpace: The current space, ColorSpace.SRGB until
set_color_space says otherwise.
__len__
fn def __len__(self) -> IntArgs:
- self (
Self)
Returns:
Int
__iter__
fn def __iter__(ref self) -> _ListIter[GradientStop, origin_of(self._stops)]Args:
- self (
Self)
Returns:
_ListIter[GradientStop, origin_of(self._stops)]
add_stop
fn def add_stop(mut self, offset: Float64, color: Color)Add a stop, keeping the ramp sorted by offset with insertion order preserved among equal offsets.
Two stops at one offset are a hard color transition, and which
was added first decides which side of it owns which color –
so the insert goes after any stop already at this offset,
and color_at reads the run’s ends accordingly.
Args:
- self (
Self) - offset (
Float64): Position 0.0 to 1.0. Stops need not be added in order. - color (
Color): This stop’s color.
color_at
fn def color_at(self, t_in: Float64) -> ColorThe ramp’s color at t_in, clamped to [0, 1] first (the “pad” extend), interpolated between the two stops bracketing it. The bracketing pair comes from a binary search over the sorted stops.
This runs once per pixel of every gradient fill, so the stops
are read through a pointer rather than copied out of the list
per probe, and the span it lands in carries its own reciprocal
and channel deltas (_Interval) rather than deriving them
again. What is left per pixel is a search, a multiply and one
four-lane multiply-add.
Args:
- self (
Self) - t_in (
Float64): Position along the ramp.
Returns:
Color: The interpolated color, transparent black if no stops have
been added yet.