Skip to content
gradient

gradient

Mojo module 🡭

gradient

Color gradients: LinearGradient, RadialGradient and ConicGradient, consumed by the gradient fills in canvas.shapes.rects (fill_rect_gradient, fill_rect_radial_gradient) and path.mojo (fill_path_gradient, fill_path_radial_gradient, fill_path_conic_gradient and their _aa variants). Those are the only gradient-aware fills; circle/ellipse/polygon variants aren’t built, and ConicGradient has no rect fill of its own.

All three reduce a point to a projected position t – distance along an axis (linear), from a center relative to a radius (radial), or the angle around a center relative to a full turn (conic) – then hand it to a GradientStops for stop lookup and interpolation. Only the projection differs, so only the projection lives on each struct, and GradientStops stands on its own as a color ramp for a caller that has already normalized a value to [0, 1].

Linear and radial extend “pad” only: a point past either endpoint or outside the radius takes that edge’s color, clamped. No repeat or reflect. A conic gradient has no such edge – its projection wraps around a full turn instead of clamping, so t is already in [0, 1) before it reaches the ramp.

Structs

  • GradientStop: One color stop: the color a ramp holds at offset, 0.0 to 1.0.
  • GradientStops: A 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.
  • 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).
  • RadialGradient: A radial gradient centered at (cx, cy) with the given radius: offset 0.0 is the center, offset 1.0 is the circle at radius. Add stops with add_stop(), then pass to fill_rect_radial_gradient/ fill_path_radial_gradient (or query color_at() directly).
  • 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).

Traits

  • ColorSource: Anything that can answer “what color is at this point?” – the fill source a gradient-filled shape queries per pixel.