Table of Contents

Class Gradient

Namespace
Egami.Drawing
Assembly
Egami.Drawing.dll

A paint whose color changes along a line, around a center or between two circles by its stops. Each pixel center maps back through the canvas transform and LocalMatrix to a position t along the gradient, which Tile brings into 0 to 1 and the stops turn into a color. On 8-bit targets t picks one of 1024 colors made once per paint; float and 16-bit targets mix the stops exactly.

public abstract record Gradient : Paint, ITextPaint, IEquatable<Paint>, IEquatable<Gradient>
Inheritance
Gradient
Implements
Derived
Inherited Members

Examples

using (RasterCanvas<Rgba8> canvas = raster.CreateCanvas())
{
    // Two colors along a line; past the ends the end colors repeat (TileMode.Clamp).
    canvas.FillRect(new RectF(0, 0, 200, 100), new LinearGradient(new Vector2(0, 0), new Vector2(200, 0), new RgbaF32(0.1f, 0.3f, 0.9f, 1), new RgbaF32(1, 0.8f, 0.2f, 1)));

    // Several stops, mixed in Oklab for even midpoints, repeating every 40 pixels.
    canvas.FillRect(new RectF(200, 0, 200, 100), new LinearGradient(
        new Vector2(200, 0),
        new Vector2(240, 0),
        new GradientStop(0, new Rgba8(230, 40, 60, 255)),
        new GradientStop(0.5f, new Rgba8(250, 210, 60, 255)),
        new GradientStop(1, new Rgba8(230, 40, 60, 255)))
    {
        Tile = TileMode.Repeat,
        Interpolation = GradientInterpolation.Oklab,
    });

    // A radial gradient stretched into an ellipse by its local matrix, and a sweep around a center.
    canvas.FillOval(new RectF(10, 110, 180, 80), new RadialGradient(Vector2.Zero, 1, new RgbaF32(1, 1, 1, 1), new RgbaF32(0, 0.4f, 0.2f, 1))
    {
        LocalMatrix = Matrix3x2.CreateScale(90, 40) * Matrix3x2.CreateTranslation(100, 150),
    });
    canvas.FillCircle(new Vector2(300, 150), 45, new SweepGradient(
        new Vector2(300, 150),
        new GradientStop(0, new Rgba8(255, 0, 0, 255)),
        new GradientStop(0.5f, new Rgba8(0, 0, 255, 255)),
        new GradientStop(1, new Rgba8(255, 0, 0, 255))));
}

Properties

ExtendEnd

Gets a value indicating whether a Clamp gradient repeats its last color past the end. Defaults to true.

public bool ExtendEnd { get; init; }

Property Value

bool

ExtendStart

Gets a value indicating whether a Clamp gradient repeats its first color before the start; when false nothing is drawn there, as a PDF shading with Extend false. Other tile modes ignore it. Defaults to true.

public bool ExtendStart { get; init; }

Property Value

bool

Interpolation

Gets the color space the stops mix in. Defaults to Encoded.

public GradientInterpolation Interpolation { get; init; }

Property Value

GradientInterpolation

Exceptions

ArgumentNullException

The value is null.

Premultiplied

Gets a value indicating whether colors mix premultiplied by their alpha, as CSS specifies, so a fade to a transparent color never darkens toward that color's hidden channels. false mixes straight colors, as Skia does by default. Defaults to true.

public bool Premultiplied { get; init; }

Property Value

bool

Stops

Gets the stops, in the order given. An offset below the one before it is treated as equal to it, as CSS does.

public IReadOnlyList<GradientStop> Stops { get; }

Property Value

IReadOnlyList<GradientStop>

Tile

Gets what happens past the gradient's ends. Defaults to Clamp.

public TileMode Tile { get; init; }

Property Value

TileMode

Exceptions

ArgumentOutOfRangeException

The value is not defined.

Methods

Equals(Gradient?)

Indicates whether the current object is equal to another object of the same type.

public virtual bool Equals(Gradient? other)

Parameters

other Gradient

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.