From 6ec30327fa1754180fc9a8e03806265b74d9398e Mon Sep 17 00:00:00 2001 From: facelessuser Date: Thu, 18 Mar 2021 07:44:52 -0600 Subject: [PATCH] Fix issue where compressed hex wouldn't be detected as color name --- coloraide/__meta__.py | 2 +- coloraide/css/colors/srgb.py | 16 ++++++---------- docs/src/markdown/about/changelog.md | 7 ++++++- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/coloraide/__meta__.py b/coloraide/__meta__.py index aecf663b2..61f9eee26 100644 --- a/coloraide/__meta__.py +++ b/coloraide/__meta__.py @@ -188,5 +188,5 @@ def parse_version(ver): return Version(major, minor, micro, release, pre, post, dev) -__version_info__ = Version(0, 1, 0, "alpha", 10) +__version_info__ = Version(0, 1, 0, "alpha", 11) __version__ = __version_info__._get_canonical() diff --git a/coloraide/css/colors/srgb.py b/coloraide/css/colors/srgb.py index b400138e1..3a33cde1e 100644 --- a/coloraide/css/colors/srgb.py +++ b/coloraide/css/colors/srgb.py @@ -62,14 +62,19 @@ def to_string( if options.get("color"): return super().to_string(alpha=alpha, precision=precision, fit=fit, **kwargs) - # Handle hes and color names + # Handle hex and color names value = '' a = util.no_nan(self.alpha) alpha = alpha is not False and (alpha is True or a < 1.0) + compress = options.get("compress", False) if options.get("hex") or options.get("names"): h = self._get_hex(options, alpha=alpha, precision=precision) if options.get("hex"): value = h + if compress: + m = RE_COMPRESS.match(value) + if m: + value = m.expand(r"#\1\2\3\4") if alpha else m.expand(r"#\1\2\3") if options.get("names"): length = len(h) - 1 index = int(length / 4) @@ -112,11 +117,7 @@ def to_string( def _get_hex(self, options, *, alpha=False, precision=None): """Get the hex `RGB` value.""" - if precision is None: - precision = self.parent.PRECISION - hex_upper = options.get("hex_upper", False) - compress = options.get("compress", False) coords = util.no_nan(self.fit_coords()) template = "#{:02x}{:02x}{:02x}{:02x}" if alpha else "#{:02x}{:02x}{:02x}" @@ -136,11 +137,6 @@ def _get_hex(self, options, *, alpha=False, precision=None): int(util.round_half_up(coords[1] * 255.0)), int(util.round_half_up(coords[2] * 255.0)) ) - - if compress: - m = RE_COMPRESS.match(value) - if m: - value = m.expand(r"#\1\2\3\4") if alpha else m.expand(r"#\1\2\3") return value @classmethod diff --git a/docs/src/markdown/about/changelog.md b/docs/src/markdown/about/changelog.md index 4d3b24da5..2c18eb7f5 100644 --- a/docs/src/markdown/about/changelog.md +++ b/docs/src/markdown/about/changelog.md @@ -1,6 +1,11 @@ # Changelog -## 0.10a10 +## 0.1.0a11 + +- **FIX**: Ensure that when `hex`, `compress`, and `names` is enabled in `to_string` for `srgb` that colors will still + match the color name if the color can be compressed. + +## 0.1.0a10 - **FIX**: Address two divide by zero cases in HSL algorithm. Was missing some special cases when luminance equals `1` or `0`.