View Source Ultraviolet.Scale (Ultraviolet v0.1.1)

Function related to creating and using color scales.

Color scales are essentually functions that map numbers to a color palette.

Summary

Functions

Retrieves a single color from the scale at the given value in the domain.

Retrieves a single color from the scale at the given value in the domain. If the given value is invalid, returns the default color. If the given value is outside of the domain, returns the closest domain bound, i.e. the highest or lowest value in the domain.

Creates a new color scale. See Ultraviolet.scale/2 for details about creating scales.

Returns a list of n equi-distant colors from the scale.

Returns a list of colors that correspond to each value in xs. in xs. If xs contains values that are not within the scale's domain, they are clipped to the domain bounds.

Types

@type t() :: %Ultraviolet.Scale{
  classes: non_neg_integer() | [number()],
  colors: [Ultraviolet.Color.t()],
  correct_lightness?: boolean(),
  domain: [number()],
  gamma: number(),
  interpolation: term(),
  longer?: boolean(),
  padding: tuple() | number(),
  positions: [number()],
  space: Ultraviolet.Color.space()
}

Functions

@spec fetch(t(), number()) :: {:ok, Ultraviolet.Color.t()} | {:error, term()}

Retrieves a single color from the scale at the given value in the domain.

Example

iex>{:ok, scale} = Ultraviolet.scale();
iex>{:ok, color} = Ultraviolet.Scale.fetch(scale, 0.25);
iex>Ultraviolet.Color.hex(color)
"#bfbfbf"
Link to this function

get(scale, x, default \\ %Color{})

View Source

Retrieves a single color from the scale at the given value in the domain. If the given value is invalid, returns the default color. If the given value is outside of the domain, returns the closest domain bound, i.e. the highest or lowest value in the domain.

Example

iex>{:ok, scale} = Ultraviolet.scale();
iex>Ultraviolet.Color.hex(Ultraviolet.Scale.get(scale, 0.25))
"#bfbfbf"
Link to this function

new(colors, options \\ [])

View Source

Creates a new color scale. See Ultraviolet.scale/2 for details about creating scales.

Returns a list of n equi-distant colors from the scale.

Example

iex>{:ok, scale} = Ultraviolet.scale("OrRd");
iex>Enum.map(Ultraviolet.Scale.take(scale, 5), &Ultraviolet.Color.hex/1)
["#fff7ec", "#fdd49e", "#fc8d59", "#d7301f", "#7f0000"]

Returns a list of colors that correspond to each value in xs. in xs. If xs contains values that are not within the scale's domain, they are clipped to the domain bounds.

Example

iex>{:ok, scale} = Ultraviolet.scale(["yellow", "008ae5"]);
iex>Ultraviolet.Scale.take_keys(scale, [0, 1])
[
  %Ultraviolet.Color{r: 255, g: 255, b: 0},
  %Ultraviolet.Color{r: 0, g: 138, b: 229},
]