Skip to content

Commit ab50d91

Browse files
css: fix green/blue channel swap in oklch gamut mapping (#4488)
gamut_mapping_xyz_to_srgb called srgb_to_oklab(clipped_r, clipped_b, clipped_g) when computing the clipped color's delta from the original during binary search, swapping g and b. The comparison color on the next line used the correct order, so the JND check was comparing against the wrong OKLab point and could converge on a less accurate clipped chroma. .a{color:oklch(0.65 0.3 50)} targeting chrome90 now lowers to #e56300 instead of #de6800.
1 parent 04627b6 commit ab50d91

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎internal/css_parser/css_color_spaces.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func gamut_mapping_xyz_to_srgb(x F64, y F64, z F64) (F64, F64, F64) {
437437
}
438438

439439
clipped_r, clipped_g, clipped_b := clip(r), clip(g), clip(b)
440-
L1, a1, b1 := srgb_to_oklab(clipped_r, clipped_b, clipped_g)
440+
L1, a1, b1 := srgb_to_oklab(clipped_r, clipped_g, clipped_b)
441441
L2, a2, b2 := srgb_to_oklab(r, g, b)
442442
E := delta_eok(L1, a1, b1, L2, a2, b2)
443443
if E.Value() < JND {

‎internal/css_parser/css_parser_test.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,16 @@ func TestColorFunctions(t *testing.T) {
606606
expectPrintedLower(t, "a { color: oklch(0.953 16% 134) }", "a {\n color: #deface;\n}\n", "")
607607
expectPrintedLower(t, "a { color: oklch(0.953 0.064 0.372turn) }", "a {\n color: #deface;\n}\n", "")
608608

609+
// Check gamut mapping for out-of-gamut colors with distinct g/b channels.
610+
// This exercises the binary-search clipping path in "gamut_mapping_xyz_to_srgb",
611+
// which previously called "srgb_to_oklab" with the green and blue arguments
612+
// swapped. That bug was masked by every other test case above because they
613+
// either stay in-gamut or happen to have green ≈ blue, so the swap was a
614+
// no-op. These colors have green and blue far enough apart for the bug to
615+
// produce a visibly different (and less accurate) clipped color.
616+
expectPrintedLower(t, "a { color: oklch(0.65 0.3 50) }", "a {\n color: #e56300;\n color: oklch(0.65 0.3 50);\n}\n", "")
617+
expectPrintedLower(t, "a { color: oklch(0.8 0.3 200) }", "a {\n color: #00dae4;\n color: oklch(0.8 0.3 200);\n}\n", "")
618+
609619
// Test alpha
610620
expectPrintedLower(t, "a { color: color(srgb 0.87 0.98 0.807 / 0.5) }", "a {\n color: rgba(222, 250, 206, .5);\n}\n", "")
611621
expectPrintedLower(t, "a { color: lab(95.38 -15 18 / 0.5) }", "a {\n color: rgba(222, 250, 206, .5);\n}\n", "")
@@ -822,9 +832,9 @@ func TestGradient(t *testing.T) {
822832
expectPrintedMangle(t, code, "a {\n background: "+gradient+"(#ff0, color(display-p3 1 0 0));\n}\n", "")
823833
expectPrintedMinify(t, code, "a{background:"+gradient+"(yellow,color(display-p3 1 0 0))}", "")
824834
expectPrintedLowerUnsupported(t, compat.ColorFunctions, code,
825-
"a {\n background:\n "+gradient+"(\n #ffff00,\n #ffe971,\n #ffd472 25%,\n "+
826-
"#ffab5f,\n #ff7b45 75%,\n #ff5e38 87.5%,\n #ff5534,\n #ff4c30,\n "+
827-
"#ff412c,\n #ff0e0e);\n "+
835+
"a {\n background:\n "+gradient+"(\n #ffff00,\n #ffe96c,\n #ffd363 25%,\n "+
836+
"#ffa852,\n #ff7a42 75%,\n #ff562c 87.5%,\n #ff4e29,\n #ff4528,\n "+
837+
"#ff3a26,\n #ff0e0e);\n "+
828838
"background:\n "+gradient+"(\n #ffff00,\n color(xyz 0.734 0.805 0.111),\n "+
829839
"color(xyz 0.699 0.693 0.087) 25%,\n color(xyz 0.627 0.501 0.048),\n "+
830840
"color(xyz 0.556 0.348 0.019) 75%,\n color(xyz 0.521 0.284 0.009) 87.5%,\n "+

0 commit comments

Comments
 (0)