[csswg-drafts] [css-values] Function(s) for Standard Means (#4700)

Crissov has just created a new issue for https://github.com/w3c/csswg-drafts:

== [css-values] Function(s) for Standard Means ==
ECMA-262 (Javascript) does not have dedicated functions (in its global `Math` object #4688) for the commonly known standard means, except for a rather unexpected one:

- _arithmetic mean_ or _average_ or _middle_: `calc( (x + y) / 2)`, `calc( (x + y + ) / 3)`
- _geometric mean_ or _arithmetic-harmonic mean_: `sqrt(x * y)`, `pow(x * y * z, 1/3)`
- _harmonic mean_: `calc(2 / (1/x + 1/y) )`, `calc(3 / (1/x + 1/y + 1/z) )`
- _quadratic mean_ or _root mean square_ (RMS): `sqrt( (x*x + y*y) / 2)`, **`hypot(x, y, z)`**
- _cubic mean_: `cbrt( (x*x*x + y*y*y) / 2)` (if it was added), `pow( 
 (pow(x, 3) + pow(y, 3) + pow(z, 3)) / 3, 1/3)` 

Those can all be described by a **generalized mean** or **power mean** by a characteristic integer exponent _i_: `pow( (pow(x, i) + pow(y, i)) / 2, 1/i)`, `pow( (pow(x, i) + pow(y, i) + pow(z, i)) / 3, 1/i)`. This exponent is −1 for the harmonic, 0 for the geometric, 1 for the arithmetic, 2 for the quadratic and 3 for the cubic mean (and so on). 

There are two lesser known means, usually specified only for two arguments, i.e. the _arithmetic-geometric mean_ and the _geometric-harmonic mean_, which result from letting _i_ be ±½.

The code example provided above use two or three arguments, but for an arbitrary number of arguments, one would need functions like `sum()` or `add()` for Σ, `product()` or `multiply()` for Π and `count()` or `n()` to calculate these means. This would be simple to do in JS, but is hard – probably impossible – in plain CSS.

In #581, a `map()` or `interpolate()` function has been proposed, which would be related to these functions. Unfortunately – and almost as usual – I cannot provide any specific use cases for such mean functions, but I think they might be valuable in working with colors, positions and transitions as well as sets of sizes, especially when using cascading variables and units that may compute to different values depending on the user's environment. The steps of `font-size` keywords, for instance, should form a geometric progression, i.e. a new step between two existing sizes would be calculated as the geometric mean thereof (which is possible already, now that `sqrt()` or `pow()` exists).

In case there was agreement to add functions to calculate standard means to CSS, there would still be the issue whether to do so in a single function `mean(..., i)` (_i_ ∈ **N**, __i__ ≥ −1; or i ∈ {`h` | `g` | `a` | `q` | `c`}; or _i_ ∈ {`harmonic` | `geometric` | `arithmetic` | `quadratic` | `cubic`}) or in several separate functions like `avg()`, `average()`, `mid()`, `middle()`, `medium()` or `amean()`, `meana()`, `mean-a()`, `arithmetic-mean()`, `mean-arithmetic()`, `mean1()` and so on.

Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/4700 using your GitHub account

Received on Friday, 24 January 2020 13:25:47 UTC