Skip to content
ConicGradient

ConicGradient

Mojo struct 🡭

ConicGradient

@memory_only
struct ConicGradient

A conic (angular) gradient centered at (cx, cy): offset 0.0 sits at start_angle, and sweeping clockwise around the center reaches offset 1.0 back at start_angle, a full turn later. Add stops with add_stop(), then pass to fill_path_conic_gradient/ fill_path_conic_gradient_aa (or query color_at() directly).

start_angle is in radians, in the convention HTML5 Canvas’s createConicGradient uses: 0.0 points along +x, and the sweep increases clockwise on screen. This package’s pixel y already increases downward, so that convention falls directly out of atan2(dy, dx) – no sign flip needed, unlike a gradient defined in a y-up math frame.

There is no rect fill for this gradient: a rectangle has no natural center to sweep around the way RadialGradient’s highlight use does, so only the path fills exist.

Fields

  • cx (Float64)
  • cy (Float64)
  • start_angle (Float64)
  • stops (GradientStops)

Implemented traits

AnyType, ColorSource, Deinitable, Movable

Methods

__init__

fn def __init__(out self, cx: Float64, cy: Float64, start_angle: Float64)

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

Args:

  • cx (Float64): Center x.
  • cy (Float64): Center y.
  • start_angle (Float64): Angle in radians where offset 0.0 (and 1.0) sit, measured from +x, increasing clockwise.
  • self (Self)

Returns:

Self

add_stop

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

Add a color stop at offset (0.0 at start_angle, 1.0 a full clockwise turn later, back at start_angle).

Args:

  • self (Self)
  • offset (Float64): Position around the sweep, 0.0 to 1.0. 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): the clockwise angle from start_angle to (x, y) around the center, as a fraction of a full turn, then the stop lookup LinearGradient.color_at uses.

(cx, cy) itself has no angle to measure. atan2(0, 0) is conventionally 0, but resolving the center that way would tie its color to start_angle rather than leaving it undefined, so the center pixel is fixed at t=0.0 (the first stop’s color) regardless of start_angle.

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.