From 311108f779e58c728e769f6eaaecd7719e805d11 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 20 Oct 2022 09:47:03 +0100 Subject: [PATCH] Animating alpha colors --- sandbox/darren/color_animate.css | 0 sandbox/darren/color_animate.py | 22 ++++++++++++++++++++++ sandbox/darren/just_a_box.py | 23 +++++++++++++++-------- src/textual/color.py | 18 +++++++++++++----- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 sandbox/darren/color_animate.css create mode 100644 sandbox/darren/color_animate.py diff --git a/sandbox/darren/color_animate.css b/sandbox/darren/color_animate.css new file mode 100644 index 000000000..e69de29bb diff --git a/sandbox/darren/color_animate.py b/sandbox/darren/color_animate.py new file mode 100644 index 000000000..fb5227d76 --- /dev/null +++ b/sandbox/darren/color_animate.py @@ -0,0 +1,22 @@ +from textual.app import App, ComposeResult +from textual.color import Color +from textual.widgets import Static + +START_COLOR = Color.parse("#FF0000EE") +END_COLOR = Color.parse("#0000FF0F") + + +class ColorAnimate(App): + def compose(self) -> ComposeResult: + box = Static("Hello, world", id="box") + box.styles.background = START_COLOR + self.box = box + yield box + + def key_a(self): + self.animator.animate(self.box.styles, "background", END_COLOR, duration=2.0) + + +app = ColorAnimate(css_path="color_animate.css") +if __name__ == "__main__": + app.run() diff --git a/sandbox/darren/just_a_box.py b/sandbox/darren/just_a_box.py index fa2ee364d..b4bc98e46 100644 --- a/sandbox/darren/just_a_box.py +++ b/sandbox/darren/just_a_box.py @@ -2,19 +2,21 @@ from __future__ import annotations from textual.app import App, ComposeResult from textual.binding import Binding +from textual.screen import Screen from textual.widgets import Static, Footer, Header -class JustABox(App): +class MainScreen(Screen): + BINDINGS = [ Binding( key="ctrl+t", action="text_fade_out", description="text-opacity fade out" ), - Binding( - key="o,f,w", - action="widget_fade_out", - description="opacity fade out", - key_display="o or f or w", + ( + "o,f,w", + "widget_fade_out", + "opacity fade out", + # key_display="o or f or w", ), ] @@ -25,11 +27,16 @@ class JustABox(App): def action_text_fade_out(self) -> None: box = self.query_one("#box1") - self.animator.animate(box.styles, "text_opacity", value=0.0, duration=1) + self.app.animator.animate(box.styles, "text_opacity", value=0.0, duration=1) def action_widget_fade_out(self) -> None: box = self.query_one("#box1") - self.animator.animate(box.styles, "opacity", value=0.0, duration=1) + self.app.animator.animate(box.styles, "opacity", value=0.0, duration=1) + + +class JustABox(App): + def on_mount(self): + self.push_screen(MainScreen()) def key_d(self): print(self.screen.styles.get_rules()) diff --git a/src/textual/color.py b/src/textual/color.py index 16108e71d..5a60bb7b6 100644 --- a/src/textual/color.py +++ b/src/textual/color.py @@ -341,13 +341,15 @@ class Color(NamedTuple): r, g, b, _ = self return Color(r, g, b, alpha) - def blend(self, destination: Color, factor: float, alpha: float = 1) -> Color: + def blend( + self, destination: Color, factor: float, alpha: float | None = None + ) -> Color: """Generate a new color between two colors. Args: destination (Color): Another color. factor (float): A blend factor, 0 -> 1. - alpha (float | None): New alpha for result. Defaults to 1. + alpha (float | None): New alpha for result. Defaults to None. Returns: Color: A new color. @@ -356,13 +358,19 @@ class Color(NamedTuple): return self elif factor == 1: return destination - r1, g1, b1, _ = self - r2, g2, b2, _ = destination + r1, g1, b1, a1 = self + r2, g2, b2, a2 = destination + + if alpha is None: + new_alpha = a1 + (a2 - a1) * factor + else: + new_alpha = alpha + return Color( int(r1 + (r2 - r1) * factor), int(g1 + (g2 - g1) * factor), int(b1 + (b2 - b1) * factor), - alpha, + new_alpha, ) def __add__(self, other: object) -> Color: