-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CSS Color Level 4 compliant gamut mapping
- Loading branch information
1 parent
0fc54f7
commit 21d0227
Showing
6 changed files
with
78 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[run] | ||
omit= | ||
omit=coloraide/gamut/fit_css_color_4.py | ||
|
||
[report] | ||
omit= | ||
omit=coloraide/gamut/fit_css_color_4.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""CSS Color Level 4 gamut mapping.""" | ||
from ..gamut import Fit, clip_channels | ||
from ..util import NaN | ||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from ..color import Color | ||
|
||
|
||
class CssColor4(Fit): | ||
"""Uses the CSS Color Level 4 algroithm for gamut mapping: Oklch: https://www.w3.org/TR/css-color-4/#binsearch.""" | ||
|
||
NAME = "css-color-4" | ||
LIMIT = 0.02 | ||
DE = "ok" | ||
SPACE = "oklch" | ||
MIN_LIGHTNESS = 0 | ||
MAX_LIGHTNESS = 100 | ||
|
||
@classmethod | ||
def fit(cls, color: 'Color', **kwargs: Any) -> None: | ||
"""Gamut mapping via Oklch chroma.""" | ||
|
||
space = color.space() | ||
mapcolor = color.convert(cls.SPACE) | ||
lightness = mapcolor.lightness | ||
|
||
# Return white or black if lightness is out of range | ||
if lightness >= cls.MAX_LIGHTNESS or lightness <= cls.MIN_LIGHTNESS: | ||
mapcolor.chroma = 0 | ||
mapcolor.hue = NaN | ||
clip_channels(color.update(mapcolor)) | ||
return | ||
|
||
# Set initial chroma boundaries | ||
low = 0.0 | ||
high = mapcolor.chroma | ||
clip_channels(color.update(mapcolor)) | ||
|
||
# Adjust chroma (using binary search). | ||
# This helps preserve the other attributes of the color. | ||
# Compress chroma until we are are right outside the gamut, but under the JND. | ||
if not mapcolor.in_gamut(space): | ||
while True: | ||
mapcolor.chroma = (high + low) * 0.5 | ||
|
||
if mapcolor.in_gamut(space, tolerance=0): | ||
low = mapcolor.chroma | ||
else: | ||
clip_channels(color.update(mapcolor)) | ||
if mapcolor.delta_e(color, method=cls.DE) < cls.LIMIT: | ||
break | ||
high = mapcolor.chroma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters