Skip to content

Truly independent web browser

LibWeb: Skip XYZ when converting between rectangular and polar

Zaggy1024fa37f63911ac
diff --git a/Libraries/LibWeb/Rust/src/css/color_conversion.rs b/Libraries/LibWeb/Rust/src/css/color_conversion.rs
index 46e97e4cd46..65bc709d6c6 100644
--- a/Libraries/LibWeb/Rust/src/css/color_conversion.rs
+++ b/Libraries/LibWeb/Rust/src/css/color_conversion.rs
@@ -486,11 +486,18 @@ fn convert_color(color_type: u8, target_type: u8, color: Color) -> Option<Color>
if color_type == target_type || color_type == RGB && target_type == SRGB {
return Some(color);
}
+ match (color_type, target_type) {
+ // https://drafts.csswg.org/css-color-4/#lab-to-lch
+ (LAB, LCH) | (OKLAB, OKLCH) => return Some(rectangular_to_polar(color)),
+ // https://drafts.csswg.org/css-color-4/#lch-to-lab
+ (LCH, LAB) | (OKLCH, OKLAB) => return Some(polar_to_rectangular(color)),
+ _ => {}
+ }
if target_type == HSL || target_type == HWB {
- let srgb = if color_type == RGB || color_type == SRGB {
- color
- } else {
- from_xyz65(SRGB, to_xyz65(color_type, color)?)?
+ let srgb = match color_type {
+ RGB | SRGB => color,
+ HSL | HWB => legacy_color_to_srgb_color(color_type, color)?,
+ _ => from_xyz65(SRGB, to_xyz65(color_type, color)?)?,
};
return Some(if target_type == HSL {
srgb_to_hsl(srgb)
@@ -525,6 +532,16 @@ mod tests {
assert!(white[2].abs() < 0.00001);
}
+ #[test]
+ fn converts_between_forms_of_the_same_color_space_exactly() {
+ assert_eq!(convert(OKLAB, OKLCH, [0.5, 0.1, 0.0, 1.0]), Some([0.5, 0.1, 0.0, 1.0]));
+ assert_eq!(convert(LAB, LCH, [50.0, 10.0, 0.0, 1.0]), Some([50.0, 10.0, 0.0, 1.0]));
+ assert_eq!(
+ convert(HWB, HSL, [180.0, 0.0, 0.25, 1.0]),
+ Some([180.0, 1.0, 0.375, 1.0])
+ );
+ }
+
#[test]
fn round_trips_supported_color_spaces() {
let source = [0.25, 0.5, 0.75, 0.8];
diff --git a/Libraries/LibWeb/Rust/src/css/color_resolution.rs b/Libraries/LibWeb/Rust/src/css/color_resolution.rs
index 1bb801fab9b..bbe345b4aae 100644
--- a/Libraries/LibWeb/Rust/src/css/color_resolution.rs
+++ b/Libraries/LibWeb/Rust/src/css/color_resolution.rs
@@ -1810,6 +1810,19 @@ fn extract_channels_and_missing_slots(
return Some((context, missing_slots));
}
+ // Lab and LCH, and Oklab and OkLCh, are forms of the same color space, so convert between them directly.
+ if matches!(
+ (origin_native_type, target_color_type),
+ (ct::LAB, ct::LCH) | (ct::LCH, ct::LAB) | (ct::OKLAB, ct::OKLCH) | (ct::OKLCH, ct::OKLAB)
+ ) {
+ let converted = color_conversion::convert(origin_native_type, target_color_type, native_channels)?;
+ for (index, slot) in channel_slots(target_color_type)?.into_iter().enumerate() {
+ context[usize::from(slot)] = Some(f64::from(converted[index]));
+ }
+ make_powerless_target_hue_missing(&mut context, &mut missing_slots, target_color_type);
+ return Some((context, missing_slots));
+ }
+
let xyz65 = native_channels_to_xyz65(native_channels, origin_native_type);
let linear_srgb =
color_conversion::convert(ct::XYZ_D65, ct::SRGB_LINEAR, xyz65).expect("XYZ-D65 converts to linear sRGB");