Skip to content

postcss-gradients-interpolation-method: skip gamut mapping #909

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

Merged
Show file tree
Hide file tree
Changes from all 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 .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
- .github/**
- packages/base-cli/**
- packages/cascade-layer-name-parser/**
- packages/color-helpers/**
- packages/css-calc/**
- packages/css-color-parser/**
- packages/css-parser-algorithms/**
- packages/css-tokenizer/**
- packages/media-query-list-parser/**
Expand Down
4 changes: 4 additions & 0 deletions packages/css-color-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to CSS Color Parser

### 1.1.0 (minor)

- Add a flag to `serializeP3` and `serializeRGB` to skip gamut mapping.

### 1.0.0 (March 25, 2023)

- Initial version
2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/index.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/serialize/p3.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ColorData } from '../color-data';
import { FunctionNode } from '@csstools/css-parser-algorithms';
import { Color } from '@csstools/color-helpers';
export declare function serializeP3(color: ColorData): FunctionNode;
export declare function serializeP3(color: ColorData, gamutMapping?: boolean): FunctionNode;
export declare function XYZ_D50_to_P3_Gamut(color: Color): Color;
2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/serialize/rgb.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ColorData } from '../color-data';
import { FunctionNode } from '@csstools/css-parser-algorithms';
import { Color } from '@csstools/color-helpers';
export declare function serializeRGB(color: ColorData): FunctionNode;
export declare function serializeRGB(color: ColorData, gamutMapping?: boolean): FunctionNode;
export declare function XYZ_D50_to_sRGB_Gamut(color: Color): Color;
9 changes: 7 additions & 2 deletions packages/css-color-parser/src/serialize/p3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import { NumberType, TokenType } from '@csstools/css-tokenizer';
import { calculations, Color, conversions, utils, xyz } from '@csstools/color-helpers';
import { toPrecision } from './to-precision';

export function serializeP3(color: ColorData): FunctionNode {
export function serializeP3(color: ColorData, gamutMapping = true): FunctionNode {
color.channels = setPowerlessComponents(color.channels, color.colorNotation);
let p3 = color.channels.map((x) => Number.isNaN(x) ? 0 : x);

if (
color.colorNotation !== ColorNotation.Display_P3
) {
p3 = XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(color).channels);
if (gamutMapping) {
p3 = XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(color).channels);
} else {
p3 = xyz.XYZ_D50_to_P3(colorData_to_XYZ_D50(color).channels);
}
}

const r = toPrecision(p3[0], 6);
Expand Down
8 changes: 6 additions & 2 deletions packages/css-color-parser/src/serialize/rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import { calculations, Color, conversions, utils, xyz } from '@csstools/color-he
import { colorData_to_XYZ_D50 } from '../color-data';
import { toPrecision } from './to-precision';

export function serializeRGB(color: ColorData): FunctionNode {
export function serializeRGB(color: ColorData, gamutMapping = true): FunctionNode {
color.channels = setPowerlessComponents(color.channels, color.colorNotation);
let srgb = color.channels.map((x) => Number.isNaN(x) ? 0 : x);

if (
color.colorNotation !== ColorNotation.RGB &&
color.colorNotation !== ColorNotation.HEX
) {
srgb = XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(color).channels);
if (gamutMapping) {
srgb = XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(color).channels);
} else {
srgb = xyz.XYZ_D50_to_sRGB(colorData_to_XYZ_D50(color).channels);
}
}

const r = Math.min(255, Math.max(0, Math.round(toPrecision(srgb[0]) * 255)));
Expand Down
7 changes: 3 additions & 4 deletions plugins/postcss-gradients-interpolation-method/.tape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ postcssTape(plugin)({
'examples/example': {
message: 'minimal example',
},
'examples/example:preserve-true': {
'examples/example:preserve-false': {
message: 'minimal example',
options: {
preserve: true
preserve: false
}
},
'examples/example:preserve-true:progressive-false': {
'examples/example:progressive-false': {
message: 'minimal example',
options: {
preserve: true,
enableProgressiveCustomProperties: false,
}
}
Expand Down
6 changes: 5 additions & 1 deletion plugins/postcss-gradients-interpolation-method/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changes to PostCSS Gradients Interpolation Method

### 3.0.0 3.0.0 (March 25, 2023)
### 3.0.1 (patch)

- Skip gamut mapping for interpolation color hints.

### 3.0.0 (March 25, 2023)

- Handle `color-mix()` internally with `@csstools/css-color-parser`

Expand Down
39 changes: 21 additions & 18 deletions plugins/postcss-gradients-interpolation-method/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */
Expand All @@ -20,9 +20,14 @@
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
}

@supports (background: linear-gradient(in oklch, red 0%, red 0% 1%, red 2%)) and (color: hsl(0 0% 0% / 0)) {
:root {
--background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}
}
```

Expand Down Expand Up @@ -89,31 +94,29 @@ instructions for:
### preserve

The `preserve` option determines whether the original notation
is preserved. By default, it is not preserved.
is preserved. By default, it is preserved.

```js
postcssGradientsInterpolationMethod({ preserve: true })
postcssGradientsInterpolationMethod({ preserve: false })
```

```pcss
.example {
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */

.example {
background-image: linear-gradient(rgb(245, 137, 137) 0%, rgb(248, 146, 114), rgb(244, 158, 94), rgb(235, 171, 82), rgb(220, 185, 81), rgb(201, 199, 95), rgb(177, 211, 118), rgb(151, 221, 146), rgb(125, 229, 177), rgb(103, 235, 208), rgb(94, 237, 237) 100%);
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
}
```

Expand All @@ -133,8 +136,8 @@ postcssGradientsInterpolationMethod({ enableProgressiveCustomProperties: false }
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}

/* becomes */
Expand All @@ -144,9 +147,9 @@ postcssGradientsInterpolationMethod({ enableProgressiveCustomProperties: false }
background-image: linear-gradient(in oklch, hsl(0deg 85% 75%) 0%, hsl(180deg 80% 65%) 100%);
}

.example {
background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
:root {
--background-image: linear-gradient(rgb(56, 87, 35) 0%, rgb(64, 83, 46), rgb(70, 79, 54), rgb(76, 74, 62), rgb(82, 69, 68), rgb(86, 64, 75), rgb(91, 58, 81), rgb(95, 51, 87), rgb(99, 44, 93), rgb(103, 34, 98), rgb(106, 21, 104) 100%);
--background-image: linear-gradient(in oklab, hsl(96, 42%, 24%) 0%, hsl(302, 67%, 25%) 100%);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export type ColorStop = {
colorData: ColorData;
position: ComponentValue;
};
export declare function interpolateColorsInColorStopsList(colorStops: Array<ColorStop>, colorSpace: TokenNode, hueInterpolationMethod: TokenNode | null): Array<ComponentValue> | false;
export declare function interpolateColorsInColorStopsList(colorStops: Array<ColorStop>, colorSpace: TokenNode, hueInterpolationMethod: TokenNode | null, wideGamut?: boolean): Array<ComponentValue> | false;
Loading