Pote.Converters.Advanced (Pote v3.0.0)

Copy Markdown View Source

Advanced color conversions for special color spaces.

Includes:

  • CIE XYZ
  • CIELAB
  • YUV (BT.601)
  • YCbCr (BT.601)
  • Color temperature (Kelvin)
  • Delta E (color distance)
  • WCAG contrast

Summary

Functions

Calculates the WCAG 2.1 contrast ratio between two colors.

Calculates the Delta E 1976 distance between two colors.

Converts CIELAB to RGB (sRGB D65).

Converts OKLab to RGB (sRGB D65).

Converts OKLCH to RGB (sRGB D65).

Converts CIE XYZ to RGB (sRGB D65).

Converts YCbCr (BT.601) to RGB.

Converts YUV (BT.601) to RGB.

Converts temperature in Kelvin to RGB.

Finds the closest Pantone color for an RGB color.

Finds the closest Pantone name for an RGB color. Returns only the name, or nil if there is no close match.

Calculates the WCAG 2.1 relative luminance of a color.

Approximates the correlated color temperature (CCT) in Kelvin from an RGB color.

Converts RGB to CIELAB (D65 illuminant).

Converts RGB to OKLab (Ottosson's perceptual space).

Converts RGB to OKLCH (OKLab in polar form).

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).

Returns the WCAG 2.1 conformance level for a given contrast ratio.

Types

lab()

@type lab() :: {float(), float(), float()}

oklab()

@type oklab() :: {float(), float(), float()}

oklch()

@type oklch() :: {float(), float(), float()}

rgb()

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

xyz()

@type xyz() :: {float(), float(), float()}

ycbcr()

@type ycbcr() :: {integer(), integer(), integer()}

yuv()

@type yuv() :: {integer(), integer(), integer()}

Functions

contrast_ratio(rgb1, rgb2)

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

Calculates the WCAG 2.1 contrast ratio between two colors.

WCAG AA requires 4.5:1 for normal text, 7:1 for AAA.

delta_e(rgb1, rgb2)

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

Calculates the Delta E 1976 distance between two colors.

This is the Euclidean distance in CIELAB space. Values < 1.0 are imperceptible.

from_lab(arg)

@spec from_lab(lab()) :: rgb()

Converts CIELAB to RGB (sRGB D65).

from_oklab(arg)

@spec from_oklab(oklab()) :: rgb()

Converts OKLab to RGB (sRGB D65).

from_oklch(arg)

@spec from_oklch(oklch()) :: rgb()

Converts OKLCH to RGB (sRGB D65).

from_xyz(arg)

@spec from_xyz(xyz()) :: rgb()

Converts CIE XYZ to RGB (sRGB D65).

from_ycbcr(arg)

@spec from_ycbcr(ycbcr()) :: rgb()

Converts YCbCr (BT.601) to RGB.

from_yuv(arg)

@spec from_yuv(yuv()) :: rgb()

Converts YUV (BT.601) to RGB.

kelvin_to_rgb(kelvin)

@spec kelvin_to_rgb(pos_integer()) :: rgb()

Converts temperature in Kelvin to RGB.

Based on Tanner Helland's algorithm for black-body radiation approximation.

nearest_pantone(rgb)

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

Finds the closest Pantone color 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

  • tuple {pantone_name, distance} or nil if there is no close match

Example

iex> Pote.Converters.Advanced.nearest_pantone({255, 0, 0})
{"Red 032 C", 0.0}

nearest_pantone_name(rgb)

@spec nearest_pantone_name(rgb()) :: String.t() | nil

Finds the closest Pantone name for an RGB color. Returns only the name, or nil if there is no close match.

Example

iex> Pote.Converters.Advanced.nearest_pantone_name({255, 0, 0})
"Red 032 C"

relative_luminance(arg)

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

Calculates the WCAG 2.1 relative luminance of a color.

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 kelvin_to_rgb/1 to find the closest matching temperature.

to_lab(rgb)

@spec to_lab(rgb()) :: lab()

Converts RGB to CIELAB (D65 illuminant).

to_oklab(arg)

@spec to_oklab(rgb()) :: oklab()

Converts RGB to OKLab (Ottosson's perceptual space).

OKLab is a perceptual color space designed for smooth, uniform interpolation: midpoints stay perceptually consistent, avoiding the "muddy middle" of RGB/HSL interpolation.

to_oklch(rgb)

@spec to_oklch(rgb()) :: oklch()

Converts RGB to OKLCH (OKLab in polar form).

Returns {l, c, h} where l is perceptual lightness (0..1), c is chroma, and h is hue in degrees (0..360).

to_xyz(arg)

@spec to_xyz(rgb()) :: xyz()

Converts RGB to CIE XYZ using the sRGB D65 matrix.

to_ycbcr(arg)

@spec to_ycbcr(rgb()) :: ycbcr()

Converts RGB to YCbCr (BT.601, digital video).

to_yuv(arg)

@spec to_yuv(rgb()) :: yuv()

Converts RGB to YUV (BT.601, PAL/NTSC broadcast).

wcag_level(ratio, size \\ :normal)

@spec wcag_level(float(), :normal | :large) :: :aaa | :aa | :fail

Returns the WCAG 2.1 conformance level for a given contrast ratio.

Levels (for normal text):

  • :aaa — 7.0+ (enhanced)
  • :aa — 4.5+ (minimum)
  • :fail — < 4.5

For large text (18pt+ or 14pt+ bold), the thresholds are lower:

  • :aaa — 4.5+
  • :aa — 3.0+
  • :fail — < 3.0

Examples

iex> Pote.Converters.Advanced.wcag_level(7.5)
:aaa

iex> Pote.Converters.Advanced.wcag_level(4.5)
:aa

iex> Pote.Converters.Advanced.wcag_level(2.0)
:fail