Color conversion functions between various color formats.
Supported Formats
- RGB - Red, Green, Blue (0-255)
- Hex - Hexadecimal color codes (#RRGGBB)
- HSL - Hue, Saturation, Lightness (H: 0-360°, S: 0-100%, L: 0-100%)
- HSV - Hue, Saturation, Value (H: 0-360°, S: 0-100%, V: 0-100%)
- CMYK - Cyan, Magenta, Yellow, Key (C: 0-100%, M: 0-100%, Y: 0-100%, K: 0-100%)
- XTerm256 - 256-color terminal palette index
Usage
iex> Pote.Conversions.rgb_to_hex({255, 128, 0})
"#FF8000"
iex> Pote.Conversions.hex_to_rgb("#FF8000")
{:ok, {255, 128, 0}}Contracts
Type aliases defined here reference the canonical types in Pote.
Summary
Functions
Blends two RGB colors with a given factor.
Clamps an integer value to the 0-255 range.
Converts CMYK to RGB.
Calculates Manhattan distance between two RGB colors. Used to find nearest color match.
Computes the WCAG 2.1 contrast ratio between two colors.
Computes the Delta E 1976 distance between two colors.
Converts hexadecimal string to RGB tuple.
Converts HSL to RGB.
Converts HSV to RGB.
Converts HWB to RGB.
Converts a color temperature in Kelvin to an RGB approximation.
Converts CIELAB to RGB (sRGB D65).
Computes the WCAG 2.1 relative luminance of a color.
Converts RGB to CMYK.
Converts RGB color to hexadecimal string.
Converts RGB to HSL (Hue, Saturation, Lightness).
Converts RGB to HSV (Hue, Saturation, Value).
Converts RGB to HWB (Hue, Whiteness, Blackness).
Approximates the correlated color temperature (CCT) in Kelvin from an RGB color.
Converts RGB to CIELAB (D65 illuminant).
Finds the closest Pantone match for an RGB color.
Converts RGB to XTerm256 color index.
Converts RGB to CIE XYZ using the sRGB D65 matrix.
Converts RGB to YCbCr (BT.601, digital video).
Converts RGB to YUV (BT.601, PAL/NTSC broadcast).
Converts XTerm256 color index to RGB tuple.
Converts CIE XYZ to RGB (sRGB D65).
Converts YCbCr (BT.601) to RGB.
Converts YUV (BT.601) to RGB.
Types
Functions
@spec blend( {integer(), integer(), integer()}, {integer(), integer(), integer()}, float() ) :: {integer(), integer(), integer()}
Blends two RGB colors with a given factor.
Parameters
color1- First RGB color tuplecolor2- Second RGB color tuplefactor- Blend factor (0.0 = color1, 1.0 = color2)
Returns
- Blended RGB color tuple
Examples
iex> blend({255, 0, 0}, {0, 0, 255}, 0.5)
{128, 0, 128}
@spec clamp(integer()) :: 0..255
Clamps an integer value to the 0-255 range.
Examples
iex> Pote.Conversions.clamp(300)
255
iex> Pote.Conversions.clamp(-10)
0
iex> Pote.Conversions.clamp(128)
128
Converts CMYK to RGB.
Parameters
cmyk- CMYK tuple {c, m, y, k} where each value is 0-100
Returns
- RGB tuple {r, g, b} where each value is 0-255
Examples
iex> cmyk_to_rgb({0.0, 49.8, 100.0, 0.0})
{255, 128, 0}
@spec color_distance(rgb(), rgb()) :: non_neg_integer()
Calculates Manhattan distance between two RGB colors. Used to find nearest color match.
Computes the WCAG 2.1 contrast ratio between two colors.
Parameters
rgb1- First RGB color tuplergb2- Second RGB color tuple
Returns
- Contrast ratio (float). WCAG AA requires 4.5:1 for normal text, 7:1 for AAA.
Examples
iex> contrast_ratio({255, 255, 255}, {0, 0, 0})
21.0
Computes the Delta E 1976 distance between two colors.
This is the Euclidean distance in CIELAB space.
Parameters
rgb1- First RGB color tuplergb2- Second RGB color tuple
Returns
- Delta E value (float). Values < 1.0 are imperceptible.
Examples
iex> delta_e({255, 0, 0}, {255, 0, 0})
0.0
Converts hexadecimal string to RGB tuple.
Parameters
hex- Hex string in format "#RRGGBB" or "RRGGBB"
Returns
{:ok, rgb}- RGB tuple on success{:error, reason}- Error tuple if conversion fails
Examples
iex> hex_to_rgb("#FF8000")
{:ok, {255, 128, 0}}
iex> hex_to_rgb("FF8000")
{:ok, {255, 128, 0}}
iex> hex_to_rgb("invalid")
{:error, :invalid_hex_format}
Converts HSL to RGB.
Parameters
hsl- HSL tuple {h, s, l} where h is 0-360, s and l are 0-100
Returns
- RGB tuple {r, g, b} where each value is 0-255
Examples
iex> hsl_to_rgb({30.0, 100.0, 50.0})
{255, 128, 0}
Converts HSV to RGB.
Parameters
hsv- HSV tuple {h, s, v} where h is 0-360, s and v are 0-100
Returns
- RGB tuple {r, g, b} where each value is 0-255
Examples
iex> hsv_to_rgb({30.0, 100.0, 100.0})
{255, 128, 0}
Converts HWB to RGB.
Parameters
hwb- HWB tuple {h, w, b} where h is 0-360, w and b are 0.0-1.0
Returns
- RGB tuple {r, g, b} where each value is 0-255
Examples
iex> hwb_to_rgb({0.0, 0.0, 0.0})
{255, 0, 0}
iex> hwb_to_rgb({0.0, 0.5, 0.5})
{128, 64, 64}
@spec kelvin_to_rgb(pos_integer()) :: rgb()
Converts a color temperature in Kelvin to an RGB approximation.
Based on Tanner Helland's algorithm for black-body radiation approximation.
Parameters
kelvin- Temperature in Kelvin (1000-40000)
Returns
- RGB tuple {r, g, b}
Examples
iex> kelvin_to_rgb(6500)
{255, 249, 253}
Converts CIELAB to RGB (sRGB D65).
Parameters
lab- Lab tuple {l, a, b}
Returns
- RGB tuple {r, g, b} where each value is 0-255
Computes the WCAG 2.1 relative luminance of a color.
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- Luminance value between 0.0 and 1.0
Examples
iex> relative_luminance({255, 255, 255})
1.0
Converts RGB to CMYK.
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- CMYK tuple {c, m, y, k} where each value is 0-100
Examples
iex> rgb_to_cmyk({255, 128, 0})
{0.0, 49.8, 100.0, 0.0}
Converts RGB color to hexadecimal string.
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- Hex string in format "#RRGGBB"
Examples
iex> rgb_to_hex({255, 128, 0})
"#FF8000"
iex> rgb_to_hex({0, 0, 0})
"#000000"
Converts RGB to HSL (Hue, Saturation, Lightness).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- HSL tuple {h, s, l} where h is 0-360, s and l are 0-100
Examples
iex> rgb_to_hsl({255, 128, 0})
{30.0, 100.0, 50.0}
Converts RGB to HSV (Hue, Saturation, Value).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- HSV tuple {h, s, v} where h is 0-360, s and v are 0-100
Examples
iex> rgb_to_hsv({255, 128, 0})
{30.0, 100.0, 100.0}
Converts RGB to HWB (Hue, Whiteness, Blackness).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- HWB tuple {h, w, b} where h is 0-360, w and b are 0.0-1.0
Examples
iex> rgb_to_hwb({255, 0, 0})
{0.0, 1.0, 0.0}
iex> rgb_to_hwb({128, 128, 128})
{0.0, 0.502, 0.498}
@spec rgb_to_kelvin(rgb()) :: pos_integer() | nil
Approximates the correlated color temperature (CCT) in Kelvin from an RGB color.
Uses an iterative search over the kelvin_to_rgb/1 function to find the
closest matching temperature. Returns nil for colors that are not
reasonably close to a black-body radiator.
Examples
iex> rgb_to_kelvin({255, 160, 60})
3200
Converts RGB to CIELAB (D65 illuminant).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- Lab tuple {l, a, b} where L is 0-100, a and b are roughly -128 to 127
Examples
iex> rgb_to_lab({255, 128, 0})
{66.89, 37.14, 75.29}
Finds the closest Pantone match for an RGB color.
Uses a curated list of popular Pantone colors and searches for the closest match using Delta E distance in CIELAB space.
Parameters
rgb- RGB tuple {r, g, b}
Returns
{pantone_name, distance}tuple, ornilif no close match
Examples
iex> rgb_to_pantone_approx({255, 0, 0})
{"Red 032 C", 0.0}
Converts RGB to XTerm256 color index.
Uses the closest match in the 256-color terminal palette.
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- XTerm256 index (0-255)
Examples
iex> rgb_to_xterm256({255, 128, 0})
208
Converts RGB to CIE XYZ using the sRGB D65 matrix.
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- XYZ tuple {x, y, z}
Examples
iex> rgb_to_xyz({255, 128, 0})
{0.487, 0.356, 0.041}
Converts RGB to YCbCr (BT.601, digital video).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- YCbCr tuple {y, cb, cr} where Y is 16-235, Cb/Cr are 16-240
Examples
iex> rgb_to_ycbcr({255, 128, 0})
{165, 69, 224}
Converts RGB to YUV (BT.601, PAL/NTSC broadcast).
Parameters
rgb- RGB tuple {r, g, b} where each value is 0-255
Returns
- YUV tuple {y, u, v} where Y is 0-255, U and V are -128 to 127
Examples
iex> rgb_to_yuv({255, 128, 0})
{165, 13, 146}
Converts XTerm256 color index to RGB tuple.
Converts CIE XYZ to RGB (sRGB D65).
Parameters
xyz- XYZ tuple {x, y, z}
Returns
- RGB tuple {r, g, b} where each value is 0-255
Converts YCbCr (BT.601) to RGB.
Parameters
ycbcr- YCbCr tuple {y, cb, cr}
Returns
- RGB tuple {r, g, b}
Converts YUV (BT.601) to RGB.
Parameters
yuv- YUV tuple {y, u, v}
Returns
- RGB tuple {r, g, b}