From 2002d8049b0392fd52445ff3ea375f8eea6a9e21 Mon Sep 17 00:00:00 2001 From: Gerben Welter Date: Tue, 1 Jun 2021 11:15:11 +0200 Subject: [PATCH 1/2] Fix broken color test by testing the values individually Testing if the sum of the color values is within range is flawed because one out of range value might still not exceed the minimum or maximum value. Multiple out of range values can even pass the test by cancelling each other out. This patch tests each value individually and raises an exception on the first out of range value. --- bpytop.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bpytop.py b/bpytop.py index 21e252e..dfb92f7 100755 --- a/bpytop.py +++ b/bpytop.py @@ -1141,9 +1141,10 @@ class Color: else: raise ValueError(f'RGB dec should be "0-255 0-255 0-255"') - ct = self.dec[0] + self.dec[1] + self.dec[2] - if ct > 255*3 or ct < 0: - raise ValueError(f'RGB values out of range: {color}') + for i in [0, 1, 2]: + if not (0 <= self.dec[i] <= 255): + raise ValueError(f'One or more RGB values are out of range: {color}') + except Exception as e: errlog.exception(str(e)) self.escape = "" From 83bbfcff1bf0dad8801d7aaeeba36dd5e86935cd Mon Sep 17 00:00:00 2001 From: Gerben Welter Date: Mon, 7 Jun 2021 08:42:30 +0200 Subject: [PATCH 2/2] Replace fix with a more idiomatic version This was suggested by @UmarJ. Thanks! --- bpytop.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bpytop.py b/bpytop.py index dfb92f7..edb47a9 100755 --- a/bpytop.py +++ b/bpytop.py @@ -1141,9 +1141,8 @@ class Color: else: raise ValueError(f'RGB dec should be "0-255 0-255 0-255"') - for i in [0, 1, 2]: - if not (0 <= self.dec[i] <= 255): - raise ValueError(f'One or more RGB values are out of range: {color}') + if not all(0 <= c <= 255 for c in self.dec): + raise ValueError(f'One or more RGB values are out of range: {color}') except Exception as e: errlog.exception(str(e))