Color.CSS.Calc
(Color v0.4.0)
Copy Markdown
A tiny calc() expression evaluator for CSS color components.
Supports:
Numeric literals (
255,0.5,-1.2).Percentages (
50%) — interpreted as the raw percent value (50.0), not as a fraction. Channel parsers inColor.CSSdecide whether to scale.The four arithmetic operators
+ - * /with the conventional precedence and associativity.Parenthesised sub-expressions.
Identifier references (
r,g,b,h,s,l,alpha, …) that resolve against abindingsmap. This is what makes relative color syntax (oklch(from teal calc(l + 0.1) c h)) work.The
nonekeyword — evaluated as0.0.
Whitespace is ignored.
Example
iex> {:ok, ast} = Color.CSS.Calc.parse("255 / 2")
iex> {:ok, value} = Color.CSS.Calc.evaluate(ast, %{})
iex> value
127.5
iex> {:ok, ast} = Color.CSS.Calc.parse("l + 0.1")
iex> {:ok, value} = Color.CSS.Calc.evaluate(ast, %{"l" => 0.5})
iex> Float.round(value, 4)
0.6
iex> {:ok, ast} = Color.CSS.Calc.parse("(r + g + b) / 3")
iex> {:ok, value} = Color.CSS.Calc.evaluate(ast, %{"r" => 200, "g" => 100, "b" => 50})
iex> Float.round(value, 4)
116.6667
Summary
Functions
Evaluates a parsed calc() AST against a binding map.
Arguments
astis a value returned byparse/1.bindingsis a map from identifier strings to numeric values.
Returns
{:ok, float}on success.{:error, reason}on division by zero or unbound identifier.
Parses a calc() body into an AST.
Arguments
stringis the body of acalc(...)expression — everything between the outermost parens.
Returns
{:ok, ast}on success.{:error, reason}on a parse error.