Skip to content

Commit daf6e38

Browse files
committed
fix for color downgrade
1 parent 0d062a7 commit daf6e38

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

CHANGELOG.md

+7-1
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] - Unreleased
9+
10+
### Fixed
11+
12+
- Fixed truecolor to eight bit color conversion
13+
14+
## [13.3.0] - 2023-01-27
915

1016
### Fixed
1117

rich/color.py

+9-5
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)