RadialGradient
Mojo struct 🡭
RadialGradient
@memory_only
struct RadialGradientA 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).
The two-circle form of SVG, Cairo and the HTML5 canvas is the
keyword constructor: offset 0.0 is then the focal circle at
(fx, fy) with radius fr, offset 1.0 the outer circle, and the
stops in between lie on the circles interpolated from one to the
other. The focal circle has to lie inside the outer one; the
constructor raises otherwise. The plain constructor is the
special case fx = cx, fy = cy, fr = 0.
Fields
- cx (
Float64) - cy (
Float64) - radius (
Float64) - fx (
Float64) - fy (
Float64) - fr (
Float64) - stops (
GradientStops)
Implemented traits
AnyType, ColorSource, Deinitable, Movable
Methods
__init__
fn def __init__(out self, cx: Float64, cy: Float64, radius: Float64)A gradient with no stops yet – add at least one with add_stop() before calling color_at().
Args:
- cx (
Float64): Center x, offset 0.0. - cy (
Float64): Center y, offset 0.0. - radius (
Float64): Distance at which offset reaches 1.0. - self (
Self)
Returns:
Self
fn def __init__(out self, cx: Float64, cy: Float64, radius: Float64, *, fx: Float64, fy: Float64, fr: Float64 = 0)The two-circle form: offset 0.0 is the focal circle, offset 1.0 the outer circle at (cx, cy, radius).
Args:
- cx (
Float64): Outer circle’s center x. - cy (
Float64): Outer circle’s center y. - radius (
Float64): Outer circle’s radius, where offset reaches 1.0. - fx (
Float64): Focal circle’s center x, where offset is 0.0. - fy (
Float64): Focal circle’s center y. - fr (
Float64): Focal circle’s radius, 0 (the default) for a focal point. - self (
Self)
Returns:
Self
Raises:
If fr is negative, or the focal circle reaches outside
the outer circle: the extended-cone form those describe
is not supported.
add_stop
fn def add_stop(mut self, offset: Float64, color: Color)Add a color stop at offset (0.0 at the center, 1.0 at radius).
Args:
- self (
Self) - offset (
Float64): Position from the center, 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) -> ColorSpaceThe 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) -> ColorThe gradient’s color at (x, y): project onto [0, 1] as distance_from_center / radius, clamp (“pad” extend – a distance is never negative, so only the far end ever clamps), then the stop lookup LinearGradient.color_at uses.
radius == 0.0 collapses every stop’s circle to one point and resolves to t=1.0, a solid fill of the highest-offset stop’s color, rather than dividing by zero.
With a focal circle, t is the largest offset whose interpolated circle passes through (x, y): with the center c(t) = f + t*(c - f) and radius r(t) = fr + t*(radius - fr), the larger root of |p - c(t)| = r(t). A point inside the focal circle has both roots negative and pads to 0.
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.