Behaviour for color format handlers.
This module defines the contract for all color format modules.
It provides default implementations for common conversions based
on to_rgb/1.
Each color format module (ANSI, Hex, RGB, ARGB, HSL, HSV, CMYK, XTerm256, Atom) implements this behaviour to provide a consistent interface for color parsing, validation, and conversion to all supported color spaces.
See Pote.Format.RGB, Pote.Format.Hex, Pote.Format.HSL, etc.
for the built-in implementations.
Usage with use
defmodule MyFormat do
use Pote.Format
@impl true
def parse(input), do: # ...
endThe use Pote.Format macro provides default implementations for
to_hex/1, to_argb/1, to_hsl/1, to_hsv/1, to_cmyk/1,
to_xterm256/1, valid?/1, name/1, and info/1.
Summary
Callbacks
Creates the format from RGB.
Returns a map with all the information about the color.
Returns the color name.
Defaults to returning nil.
Parses a string or value into the specific format.
Converts the format to ARGB {a, r, g, b}. Defaults to returning {255, r, g, b}.
Converts the format to CMYK.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_cmyk/1.
Converts the format to hexadecimal.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hex/1.
Converts the format to HSL.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hsl/1.
Converts the format to HSV.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hsv/1.
Converts the format to RGB.
Converts the format to XTerm256.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_xterm256/1.
Validates whether the value is valid for this format.
Defaults to returning true if parse/1 succeeds.
Functions
Helper to implement valid?/1 based on parse/1.
Types
@type t() :: any()
Callbacks
Creates the format from RGB.
Returns a map with all the information about the color.
Returns the color name.
Defaults to returning nil.
Parses a string or value into the specific format.
@callback to_argb(t()) :: {0..255, 0..255, 0..255, 0..255}
Converts the format to ARGB {a, r, g, b}. Defaults to returning {255, r, g, b}.
Converts the format to CMYK.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_cmyk/1.
Converts the format to hexadecimal.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hex/1.
Converts the format to HSL.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hsl/1.
Converts the format to HSV.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_hsv/1.
Converts the format to RGB.
@callback to_xterm256(t()) :: Pote.xterm256()
Converts the format to XTerm256.
Defaults to using to_rgb/1 and then Pote.Converters.RGB.to_xterm256/1.
Validates whether the value is valid for this format.
Defaults to returning true if parse/1 succeeds.