Skip to content

Commit

Permalink
Fix HWB round trip of out of gamut colors (#427)
Browse files Browse the repository at this point in the history
* Fix HWB round trip of out of gamut colors

* Fix mypy
  • Loading branch information
facelessuser authored Aug 6, 2024
1 parent cafc17f commit 7a69eb0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions coloraide/spaces/hsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ...types import Vector


def srgb_to_hsl(rgb: Vector) -> Vector:
def srgb_to_hsl(rgb: Vector, correct_neg_sat: bool = True) -> Vector:
"""Convert sRGB to HSL."""

r, g, b = rgb
Expand All @@ -29,7 +29,7 @@ def srgb_to_hsl(rgb: Vector) -> Vector:
h *= 60.0

# Adjust for negative saturation
if s < 0:
if correct_neg_sat and s < 0:
s *= -1.0
h += 180.0

Expand Down
3 changes: 2 additions & 1 deletion coloraide/spaces/hwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
def srgb_to_hwb(srgb: Vector) -> Vector:
"""HWB to sRGB."""

return [srgb_to_hsl(srgb)[0], min(srgb), 1 - max(srgb)]
# Do not use a corrected hue due to negative saturation as HWB ignores the context of negative saturation.
return [srgb_to_hsl(srgb, correct_neg_sat=False)[0], min(srgb), 1 - max(srgb)]


def hwb_to_srgb(hwb: Vector) -> Vector:
Expand Down
4 changes: 4 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
spaces if desired via the `pspace` parameter. Additionally, documentation has been added so users can easily
recreate the aforementioned pre-configured methods themselves or their own desired variants.

- **FIX**: HWB should not use a corrected HSL hue when the color yields a negative saturation as it does not calculate
the whiteness and blackness with the negative saturation. Using a corrected hue without the negative saturation
context leads to bad round trip with out of gamut colors.

## 3.3.1

- **FIX**: Ray trace gamut mapping algorithm will better handle perceptual spaces like CAM16 and HCT which have
Expand Down
4 changes: 3 additions & 1 deletion tests/test_hwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class TestHWB(util.ColorAssertsPyTest):
# Test percent ranges
('color(--hwb 0 0% 0%)', 'color(--hwb 0 0 none)'),
('color(--hwb 360 100% 100%)', 'color(--hwb 360 1 1 / 1)'),
('color(--hwb -360 -100% -100%)', 'color(--hwb -360 -1 -1 / 1)')
('color(--hwb -360 -100% -100%)', 'color(--hwb -360 -1 -1 / 1)'),
# Test out of gamut conversion
('lab(100 104.3 -50.9)', 'color(--hwb 311.21 0.58776 -0.5935 / 1)')
]

@pytest.mark.parametrize('color1,color2', COLORS)
Expand Down

0 comments on commit 7a69eb0

Please sign in to comment.