Color.Sigil (Color v0.4.0)

Copy Markdown

A sigil for writing color literals in code. Import this module and use ~COLOR to build any supported color.

Elixir version

Multi-character sigil names (~COLOR) require Elixir 1.15 or later. On older versions this module is not compiled at all — Code.ensure_loaded?(Color.Sigil) returns false and the hex / CSS / numeric constructors on Color.SRGB and the other struct modules are the supported way to build color literals.

Hex / CSS name (no modifier)

import Color.Sigil

~COLOR[#ff0000]
~COLOR[#f80]
~COLOR[#ff000080]
~COLOR[rebeccapurple]

These all return a Color.SRGB struct.

Unit-range sRGB (r modifier)

~COLOR[1.0, 0.5, 0.0]r

0..255 sRGB (b modifier — for "byte")

~COLOR[255, 128, 0]b

CIE Lab (l modifier)

~COLOR[53.24, 80.09, 67.20]l

Oklab (o modifier)

~COLOR[0.63, 0.22, 0.13]o

CIE XYZ (x modifier)

~COLOR[0.4125, 0.2127, 0.0193]x

HSL (h modifier)

~COLOR[0.5, 1.0, 0.5]h

HSV (v modifier)

~COLOR[0.5, 1.0, 1.0]v

CMYK (k modifier)

~COLOR[0.0, 0.5, 1.0, 0.0]k

All numeric forms accept comma-separated fields. Whitespace around the separators is allowed. The parser raises ArgumentError if the string cannot be interpreted.

Summary

Functions

Implements the ~COLOR sigil.

Functions

sigil_COLOR(body, modifiers)

(macro)

Implements the ~COLOR sigil.

See the module doc for the supported modifiers.

Arguments

  • body is the string inside the sigil delimiters.

  • modifiers is the list of modifier characters after the closing delimiter (e.g. ~COLOR[1.0, 0.5, 0.0]r passes [?r]).

Returns

  • The appropriate color struct.

Examples

iex> import Color.Sigil
iex> ~COLOR[#ff0000]
%Color.SRGB{r: 1.0, g: 0.0, b: 0.0, alpha: nil}

iex> import Color.Sigil
iex> ~COLOR[red]
%Color.SRGB{r: 1.0, g: 0.0, b: 0.0, alpha: nil}

iex> import Color.Sigil
iex> ~COLOR[1.0, 0.5, 0.0]r
%Color.SRGB{r: 1.0, g: 0.5, b: 0.0, alpha: nil}

iex> import Color.Sigil
iex> ~COLOR[255, 128, 0]b
%Color.SRGB{r: 1.0, g: 0.5019607843137255, b: 0.0, alpha: nil}

iex> import Color.Sigil
iex> ~COLOR[53.24, 80.09, 67.20]l
%Color.Lab{l: 53.24, a: 80.09, b: 67.2, alpha: nil, illuminant: :D65, observer_angle: 2}

iex> import Color.Sigil
iex> ~COLOR[0.63, 0.22, 0.13]o
%Color.Oklab{l: 0.63, a: 0.22, b: 0.13, alpha: nil}