Fix color copying for color_*()

This commit is contained in:
Patrick Moessler 2023-02-18 22:51:52 +01:00
parent f2f8ccaf56
commit e5a0dcaf61

View file

@ -2,11 +2,15 @@ from dataclasses import dataclass
from enum import Enum from enum import Enum
import math import math
import random import random
from typing import Tuple from typing import Generator, Tuple
import pygame as pg import pygame as pg
from abc import abstractmethod from abc import abstractmethod
def copy_color(source: pg.Color) -> pg.Color:
return pg.Color(source.r, source.g, source.b, source.a)
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class Colors: class Colors:
Black = pg.Color(0, 0, 0) Black = pg.Color(0, 0, 0)
@ -20,19 +24,18 @@ class Colors:
def color_wheel(hue=0, increase=1) -> pg.Color: def color_wheel(hue=0, increase=1) -> pg.Color:
color = Colors.Red color = copy_color(Colors.Red)
h, s, l, a = color.hsla h, s, l, a = color.hsla
color.hsla = hue, s, l, a h = hue
while True: while True:
yield color
h, s, l, a = color.hsla
h = (h + increase) % 360
color.hsla = h, s, l, a color.hsla = h, s, l, a
yield color
h = (h + increase) % 360
def color_randomize() -> pg.Color: def color_randomize() -> pg.Color:
color = Colors.Red color = copy_color(Colors.Red)
h, s, l, a = color.hsla h, s, l, a = color.hsla
color.hsla = random.randint(0, 359), s, l, a color.hsla = random.randint(0, 359), s, l, a
@ -57,12 +60,10 @@ def transform_bounce(
size_x, size_y = yield (bounds.centerx, bounds.centery) size_x, size_y = yield (bounds.centerx, bounds.centery)
while True: while True:
pos_x = ( pos_x = (
math.cos(x_factor * ticks) * (bounds.width - 2 * size_x) // 2 math.cos(x_factor * ticks) * (bounds.width - size_x) // 2 + bounds.centerx
+ bounds.centerx
) )
pos_y = ( pos_y = (
math.sin(y_factor * ticks) * (bounds.height - 2 * size_y) // 2 math.sin(y_factor * ticks) * (bounds.height - size_y) // 2 + bounds.centery
+ bounds.centery
) )
ticks += velocity / 180 * math.pi ticks += velocity / 180 * math.pi