Macaw.Draw.Scale.Color (macaw_draw v0.1.0)

Copy Markdown View Source

Sequential and diverging color scales, and the d3 schemes (spec/06 §14).

A scale maps a number to a literal %Macaw.Layers.Color{}, never a string (macaw_layers spec/04 §1.1): its interpolator is a scheme's stops, a list of literal colors, or a 1-arity function of t ∈ [0, 1] answering one. A sequential scale's domain is [d0, d1]; a diverging one's [d0, d1, d2], d1 the pivot at t = 0.5. Clamping is on by default.

{:ok, scale} = Macaw.Draw.Scale.Color.sequential("blues")
scale |> Macaw.Draw.Scale.Color.apply(1) |> Macaw.Layers.Color.to_hex()
#=> "#084594"

Stops interpolate per channel to an 8-bit color, opacity included, the answer equal to visualize's for the same stops and t. A categorical scheme is a stop list like any other: to pick its colors discretely, give scheme/1 as an Ordinal scale's range.

Summary

Types

A refusal: faults with their paths.

An interpolator as a scale holds it: literal stops, or a function of t.

t()

Functions

The literal color of a number (spec/06 §14.3): sequential t = normalize(v, d0, d1), the interpolator's middle in a collapsed domain; diverging by the half the value lies in, a value beyond a collapsed half at that half's end; t clamped when the scale clamps. nil for a value that is no number, and for a function interpolator's answer that is no literal color.

0: a color scale has no bandwidth.

Sets clamping of t to [0, 1] (on by default); answers the struct.

A diverging scale over [0, 0.5, 1] from an interpolator, as sequential/1 takes.

diverging/1, answering the scale or raising Macaw.Draw.Error.

Sets the domain, two numbers for a sequential scale and three for a diverging one: {:ok, scale}, or {:error, faults}, another length {[], {:type, :domain, type}}.

domain/2, answering the scale or raising Macaw.Draw.Error.

nil: a color scale is not invertible.

Identity.

Identity: a color scale has no padding.

Replaces the interpolator, as sequential/1 takes one: {:ok, scale} or {:error, faults}.

range/2, answering the scale or raising Macaw.Draw.Error.

A scheme's stops as literal colors, by its name, an atom or its string; nil for a name no scheme has.

The 25 scheme names, in the order of spec/06 §14.4.

A sequential scale over [0, 1] from an interpolator: a scheme's name (atom or string), a list of literal colors, or a 1-arity function. {:ok, scale} or {:error, faults} (spec/06 §14.2).

sequential/1, answering the scale or raising Macaw.Draw.Error.

Macaw.Draw.Scale.Linear.ticks/2 over the domain's extent, [d0, d1] or [d0, d2].

Types

error()

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

A refusal: faults with their paths.

interpolator()

@type interpolator() ::
  [Macaw.Layers.Color.t()] | (number() -> Macaw.Layers.Color.t())

An interpolator as a scale holds it: literal stops, or a function of t.

t()

@type t() :: %Macaw.Draw.Scale.Color{
  clamp?: boolean(),
  domain: [number()],
  interpolator: interpolator(),
  type: :sequential | :diverging
}

Functions

apply(scale, value)

@spec apply(t(), term()) :: Macaw.Layers.Color.t() | nil | error()

The literal color of a number (spec/06 §14.3): sequential t = normalize(v, d0, d1), the interpolator's middle in a collapsed domain; diverging by the half the value lies in, a value beyond a collapsed half at that half's end; t clamped when the scale clamps. nil for a value that is no number, and for a function interpolator's answer that is no literal color.

bandwidth(arg1)

@spec bandwidth(t()) :: 0 | error()

0: a color scale has no bandwidth.

clamp(scale, clamp?)

@spec clamp(t(), boolean()) :: t() | error()

Sets clamping of t to [0, 1] (on by default); answers the struct.

diverging(interpolator)

@spec diverging(term()) :: {:ok, t()} | error()

A diverging scale over [0, 0.5, 1] from an interpolator, as sequential/1 takes.

diverging!(interpolator)

@spec diverging!(term()) :: t()

diverging/1, answering the scale or raising Macaw.Draw.Error.

domain(scale, domain)

@spec domain(t(), term()) :: {:ok, t()} | error()

Sets the domain, two numbers for a sequential scale and three for a diverging one: {:ok, scale}, or {:error, faults}, another length {[], {:type, :domain, type}}.

domain!(scale, domain)

@spec domain!(t(), term()) :: t()

domain/2, answering the scale or raising Macaw.Draw.Error.

invert(arg1, value)

@spec invert(t(), term()) :: nil | error()

nil: a color scale is not invertible.

nice(scale)

@spec nice(t()) :: t() | error()

Identity.

padding(scale, padding)

@spec padding(t(), number()) :: t() | error()

Identity: a color scale has no padding.

range(scale, interpolator)

@spec range(t(), term()) :: {:ok, t()} | error()

Replaces the interpolator, as sequential/1 takes one: {:ok, scale} or {:error, faults}.

range!(scale, interpolator)

@spec range!(t(), term()) :: t()

range/2, answering the scale or raising Macaw.Draw.Error.

scheme(name)

@spec scheme(term()) :: [Macaw.Layers.Color.t()] | nil

A scheme's stops as literal colors, by its name, an atom or its string; nil for a name no scheme has.

schemes()

@spec schemes() :: [atom()]

The 25 scheme names, in the order of spec/06 §14.4.

sequential(interpolator)

@spec sequential(term()) :: {:ok, t()} | error()

A sequential scale over [0, 1] from an interpolator: a scheme's name (atom or string), a list of literal colors, or a 1-arity function. {:ok, scale} or {:error, faults} (spec/06 §14.2).

sequential!(interpolator)

@spec sequential!(term()) :: t()

sequential/1, answering the scale or raising Macaw.Draw.Error.

ticks(scale, count)

@spec ticks(t(), number()) :: [number()] | error()

Macaw.Draw.Scale.Linear.ticks/2 over the domain's extent, [d0, d1] or [d0, d2].