Skip to content

Commit

Permalink
Merge pull request #2785 from Textualize/color-algo
Browse files Browse the repository at this point in the history
fix for color downgrade
  • Loading branch information
willmcgugan committed Jan 28, 2023
2 parents 0d062a7 + 056f43a commit 4cf2446
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 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] - 2023-01-28

### Fixed

- Fixed truecolor to eight bit color conversion

## [13.3.0] - 2023-01-27

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/Textualize/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "13.3.0"
version = "13.3.1"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <willmcgugan@gmail.com>"]
license = "MIT"
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 4cf2446

Please sign in to comment.