File size: 3,661 Bytes
5b76e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import functools
from typing import Iterable

from rich.cells import cell_len
from rich.color import Color
from rich.console import ConsoleOptions, Console, RenderResult, RenderableType
from rich.segment import Segment
from rich.style import Style

from textual.renderables._blend_colors import blend_colors


@functools.lru_cache(maxsize=1024)
def _get_blended_style_cached(
    bg_color: Color, fg_color: Color, opacity: float
) -> Style:
    """Blend from one color to another.

    Cached because when a UI is static the opacity will be constant.

    Args:
        bg_color (Color): Background color.
        fg_color (Color): Foreground color.
        opacity (float): Opacity.

    Returns:
        Style: Resulting style.
    """
    return Style.from_color(
        color=blend_colors(bg_color, fg_color, ratio=opacity),
        bgcolor=bg_color,
    )


class TextOpacity:
    """Blend foreground in to background."""

    def __init__(self, renderable: RenderableType, opacity: float = 1.0) -> None:
        """Wrap a renderable to blend foreground color into the background color.

        Args:
            renderable (RenderableType): The RenderableType to manipulate.
            opacity (float): The opacity as a float. A value of 1.0 means text is fully visible.
        """
        self.renderable = renderable
        self.opacity = opacity

    @classmethod
    def process_segments(
        cls, segments: Iterable[Segment], opacity: float
    ) -> Iterable[Segment]:
        """Apply opacity to segments.

        Args:
            segments (Iterable[Segment]): Incoming segments.
            opacity (float): Opacity to apply.

        Returns:
            Iterable[Segment]: Segments with applied opacity.

        """
        _Segment = Segment
        _from_color = Style.from_color
        if opacity == 0:
            for text, style, control in segments:
                invisible_style = _from_color(bgcolor=style.bgcolor)
                yield _Segment(cell_len(text) * " ", invisible_style)
        else:
            for segment in segments:
                text, style, control = segment
                if not style:
                    yield segment
                    continue

                color = style.color
                bgcolor = style.bgcolor
                if color and color.triplet and bgcolor and bgcolor.triplet:
                    color_style = _get_blended_style_cached(bgcolor, color, opacity)
                    yield _Segment(text, style + color_style)
                else:
                    yield segment

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        segments = console.render(self.renderable, options)
        return self.process_segments(segments, self.opacity)


if __name__ == "__main__":
    from rich.live import Live
    from rich.panel import Panel
    from rich.text import Text

    from time import sleep

    console = Console()

    panel = Panel.fit(
        Text("Steak: £30", style="#fcffde on #03761e"),
        title="Menu",
        style="#ffffff on #000000",
    )
    console.print(panel)

    opacity_panel = TextOpacity(panel, opacity=0.5)
    console.print(opacity_panel)

    def frange(start, end, step):
        current = start
        while current < end:
            yield current
            current += step

        while current >= 0:
            yield current
            current -= step

    import itertools

    with Live(opacity_panel, refresh_per_second=60) as live:
        for value in itertools.cycle(frange(0, 1, 0.05)):
            opacity_panel.value = value
            sleep(0.05)