mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Animating alpha colors
This commit is contained in:
0
sandbox/darren/color_animate.css
Normal file
0
sandbox/darren/color_animate.css
Normal file
22
sandbox/darren/color_animate.py
Normal file
22
sandbox/darren/color_animate.py
Normal file
@@ -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()
|
||||
@@ -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())
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user