Skip to content

Truly independent web browser

LibWeb: Stop clamping hwb() whiteness and blackness to 100%

Andreas Klingc066c4d9b457
diff --git a/Libraries/LibWeb/Rust/src/css/absolutize.rs b/Libraries/LibWeb/Rust/src/css/absolutize.rs
index 0e902ae8806..365da17a06f 100644
--- a/Libraries/LibWeb/Rust/src/css/absolutize.rs
+++ b/Libraries/LibWeb/Rust/src/css/absolutize.rs
@@ -474,8 +474,13 @@ fn hwb_to_absolutized_rgb(
blackness_0_100: f64,
alpha_0_1: f64,
) -> StyleValueData {
- let whiteness = (whiteness_0_100 / 100.0).clamp(0.0, 1.0) as f32;
- let blackness = (blackness_0_100 / 100.0).clamp(0.0, 1.0) as f32;
+ // https://drafts.csswg.org/css-color-4/#the-hwb-notation
+ // Values outside of these ranges are not invalid; hue angles outside the range [0,360) will be normalized to
+ // that range and values of white and black which sum to 100% or greater will produce achromatic colors as
+ // described below.
+ // NB: The handling of negative white or black is unspecified, so they are clamped to 0%.
+ let whiteness = (whiteness_0_100 / 100.0).max(0.0) as f32;
+ let blackness = (blackness_0_100 / 100.0).max(0.0) as f32;
if whiteness + blackness >= 1.0 {
// The gray channel value is shared across the three slots, like the C++ code.
diff --git a/Libraries/LibWeb/Rust/src/css/color_resolution.rs b/Libraries/LibWeb/Rust/src/css/color_resolution.rs
index 1776bf19720..ce68d604676 100644
--- a/Libraries/LibWeb/Rust/src/css/color_resolution.rs
+++ b/Libraries/LibWeb/Rust/src/css/color_resolution.rs
@@ -962,8 +962,9 @@ fn construct_color(color_type: u8, c1: f64, c2: f64, c3: f64, alpha: f64) -> Opt
},
ct::HSL => Rgba::from_hsla(c1, c2 / 100.0, c3 / 100.0, alpha),
ct::HWB => {
- let whiteness = (c2.clamp(0.0, 100.0) / 100.0) as f32;
- let blackness = (c3.clamp(0.0, 100.0) / 100.0) as f32;
+ // NB: White and black are not clamped to 100%, see hwb_to_absolutized_rgb().
+ let whiteness = (c2.max(0.0) / 100.0) as f32;
+ let blackness = (c3.max(0.0) / 100.0) as f32;
if whiteness + blackness >= 1.0 {
let gray = fraction_to_byte(f64::from(whiteness / (whiteness + blackness)));
return Some(Rgba {
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-color/parsing/color-computed-hwb.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-color/parsing/color-computed-hwb.txt
index 37ad822701c..60f79b43322 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-color/parsing/color-computed-hwb.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-color/parsing/color-computed-hwb.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 70 tests
-65 Pass
-5 Fail
+70 Pass
Pass Property color value 'hwb(120 30% 50%)'
Pass Property color value 'hwb(120 60% 60%)'
Pass Property color value 'hwb(0 60% 60%)'
@@ -31,11 +30,11 @@ Pass Property color value 'hwb(0 100% 50% / 0)'
Pass Property color value 'hwb(0 100% 100%)'
Pass Property color value 'hwb(0 70% 60%)'
Pass Property color value 'hwb(0 260% 260%)'
-Fail Property color value 'hwb(0 200% 100%)'
-Fail Property color value 'hwb(0 100% 200%)'
-Fail Property color value 'hwb(0 150% 50%)'
-Fail Property color value 'hwb(0 50% 150%)'
-Fail Property color value 'hwb(0 180% 120%)'
+Pass Property color value 'hwb(0 200% 100%)'
+Pass Property color value 'hwb(0 100% 200%)'
+Pass Property color value 'hwb(0 150% 50%)'
+Pass Property color value 'hwb(0 50% 150%)'
+Pass Property color value 'hwb(0 180% 120%)'
Pass Property color value 'hwb(0 50% 50%)' [HWB value should parse and round correctly]
Pass Property color value 'hwb(30 50% 50%)' [HWB value should parse and round correctly]
Pass Property color value 'hwb(60 50% 50%)' [HWB value should parse and round correctly]