Skip to content

Commit

Permalink
Fix issue where compressed hex wouldn't be detected as color name
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Mar 18, 2021
1 parent 0b4d125 commit 6ec3032
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion coloraide/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 6 additions & 10 deletions coloraide/css/colors/srgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down

0 comments on commit 6ec3032

Please sign in to comment.