From daf6e38f553cb76de3aefbc847e15d8055913f8d Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 27 Jan 2023 23:14:16 +0100 Subject: [PATCH] fix for color downgrade --- CHANGELOG.md | 8 +++++++- rich/color.py | 14 +++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1ee8ead..9ede7a9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/rich/color.py b/rich/color.py index 35281eb9..dfe45593 100644 --- a/rich/color.py +++ b/rich/color.py @@ -518,10 +518,9 @@ class Color(NamedTuple): # 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 @@ -531,8 +530,13 @@ class Color(NamedTuple): 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)