Gradients

Gradient fills for shapes drawn on a Canvas: LinearGradient, RadialGradient and SweepGradient, passed as the fill of Rect, Circle, Ellipse, Arc, Path, Polygon, BezierPath or Fill. nib.draw.Text does not take a gradient. PointMode sets how the Points command connects its points.

All color parameters accept hex strings ("#FF0000") or nib.Color objects.


PointMode

Determines how a list of points is interpreted by the Points draw command.

Value Description
POINTS Draw each point as an individual dot.
LINES Draw lines between consecutive pairs of points.
POLYGON Draw connected line segments through all points.

LinearGradient

A linear gradient fill that transitions colors along a straight line.

Constructor

nib.draw.LinearGradient(start, end, colors, stops=None)

Parameters

Parameter Type Default Description
start tuple[float, float] required Starting point (x, y) of the gradient in canvas coordinates.
end tuple[float, float] required Ending point (x, y) of the gradient in canvas coordinates.
colors list[str | Color] required List of colors. Minimum two.
stops list[float] None Optional stop positions from 0.0 to 1.0. Must match the length of colors. Evenly distributed when omitted.

Example

nib.draw.LinearGradient(
    start=(0, 0),
    end=(200, 200),
    colors=["#FF0000", "#00FF00", "#0000FF"],
    stops=[0.0, 0.5, 1.0],
)

RadialGradient

A radial gradient fill that transitions colors outward from a center point.

Constructor

nib.draw.RadialGradient(center, radius, colors, stops=None, focus=None)

Parameters

Parameter Type Default Description
center tuple[float, float] required Center point (x, y) of the gradient in canvas coordinates.
radius float required Radius of the gradient in points.
colors list[str | Color] required List of colors. Minimum two.
stops list[float] None Optional stop positions from 0.0 to 1.0.
focus tuple[float, float] None Optional focus point (x, y) for off-center gradients.

Example

nib.draw.RadialGradient(
    center=(100, 100),
    radius=80,
    colors=[nib.Color.YELLOW, nib.Color.RED],
)

SweepGradient

A sweep (conic/angular) gradient that transitions colors around a center point.

Constructor

nib.draw.SweepGradient(center, colors, stops=None, start_angle=0, end_angle=6.283)

Parameters

Parameter Type Default Description
center tuple[float, float] required Center point (x, y) of the gradient.
colors list[str | Color] required List of colors.
stops list[float] None Optional stop positions from 0.0 to 1.0.
start_angle float 0 Starting angle in radians.
end_angle float 6.283 Ending angle in radians (default is 2 * pi, a full circle).

Example

# Color wheel
nib.draw.SweepGradient(
    center=(100, 100),
    colors=[nib.Color.RED, nib.Color.GREEN, nib.Color.BLUE, nib.Color.RED],
)