Color.RGB (Color v0.4.0)

Copy Markdown

Linear (un-companded) RGB relative to a named working space.

See Color.SRGB for the companded sRGB working-space color type. This module covers every RGB working space listed in Color.RGB.WorkingSpace: :SRGB, :Adobe, :ProPhoto, :Rec709, :Rec2020 etc. Channels are unit floats.

The working-space conversion matrix is computed lazily and memoised in :persistent_term, so the first call for a given working space pays the Lindbloom derivation cost once and subsequent calls are a plain :persistent_term.get/1.

Summary

Types

t()

Linear RGB in any named working space. The working_space field identifies which set of primaries and reference white to use; see Color.RGB.WorkingSpace.rgb_working_spaces/0 for the full list.

Functions

Converts a CIE XYZ color to linear RGB in the given working space.

Converts a linear RGB color to CIE XYZ.

Types

t()

@type t() :: %Color.RGB{
  alpha: Color.Types.alpha(),
  b: float() | nil,
  g: float() | nil,
  r: float() | nil,
  working_space: Color.Types.working_space() | nil
}

Linear RGB in any named working space. The working_space field identifies which set of primaries and reference white to use; see Color.RGB.WorkingSpace.rgb_working_spaces/0 for the full list.

Functions

from_xyz(xyz, working_space)

Converts a CIE XYZ color to linear RGB in the given working space.

Arguments

  • xyz is a Color.XYZ struct. Its illuminant must match the working space — if not, chromatically adapt first.

  • working_space is an atom naming the target RGB working space.

Returns

Examples

iex> xyz = %Color.XYZ{x: 0.95047, y: 1.0, z: 1.08883, illuminant: :D65, observer_angle: 2}
iex> {:ok, rgb} = Color.RGB.from_xyz(xyz, :SRGB)
iex> {Float.round(rgb.r, 3), Float.round(rgb.g, 3), Float.round(rgb.b, 3)}
{1.0, 1.0, 1.0}

to_xyz(rgb)

Converts a linear RGB color to CIE XYZ.

Arguments

  • rgb is a Color.RGB struct whose :working_space names an RGB working space (for example :SRGB, :Adobe, :ProPhoto).

Returns

  • A Color.XYZ struct tagged with the working space's illuminant and 2° observer angle.

Examples

iex> {:ok, xyz} = Color.RGB.to_xyz(%Color.RGB{r: 1.0, g: 1.0, b: 1.0, working_space: :SRGB})
iex> {Float.round(xyz.x, 4), Float.round(xyz.y, 4)}
{0.9505, 1.0}