Skip to content

Commit 4cf2446

Browse files
authored
Merge pull request #2785 from Textualize/color-algo
fix for color downgrade
2 parents 0d062a7 + 056f43a commit 4cf2446

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [13.3.0]
8+
## [13.3.1] - 2023-01-28
9+
10+
### Fixed
11+
12+
- Fixed truecolor to eight bit color conversion
13+
14+
## [13.3.0] - 2023-01-27
915

1016
### Fixed
1117

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/Textualize/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "13.3.0"
5+
version = "13.3.1"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <willmcgugan@gmail.com>"]
88
license = "MIT"

rich/color.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,9 @@ def downgrade(self, system: ColorSystem) -> "Color":
518518
# Convert to 8-bit color from truecolor color
519519
if system == ColorSystem.EIGHT_BIT and self.system == ColorSystem.TRUECOLOR:
520520
assert self.triplet is not None
521-
red, green, blue = self.triplet.normalized
522-
_h, l, s = rgb_to_hls(red, green, blue)
523-
# If saturation is under 10% assume it is grayscale
524-
if s < 0.1:
521+
_h, l, s = rgb_to_hls(*self.triplet.normalized)
522+
# If saturation is under 15% assume it is grayscale
523+
if s < 0.15:
525524
gray = round(l * 25.0)
526525
if gray == 0:
527526
color_number = 16
@@ -531,8 +530,13 @@ def downgrade(self, system: ColorSystem) -> "Color":
531530
color_number = 231 + gray
532531
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
533532

533+
red, green, blue = self.triplet
534+
six_red = red / 95 if red < 95 else 1 + (red - 95) / 40
535+
six_green = green / 95 if green < 95 else 1 + (green - 95) / 40
536+
six_blue = blue / 95 if blue < 95 else 1 + (blue - 95) / 40
537+
534538
color_number = (
535-
16 + 36 * round(red * 5.0) + 6 * round(green * 5.0) + round(blue * 5.0)
539+
16 + 36 * round(six_red) + 6 * round(six_green) + round(six_blue)
536540
)
537541
return Color(self.name, ColorType.EIGHT_BIT, number=color_number)
538542

0 commit comments

Comments
 (0)