Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Raph Levien
Bruce Mitchener, Jr.
Tom Churchman
Jordan Johnson
Muhammed Çiçekel
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. ([#237][] by [@mamicicekel][])

## [0.3.3][] (2026-05-05)

Expand Down Expand Up @@ -183,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
Expand Down Expand Up @@ -245,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
Expand Down
85 changes: 81 additions & 4 deletions color/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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),
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -228,7 +287,15 @@ mod tests {
// Perhaps this should actually serialize to `rgb(...)`.
(
DynamicColor::from_alpha_color(AlphaColor::<Hsl>::new([120., 50., 25., 1.])),
"hsl(120, 50, 25)",
"hsl(120, 50%, 25%)",
),
(
DynamicColor::from_alpha_color(AlphaColor::<Hsl>::new([70., 8., 15., 0.5])),
"hsla(70, 8%, 15%, 0.5)",
),
(
DynamicColor::from_alpha_color(AlphaColor::<Hwb>::new([120., 30., 50., 1.])),
"hwb(120 30% 50%)",
),
] {
let result = format!("{color}");
Expand All @@ -239,6 +306,16 @@ mod tests {
}
}

#[test]
fn hsl_missing_component_uses_modern_syntax() {
let mut color =
DynamicColor::from_alpha_color(AlphaColor::<Hsl>::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 {
Expand Down
Loading