Tint v0.3.1 Tint.RGB View Source

A color in the RGB (red, green, blue) colorspace.

Link to this section Summary

Functions

Calculates the distance of two colors using the Euclidean distance algorithm.

Builds a new RGB color from the given hex code.

Builds a new RGB color from the given hex code. Raises when the given hex code is invalid.

Builds a new RGB color from red, green and blue color ratios.

Converts a tuple containing hue, saturation and value into a Tint.RGB struct.

A version of the Euclidean distance algorithm that uses weights that are optimized for human color perception.

Finds the nearest color for the specified color using the given color palette and an optional distance algorithm.

Builds a new RGB color from red, green and blue color parts. Please always use this function to build a new RGB color.

Converts a RGB color to a hex code.

Builds a tuple containing the ratios of the red, green and blue components of a given color.

Converts RGB color into a tuple containing the red, green and blue parts.

Link to this section Types

Link to this type

t() View Source
t() :: %Tint.RGB{
  blue: non_neg_integer(),
  green: non_neg_integer(),
  red: non_neg_integer()
}

Link to this section Functions

Link to this function

euclidean_distance(color, other_color, opts \\ []) View Source (since 0.2.0)

Calculates the distance of two colors using the Euclidean distance algorithm.

Options

  • :weights - A tuple defining the weights for the red, green and blue color components. Defaults to {1, 1, 1}.
Link to this function

from_hex(code) View Source
from_hex(String.t()) :: {:ok, t()} | :error

Builds a new RGB color from the given hex code.

Examples

iex> Tint.RGB.from_hex("#FF7F1E")
{:ok, %Tint.RGB{red: 255, green: 127, blue: 30}}

iex> Tint.RGB.from_hex("invalid")
:error
Link to this function

from_hex!(code) View Source
from_hex!(String.t()) :: t() | no_return()

Builds a new RGB color from the given hex code. Raises when the given hex code is invalid.

Examples

iex> Tint.RGB.from_hex!("#FF7F1E")
#Tint.RGB<255,127,30>

iex> Tint.RGB.from_hex!("invalid")
** (ArgumentError) Invalid hex code: invalid
Link to this function

from_ratios(red_ratio, green_ratio, blue_ratio) View Source
from_ratios(
  Decimal.t() | number(),
  Decimal.t() | number(),
  Decimal.t() | number()
) :: t()

Builds a new RGB color from red, green and blue color ratios.

Example

iex> Tint.RGB.from_ratios(1, 0.5, 0)
#Tint.RGB<255,128,0>
Link to this function

from_tuple(arg) View Source
from_tuple(
  {Decimal.t() | number(), Decimal.t() | number(), Decimal.t() | number()}
) :: t()

Converts a tuple containing hue, saturation and value into a Tint.RGB struct.

Example

iex> Tint.RGB.from_tuple({255, 127, 30})
#Tint.RGB<255,127,30>
Link to this function

human_euclidean_distance(color, other_color) View Source (since 0.2.0)
human_euclidean_distance(t(), Tint.RGB.Convertible.t()) :: float()

A version of the Euclidean distance algorithm that uses weights that are optimized for human color perception.

Link to this function

nearest(color, palette, distance_algorithm \\ &human_euclidean_distance/2) View Source (since 0.2.0)
nearest(t(), [Tint.RGB.Convertible.t()], (t(), t() -> number())) ::
  nil | Tint.RGB.Convertible.t()

Finds the nearest color for the specified color using the given color palette and an optional distance algorithm.

Link to this function

new(red, green, blue) View Source
new(Decimal.t() | number(), Decimal.t() | number(), Decimal.t() | number()) ::
  t()

Builds a new RGB color from red, green and blue color parts. Please always use this function to build a new RGB color.

Examples

iex> Tint.RGB.new(0, 0, 0)
#Tint.RGB<0,0,0>

iex> Tint.RGB.new(255, 127, 30)
#Tint.RGB<255,127,30>

iex> Tint.RGB.new(256, -1, 0)
** (Tint.OutOfRangeError) Value 256 is out of range [0,255]
Link to this function

to_hex(color) View Source
to_hex(t()) :: String.t()

Converts a RGB color to a hex code.

Example

iex> Tint.RGB.to_hex(%Tint.RGB{red: 255, green: 127, blue: 30})
"#FF7F1E"
Link to this function

to_ratios(color) View Source
to_ratios(t()) :: {Decimal.t(), Decimal.t(), Decimal.t()}

Builds a tuple containing the ratios of the red, green and blue components of a given color.

Converts RGB color into a tuple containing the red, green and blue parts.

Example

iex> Tint.RGB.to_tuple(%Tint.RGB{red: 255, green: 127, blue: 30})
{255, 127, 30}