From 856b2d43a025c1a96be278ebf7b3101e73e4fda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20=C3=87i=C3=A7ekel?= Date: Thu, 5 Nov 2026 17:54:36 +0300 Subject: [PATCH 1/2] Serialize hsl and hwb percentage components with a percent sign. Legacy hsl() requires % on saturation and lightness, and hwb() requires % on whiteness and blackness. A missing hsl component uses the modern form, which is the syntax that can represent none. --- AUTHORS | 1 + CHANGELOG.md | 1 + color/src/serialize.rs | 85 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index ef3ac2ba..a4a6a750 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,3 +8,4 @@ Raph Levien Bruce Mitchener, Jr. Tom Churchman Jordan Johnson +Muhammed Çiçekel diff --git a/CHANGELOG.md b/CHANGELOG.md index 24b94cc9..bed84d27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ This release has an [MSRV][] of 1.86. ### Fixed - Parse the named colors spelled grey (`grey`, `darkgrey`, `dimgrey`, …). ([#231][] by [@lehni][]) +- Serialize `hsl()` / `hsla()` saturation and lightness, and `hwb()` whiteness and blackness, with the `%` unit CSS Color 4 requires. ## [0.3.3][] (2026-05-05) diff --git a/color/src/serialize.rs b/color/src/serialize.rs index 0522d2f7..5a8dd183 100644 --- a/color/src/serialize.rs +++ b/color/src/serialize.rs @@ -75,6 +75,65 @@ fn write_legacy_function( write!(f, ")") } +/// Write a component that CSS requires to be a percentage, unless it is `none`. +fn write_percentage_component(color: &DynamicColor, ix: usize, f: &mut Formatter<'_>) -> Result { + write_scaled_component(color, ix, f, 1.0)?; + if !color.flags.missing().contains(ix) { + write!(f, "%")?; + } + Ok(()) +} + +/// `hsl()` / `hsla()`. +/// +/// The comma-separated legacy form requires `%` on saturation and lightness. +/// `none` is not allowed there, so a missing component uses the modern form. +fn write_hsl(color: &DynamicColor, f: &mut Formatter<'_>) -> Result { + let missing = color.flags.missing(); + let modern = missing.contains(0) || missing.contains(1) || missing.contains(2); + if modern { + write!(f, "hsl(")?; + write_scaled_component(color, 0, f, 1.0)?; + write!(f, " ")?; + write_percentage_component(color, 1, f)?; + write!(f, " ")?; + write_percentage_component(color, 2, f)?; + if color.components[3] < 1.0 { + write!(f, " / ")?; + write_scaled_component(color, 3, f, 1.0)?; + } + return write!(f, ")"); + } + + let opt_a = if color.components[3] < 1.0 { "a" } else { "" }; + write!(f, "hsl{opt_a}(")?; + write_scaled_component(color, 0, f, 1.0)?; + write!(f, ", ")?; + write_percentage_component(color, 1, f)?; + write!(f, ", ")?; + write_percentage_component(color, 2, f)?; + if color.components[3] < 1.0 { + write!(f, ", ")?; + write_scaled_component(color, 3, f, 1.0)?; + } + write!(f, ")") +} + +/// `hwb()`. Whiteness and blackness are percentages in CSS Color 4. +fn write_hwb(color: &DynamicColor, f: &mut Formatter<'_>) -> Result { + write!(f, "hwb(")?; + write_scaled_component(color, 0, f, 1.0)?; + write!(f, " ")?; + write_percentage_component(color, 1, f)?; + write!(f, " ")?; + write_percentage_component(color, 2, f)?; + if color.components[3] < 1.0 { + write!(f, " / ")?; + write_scaled_component(color, 3, f, 1.0)?; + } + write!(f, ")") +} + impl core::fmt::Display for DynamicColor { fn fmt(&self, f: &mut Formatter<'_>) -> Result { if let Some(color_name) = self.flags.color_name() { @@ -97,8 +156,8 @@ impl core::fmt::Display for DynamicColor { ColorSpaceTag::Rec2020 => write_color_function(self, "rec2020", f), ColorSpaceTag::Aces2065_1 => write_color_function(self, "--aces2065-1", f), ColorSpaceTag::AcesCg => write_color_function(self, "--acescg", f), - ColorSpaceTag::Hsl => write_legacy_function(self, "hsl", 1.0, f), - ColorSpaceTag::Hwb => write_modern_function(self, "hwb", f), + ColorSpaceTag::Hsl => write_hsl(self, f), + ColorSpaceTag::Hwb => write_hwb(self, f), ColorSpaceTag::XyzD50 => write_color_function(self, "xyz-d50", f), ColorSpaceTag::XyzD65 => write_color_function(self, "xyz-d65", f), ColorSpaceTag::Lab => write_modern_function(self, "lab", f), @@ -152,7 +211,7 @@ impl core::fmt::UpperHex for Rgba8 { mod tests { extern crate alloc; - use crate::{AlphaColor, DynamicColor, Hsl, Oklab, Srgb, XyzD65, parse_color}; + use crate::{AlphaColor, DynamicColor, Hsl, Hwb, Missing, Oklab, Srgb, XyzD65, parse_color}; use alloc::format; #[test] @@ -228,7 +287,15 @@ mod tests { // Perhaps this should actually serialize to `rgb(...)`. ( DynamicColor::from_alpha_color(AlphaColor::::new([120., 50., 25., 1.])), - "hsl(120, 50, 25)", + "hsl(120, 50%, 25%)", + ), + ( + DynamicColor::from_alpha_color(AlphaColor::::new([70., 8., 15., 0.5])), + "hsla(70, 8%, 15%, 0.5)", + ), + ( + DynamicColor::from_alpha_color(AlphaColor::::new([120., 30., 50., 1.])), + "hwb(120 30% 50%)", ), ] { let result = format!("{color}"); @@ -239,6 +306,16 @@ mod tests { } } + #[test] + fn hsl_missing_component_uses_modern_syntax() { + let mut color = + DynamicColor::from_alpha_color(AlphaColor::::new([120., 50., 25., 1.])); + let mut missing = Missing::EMPTY; + missing.insert(1); + color.flags.set_missing(missing); + assert_eq!(format!("{color}"), "hsl(120 none 25%)"); + } + #[test] fn roundtrip_named_colors() { for name in crate::x11_colors::NAMES { From 7a7ab7a21510eb7a8074f26d0163f5738dc0af38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20=C3=87i=C3=A7ekel?= Date: Thu, 5 Nov 2026 17:55:03 +0300 Subject: [PATCH 2/2] Cite the changelog entry for the hsl and hwb percent fix. --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bed84d27..fb9d0093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ This release has an [MSRV][] of 1.86. ### Fixed - Parse the named colors spelled grey (`grey`, `darkgrey`, `dimgrey`, …). ([#231][] by [@lehni][]) -- Serialize `hsl()` / `hsla()` saturation and lightness, and `hwb()` whiteness and blackness, with the `%` unit CSS Color 4 requires. +- Serialize `hsl()` / `hsla()` saturation and lightness, and `hwb()` whiteness and blackness, with the `%` unit CSS Color 4 requires. ([#237][] by [@mamicicekel][]) ## [0.3.3][] (2026-05-05) @@ -184,6 +184,7 @@ This is the initial release. [@l0uisgrange]: https://github.com/l0uisgrange [@LaurenzV]: https://github.com/LaurenzV [@lehni]: https://github.com/lehni +[@mamicicekel]: https://github.com/mamicicekel [@MightyBurger]: https://github.com/MightyBurger [@raphlinus]: https://github.com/raphlinus [@sagudev]: https://github.com/sagudev @@ -246,6 +247,7 @@ This is the initial release. [#218]: https://github.com/linebender/color/pull/218 [#219]: https://github.com/linebender/color/pull/219 [#231]: https://github.com/linebender/color/pull/231 +[#237]: https://github.com/linebender/color/pull/237 [Unreleased]: https://github.com/linebender/color/compare/v0.3.3...HEAD [0.3.3]: https://github.com/linebender/color/releases/tag/v0.3.3