Blendend.Style.Gradient (blendend v0.3.0)
View SourceGradient style helpers for Blendend.
This module works with gradient values that can be
used as fill or stroke styles in Blendend.Canvas / Blendend.Draw
functions.
This module is used in three steps:
Create a gradient with one of:
Add color stops using
add_stop/3(oradd_stop!/3).(Optionally) configure how the gradient behaves:
set_extend/2/set_extend!/2– choose how the gradient extends outside its 0.0–1.0 range (:pad,:repeat,:reflect).set_transform/2/set_transform!/2– apply aBlendend.Matrix2Dtransform that controls how the gradient is positioned, rotated or scaled in canvas coordinates.reset_transform/1/reset_transform!/1– clear any transform back to identity.
Gradients created here are typically passed as the :gradient or
:stroke_gradient option to drawing functions such as
Blendend.Canvas.Fill.rect/6
Blendend.Canvas.Stroke.circle/5.
Summary
Functions
Adds a color stop to a gradient.
Same as add_stop/3, but returns the gradient directly.
Creates a conic (angular) gradient.
Same as conic/3, but returns the gradient directly.
Creates a conic gradient and adds stops.
Creates a linear gradient between two points.
Same as linear/4, but on success, returns the gradient.
Creates a linear gradient for the given line and adds stops in one go.
Creates a radial gradient.
Same as radial/6, but returns the gradient directly.
Creates a radial gradient and adds stops.
Resets a gradient’s transform to the identity matrix.
Same as reset_transform/1, but returns the gradient directly.
Sets the extend mode of a gradient.
Same as set_extend/2, but returns the gradient directly.
Sets the transform matrix used when sampling a gradient.
Same as set_transform/2, but returns the gradient directly.
Types
Functions
Adds a color stop to a gradient.
grad– a gradient resourceoffset– a numeric position along the gradient (usually0.0..1.0)color– a color resource created withBlendend.Style.Color.rgb/3or/4
On success, returns :ok.
On failure, returns {:error, reason}.
Same as add_stop/3, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.
Creates a conic (angular) gradient.
The gradient is defined by:
cx,cy– center of rotationangle– starting angle in radians
On success, returns {:ok, gradient}.
On failure, returns {:error, reason}.
Same as conic/3, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.
@spec conic_from_stops( {number(), number(), number()}, [{float(), Blended.Style.Color.t()}], keyword() ) :: t()
Creates a conic gradient and adds stops.
{cx, cy, angle}– center and angle in radiansstops– list of{offset, color}
Options:
:extend– extend mode (:pad | :repeat | :reflect), defaults to:pad.
Creates a linear gradient between two points.
The gradient line goes from (x0, y0) to (x1, y1).
On success, returns {:ok, gradient} where gradient is a gradient
resource.
On failure, returns {:error, reason}.
Use add_stop/3 to populate the gradient with one or more
color stops, then pass the gradient as gradient: or stroke_gradient:
in drawing options.
Offsets given to add_stop/3 are typically in the range
0.0..1.0 (start to end).
Examples
iex> {:ok, grad} = Blendend.Style.Gradient.linear(0.0, 0.0, 0.0, 200.0)
iex> :ok = Blendend.Style.Gradient.add_stop(grad, 0.0, Blendend.Style.Color.rgb!(255, 0, 0))
iex> :ok = Blendend.Style.Gradient.add_stop(grad, 1.0, Blendend.Style.Color.rgb!(0, 0, 255))
iex> rect 0, 0, 200, 200, gradient: grad
Same as linear/4, but on success, returns the gradient.
On failure, raises Blendend.Error.
@spec linear_from_stops( {number(), number(), number(), number()}, [{float(), Blended.Style.Color.t()}], keyword() ) :: t()
Creates a linear gradient for the given line and adds stops in one go.
line–{x0, y0, x1, y1}stops– list of{offset, color}whereoffset is 0.0..1.0
Options:
:extend–:pad | :repeat | :reflect(default::pad)
Returns a gradient resource suitable for use as fill or stroke.
@spec radial(number(), number(), number(), number(), number(), number()) :: {:ok, t()} | {:error, term()}
Creates a radial gradient.
The gradient is defined by:
cx0,cy0,r0– inner circle center and radiuscx1,cy1,r1– outer circle center and radius
On success, returns {:ok, gradient}.
On failure, returns {:error, reason}.
Same as radial/6, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.
@spec radial_from_stops( {number(), number(), number(), number(), number(), number()}, [{float(), Blended.Style.Color.t()}], keyword() ) :: t()
Creates a radial gradient and adds stops.
cx0,cy0,r0– inner circle center and radiuscx1,cy1,r1– outer circle center and radiusstops– list of{offset, color}
Options:
:extend– extend mode (:pad | :repeat | :reflect), defaults to:pad.
Resets a gradient’s transform to the identity matrix.
After calling this function, the gradient is mapped directly in canvas coordinates using the positions it was created with, without any extra rotation / scaling / translation.
On success, returns :ok.
On failure, returns {:error, reason}.
Same as reset_transform/1, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.
@spec set_extend(t(), extend_mode()) :: :ok | {:error, term()}
Sets the extend mode of a gradient.
The extend mode controls how the gradient behaves outside the range
covered by its stops (typically offsets 0.0..1.0). Supported modes:
:pad– clamp to the edge colors (default):repeat– repeat the gradient pattern:reflect– repeat the gradient, flipping direction every cycle
On success, returns :ok.
On failure, returns {:error, reason}.
@spec set_extend!(t(), extend_mode()) :: t()
Same as set_extend/2, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.
@spec set_transform(t(), Blendend.Matrix2D.t()) :: :ok | {:error, term()}
Sets the transform matrix used when sampling a gradient.
The matrix is expressed in canvas coordinates and controls how the gradient is positioned, rotated, or scaled relative to the canvas.
On success, returns :ok.
On failure, returns {:error, reason}.
@spec set_transform!(t(), Blendend.Matrix2D.t()) :: t()
Same as set_transform/2, but returns the gradient directly.
On success, returns gradient.
On failure, raises Blendend.Error.