Macaw.Draw.Interpolate (macaw_draw v0.1.0)

Copy Markdown View Source

Interpolator factories (spec/07 §17), copied from visualize's Visualize.Interpolate: each answers {:ok, fun}, fun a 1-arity function of t, or {:error, faults} for endpoints it cannot interpolate, and has a ! twin answering the function itself.

iex> {:ok, half} = Macaw.Draw.Interpolate.number(0, 100)
iex> half.(0.5)
50.0

t is not clamped unless stated; callers pass [0, 1] for the nominal range. Motion between two states, a zoom's smooth path and a color ramp read these.

Colors are Macaw.Layers.Color terms. rgb/2, hsl/2 and hsl_long/2 take two literal colors (%Macaw.Layers.Color{}) and nothing else, an endpoint that is none {[i], {:type, :color}}; they answer an opaque 8-bit literal, as visualize's factories have since #1006. The blending is macaw_layers' (Macaw.Layers.Color.interpolate/4 in :rgb, :hsl and :hsl_long): each endpoint goes in as its opaque 8-bit channels, so an endpoint's opacity is not blended, and each answer comes out rounded to them.

The other refusals: discrete/1 of [] is {[], {:type, :non_empty}}, array/2 of unequal lengths {[], {:lengths, la, lb}}, basis/1 of fewer than two values {[], {:type, :values}}, and zoom/2 of a view whose width is not positive {[i, 2], {:not_positive, w}}.

Summary

Types

A refusal: faults with their paths.

An interpolator: a function of t.

Functions

Element-wise number/2 over two equal-length lists of numbers: a list.

As array/2, the value itself, raising Macaw.Draw.Error with its faults.

A uniform cubic B-spline through at least two numbers, its ends clamped: t is clamped to [0, 1], and t = 0 and 1 are exactly the first and last values.

As basis/1, the value itself, raising Macaw.Draw.Error with its faults.

The blend of two Dates at day precision, rounded.

As date/2, the value itself, raising Macaw.Draw.Error with its faults.

The blend of two DateTimes at millisecond precision, rounded.

As datetime/2, the value itself, raising Macaw.Draw.Error with its faults.

One of n values: values[clamp(⌊t·n⌋, 0, n − 1)].

As discrete/1, the value itself, raising Macaw.Draw.Error with its faults.

HSL with the shorter hue arc: an opaque 8-bit literal color.

As hsl/2, the value itself, raising Macaw.Draw.Error with its faults.

HSL with the longer hue arc: an opaque 8-bit literal color.

As hsl_long/2, the value itself, raising Macaw.Draw.Error with its faults.

a + (b − a)·t.

As number/2, the value itself, raising Macaw.Draw.Error with its faults.

Each channel linear, rounded and clamped: an opaque 8-bit literal color.

As rgb/2, the value itself, raising Macaw.Draw.Error with its faults.

round(a + (b − a)·t), an integer.

As round/2, the value itself, raising Macaw.Draw.Error with its faults.

The numbers embedded in b blended pairwise with those in a, the rest of the text b's: a missing number of a starts from 0 and extras in a are ignored; a whole result prints as an integer, else a float rounded to 4 decimals.

As string/2, the value itself, raising Macaw.Draw.Error with its faults.

Two SVG transform strings blended: the first translate(x[, y]), scale(s[, sy]) and rotate(a) of each, a component in one string only held constant, one in neither the identity; written translate(x, y) scale(sx, sy) rotate(r) without the identity components, or "".

As transform/2, the value itself, raising Macaw.Draw.Error with its faults.

van Wijk and Nuij's smooth zoom between two views [cx, cy, w], ρ = 1.4: a view. When the centers coincide (distance below 1.0e-6) only w changes, exponentially.

As zoom/2, the value itself, raising Macaw.Draw.Error with its faults.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

interpolator(value)

@type interpolator(value) :: (number() -> value)

An interpolator: a function of t.

Functions

array(a, b)

@spec array([number()], [number()]) :: {:ok, interpolator([number()])} | error()

Element-wise number/2 over two equal-length lists of numbers: a list.

array!(a1, a2)

@spec array!(term(), term()) :: term()

As array/2, the value itself, raising Macaw.Draw.Error with its faults.

basis(values)

@spec basis([number()]) :: {:ok, interpolator(float())} | error()

A uniform cubic B-spline through at least two numbers, its ends clamped: t is clamped to [0, 1], and t = 0 and 1 are exactly the first and last values.

basis!(a1)

@spec basis!(term()) :: term()

As basis/1, the value itself, raising Macaw.Draw.Error with its faults.

date(a, b)

@spec date(Date.t(), Date.t()) :: {:ok, interpolator(Date.t())} | error()

The blend of two Dates at day precision, rounded.

date!(a1, a2)

@spec date!(term(), term()) :: term()

As date/2, the value itself, raising Macaw.Draw.Error with its faults.

datetime(a, b)

@spec datetime(DateTime.t(), DateTime.t()) ::
  {:ok, interpolator(DateTime.t())} | error()

The blend of two DateTimes at millisecond precision, rounded.

datetime!(a1, a2)

@spec datetime!(term(), term()) :: term()

As datetime/2, the value itself, raising Macaw.Draw.Error with its faults.

discrete(values)

@spec discrete([term()]) :: {:ok, interpolator(term())} | error()

One of n values: values[clamp(⌊t·n⌋, 0, n − 1)].

discrete!(a1)

@spec discrete!(term()) :: term()

As discrete/1, the value itself, raising Macaw.Draw.Error with its faults.

hsl(a, b)

HSL with the shorter hue arc: an opaque 8-bit literal color.

hsl!(a1, a2)

@spec hsl!(term(), term()) :: term()

As hsl/2, the value itself, raising Macaw.Draw.Error with its faults.

hsl_long(a, b)

HSL with the longer hue arc: an opaque 8-bit literal color.

hsl_long!(a1, a2)

@spec hsl_long!(term(), term()) :: term()

As hsl_long/2, the value itself, raising Macaw.Draw.Error with its faults.

number(a, b)

@spec number(number(), number()) :: {:ok, interpolator(number())} | error()

a + (b − a)·t.

number!(a1, a2)

@spec number!(term(), term()) :: term()

As number/2, the value itself, raising Macaw.Draw.Error with its faults.

rgb(a, b)

Each channel linear, rounded and clamped: an opaque 8-bit literal color.

rgb!(a1, a2)

@spec rgb!(term(), term()) :: term()

As rgb/2, the value itself, raising Macaw.Draw.Error with its faults.

round(a, b)

@spec round(number(), number()) :: {:ok, interpolator(integer())} | error()

round(a + (b − a)·t), an integer.

round!(a1, a2)

@spec round!(term(), term()) :: term()

As round/2, the value itself, raising Macaw.Draw.Error with its faults.

string(a, b)

@spec string(String.t(), String.t()) :: {:ok, interpolator(String.t())} | error()

The numbers embedded in b blended pairwise with those in a, the rest of the text b's: a missing number of a starts from 0 and extras in a are ignored; a whole result prints as an integer, else a float rounded to 4 decimals.

iex> {:ok, fun} = Macaw.Draw.Interpolate.string("10px", "20px")
iex> fun.(0.5)
"15px"

string!(a1, a2)

@spec string!(term(), term()) :: term()

As string/2, the value itself, raising Macaw.Draw.Error with its faults.

transform(a, b)

@spec transform(String.t(), String.t()) :: {:ok, interpolator(String.t())} | error()

Two SVG transform strings blended: the first translate(x[, y]), scale(s[, sy]) and rotate(a) of each, a component in one string only held constant, one in neither the identity; written translate(x, y) scale(sx, sy) rotate(r) without the identity components, or "".

transform!(a1, a2)

@spec transform!(term(), term()) :: term()

As transform/2, the value itself, raising Macaw.Draw.Error with its faults.

zoom(a, b)

@spec zoom([number()], [number()]) :: {:ok, interpolator([float()])} | error()

van Wijk and Nuij's smooth zoom between two views [cx, cy, w], ρ = 1.4: a view. When the centers coincide (distance below 1.0e-6) only w changes, exponentially.

zoom!(a1, a2)

@spec zoom!(term(), term()) :: term()

As zoom/2, the value itself, raising Macaw.Draw.Error with its faults.