Tint.RGB (Tint v1.1.0) View Source

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

Link to this section Summary

Functions

Calculates the complementary of the given RGB color.

Calculates the Euclidean distance of two colors.

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.

Determines whether the given color is grayish based on the distance of the red, green an blue channels of the color.

Determines whether the given color is a grayscale color which basically means that the red, green and blue channels of the color have the same value.

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

Finds the n nearest colors 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 a RGB color into a tuple containing the red, green and blue channels.

Link to this section Types

Specs

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

Link to this section Functions

Link to this function

complementary_color(color)

View Source (since 1.1.0)

Specs

complementary_color(t()) :: t()

Calculates the complementary of the given RGB color.

Example

iex> Tint.RGB.complementary_color(%Tint.RGB{red: 255, green: 0, blue: 0})
#Tint.RGB<0,255,255 (#00FFFF)>
Link to this function

euclidean_distance(color, other_color, opts \\ [])

View Source (since 0.2.0)

Specs

euclidean_distance(Tint.color(), Tint.color(), Keyword.t()) :: float()

Calculates the Euclidean distance of two colors.

Options

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

Specs

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("F00")
{:ok, %Tint.RGB{red: 255, green: 0, blue: 0}}

iex> Tint.RGB.from_hex("invalid")
:error

Specs

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 (#FF7F1E)>

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

Specs

from_ratios(number() | String.t(), number() | String.t(), number() | String.t()) ::
  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 (#FF8000)>

Specs

from_tuple(
  {number() | String.t(), number() | String.t(), number() | String.t()}
) :: 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 (#FF7F1E)>
Link to this function

grayish?(color, tolerance)

View Source

Specs

grayish?(t(), non_neg_integer()) :: boolean()

Determines whether the given color is grayish based on the distance of the red, green an blue channels of the color.

Additionally, you have to specify a tolerance that defines how far the min and the max channels may be apart from each other. A tolerance of 0 means that the color has to be an exact grayscale color. A tolerance of 255 means that any color is regarded gray.

Link to this function

grayscale?(color)

View Source (since 1.0.0)

Specs

grayscale?(t()) :: boolean()

Determines whether the given color is a grayscale color which basically means that the red, green and blue channels of the color have the same value.

Link to this function

nearest_color(color, palette, distance_algorithm \\ Distance.Euclidean)

View Source (since 1.0.0)

Specs

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

Link to this function

nearest_colors(color, palette, n, distance_algorithm \\ Distance.Euclidean)

View Source (since 1.0.0)

Specs

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

Specs

new(number() | String.t(), number() | String.t(), number() | String.t()) :: 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 (#000000)>

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

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

Specs

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"

Specs

to_ratios(t()) :: {float(), float(), float()}

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

Specs

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

Example

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