Skip to content

Commit a35e024

Browse files
fix(css): keep a color-mix() whose hue is only a conversion's residue
1 parent 0af51c4 commit a35e024

4 files changed

Lines changed: 81 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep a `color-mix()` whose hue rests on a gray's conversion residue.

‎lib/css/syntax.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10108,6 +10108,35 @@ const _isGray = (linear) => {
1010810108
return high - low <= Math.abs(high) * 1e-9 + 1e-12;
1010910109
};
1011010110

10111+
/**
10112+
* Whether a color is gray only as far as its own conversion can tell: its
10113+
* channels sit within the spread that trip could have invented. The hue such a
10114+
* color carries is the residue's rather than the color's, so no two engines
10115+
* name the same one.
10116+
* @param {{ linear: number[], conversion: number }} color a resolved color
10117+
* @returns {boolean} true where the hue rests on the conversion's last bits
10118+
*/
10119+
const _isResidualGray = (color) => {
10120+
// WHY: a chroma of zero in Lab or Oklch is neutral in that space and reaches
10121+
// sRGB a residue away from neutral — measured against Chromium 141, up to
10122+
// 9.5e-5 of a channel, where webpack's own lands 1.97e-8 away. Neither is a
10123+
// color anyone can see, but a hue interpolates at full weight however small
10124+
// the chroma stating it, so the two part by 25 bytes on the mix.
10125+
const raw = color.linear.map((channel) => _gammaEncode(channel) * 255);
10126+
const scale = Math.max(
10127+
Math.abs(color.linear[0]),
10128+
Math.abs(color.linear[1]),
10129+
Math.abs(color.linear[2])
10130+
);
10131+
const high = Math.max(raw[0], raw[1], raw[2]);
10132+
const low = Math.min(raw[0], raw[1], raw[2]);
10133+
return (
10134+
high - low <=
10135+
_conversionMargin(high, color.conversion, scale) +
10136+
_conversionMargin(low, color.conversion, scale)
10137+
);
10138+
};
10139+
1011110140
/**
1011210141
* A color written in the space it was computed in — the answer for one no sRGB
1011310142
* byte holds, or one whose byte two engines could split over. Only ever the
@@ -10375,9 +10404,19 @@ const _foldColorMix = (inner, hexAlpha, forFallback) => {
1037510404
) {
1037610405
return null;
1037710406
}
10407+
// ...as is one a color states only through its conversion's residue,
10408+
// which every engine leaves in a different place.
10409+
if (!gray[at] && _isResidualGray(colors[at])) return null;
1037810410
}
1037910411
if (gray[0] !== gray[1]) {
1038010412
const stated = gray[0] ? 1 : 0;
10413+
// WHY: a gray written in a polar space states a hue the engines split
10414+
// over. Measured on `color-mix(in hsl, hwb(322.26 56.57% 49.83%) 7.3%,
10415+
// oklch(.280497 .052616 353.043))`: Chrome 147 reads the hue as missing
10416+
// and takes the other color's, painting 68,35,50, where Firefox 156
10417+
// keeps the 322.26 it states and paints 68,35,56.
10418+
const written = COLOR_SPACE_MODEL.get(colors[1 - stated].space);
10419+
if (written !== undefined && written.hue !== -1) return null;
1038110420
components[1 - stated][space.hue] = components[stated][space.hue];
1038210421
}
1038310422
}

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,7 @@ const FILED_WPT_TREE_DEFECTS = new Map();
201201

202202
// Color rewrites one engine paints differently from the color they replaced,
203203
// keyed by the color as written.
204-
const FILED_COLOR_REWRITES = new Map([
205-
[
206-
"color-mix(in hsl, hwb(322.26 56.57% 49.83%) 7.3%, oklch(0.280497 0.052616 353.043))",
207-
"firefox only: a printer defect, not yet decided — the fold matches what Chromium computes and Gecko lands six units away in blue, which is past the byte the quantization explains. Measured in Firefox 156: the source paints 68,35,56 and the `#442332` it is printed as paints 68,35,50; Chrome 147 paints 68,35,50 for both"
208-
]
209-
]);
204+
const FILED_COLOR_REWRITES = new Map();
210205

211206
// Enumerated values the printer lower-cases that one engine does not read
212207
// case-insensitively, keyed as the check reports them.

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9501,6 +9501,42 @@ describe("CssSyntax minify — color-mix()", () => {
95019501
expect(minify(pale)).toBe(pale);
95029502
});
95039503

9504+
it("declines a mix whose hue is only its conversion's residue", () => {
9505+
// A chroma of zero in Lab or Oklch is neutral there and reaches sRGB a
9506+
// residue away from neutral, so the hue is wherever that residue points:
9507+
// Chromium 141 reads 160.8deg for this color where webpack reads 91.76deg.
9508+
const lab =
9509+
"a{color:color-mix(in hsl,lch(50 0 200) 50%,oklch(.280497 .052616 353.043))}";
9510+
expect(minify(lab)).toBe(lab);
9511+
// The residue names a hue however small the chroma stating it is, since a
9512+
// hue interpolates at full weight: this folded to 96,83,68 against the
9513+
// 71,68,96 Chromium paints.
9514+
const written =
9515+
"a{color:color-mix(in hsl,lab(50 0 0) 50%,oklch(.280497 .052616 353.043))}";
9516+
expect(minify(written)).toBe(written);
9517+
const slight =
9518+
"a{color:color-mix(in hsl,lch(50 0 200) 7.3%,oklch(.280497 .052616 353.043))}";
9519+
expect(minify(slight)).toBe(slight);
9520+
// A gray that reached sRGB through no conversion states no hue at all, so
9521+
// that mix is still the one every engine computes.
9522+
expect(minify("a{color:color-mix(in hsl,#808080 50%,#f00)}")).toBe(
9523+
"a{color:#bf4040}"
9524+
);
9525+
});
9526+
9527+
it("declines a mix where a gray states a hue of its own", () => {
9528+
// A gray written in a polar space states a hue the engines split over:
9529+
// Chrome 147 reads it as missing and takes the other color's, painting
9530+
// 68,35,50, where Firefox 156 keeps the 322.26 and paints 68,35,56.
9531+
const hwb =
9532+
"a{color:color-mix(in hsl,hwb(322.26 56.57% 49.83%) 7.3%,oklch(.280497 .052616 353.043))}";
9533+
expect(minify(hwb)).toBe(hwb);
9534+
// The same gray with no hue to state leaves the mix every engine agrees on.
9535+
expect(minify("a{color:color-mix(in hsl,#888 7.3%,oklch(.280497 .052616 353.043))}")).toBe(
9536+
"a{color:#442332}"
9537+
);
9538+
});
9539+
95049540
it("writes a mix in the space it was made in where no byte holds it", () => {
95059541
// A channel landing on a `.5` boundary is where implementations disagree
95069542
// over the byte — but not over the mix, which both colors were written in

0 commit comments

Comments
 (0)