View Source Tint.RGB (Tint v1.3.0)
A color in the RGB (red, green, blue) colorspace.
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.
Types
@type t() :: %Tint.RGB{ blue: non_neg_integer(), green: non_neg_integer(), red: non_neg_integer() }
Functions
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)>
@spec 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}
.
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
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
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)>
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)>
@spec 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.
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.
nearest_color(color, palette, distance_algorithm \\ Distance.Euclidean)
View Source (since 1.0.0)@spec nearest_color( Tint.color(), [Tint.color()], Tint.Distance.distance_algorithm() ) :: nil | Tint.color()
Finds the nearest color for the specified color using the given color palette and an optional distance algorithm.
nearest_colors(color, palette, n, distance_algorithm \\ Distance.Euclidean)
View Source (since 1.0.0)@spec nearest_colors( Tint.color(), [Tint.color()], non_neg_integer(), Tint.Distance.distance_algorithm() ) :: [Tint.color()]
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.
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]
Converts a RGB color to a hex code.
Example
iex> Tint.RGB.to_hex(%Tint.RGB{red: 255, green: 127, blue: 30})
"#FF7F1E"
Builds a tuple containing the ratios of the red, green and blue components of a given color.
@spec to_tuple(t()) :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
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}