Skip to content

Commit

Permalink
[css-values-4] Add clamp(), per WG resolution. Fixes #2519.
Browse files Browse the repository at this point in the history
  • Loading branch information
tabatkins committed May 10, 2018
1 parent ce8046f commit 3b10e5e
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions css-values-4/Overview.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,18 +1585,22 @@ Functional Notations</h2>
-->

<h3 id="calc-notation">
Mathematical Expressions: ''calc()'', ''min()'', and ''max()''</h3>
Mathematical Expressions: ''calc()'', ''min()'', ''max()'', and ''clamp()''</h3>

The <dfn export lt="math function">math functions</dfn>,
<dfn>calc()</dfn>, <dfn>min()</dfn>, and <dfn>max()</dfn>,
<dfn>calc()</dfn>, <dfn>min()</dfn>, <dfn>max()</dfn>, and <dfn>clamp()</dfn>
allow mathematical expressions
with addition (''+''), subtraction (''-''), multiplication (''*''), and division (''/'')
to be used as component values.
A ''calc()'' function represents the result of the mathematical calculation it contains,
using standard operator precedence rules;
a ''min()'' or ''max()'' function represents
the smallest (most negative) or largest (most positive), respectively,
comma-separated calculation it contains.
comma-separated calculation it contains;
a ''clamp()'' function represents its central calculation,
clamped according to its min and max calculations
(given ''clamp(MIN, VAL, MAX)'',
it is resolved exactly identically to ''max(MIN, min(VAL, MAX))'').

A [=math function=] can be used wherever
<<length>>,
Expand Down Expand Up @@ -1672,16 +1676,42 @@ Mathematical Expressions: ''calc()'', ''min()'', and ''max()''</h3>
</pre>
</div>

<div class=note>
Note that ''clamp()'',
matching CSS conventions elsewhere,
has its minimum value "win" over its maximum value
if the two are in the "wrong order".
That is, ''clamp(100px, ..., 50px)''
will resolve to ''100px'',
exceeding its stated "max" value.

If alternate resolution mechanics are desired
they can be achieved by combining ''clamp()'' with ''min()'' or ''max()'':

: To have MAX win over MIN:
::
''clamp(min(MIN, MAX), VAL, MAX)''.
If you want to avoid repeating the MAX calculation,
you can just reverse the nesting of functions that ''clamp()'' is defined against--
''min(MAX, max(MIN, VAL))''.

: To have MAX and MIN "swap" when they're in the wrong order:
::
''clamp(min(MIN, MAX), VAL, max(MIN, MAX))''.
Unfortunately, there's no easy way to do this without repeating the MIN and MAX terms.
</div>


<h4 id='calc-syntax'>
Syntax</h4>

The syntax of a [=math function=] is:

<pre class='prod'>
<<calc()>> = calc( <<calc-sum>> )
<<min()>> = min( <<calc-sum>># )
<<max()>> = max( <<calc-sum>># )
<<calc()>> = calc( <<calc-sum>> )
<<min()>> = min( <<calc-sum>># )
<<max()>> = max( <<calc-sum>># )
<<clamp()>> = clamp( <<calc-sum>>#{3} )
<dfn>&lt;calc-sum></dfn> = <<calc-product>> [ [ '+' | '-' ] <<calc-product>> ]*
<dfn>&lt;calc-product></dfn> = <<calc-value>> [ [ '*' | '/' ] <<calc-value>> ]*
<dfn>&lt;calc-value></dfn> = <<number>> | <<dimension>> | <<percentage>> | ( <<calc-sum>> )
Expand Down Expand Up @@ -1788,7 +1818,7 @@ Type Checking</h4>

* The [=CSSNumericValue/type=] of a ''calc()'' expression
is the [=CSSNumericValue/type=] of its contained expression.
The [=CSSNumericValue/type=] of a ''min()'' or ''max()'' expression
The [=CSSNumericValue/type=] of a ''min()'', ''max()'', or ''clamp()'' expression
is the result of [=add two types|adding the types=]
of its comma-separated expressions.
If the result is failure,
Expand Down

0 comments on commit 3b10e5e

Please sign in to comment.