less/less.js — pull request #1704 by seven-phases-max (2013-12-02 23:10:18+00:00)
Title: Color blending functions with transparency.

Color blending functions now respect alpha channel with compositing and blending implemented according to [this W3C draft](http://www.w3.org/TR/compositing-1).

Changed functions:
```
multiply
screen
overlay
softlight
hardlight
difference
exclusion
average
negation
```
This update is a "breaking changes" one. Backward compatibility:
* Used blending algorithms are the same as those in the old implementation except the `softlight` mode whose formula is gently different.
* Since the blending algorithms are the same (but not the math itself), results for non-transparent colors passed in are similar to what the old functions return, except each color channel value may vary by +/-1 (as result of rounding applied after a math with different fp-precision side-effects, see below though).

This patch does not add any new functions. The referenced specification defines some additional blending modes not present in LESS but this is a subject for another feature-request/update and needs further discussions/decisions.

Differences from the W3C spec.:
The functions do not clamp their output (as well as input) color values despite the spec. states that:
>The result of the mixing formula must be clamped to the minimum and maximum values of the color range.
Just like the old blending function do (as well as color operations). The rationale behind this is to allow intermediate out-of-range values inside multiple color op/blend chains e.g.:
```
color: (#333 + red(average(#444, (#111 - #777)));
```
So this update assumes that color values are clipped on final CSS output (i.e. #1693, also related #1676).
Well, in general, this whole thing of "clamping or not clamping of intermediate results" needs further discussion and may require some further changes/updates/fine-tuning, but it's somewhat out this patch scope (again) since we need to review that functionality in context of all color functions, conversions and whatever related things (for instance, contrary to color math ops and blending functions, the "Color Operations" functions like `saturate` always clamp their input and output values. So ideally, we'd probably like this all to be somehow unified eventually).

---
#### P.S.
It is actually possible to fine-tune the new functions to output exactly the same as old values on rounding when non-transparent color passed in (for example the simplest way would to just requantize the result to some reasonable precision by using something like `r[i] = Math.round(cr * 2550000) / 10000;` in [this line](https://github.com/seven-phases-max/less.js/compare/color-blending-with-transparency)). But I did not bother with this to keep the code "clean" since the whole patch is "breaking changes" anyway.