prettier bars

This commit is contained in:
Will McGugan 2020-10-16 12:32:15 +01:00
parent 509decea2b
commit ddea7878a9
3 changed files with 24 additions and 1 deletions

View File

@ -6,6 +6,7 @@ Use Bar to renderer a sort-of cirlce.
import math import math
from rich.bar import Bar from rich.bar import Bar
from rich.color import Color
from rich import print from rich import print
@ -14,5 +15,6 @@ SIZE = 40
for row in range(SIZE): for row in range(SIZE):
y = (row / SIZE) * 2 - 1 y = (row / SIZE) * 2 - 1
x = math.sqrt(1 - y * y) x = math.sqrt(1 - y * y)
bar = Bar(1, width=SIZE * 2, begin=1 - x, end=x, color="red") color = Color.from_rgb((y + 1) * 127, 0, 0)
bar = Bar(1, width=SIZE * 2, begin=1 - x, end=x, color=color)
print(bar) print(bar)

View File

@ -339,6 +339,21 @@ class Color(NamedTuple):
""" """
return cls(name=triplet.hex, type=ColorType.TRUECOLOR, triplet=triplet) return cls(name=triplet.hex, type=ColorType.TRUECOLOR, triplet=triplet)
@classmethod
def from_rgb(cls, red: float, green: float, blue: float) -> "Color":
"""Create a truecolor from three color components in the range(0->255).
Args:
red (float): Red component.
green (float): Green component.
blue (float): Blue component.
Returns:
Color: A new color object.
"""
triplet = ColorTriplet(int(red), int(green), int(blue))
return cls.from_triplet(triplet)
@classmethod @classmethod
def default(cls) -> "Color": def default(cls) -> "Color":
"""Get a Color instance representing the default color. """Get a Color instance representing the default color.

View File

@ -67,6 +67,12 @@ def test_from_triplet() -> None:
) )
def test_from_rgb() -> None:
assert Color.from_rgb(0x10, 0x20, 0x30) == Color(
"#102030", ColorType.TRUECOLOR, None, ColorTriplet(0x10, 0x20, 0x30)
)
def test_default() -> None: def test_default() -> None:
assert Color.default() == Color("default", ColorType.DEFAULT, None, None) assert Color.default() == Color("default", ColorType.DEFAULT, None, None)