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

View file

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

View file

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

View file

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

View file

@ -1,9 +1,9 @@
from typing import Any, Optional, Tuple from typing import Any, Optional, Tuple
import pygame as pg import pygame as pg
from effects.effect import MovingEffect from effects.effect import MovingEffect
from util.color import Colors from util.color import Colors, DynamicColor
import math import math
from typing import Union, Generator from typing import Generator
from util.transform import PositionGenerator from util.transform import PositionGenerator
@ -19,8 +19,8 @@ class Moonflower(MovingEffect):
self, self,
bounds: pg.Rect, bounds: pg.Rect,
colors: Tuple[ colors: Tuple[
Union[pg.Color, Generator[pg.Color, None, None]], DynamicColor,
Union[pg.Color, Generator[pg.Color, None, None]], DynamicColor,
], ],
mover: Optional[PositionGenerator] = None, mover: Optional[PositionGenerator] = None,
beat_color: bool = False, 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 from typing import Any, Tuple
import pygame as pg import pygame as pg
from effects.effect import Effect from effects.effect import Effect
from util.color import Colors from util.color import Colors, DynamicColor
from util.transform import transform_bounce from util.transform import transform_bounce
import math import math
from typing import Union, Generator from typing import Generator
def calc_radii(size, f): def calc_radii(size, f):
@ -17,7 +17,7 @@ class RotatingPoly(Effect):
def __init__( def __init__(
self, self,
bounds: pg.Rect, bounds: pg.Rect,
color: Union[pg.Color, Generator[pg.Color, None, None]], color: DynamicColor,
beat_color: bool = False, beat_color: bool = False,
size: int = 100, size: int = 100,
velocity: Tuple[float, float] = (1, 10), velocity: Tuple[float, float] = (1, 10),

View file

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

View file

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

View file

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

View file

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