w3ctag / design-reviews

W3C specs and API reviews
Creative Commons Zero v1.0 Universal
324 stars 55 forks source link

CSS color-mix function #526

Closed kbabbitt closed 4 years ago

kbabbitt commented 4 years ago

Saluton TAG!

I'm requesting a TAG review of the CSS color-mix function.

color-mix takes two color specifications and returns the result of mixing them, in a given colorspace, by a specified amount. For example, color-mix(red yellow 40%) produces a mixture of 40% red and 60% yellow.

Further details:

We'd prefer the TAG provide feedback as (please delete all but the desired option):

💬 leave review feedback as a comment in this issue and @-notify @kbabbitt

svgeesus commented 4 years ago

Looping in the CSS Color 5 spec editors: @LeaVerou @argyleink @una @svgeesus

Also noting that color-mix() depends on the section Interpolation in the same specification.

Thanks to @kbabbitt for offering to contribute tests. While there are none in WPT, the specification does have a number of examples (with full working and the correct end result) which could swiftly be turned into WPT reftests once there is a fledgling implementation to test against.

atanassov commented 4 years ago

@kbabbitt to help speed up the review, it would be helpful if you can add an explainer about the feature and some motivational examples.

alice commented 4 years ago

Example showing mixing red and yellow and getting a kind of khaki color

This example seemed a little odd to me. The mixed color is labelled "red-orange", but that's not how I would describe it - it looks more brown or khaki. It's a surprising result for mixing red and orange. Is this a typo somewhere, or is this what I would expect if I called color-mix(red yellow 65%);, using the default color mixing options?

atanassov commented 4 years ago

@alice this is a good feedback to the authors of the spec. @svgeesus - perhaps you folks should try and address this? Happy to open a separate CSSWG issue if it helps.

LeaVerou commented 4 years ago

@alice @atanassov This is certainly an error in the calculations. My result is much closer to actual orange-red:

image

Though keep in mind that gamut mapping is needed to display the color in current sRGB browsers, so the end result depends on the gamut mapping algorithm, but is certainly not the khaki/olive that is currently there.

We will address the issue in the spec promptly. There is already a PR, though we also need to flesh out which color the percentage applies to.

svgeesus commented 4 years ago

I'm thinking to accept the PR from @dbaron because it avoids out of gamut colors and corrects the error in the calculation (which was originally done by hand). Any syntax shuffle is then easy to add on top.

svgeesus commented 4 years ago

Thanks @dbaron for the careful PR and so glad I left that comment of ugly code in the SVG:) Merged

kbabbitt commented 4 years ago

Explainer for this feature is now available at: https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/CSSColorMix/explainer.md

I'll edit the original request to include this link as well.

svgeesus commented 4 years ago

For example, color-mix(red yellow 40%) produces a mixture of 40% red and 60% yellow.

The syntax has now changed as a result of resolving https://github.com/w3c/csswg-drafts/issues/5258 so that example would now be

color-mix(red 40%, yellow) or indeed color-mix(red 40%, yellow 60%) or, in sRGB rather than LCH color-mix(srgb red 40%, yellow)

svgeesus commented 4 years ago

@kbabbitt I sent a PR on your explainer, updating to the latest syntax.

kbabbitt commented 4 years ago

@svgeesus Merged, thanks for the update!

alice commented 4 years ago

The new syntax looks great, and the explainer does a good job of motivating and explaining the feature.

I have some questions about the non-default values for hue-adjuster, but they're really more about curiosity, so I'll file an issue on csswg.

Thank you for bringing this to us!

mysteryDate commented 1 year ago

Shipping this in blink soon:

https://groups.google.com/a/chromium.org/g/blink-dev/c/r0QATT8-kOw

Our implementation matches what is described here. So syntax is slightly different than described in this issue. Color spaces get the "in" keyword and a comma precedes the first color:

color-mix() = color-mix( <color-interpolation-method> , [ <color> && <percentage [0,100]>? ]#{2}) i.e. color-mix(in lch, red 40%, yellow)

This matches what has been implemented in webkit and gecko. We aimed to have these tests in WPT passing: https://github.com/web-platform-tests/wpt/blob/master/css/css-color/parsing/color-computed-color-mix-function.html https://github.com/web-platform-tests/wpt/blob/master/css/css-color/parsing/color-invalid-color-mix-function.html https://github.com/web-platform-tests/wpt/blob/master/css/css-color/parsing/color-valid-color-mix-function.html

And to pass all tests on wpt interop: wpt.fyi/results/css/css-color