Pote.Conversions (Pote v1.0.0)

Copy Markdown View Source

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

cmyk()

@type cmyk() :: Pote.cmyk()

hex()

@type hex() :: Pote.hex()

hsl()

@type hsl() :: Pote.hsl()

hsv()

@type hsv() :: Pote.hsv()

rgb()

@type rgb() :: Pote.rgb()

xterm256()

@type xterm256() :: Pote.xterm256()

Functions

blend(arg1, arg2, factor)

@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 tuple
  • color2 - Second RGB color tuple
  • factor - 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}

clamp(value)

@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

cmyk_to_rgb(arg)

@spec cmyk_to_rgb(cmyk()) :: rgb()

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}

color_distance(arg1, arg2)

@spec color_distance(rgb(), rgb()) :: non_neg_integer()

Calculates Manhattan distance between two RGB colors. Used to find nearest color match.

contrast_ratio(rgb1, rgb2)

@spec contrast_ratio(rgb(), rgb()) :: float()

Computes the WCAG 2.1 contrast ratio between two colors.

Parameters

  • rgb1 - First RGB color tuple
  • rgb2 - 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

delta_e(rgb1, rgb2)

@spec delta_e(rgb(), rgb()) :: float()

Computes the Delta E 1976 distance between two colors.

This is the Euclidean distance in CIELAB space.

Parameters

  • rgb1 - First RGB color tuple
  • rgb2 - 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

hex_to_rgb(hex)

@spec hex_to_rgb(hex()) :: {:ok, rgb()} | {:error, :invalid_hex_format}

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}

hsl_to_rgb(arg)

@spec hsl_to_rgb(hsl()) :: rgb()

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}

hsv_to_rgb(arg)

@spec hsv_to_rgb(hsv()) :: rgb()

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}

hwb_to_rgb(arg)

@spec hwb_to_rgb({number(), number(), number()}) :: rgb()

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}

kelvin_to_rgb(kelvin)

@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}

lab_to_rgb(arg)

@spec lab_to_rgb({float(), float(), float()}) :: rgb()

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

relative_luminance(arg)

@spec relative_luminance(rgb()) :: float()

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

rgb_to_cmyk(arg)

@spec rgb_to_cmyk(rgb()) :: cmyk()

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}

rgb_to_hex(arg)

@spec rgb_to_hex(rgb()) :: hex()

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"

rgb_to_hsl(arg)

@spec rgb_to_hsl(rgb()) :: hsl()

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}

rgb_to_hsv(arg)

@spec rgb_to_hsv(rgb()) :: hsv()

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}

rgb_to_hwb(arg)

@spec rgb_to_hwb(rgb()) :: {float(), float(), float()}

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}

rgb_to_kelvin(rgb)

@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

rgb_to_lab(rgb)

@spec rgb_to_lab(rgb()) :: {float(), float(), float()}

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}

rgb_to_pantone_approx(rgb)

@spec rgb_to_pantone_approx(rgb()) :: {String.t(), float()} | nil

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, or nil if no close match

Examples

iex> rgb_to_pantone_approx({255, 0, 0})
{"Red 032 C", 0.0}

rgb_to_xterm256(arg)

@spec rgb_to_xterm256(rgb()) :: xterm256()

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

rgb_to_xyz(arg)

@spec rgb_to_xyz(rgb()) :: {float(), float(), float()}

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}

rgb_to_ycbcr(arg)

@spec rgb_to_ycbcr(rgb()) :: {integer(), integer(), integer()}

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}

rgb_to_yuv(arg)

@spec rgb_to_yuv(rgb()) :: {integer(), integer(), integer()}

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}

xterm256_to_rgb(index)

@spec xterm256_to_rgb(xterm256()) :: rgb()

Converts XTerm256 color index to RGB tuple.

xyz_to_rgb(arg)

@spec xyz_to_rgb({float(), float(), float()}) :: rgb()

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

ycbcr_to_rgb(arg)

@spec ycbcr_to_rgb({integer(), integer(), integer()}) :: rgb()

Converts YCbCr (BT.601) to RGB.

Parameters

  • ycbcr - YCbCr tuple {y, cb, cr}

Returns

  • RGB tuple {r, g, b}

yuv_to_rgb(arg)

@spec yuv_to_rgb({integer(), integer(), integer()}) :: rgb()

Converts YUV (BT.601) to RGB.

Parameters

  • yuv - YUV tuple {y, u, v}

Returns

  • RGB tuple {r, g, b}