type alias for dynamiccolor

This commit is contained in:
Patrick Moessler 2023-02-23 02:10:50 +01:00
parent 9422ccce07
commit 4720eb816c
11 changed files with 147 additions and 42 deletions

View file

@ -1,7 +1,6 @@
from effects.effect import MovingEffect
from typing import Any, Optional
from typing import Union, Generator
from util.color import Colors
from util.color import Colors, DynamicColor
import math
import pygame as pg
import random
@ -13,7 +12,7 @@ class BouncingSpot(MovingEffect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
sizes=(10, 100),
velocity=(1, 10),
mover: Optional[PositionGenerator] = None,

View file

@ -1,7 +1,6 @@
from effects.effect import Effect
from typing import Any
from typing import Union, Generator
from util.color import Colors
from util.color import Colors, DynamicColor
from util.transform import transform_bounce
import math
import pygame as pg
@ -12,7 +11,7 @@ class CrazyPolys(Effect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
sizes=(100, 500),
velocity=(0.1, 1),
x_factor=(0.1, 1),

View file

@ -1,9 +1,9 @@
from typing import Any, List, Tuple
import pygame as pg
from effects.effect import Effect
from util.color import Colors
from util.color import Colors, DynamicColor
import random
from typing import Union, Generator
from typing import Generator
def fade_statemachine(
@ -57,7 +57,7 @@ class DoubleSpot(Effect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
radius: int = 200,
hold: int = 60 * 1,
fade_out: bool = False,

View file

@ -1,17 +1,17 @@
from typing import Any, List, Tuple
import pygame as pg
from effects.effect import Effect
from util.color import Colors, color_fade
from util.color import Colors, color_fade, DynamicColor
from util.transform import transform_falling
import random
from typing import Union, Generator
from typing import Generator
class Drop(pg.sprite.Sprite):
def __init__(
self,
bounds: pg.rect.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
acceleration=1.0,
) -> None:
@ -65,8 +65,8 @@ class Drops(Effect):
self,
bounds: pg.Rect,
colors: Tuple[
Union[pg.Color, Generator[pg.Color, None, None]],
Union[pg.Color, Generator[pg.Color, None, None]],
DynamicColor,
DynamicColor,
],
sizes=(10, 100),
drop_rate=0.2,

View file

@ -1,9 +1,9 @@
from typing import Any, Optional, Tuple
import pygame as pg
from effects.effect import MovingEffect
from util.color import Colors
from util.color import Colors, DynamicColor
import math
from typing import Union, Generator
from typing import Generator
from util.transform import PositionGenerator
@ -19,8 +19,8 @@ class Moonflower(MovingEffect):
self,
bounds: pg.Rect,
colors: Tuple[
Union[pg.Color, Generator[pg.Color, None, None]],
Union[pg.Color, Generator[pg.Color, None, None]],
DynamicColor,
DynamicColor,
],
mover: Optional[PositionGenerator] = None,
beat_color: bool = False,

108
effects/movingwave.py Normal file
View file

@ -0,0 +1,108 @@
from typing import Any, Generator, Optional, Union
import pygame as pg
from effects.effect import MovingEffect
from util.color import Colors, rainbow_surface
from util.transform import PositionGenerator
import math
class MovingWave(MovingEffect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
wave_count: float = 2,
wave_height: int = 0,
thickness: int = 10,
hue: int = 0,
color_inc: int = 1,
start_phase: int = 0,
scroll_speed: float = 5,
mover: Optional[PositionGenerator] = None,
*groups: pg.sprite.Group
) -> None:
image = pg.Surface(bounds.size)
image.fill(Colors.Black)
image.set_colorkey(Colors.Black)
super().__init__(image, bounds, mover, *groups)
self.subrect = pg.rect.Rect(0, 0, bounds.width, bounds.height)
self.wave_height = wave_height or bounds.height
self.bounds = bounds
self.scroll_speed = scroll_speed
next(self.mover)
self.rainbow = pg.Surface((bounds.width, self.wave_height))
rainbow_surface(self.rainbow, "h", hue, color_inc)
self.wave_image = pg.Surface(
(bounds.width * (wave_count + 1) / wave_count, self.wave_height)
)
self.wave_image.fill(Colors.Black)
self.wave_image.set_colorkey(Colors.Black)
y_halfheight = self.wave_image.get_height() // 2
points = [
(
x,
y_halfheight
+ (y_halfheight - thickness)
* math.sin(
math.pi
* (
start_phase / 180
+ x / self.wave_image.get_width() * 2 * (wave_count + 1)
)
),
)
for x in range(self.wave_image.get_width())
]
pg.draw.lines(self.wave_image, Colors.White, False, points, width=thickness)
pg.draw.lines(
self.wave_image,
Colors.White,
False,
[(x + 2, y) for x, y in points],
width=thickness,
)
self.update(is_beat=False)
def update(self, *args: Any, **kwargs: Any) -> None:
self.image.fill(Colors.Black)
# rainbow_surface(self.image)
pos = self.mover.send(
((self.bounds.width, self.wave_height), kwargs["is_beat"])
)
# pg.draw.rect(self.image, Colors.Black, (0, 0, self.bounds.width, pos[1]))
# pg.draw.rect(
# self.image,
# Colors.Black,
# (
# 0,
# pos[1] + self.wave_height,
# self.bounds.width,
# self.bounds.height - pos[1] + self.wave_height,
# ),
# )
self.image.blit(self.rainbow, pos)
self.image.blit(self.wave_image, pos, self.subrect, pg.BLEND_MULT)
self.subrect.move_ip(self.scroll_speed, 0)
if self.subrect.right >= self.wave_image.get_rect().right:
self.subrect.left = 0
# pg.draw.ellipse(
# self.image,
# self.color if isinstance(self.color, pg.Color) else next(self.color),
# ((0, 0), self.rect.size),
# )
# self.ticks += int(self.velocity / 180 * math.pi)
# self.velocity = random.randint(self.min_velocity, self.max_velocity)

View file

@ -1,10 +1,10 @@
from typing import Any, Tuple
import pygame as pg
from effects.effect import Effect
from util.color import Colors
from util.color import Colors, DynamicColor
from util.transform import transform_bounce
import math
from typing import Union, Generator
from typing import Generator
def calc_radii(size, f):
@ -17,7 +17,7 @@ class RotatingPoly(Effect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
beat_color: bool = False,
size: int = 100,
velocity: Tuple[float, float] = (1, 10),

View file

@ -1,9 +1,7 @@
from typing import Any, Optional, Tuple
import pygame as pg
from effects.effect import MovingEffect
from util.color import Colors
from typing import Union, Generator
from util.color import Colors, DynamicColor
from util.transform import PositionGenerator
@ -12,8 +10,8 @@ class ScanReticle(MovingEffect):
self,
bounds: pg.Rect,
colors: Tuple[
Union[pg.Color, Generator[pg.Color, None, None]],
Union[pg.Color, Generator[pg.Color, None, None]],
DynamicColor,
DynamicColor,
],
mover: Optional[PositionGenerator] = None,
*groups: pg.sprite.Group

View file

@ -1,18 +1,17 @@
from typing import Any, Optional
import pygame as pg
from effects.effect import MovingEffect
from util.color import Colors
from util.transform import PositionGenerator, transform_bounce
from util.color import Colors, DynamicColor
from util.transform import PositionGenerator
import random
import math
from typing import Union, Generator
class Spiro(MovingEffect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
sizes=(100, 500),
velocity=(0.1, 1),
segments=100,

View file

@ -1,9 +1,9 @@
from typing import Any, List, Tuple
import pygame as pg
from effects.effect import Effect
from util.color import Colors
from util.color import Colors, DynamicColor
import random
from typing import Union, Generator
from typing import Generator
def fade_statemachine(
@ -57,7 +57,7 @@ class Starfield(Effect):
def __init__(
self,
bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]],
color: DynamicColor,
radius: int = 20,
star_factor: float = 0.2,
hold: int = 60 * 3,

View file

@ -1,9 +1,13 @@
from dataclasses import dataclass
import random
from typing import Generator, Iterable, Literal, Sequence
from typing import Generator, Literal, Sequence, Union
import pygame as pg
ColorGenerator = Generator[pg.Color, None, None]
DynamicColor = Union[pg.Color, ColorGenerator]
def copy_color(source: pg.Color) -> pg.Color:
return pg.Color(source.r, source.g, source.b, source.a)
@ -20,7 +24,7 @@ class Colors:
Magenta = pg.Color(255, 0, 255)
def color_wheel(hue=0, increase=1) -> Generator[pg.Color, None, None]:
def color_wheel(hue=0, increase=1) -> ColorGenerator:
color = copy_color(Colors.Red)
h, s, l, a = color.hsla
h = hue % 360
@ -31,7 +35,7 @@ def color_wheel(hue=0, increase=1) -> Generator[pg.Color, None, None]:
h = (h + increase) % 360
def color_randomize() -> Generator[pg.Color, None, None]:
def color_randomize() -> ColorGenerator:
color = copy_color(Colors.Red)
h, s, l, a = color.hsla
color.hsla = random.randrange(0, 360 // 5) * 5, s, l, a
@ -41,9 +45,7 @@ def color_randomize() -> Generator[pg.Color, None, None]:
color.hsla = random.randrange(0, 360 // 5) * 5, s, l, a
def color_fadeout(
initial_color: pg.Color, fade_speed: float
) -> Generator[pg.Color, None, None]:
def color_fadeout(initial_color: pg.Color, fade_speed: float) -> ColorGenerator:
color = copy_color(initial_color)
h, s, l, a = color.hsla
l_float = float(l)
@ -58,7 +60,7 @@ def color_fadeout(
def color_fade(
initial_color: pg.Color, end_color: pg.Color, duration: int
) -> Generator[pg.Color, None, None]:
) -> ColorGenerator:
color = copy_color(initial_color)
h, s, l, a = color.hsla
h2, s2, l2, a2 = end_color.hsla
@ -84,13 +86,13 @@ def color_fade(
a_f += a_inc
def color_cycle(seq: Sequence[pg.Color]) -> Generator[pg.Color, None, None]:
def color_cycle(seq: Sequence[pg.Color]) -> ColorGenerator:
it = iter(seq)
while True:
yield next(it)
def color_shuffle(seq: Sequence[pg.Color]) -> Generator[pg.Color, None, None]:
def color_shuffle(seq: Sequence[pg.Color]) -> ColorGenerator:
while True:
yield random.choice(seq)