Color.RGB.WorkingSpace (Color v0.11.0)

Copy Markdown

Summary

Functions

Looks up an RGB working space by its CSS Color 4 name.

Returns the (x, y) chromaticities of a working space's three primaries plus its illuminant atom.

Returns the RGB→XYZ and XYZ→RGB conversion matrices for a named RGB working space, computed from its primaries and reference white using the Lindbloom formulas.

Returns the CSS Color 4 name for an internal working-space atom, or nil if there isn't one.

Functions

from_css_name(name)

@spec from_css_name(String.t()) :: {:ok, atom()} | {:error, Exception.t()}

Looks up an RGB working space by its CSS Color 4 name.

Arguments

  • name is a string.

Returns

  • {:ok, atom} — the internal working-space atom.

  • {:error, reason} if the name is unknown.

Examples

iex> Color.RGB.WorkingSpace.from_css_name("display-p3")
{:ok, :P3_D65}

iex> Color.RGB.WorkingSpace.from_css_name("srgb")
{:ok, :SRGB}

primaries(working_space)

@spec primaries(atom()) :: %{
  red: {float(), float()},
  green: {float(), float()},
  blue: {float(), float()},
  illuminant: atom()
}

Returns the (x, y) chromaticities of a working space's three primaries plus its illuminant atom.

Arguments

  • working_space is an atom identifying the working space, for example :SRGB, :P3_D65, :Rec2020.

Returns

  • %{red: {x, y}, green: {x, y}, blue: {x, y}, illuminant: atom}.

Examples

iex> p = Color.RGB.WorkingSpace.primaries(:SRGB)
iex> p.red
{0.64, 0.33}

iex> Color.RGB.WorkingSpace.primaries(:P3_D65).illuminant
:D65

rgb_conversion_matrix(rgb_space)

@spec rgb_conversion_matrix(atom()) ::
  {:ok,
   %{
     to_xyz: list(),
     from_xyz: list(),
     illuminant: atom(),
     observer_angle: 2 | 10,
     gamma: term()
   }}
  | {:error, Exception.t()}

Returns the RGB→XYZ and XYZ→RGB conversion matrices for a named RGB working space, computed from its primaries and reference white using the Lindbloom formulas.

Arguments

  • rgb_space is the working-space atom (for example :sRGB, :Adobe, :ProPhoto).

Returns

  • An {:ok, %{to_xyz: m, from_xyz: mi, illuminant: atom, observer_angle: 2}} tuple where m and mi are 3x3 matrices represented as lists of rows.

  • {:error, reason} when the working space is unknown.

Examples

iex> {:ok, %{to_xyz: [[a, _, _] | _]}} = Color.RGB.WorkingSpace.rgb_conversion_matrix(:SRGB)
iex> Float.round(a, 4)
0.4125

rgb_working_spaces()

to_css_name(atom)

@spec to_css_name(atom()) :: String.t() | nil

Returns the CSS Color 4 name for an internal working-space atom, or nil if there isn't one.

Arguments

  • atom is a working-space atom.

Returns

  • A string or nil.

Examples

iex> Color.RGB.WorkingSpace.to_css_name(:P3_D65)
"display-p3"

iex> Color.RGB.WorkingSpace.to_css_name(:SRGB)
"srgb"

iex> Color.RGB.WorkingSpace.to_css_name(:Bruce)
nil