Search documentation of Plushie

# Plushie.Type.Gradient (Plushie v0.7.2)

Copy Markdown View Source

Gradient type for container and style backgrounds.

Build gradients with `linear/3`:

    Plushie.Type.Gradient.linear({0, 0}, {100, 100}, [
      {0.0, "#ff0000"},
      {0.5, "#00ff00"},
      {1.0, "#0000ff"}
    ])

Or from an angle with `linear_from_angle/2`:

    Plushie.Type.Gradient.linear_from_angle(90, [
      {0.0, "#ff0000"},
      {1.0, "#0000ff"}
    ])

Stop colors accept any form `Plushie.Type.Color.cast/1` supports (named atoms, hex strings, RGBA maps). They are normalized to canonical hex strings during construction.

## Wire format

Uses the same coordinate-based format as `Plushie.Canvas.Gradient`:

    %{
      type: "linear",
      start: [0, 0],
      end: [100, 100],
      stops: [[0.0, "#ff0000"], [1.0, "#0000ff"]]
    }

## Summary

### Types

stop()

A gradient color stop: `{offset, color}` where offset is 0.0-1.0.

stop_color()

Color input for gradient stops: any form `Color.cast/1` supports.

t()

### Functions

cast(v)

Validates a gradient value.

linear(from, to, stops)

Creates a linear gradient between two coordinate points.

linear_from_angle(angle, stops)

Creates a linear gradient from an angle (degrees) and stops.

## Types

# stop()

    @type stop() :: {float(), String.t()}

A gradient color stop: `{offset, color}` where offset is 0.0-1.0.

# stop_color()

    @type stop_color() :: Plushie.Type.Color.input()

Color input for gradient stops: any form `Color.cast/1` supports.

# t()

    @type t() :: %Plushie.Type.Gradient{
      from: {number(), number()},
      stops: [stop()],
      to: {number(), number()}
    }

## Functions

# cast(v)

Validates a gradient value.

Accepts `%Gradient{}` structs directly, or plain maps with `from`/`to` coordinate tuples and `stops`.

# linear(from, to, stops)

    @spec linear(
      from :: {number(), number()},
      to :: {number(), number()},
      stops :: [{number(), stop_color()}]
    ) :: t()

Creates a linear gradient between two coordinate points.

# linear_from_angle(angle, stops)

    @spec linear_from_angle(angle :: number(), stops :: [{number(), stop_color()}]) :: t()

Creates a linear gradient from an angle (degrees) and stops.

The angle is converted to start/end coordinates on a unit square (0,0 to 1,1). Use this when you want angle-based gradients without computing coordinates manually.