Tint v1.0.0-rc.0 Tint.RGB View Source

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

Link to this section Summary

Functions

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

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)
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}.
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 (#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
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 (#FF8000)>
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 (#FF7F1E)>
Link to this function

grayish?(color, tolerance) View Source
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)
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_calculator \\ Distance.Euclidean) View Source (since 1.0.0)

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_calculator \\ Distance.Euclidean) View Source (since 1.0.0)

Finds the n nearest colors 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 (#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]
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 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}