View Source Vivid.RGBA (vivid v0.4.4)

Defines a colour in RGBA colour space.

The three colour channels and the alpha channels are stored as numeric values between 0 and 1.

Example

iex> use Vivid
...> RGBA.black()
Vivid.RGBA.init(0, 0, 0, 1)
iex> RGBA.white()
Vivid.RGBA.init(1, 1, 1, 1)
iex> RGBA.init(1, 0, 0, 0.5)
Vivid.RGBA.init(1, 0, 0, 0.5)

Summary

Functions

Return the alpha component of the colour.

Shorthand for black.

Return the blue component of the colour.

Return the green component of the colour.

Create a colour. Like magic.

Create a colour. Like magic.

Return the luminance of a colour, using some colour mixing ratios I found on stack exchange.

Blend two colours together using their alpha information using the "over" algorithm.

Return the red component of the colour.

Convert a colour to an ASCII character.

Convert a colour to HTML style hex.

Shorthand for white.

Types

@type t() :: %Vivid.RGBA{
  a_blue: zero_to_one(),
  a_green: zero_to_one(),
  a_red: zero_to_one(),
  alpha: zero_to_one(),
  blue: zero_to_one(),
  green: zero_to_one(),
  red: zero_to_one()
}
@type zero_to_one() :: number()

Functions

@spec alpha(t()) :: zero_to_one()

Return the alpha component of the colour.

Example

iex> Vivid.RGBA.init(0.7, 0.6, 0.5, 0.4)
...> |> Vivid.RGBA.alpha
0.4
@spec black() :: t()

Shorthand for black.

Example

iex> Vivid.RGBA.black
Vivid.RGBA.init(0, 0, 0, 1)
@spec blue(t()) :: zero_to_one()

Return the blue component of the colour.

Example

iex> Vivid.RGBA.init(0.7, 0.6, 0.5, 0.4)
...> |> Vivid.RGBA.blue
0.5
@spec green(t()) :: zero_to_one()

Return the green component of the colour.

Example

iex> Vivid.RGBA.init(0.7, 0.6, 0.5, 0.4)
...> |> Vivid.RGBA.green
0.6
@spec init(zero_to_one(), zero_to_one(), zero_to_one()) :: t()

Create a colour. Like magic.

  • red the red component of the colour.
  • green the green component of the colour.
  • blue the blue component of the colour.

All values are between zero and one.

When the alpha argument is omitted it is assumed to be 1 (ie completely opaque).

Example

iex> Vivid.RGBA.init(0.1, 0.2, 0.3, 0.4)
Vivid.RGBA.init(0.1, 0.2, 0.3, 0.4)
Link to this function

init(red, green, blue, alpha)

View Source
@spec init(zero_to_one(), zero_to_one(), zero_to_one(), zero_to_one()) :: t()

Create a colour. Like magic.

  • red the red component of the colour.
  • green the green component of the colour.
  • blue the blue component of the colour.
  • alpha the opacity of the colour.

All values are between zero and one.

Example

iex> Vivid.RGBA.init(0.1, 0.2, 0.3, 0.4)
Vivid.RGBA.init(0.1, 0.2, 0.3, 0.4)
@spec luminance(t()) :: zero_to_one()

Return the luminance of a colour, using some colour mixing ratios I found on stack exchange.

Examples

iex> Vivid.RGBA.init(1,0,0) |> Vivid.RGBA.luminance
0.2128

iex> Vivid.RGBA.white |> Vivid.RGBA.luminance
1.0

iex> Vivid.RGBA.black |> Vivid.RGBA.luminance
0.0
@spec over(t(), t()) :: t()

Blend two colours together using their alpha information using the "over" algorithm.

Examples

iex> Vivid.RGBA.over(Vivid.RGBA.black, Vivid.RGBA.init(1,1,1, 0.5))
Vivid.RGBA.init(0.5, 0.5, 0.5, 1.0)
@spec red(t()) :: zero_to_one()

Return the red component of the colour.

Example

iex> Vivid.RGBA.init(0.7, 0.6, 0.5, 0.4)
...> |> Vivid.RGBA.red
0.7
@spec to_ascii(t()) :: String.t()

Convert a colour to an ASCII character.

This isn't very scientific, but helps with debugging and is used in the implementations of String.Chars for Vivid types.

The chacaters used (from black to white) are " .:-=+*#%@". These are chosen based on the luminance/1 value of the colour.

@spec to_hex(t()) :: String.t()

Convert a colour to HTML style hex.

Example

iex> Vivid.RGBA.init(0.7, 0.6, 0.5)
...> |> Vivid.RGBA.to_hex
"#B39980"
@spec white() :: t()

Shorthand for white.

Example

iex> Vivid.RGBA.white
Vivid.RGBA.init(1, 1, 1, 1)