TinyColor.Parser (tiny_color v0.1.2)

Provides a parser for css 3 color strings

Link to this section Summary

Functions

Parses the given binary as css_color.

Parses the given binary as css_unit.

Parses a CSS Level 3 compliant color code into a TinyColor struct

Link to this section Functions

Link to this function

css_color(binary, opts \\ [])

Specs

css_color(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as css_color.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the css_color (start position) as {line, column_on_line}.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map
Link to this function

css_unit(binary, opts \\ [])

Specs

css_unit(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as css_unit.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the css_unit (start position) as {line, column_on_line}.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

Parses a CSS Level 3 compliant color code into a TinyColor struct

Examples

iex> TinyColor.Parser.parse("#08f")
{:ok, %TinyColor.RGB{red: 0.0, green: 136.0, blue: 255.0}}

iex> TinyColor.Parser.parse("#08ff")
{:ok, %TinyColor.RGB{red: 0.0, green: 136.0, blue: 255.0, alpha: 1.0}}

iex> TinyColor.Parser.parse("rgb(0, 128, 255)")
{:ok, %TinyColor.RGB{red: 0.0, green: 128.0, blue: 255.0}}

iex> TinyColor.Parser.parse("rgb 0% 50% 100%")
{:ok, %TinyColor.RGB{red: 0.0, green: 127.5, blue: 255.0}}

iex> TinyColor.Parser.parse("rgba(0, 128, 255, 0.5)")
{:ok, %TinyColor.RGB{red: 0.0, green: 128.0, blue: 255.0, alpha: 0.5}}

iex> TinyColor.Parser.parse("rgba 0% 50% 100% 50%")
{:ok, %TinyColor.RGB{red: 0.0, green: 127.5, blue: 255.0, alpha: 0.5}}

iex> TinyColor.Parser.parse("rgba(0, 128, 255, 1.0)")
{:ok, %TinyColor.RGB{red: 0.0, green: 128.0, blue: 255.0, alpha: 1.0}}