From 5b1c87b6a6006b69779f8ea048d0f691bc19e085 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Mon, 15 Sep 2025 09:52:56 +0200 Subject: [PATCH] Fix incorrect ANSI color contrast adjustment on some background colors (#38155) The `Hsla` -> `Rgba` conversion sometimes results in negative (but very close to 0) color components due to floating point imprecision, causing the `.powf(constants.main_trc)` computations in the `srgb_to_y` function to evaluate to `NaN`. This propagates to `apca_contrast` which then makes `ensure_minimum_contrast` unconditionally return `black` for certain background colors. This PR addresses this by clamping the rgba components in `impl From for Rgba` to 0-1. Before/after: before after Release Notes: - Fixed an issue where ANSI colors were incorrectly adjusted to improve contrast on some background colors --- crates/gpui/src/color.rs | 6 +++--- crates/ui/src/utils/apca_contrast.rs | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/gpui/src/color.rs b/crates/gpui/src/color.rs index c6c4cdc77b133105a7c233417efae06d5c7aa220..b84f8699e38f015232313e49dd748a2b049c146d 100644 --- a/crates/gpui/src/color.rs +++ b/crates/gpui/src/color.rs @@ -151,9 +151,9 @@ impl From for Rgba { }; Rgba { - r, - g, - b, + r: r.clamp(0., 1.), + g: g.clamp(0., 1.), + b: b.clamp(0., 1.), a: color.a, } } diff --git a/crates/ui/src/utils/apca_contrast.rs b/crates/ui/src/utils/apca_contrast.rs index 522dca3e91341adf9056b3d03e3b5536cfcdc695..341b44670d867072ef7f7ac10fba098128cdc8af 100644 --- a/crates/ui/src/utils/apca_contrast.rs +++ b/crates/ui/src/utils/apca_contrast.rs @@ -393,6 +393,13 @@ mod tests { ); } + #[test] + fn test_srgb_to_y_nan_issue() { + let dark_red = hsla_from_hex(0x5f0000); + let y_dark_red = srgb_to_y(dark_red, &APCAConstants::default()); + assert!(!y_dark_red.is_nan()); + } + #[test] fn test_ensure_minimum_contrast() { let white_bg = hsla(0.0, 0.0, 1.0, 1.0);