Skip to content

Commit 7d44ff3

Browse files
authored
Add a method parameter to toGamut (#288)
1 parent 6e9368e commit 7d44ff3

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

lib/src/value/color.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ type HueInterpolationMethod =
9090
| 'longer'
9191
| 'shorter';
9292

93+
/**
94+
* Methods by which colors in bounded spaces can be mapped to within their
95+
* gamut.
96+
*/
97+
type GamutMapMethod = 'clip' | 'local-minde';
98+
9399
/** Options for specifying any channel value. */
94100
type ChannelOptions = {
95101
[key in ChannelName]?: number | null;
@@ -153,6 +159,14 @@ function encodeSpaceForColorJs(space?: KnownColorSpace): string | undefined {
153159
return space;
154160
}
155161

162+
/**
163+
* Normalize discrepancies between Sass's [GamutMapMethod] and Color.js's
164+
* `method` option.
165+
*/
166+
function encodeGamutMapMethodForColorJs(method: GamutMapMethod): string {
167+
return method === 'local-minde' ? 'css' : method;
168+
}
169+
156170
/**
157171
* Normalize discrepancies between Sass color spaces and ColorJS color space
158172
* ids, converting ColorJS values to Sass values.
@@ -714,18 +728,21 @@ export class SassColor extends Value {
714728

715729
/**
716730
* Returns a copy of this color, modified so it is in-gamut for the specified
717-
* `space`—or the current color space if `space` is not specified—using the
718-
* recommended [CSS Gamut Mapping Algorithm][css-mapping] to map out-of-gamut
719-
* colors into the desired gamut with as little perceptual change as possible.
720-
*
721-
* [css-mapping]:
722-
* https://www.w3.org/TR/css-color-4/#css-gamut-mapping-algorithm
731+
* `space`—or the current color space if `space` is not specified—using
732+
* `method` to map out-of-gamut colors into the desired gamut.
723733
*/
724-
toGamut(space?: KnownColorSpace): SassColor {
734+
toGamut({
735+
space,
736+
method,
737+
}: {
738+
space?: KnownColorSpace;
739+
method: GamutMapMethod;
740+
}): SassColor {
725741
if (this.isInGamut(space)) return this;
726-
const color = this.color
727-
.clone()
728-
.toGamut({space: encodeSpaceForColorJs(space)});
742+
const color = this.color.clone().toGamut({
743+
space: encodeSpaceForColorJs(space),
744+
method: encodeGamutMapMethodForColorJs(method),
745+
});
729746
return new SassColor({color, space: space ?? this.space});
730747
}
731748

0 commit comments

Comments
 (0)