Add colors attribute to Bar class
This list lets you specify a set of colors that the bar should be painted with depending on the percentage the bar is filled
This commit is contained in:
parent
6073454ed3
commit
11aee12320
1 changed files with 39 additions and 4 deletions
|
@ -1,12 +1,37 @@
|
|||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from . import color
|
||||
from ..geometry import Point
|
||||
|
||||
class Bar:
|
||||
def __init__(self, *, position: Point, width: int):
|
||||
'''A bar that expresses a percentage.'''
|
||||
|
||||
def __init__(self, *, position: Point, width: int, colors: Optional[List[Tuple[float, color.Color]]] = None):
|
||||
'''
|
||||
Instantiate a new Bar
|
||||
|
||||
Arguments
|
||||
---------
|
||||
position : Point
|
||||
The position within a console to render this bar
|
||||
width : int
|
||||
The length of the bar in tiles
|
||||
colors : List[Tuple[float, color.Color]]
|
||||
A list of two-tuples specifying a percentage and color to draw the bar. If the bar is less than or equal to
|
||||
the specified percentage, that color will be chosen. For example, if the bar is 45% filled, and this colors
|
||||
array is specified:
|
||||
|
||||
```
|
||||
[(0.25, RED), (0.5, ORANGE), (0.75, YELLOW), (1.0, GREEN)]
|
||||
```
|
||||
|
||||
The bar will be painted `ORANGE` because 45% is greater than 25% and less than 50%.
|
||||
'''
|
||||
self.position = position
|
||||
self.width = width
|
||||
self.colors = sorted(colors, key=lambda c: c[0]) if colors is not None else []
|
||||
|
||||
self._percent_filled = 1.0
|
||||
|
||||
|
@ -20,8 +45,18 @@ class Bar:
|
|||
self._percent_filled = min(1, max(0, value))
|
||||
|
||||
def render_to_console(self, console):
|
||||
console.draw_rect(x=self.position.x, y=self.position.y, width=self.width, height=1, ch=1, bg=color.GREY10)
|
||||
'''Draw this bar to the console'''
|
||||
# Draw the background first
|
||||
console.draw_rect(x=self.position.x, y=self.position.y, width=self.width, height=1, ch=1, bg=color.GREY12)
|
||||
|
||||
percent_filled = self._percent_filled
|
||||
if percent_filled > 0:
|
||||
for color_spec in self.colors:
|
||||
if percent_filled <= color_spec[0]:
|
||||
bar_color = color_spec[1]
|
||||
break
|
||||
else:
|
||||
bar_color = color.GREY50
|
||||
|
||||
if self._percent_filled > 0:
|
||||
filled_width = round(self._percent_filled * self.width)
|
||||
console.draw_rect(x=self.position.x, y=self.position.y, width=filled_width, height=1, ch=1, bg=color.GREY50)
|
||||
console.draw_rect(x=self.position.x, y=self.position.y, width=filled_width, height=1, ch=1, bg=bar_color)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue