Rename module, use positional args for Segment, rename value -> opacity in Opacity

This commit is contained in:
Darren Burns
2022-02-10 09:56:06 +00:00
parent 7109ec0796
commit 42b1f7ef4a
4 changed files with 16 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ from rich.console import ConsoleOptions, Console, RenderResult, RenderableType
from rich.segment import Segment
from rich.style import Style
from textual.renderables.utilities import blend_colors
from textual.renderables._blend_colors import blend_colors
class Opacity:
@@ -13,24 +13,25 @@ class Opacity:
Args:
renderable (RenderableType): The RenderableType to manipulate.
value (float): The opacity as a float. A value of 1.0 means text is fully visible.
opacity (float): The opacity as a float. A value of 1.0 means text is fully visible.
"""
def __init__(self, renderable: RenderableType, value: float = 1.0) -> None:
def __init__(self, renderable: RenderableType, opacity: float = 1.0) -> None:
self.renderable = renderable
self.value = value
self.opacity = opacity
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
segments = console.render(self.renderable, options)
opacity = self.value
opacity = self.opacity
for segment in segments:
style = segment.style
if not style:
yield segment
continue
fg, bg = style.color, style.bgcolor
fg = style.color
bg = style.bgcolor
if fg and fg.triplet and bg and bg.triplet:
yield Segment(
text=segment.text,
@@ -69,7 +70,7 @@ if __name__ == "__main__":
)
console.print(panel)
opacity_panel = Opacity(panel, value=0.5)
opacity_panel = Opacity(panel, opacity=0.5)
console.print(opacity_panel)
def frange(start, end, step):

View File

@@ -8,7 +8,7 @@ from rich.console import ConsoleOptions, Console, RenderResult
from rich.segment import Segment
from rich.style import Style
from textual.renderables.utilities import blend_colors
from textual.renderables._blend_colors import blend_colors
T = TypeVar("T", int, float)

View File

@@ -14,37 +14,37 @@ def text():
def test_simple_opacity(text):
blended_red_on_green = "\x1b[38;2;127;127;0;48;2;0;255;0m"
assert render(Opacity(text, value=.5)) == (
assert render(Opacity(text, opacity=.5)) == (
f"{blended_red_on_green}Hello, world!{STOP}"
)
def test_value_zero_sets_foreground_color_to_background_color(text):
foreground = background = "0;255;0"
assert render(Opacity(text, value=0)) == (
assert render(Opacity(text, opacity=0)) == (
f"\x1b[38;2;{foreground};48;2;{background}mHello, world!{STOP}"
)
def test_opacity_value_of_one_noop(text):
assert render(Opacity(text, value=1)) == render(text)
assert render(Opacity(text, opacity=1)) == render(text)
def test_ansi_colors_noop():
ansi_colored_text = Text("Hello, world!", style="red on green", end="")
assert render(Opacity(ansi_colored_text, value=.5)) == render(ansi_colored_text)
assert render(Opacity(ansi_colored_text, opacity=.5)) == render(ansi_colored_text)
def test_opacity_no_style_noop():
text_no_style = Text("Hello, world!", end="")
assert render(Opacity(text_no_style, value=.2)) == render(text_no_style)
assert render(Opacity(text_no_style, opacity=.2)) == render(text_no_style)
def test_opacity_only_fg_noop():
text_only_fg = Text("Hello, world!", style="#ff0000", end="")
assert render(Opacity(text_only_fg, value=.5)) == render(text_only_fg)
assert render(Opacity(text_only_fg, opacity=.5)) == render(text_only_fg)
def test_opacity_only_bg_noop():
text_only_bg = Text("Hello, world!", style="on #ff0000", end="")
assert render(Opacity(text_only_bg, value=.5)) == render(text_only_bg)
assert render(Opacity(text_only_bg, opacity=.5)) == render(text_only_bg)