Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Color 4] Require an explicit gamut mapping method #3836

Merged
merged 8 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions accepted/color-4-new-spaces-js.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export type HueInterpolationMethod =
| 'increasing'
| 'longer'
| 'shorter';

export type GamutMapMethod = 'clip' | 'local-minde';
```

### New Color Functions
Expand Down
11 changes: 6 additions & 5 deletions accepted/color-4-new-spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,12 @@ one or more of its channels out of bounds, like `rgb(300 0 0)`).

#### `color.to-gamut()`

This function returns a color that is in the given gamut, using the current CSS
Color 4's `local-minde` algorithm to 'map' out-of-gamut colors into the desired
gamut with as little perceptual change as possible. In many cases this can be
more reliable for generating fallback values, rather than the 'channel clipping'
approach used by current browsers.
This function returns a color that is in the given gamut, using the given
mapping algorithm to convert out-of-gamut colors into the desired gamut with as
little perceptual change as possible. The current recommended algorithm is
`local-minde`, which is defined in the current candidate recommendation of CSS
Color 4, which can in many cases be more reliable for generating fallback values
than the "channel clipping" approach used by current browsers.

```scss
$green: oklch(0.8 2 150);
Expand Down
1 change: 1 addition & 0 deletions js-api-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
ColorSpaceLab,
ColorSpaceRgb,
ColorSpaceXyz,
GamutMapMethod,
HueInterpolationMethod,
KnownColorSpace,
ListSeparator,
Expand Down
38 changes: 22 additions & 16 deletions js-api-doc/value/color.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ export type HueInterpolationMethod =
| 'longer'
| 'shorter';

/**
* Methods by which colors in bounded spaces can be mapped to within their
* gamut.
*
* * `local-minde`: The algorithm specified in [the original Color Level 4
* candidate recommendation]. This maps in the Oklch color space, using the
* [deltaEOK] color difference formula and the [local-MINDE] improvement.
*
* * `clip`: Clamp each color channel that's outside the gamut to the minimum or
* maximum value for that channel. This algorithm will produce poor visual
* results, but it may be useful to match the behavior of other situations in
* which a color can be clipped.
*
* [the original Color Level 4 candidate recommendation]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#css-gamut-mapping
* [deltaEok]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#color-difference-OK
nex3 marked this conversation as resolved.
Show resolved Hide resolved
* [local-MINDE]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#GM-chroma-local-MINDE
*/
export type GamutMapMethod = 'clip' | 'local-minde';

/**
* Sass's [color type](https://sass-lang.com/documentation/values/colors).
*
Expand Down Expand Up @@ -269,25 +288,12 @@ export class SassColor extends Value {

/**
* Returns a copy of this color, modified so it is in-gamut for the specified
* `space`—or the current color space if `space` is not specified—using one of
* the following methods to map out-of-gamut colors into the desired gamut:
*
* * `local-minde`: The algorithm specified in [the original Color Level 4
* candidate recommendation]. This maps in the Oklch color space, using the
* [deltaEOK] color difference formula and the [local-MINDE] improvement.
*
* * `clip`: Clamp each color channel that's outside the gamut to the minimum
* or maximum value for that channel. This algorithm will produce poor
* visual results, but it may be useful to match the behavior of other
* situations in which a color can be clipped.
*
* [the original Color Level 4 candidate recommendation]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#css-gamut-mapping
* [the original Color Level 4 candidate recommendation]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#color-difference-OK
* [local-MINDE]: https://www.w3.org/TR/2024/CRD-css-color-4-20240213/#GM-chroma-local-MINDE
* `space`—or the current color space if `space` is not specified—using
* `method` to map out-of-gamut colors into the desired gamut.
*/
toGamut(options: {
space?: KnownColorSpace;
method: 'clip' | 'local-minde';
method: GamutMapMethod;
}): SassColor;

/**
Expand Down
1 change: 1 addition & 0 deletions js-api-doc/value/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export {
ColorSpaceXyz,
ChannelNameXyz,
ChannelName,
GamutMapMethod,
KnownColorSpace,
PolarColorSpace,
RectangularColorSpace,
Expand Down
1 change: 1 addition & 0 deletions spec/js-api/index.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export {
ColorSpaceLab,
ColorSpaceRgb,
ColorSpaceXyz,
GamutMapMethod,
HueInterpolationMethod,
KnownColorSpace,
ListSeparator,
Expand Down
13 changes: 11 additions & 2 deletions spec/js-api/value/color.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {Value} from './index';
* [`PolarColorSpace`](#polarcolorspace)
* [`RectangularColorSpace`](#rectangularcolorspace)
* [`HueInterpolationMethod`](#hueinterpolationmethod)
* [`GamutMapMethod`](#gamutmapmethod)
* [`SassColor`](#sasscolor)
* [`internal`](#internal)
* [Constructor](#constructor)
Expand Down Expand Up @@ -224,6 +225,14 @@ export type HueInterpolationMethod =
| 'shorter';
```

### `GamutMapMethod`

Methods by which colors in bounded spaces can be mapped to within their gamut.

```ts
export type GamutMapMethod = 'clip' | 'local-minde';
nex3 marked this conversation as resolved.
Show resolved Hide resolved
```

### `SassColor`

The JS API representation of a Sass color.
Expand Down Expand Up @@ -557,11 +566,11 @@ Returns the result of [`color.to-gamut(internal, space, method)`].
```ts
toGamut(options: {
space?: KnownColorSpace;
method: 'clip' | 'local-minde';
method: GamutMapMethod;
}): SassColor;
```

[`color.to-gamut(internal, space, method)`]: ./color-4-new-spaces.md#colorto-gamut-1
[`color.to-gamut(internal, space, method)`]: ../../../accepted/color-4-new-spaces.md#colorto-gamut-1

#### `channelsOrNull`

Expand Down
1 change: 1 addition & 0 deletions spec/js-api/value/index.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export {
ColorSpaceXyz,
ChannelNameXyz,
ChannelName,
GamutMapMethod,
KnownColorSpace,
PolarColorSpace,
RectangularColorSpace,
Expand Down
Loading