Improved colour support · commit-0/python-progressbar@58724ab · GitHub
Skip to content

Commit 58724ab

Browse files
committed
Improved colour support
1 parent 1c432ff commit 58724ab

4 files changed

Lines changed: 44 additions & 33 deletions

File tree

progressbar/bar.py

Lines changed: 1 addition & 1 deletion

progressbar/terminal/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from python_utils import converters, types
1212

1313
from .os_specific import getch
14+
from .. import base
1415

1516
ESC = '\x1B'
1617

@@ -411,7 +412,7 @@ def __call__(self, value: float):
411412

412413
def get_color(self, value: float) -> Color:
413414
'Map a value from 0 to 1 to a color'
414-
if value <= 0:
415+
if value is base.Undefined or value is base.UnknownLength or value <= 0:
415416
return self.colors[0]
416417
elif value >= 1:
417418
return self.colors[-1]
@@ -451,7 +452,7 @@ def get_color(value: float, color: OptionalColor) -> Color | None:
451452

452453
def apply_colors(
453454
text: str,
454-
value: float | None = None,
455+
percentage: float | None = None,
455456
*,
456457
fg: OptionalColor = None,
457458
bg: OptionalColor = None,
@@ -461,14 +462,14 @@ def apply_colors(
461462
if fg is None and bg is None:
462463
return text
463464

464-
if value is None:
465+
if percentage is None:
465466
if fg_none is not None:
466467
text = fg_none.fg(text)
467468
if bg_none is not None:
468469
text = bg_none.bg(text)
469470
else:
470-
fg = get_color(value, fg)
471-
bg = get_color(value, bg)
471+
fg = get_color(percentage * 0.01, fg)
472+
bg = get_color(percentage * 0.01, bg)
472473

473474
if fg is not None:
474475
text = fg.fg(text)

progressbar/terminal/colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,13 @@
965965
# Check if the background is light or dark. This is by no means a foolproof
966966
# method, but there is no reliable way to detect this.
967967
if os.environ.get('COLORFGBG', '15;0').split(';')[-1] == str(white.xterm):
968-
print('light background')
969968
# Light background
970969
gradient = light_gradient
970+
primary = black
971971
else:
972-
print('dark background')
973972
# Default, expect a dark background
974973
gradient = dark_gradient
974+
primary = white
975975

976976
if __name__ == '__main__':
977977
red = Colors.register(RGB(255, 128, 128))

progressbar/widgets.py

Lines changed: 35 additions & 25 deletions

0 commit comments

Comments
 (0)