Color.CSS.Names (Color v0.4.0)

Copy Markdown

The 148 named colors defined by CSS Color Module Level 4, mapped to their 8-bit sRGB values.

Summary

Functions

Returns the raw CSS-name → {r, g, b} map (0..255 bytes).

Returns whether the given name is a known CSS color name.

Looks up a CSS named color and returns its 8-bit sRGB components.

Returns the full list of CSS color name strings.

Finds the CSS named color that is perceptually closest to the given color using CIEDE2000.

Normalises a color-name string: lowercases and strips underscores, hyphens and whitespace. Exposed so callers can match on the canonical form.

Functions

all()

@spec all() :: %{required(String.t()) => {0..255, 0..255, 0..255}}

Returns the raw CSS-name → {r, g, b} map (0..255 bytes).

Returns

  • A map.

known?(name)

@spec known?(atom() | String.t()) :: boolean()

Returns whether the given name is a known CSS color name.

Arguments

  • name is a string or atom.

Returns

  • A boolean.

Examples

iex> Color.CSS.Names.known?("red")
true

iex> Color.CSS.Names.known?(:misty_rose)
true

iex> Color.CSS.Names.known?("notacolor")
false

lookup(name)

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

Looks up a CSS named color and returns its 8-bit sRGB components.

The lookup is case-insensitive, accepts strings or atoms, and ignores underscores, hyphens and whitespace in the name. So "rebecca purple", :rebecca_purple, "Rebecca-Purple" and "rebeccapurple" all resolve to the same color. Both US and UK spellings (gray / grey) are supported. "transparent" maps to (0, 0, 0) — the alpha is the caller's concern.

Arguments

  • name is a string or an atom.

Returns

  • {:ok, {r, g, b}} with each channel in 0..255.

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

Examples

iex> Color.CSS.Names.lookup("rebeccapurple")
{:ok, {102, 51, 153}}

iex> Color.CSS.Names.lookup("Red")
{:ok, {255, 0, 0}}

iex> Color.CSS.Names.lookup(:misty_rose)
{:ok, {255, 228, 225}}

iex> Color.CSS.Names.lookup("chroma green")
{:ok, {0, 177, 64}}

names()

@spec names() :: [String.t()]

Returns the full list of CSS color name strings.

Returns

  • A sorted list of strings.

nearest(input)

@spec nearest(Color.input()) ::
  {:ok, {String.t(), Color.SRGB.t(), float()}} | {:error, Exception.t()}

Finds the CSS named color that is perceptually closest to the given color using CIEDE2000.

Arguments

Returns

  • {:ok, {name, %Color.SRGB{}, delta_e}} on success.

Examples

iex> {:ok, {name, _srgb, _de}} = Color.CSS.Names.nearest(%Color.SRGB{r: 1.0, g: 0.01, b: 0.0})
iex> name
"red"

normalize(name)

@spec normalize(String.t()) :: String.t()

Normalises a color-name string: lowercases and strips underscores, hyphens and whitespace. Exposed so callers can match on the canonical form.

Arguments

  • name is a string.

Returns

  • The normalised string.

Examples

iex> Color.CSS.Names.normalize("Misty Rose")
"mistyrose"

iex> Color.CSS.Names.normalize("rebecca-purple")
"rebeccapurple"