From 0282fa11230a7411691ea563a0be0ef03bd7ec4d Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Wed, 21 Aug 2024 15:56:57 +0200 Subject: [PATCH] more params, refactor --- waves.py | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/waves.py b/waves.py index a3ccb53..1d5a594 100755 --- a/waves.py +++ b/waves.py @@ -10,23 +10,14 @@ from utils import random_color # dimensions of the output image -WIDTH, HEIGHT = 1920, 1080 # how much should the phases be offset? WAVE_OFFSET = math.pi / random.choice([1, 2, 4]) #-1.9 -# amplitude of the sine wave -#AMPLITUDE = 9 -# only 1 color with shades? -MONOCHROME = True -# works only if monochrome is set - uses todays date as base for the color -DATE_BASED_COLOR = True -DATE_BASED_AMPLITUDE = True -DATE_BASED_COUNT = True # background black? White otherwise: DARK_BG = True # precision of the calculation PRECISION = 10000 -def days_color(date: datetime) -> tuple[float, float, float]: +def get_color_from_date(date: datetime) -> tuple[float, float, float]: '''Return a color based on the progress through the year.''' # a day between 1 and 365 (inclusive) today = date.timetuple().tm_yday @@ -35,14 +26,13 @@ def days_color(date: datetime) -> tuple[float, float, float]: progress = today/days_in_year return colorsys.hsv_to_rgb(progress, 1, 0.9) -def days_amp(date: datetime, waves) -> float: +def get_amplitude_from_date(date: datetime, waves) -> float: '''Return the amplitude of waves, based on progress through the month.''' days_in_month = calendar.monthrange(date.year, date.month)[1] max_amp = 1/waves/2 return date.day/days_in_month * max_amp -def create_wpotd(width: int, height: int): - #TODO: return a `file`-like object +def create_wpotd(width: int, height: int, date: datetime = datetime.now(), dark: bool = True) -> cairo.ImageSurface: surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) ctx = cairo.Context(surface) @@ -51,36 +41,25 @@ def create_wpotd(width: int, height: int): lastpoints = [(x/PRECISION, 0) for x in range(PRECISION+1)] - date = datetime.today() frequency = random.randint(10, 40) - if DATE_BASED_COUNT: - # generate as many waves as the number of the current month - waves = date.month - else: - waves = 12 - if DATE_BASED_AMPLITUDE: - amplitude = days_amp(date, waves) - else: - amplitude = 25 - if MONOCHROME: - if DATE_BASED_COLOR: - r, g, b = days_color(date) #datetime.datetime.now()) - else: - r, g, b = random_color() + waves = date.month + amplitude = get_amplitude_from_date(date, waves) + + r, g, b = get_color_from_date(date) alpha_step = 1/waves - if DARK_BG: + ctx.rectangle(0, 0, 1, 1) + if dark: # make bg black - ctx.rectangle(0, 0, 1, 1) ctx.set_source_rgb(0, 0, 0) - ctx.fill() + else: + ctx.set_source_rgb(255, 255, 255) + ctx.fill() wave_height = 1/waves step_size = 1/PRECISION for wave_index in range(waves+1): - if not MONOCHROME: - r, g, b = random_color() points = [] x = 0 while x < 1: @@ -89,8 +68,6 @@ def create_wpotd(width: int, height: int): points.append((x, (y + (0.5 + wave_index)*wave_height))) x += step_size #print(f'Draw {len(points)} points for curve {num}') - if not MONOCHROME: - ctx.set_source_rgb(r, g, b) else: # make more transparent toward bottom ctx.set_source_rgba(r, g, b, 1 - (alpha_step * wave_index)) @@ -109,7 +86,7 @@ def create_wpotd(width: int, height: int): #surface.write_to_png(output) # Output to PNG def main(): - output = create_wpotd(1920, 1080) + output = create_wpotd(1920, 1080, dark=False) output.write_to_png('out/waves.png') if __name__ == '__main__':