Skip to content

Commit

Permalink
fix for color downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 27, 2023
1 parent 0d062a7 commit daf6e38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [13.3.0]
## [13.3.1] - Unreleased

### Fixed

- Fixed truecolor to eight bit color conversion

## [13.3.0] - 2023-01-27

### Fixed

Expand Down
14 changes: 9 additions & 5 deletions rich/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,9 @@ def downgrade(self, system: ColorSystem) -> "Color":
# Convert to 8-bit color from truecolor color
if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR:
assert self.triplet is not None
red, green, blue = self.triplet.normalized
_h, l, s = rgb_to_hls(red, green, blue)
# If saturation is under 10% assume it is grayscale
if s < 0.1:
_h, l, s = rgb_to_hls(*self.triplet.normalized)
# If saturation is under 15% assume it is grayscale
if s < 0.15:
gray = round(l * 25.0)
if gray == 0:
color_number = 16
Expand All @@ -531,8 +530,13 @@ def downgrade(self, system: ColorSystem) -> "Color":
color_number = 231 + gray
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)

red, green, blue = self.triplet
six_red = red / 95 if red < 95 else 1 + (red - 95) / 40
six_green = green / 95 if green < 95 else 1 + (green - 95) / 40
six_blue = blue / 95 if blue < 95 else 1 + (blue - 95) / 40

color_number = (
16 + 36 * round(red * 5.0) + 6 * round(green * 5.0) + round(blue * 5.0)
16 + 36 * round(six_red) + 6 * round(six_green) + round(six_blue)
)
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)

Expand Down

0 comments on commit daf6e38

Please sign in to comment.