black formatting
This commit is contained in:
parent
0282fa1123
commit
82c19b690b
54
waves.py
54
waves.py
@ -6,71 +6,75 @@ from datetime import datetime
|
|||||||
import calendar
|
import calendar
|
||||||
import random
|
import random
|
||||||
import colorsys
|
import colorsys
|
||||||
from utils import random_color
|
|
||||||
|
|
||||||
|
|
||||||
# dimensions of the output image
|
# dimensions of the output image
|
||||||
# how much should the phases be offset?
|
# how much should the phases be offset?
|
||||||
WAVE_OFFSET = math.pi / random.choice([1, 2, 4]) #-1.9
|
WAVE_OFFSET = math.pi / random.choice([1, 2, 4]) # -1.9
|
||||||
# background black? White otherwise:
|
# background black? White otherwise:
|
||||||
DARK_BG = True
|
DARK_BG = True
|
||||||
# precision of the calculation
|
# precision of the calculation
|
||||||
PRECISION = 10000
|
PRECISION = 10000
|
||||||
|
|
||||||
|
|
||||||
def get_color_from_date(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.'''
|
"""Return a color based on the progress through the year."""
|
||||||
# a day between 1 and 365 (inclusive)
|
# a day between 1 and 365 (inclusive)
|
||||||
today = date.timetuple().tm_yday
|
today = date.timetuple().tm_yday
|
||||||
days_in_year = 365 + calendar.isleap(date.year)
|
days_in_year = 365 + calendar.isleap(date.year)
|
||||||
# between 0 and 1, how far through the year are we?
|
# between 0 and 1, how far through the year are we?
|
||||||
progress = today/days_in_year
|
progress = today / days_in_year
|
||||||
return colorsys.hsv_to_rgb(progress, 1, 0.9)
|
return colorsys.hsv_to_rgb(progress, 1, 0.9)
|
||||||
|
|
||||||
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, date: datetime = datetime.now(), dark: bool = True) -> cairo.ImageSurface:
|
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, date: datetime = datetime.now(), dark: bool = True
|
||||||
|
) -> cairo.ImageSurface:
|
||||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
||||||
ctx = cairo.Context(surface)
|
ctx = cairo.Context(surface)
|
||||||
|
|
||||||
ctx.scale(width, height) # Normalizing the canvas
|
ctx.scale(width, height) # Normalizing the canvas
|
||||||
#ctx.set_antialias(cairo.Antialias.BEST)
|
# ctx.set_antialias(cairo.Antialias.BEST)
|
||||||
|
|
||||||
lastpoints = [(x/PRECISION, 0) for x in range(PRECISION+1)]
|
lastpoints = [(x / PRECISION, 0) for x in range(PRECISION + 1)]
|
||||||
|
|
||||||
frequency = random.randint(10, 40)
|
frequency = random.randint(10, 40)
|
||||||
waves = date.month
|
waves = date.month
|
||||||
amplitude = get_amplitude_from_date(date, waves)
|
amplitude = get_amplitude_from_date(date, waves)
|
||||||
|
|
||||||
r, g, b = get_color_from_date(date)
|
r, g, b = get_color_from_date(date)
|
||||||
alpha_step = 1/waves
|
alpha_step = 1 / waves
|
||||||
|
|
||||||
ctx.rectangle(0, 0, 1, 1)
|
# background color
|
||||||
if dark:
|
if dark:
|
||||||
# make bg black
|
|
||||||
ctx.set_source_rgb(0, 0, 0)
|
ctx.set_source_rgb(0, 0, 0)
|
||||||
else:
|
else:
|
||||||
ctx.set_source_rgb(255, 255, 255)
|
ctx.set_source_rgb(255, 255, 255)
|
||||||
|
ctx.rectangle(0, 0, 1, 1)
|
||||||
ctx.fill()
|
ctx.fill()
|
||||||
|
|
||||||
wave_height = 1/waves
|
wave_height = 1 / waves
|
||||||
step_size = 1/PRECISION
|
step_size = 1 / PRECISION
|
||||||
|
|
||||||
for wave_index in range(waves+1):
|
for wave_index in range(waves + 1):
|
||||||
points = []
|
points = []
|
||||||
x = 0
|
x = 0
|
||||||
while x < 1:
|
while x < 1:
|
||||||
# step along, create points along the wave
|
# step along, create points along the wave
|
||||||
y = amplitude * math.sin(frequency * x + (wave_index * WAVE_OFFSET))
|
y = amplitude * math.sin(frequency * x + (wave_index * WAVE_OFFSET))
|
||||||
points.append((x, (y + (0.5 + wave_index)*wave_height)))
|
points.append((x, (y + (0.5 + wave_index) * wave_height)))
|
||||||
x += step_size
|
x += step_size
|
||||||
#print(f'Draw {len(points)} points for curve {num}')
|
# print(f'Draw {len(points)} points for curve {num}')
|
||||||
else:
|
else:
|
||||||
# make more transparent toward bottom
|
# make more transparent toward bottom
|
||||||
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * wave_index))
|
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * wave_index))
|
||||||
# draw waves
|
# draw waves
|
||||||
ctx.move_to(*points[0])
|
ctx.move_to(*points[0])
|
||||||
for p in points[1:]:
|
for p in points[1:]:
|
||||||
@ -83,11 +87,13 @@ def create_wpotd(width: int, height: int, date: datetime = datetime.now(), dark:
|
|||||||
ctx.fill()
|
ctx.fill()
|
||||||
lastpoints = points
|
lastpoints = points
|
||||||
return surface
|
return surface
|
||||||
#surface.write_to_png(output) # Output to PNG
|
# surface.write_to_png(output) # Output to PNG
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
output = create_wpotd(1920, 1080, dark=False)
|
output = create_wpotd(1920, 1080, dark=False)
|
||||||
output.write_to_png('out/waves.png')
|
output.write_to_png("out/waves.png")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user