Skip to content
LinearGradient

LinearGradient

Mojo struct 🡭

LinearGradient

@memory_only
struct LinearGradient

A linear gradient along the axis from (x0, y0) to (x1, y1). Add stops with add_stop(), then pass to fill_rect_gradient/ fill_path_gradient (or query color_at() directly).

Stops need not be added in offset order: add_stop inserts each into place, so stops is always sorted by offset and color_at() finds its bracketing pair by binary search.

Fields

  • x0 (Float64)
  • y0 (Float64)
  • x1 (Float64)
  • y1 (Float64)
  • stops (GradientStops)

Implemented traits

AnyType, ColorSource, Deinitable, Movable

Methods

__init__

fn def __init__(out self, x0: Float64, y0: Float64, x1: Float64, y1: Float64)

A gradient with no stops yet – add at least one with add_stop() before calling color_at().

Args:

  • x0 (Float64): Axis start x, offset 0.0.
  • y0 (Float64): Axis start y, offset 0.0.
  • x1 (Float64): Axis end x, offset 1.0.
  • y1 (Float64): Axis end y, offset 1.0.
  • self (Self)

Returns:

Self

add_stop

fn def add_stop(mut self, offset: Float64, color: Color)

Add a color stop at offset (0.0 to 1.0 along the axis).

Args:

  • self (Self)
  • offset (Float64): Position along the axis, 0.0 at (x0, y0), 1.0 at (x1, y1). Stops need not be added in offset order; each is inserted into place.
  • color (Color): This stop’s color.

set_color_space

fn def set_color_space(mut self, space: ColorSpace)

Set the space the ramp interpolates in; see GradientStops.set_color_space.

Args:

  • self (Self)
  • space (ColorSpace): The space the ramp interpolates in.

color_space

fn def color_space(self) -> ColorSpace

The space the ramp interpolates in.

Args:

  • self (Self)

Returns:

ColorSpace: The current space, ColorSpace.SRGB by default.

color_at

fn def color_at(self, x: Float64, y: Float64) -> Color

The gradient’s color at (x, y): project onto the axis, clamp to [0, 1] (“pad” extend), then interpolate between the two stops bracketing that position.

Args:

  • self (Self)
  • x (Float64): Point x.
  • y (Float64): Point y.

Returns:

Color: The interpolated color, transparent black if no stops have been added yet.