You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.7 KiB
Python

3 years ago
#!/usr/bin/env python3
import cairo
import math
2 months ago
from datetime import datetime
import calendar
import random
2 months ago
import colorsys
2 months ago
import argparse
3 years ago
3 years ago
# precision of the calculation
PRECISION = 10000
3 years ago
2 months ago
def get_color_from_date(date: datetime) -> tuple[float, float, float]:
2 months ago
"""Return a color based on the progress through the year."""
# a day between 1 and 365 (inclusive)
today = date.timetuple().tm_yday
2 months ago
days_in_year = 365 + calendar.isleap(date.year)
# between 0 and 1, how far through the year are we?
2 months ago
progress = today / days_in_year
3 years ago
return colorsys.hsv_to_rgb(progress, 1, 0.9)
2 months ago
def get_amplitude_from_date(date: datetime, waves) -> float:
2 months ago
"""Return the amplitude of waves, based on progress through the month."""
days_in_month = calendar.monthrange(date.year, date.month)[1]
2 months ago
max_amp = 1 / waves / 2
return date.day / days_in_month * max_amp
2 months ago
def create_wpotd(
2 months ago
width: int,
height: int,
frequency: int,
wave_offset: float,
date: datetime = datetime.now(),
dark: bool = True,
2 months ago
) -> cairo.ImageSurface:
2 months ago
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
3 years ago
ctx = cairo.Context(surface)
2 months ago
ctx.scale(width, height) # Normalizing the canvas
2 months ago
# ctx.set_antialias(cairo.Antialias.BEST)
3 years ago
2 months ago
lastpoints = [(x / PRECISION, 0) for x in range(PRECISION + 1)]
waves = date.month
amplitude = get_amplitude_from_date(date, waves)
r, g, b = get_color_from_date(date)
2 months ago
alpha_step = 1 / waves
2 months ago
2 months ago
# background color
if dark:
3 years ago
ctx.set_source_rgb(0, 0, 0)
else:
ctx.set_source_rgb(255, 255, 255)
2 months ago
ctx.rectangle(0, 0, 1, 1)
ctx.fill()
2 months ago
wave_height = 1 / waves
step_size = 1 / PRECISION
2 months ago
2 months ago
for wave_index in range(waves + 1):
3 years ago
points = []
x = 0
while x < 1:
2 months ago
# step along, create points along the wave
2 months ago
y = amplitude * math.sin(frequency * x + (wave_index * wave_offset))
2 months ago
points.append((x, (y + (0.5 + wave_index) * wave_height)))
3 years ago
x += step_size
2 months ago
# print(f'Draw {len(points)} points for curve {num}')
else:
2 months ago
# make more transparent toward bottom
2 months ago
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * wave_index))
3 years ago
# draw waves
3 years ago
ctx.move_to(*points[0])
for p in points[1:]:
ctx.line_to(*p)
3 years ago
ctx.set_line_width(0.0004)
ctx.stroke_preserve()
for p in reversed(lastpoints):
ctx.line_to(*p)
ctx.line_to(*points[0])
ctx.fill()
3 years ago
lastpoints = points
2 months ago
return surface
2 months ago
3 years ago
3 years ago
def main():
2 months ago
parser = argparse.ArgumentParser()
parser.add_argument("width", type=int, help="Width of the image")
parser.add_argument("height", type=int, help="Height of the image")
parser
parser.add_argument("--dark", help="Draw on dark background", action="store_true")
parser.add_argument(
"-o",
"--offset",
type=float,
help="How much the waves should be offset to each other",
)
parser.add_argument("-f", "--frequency", type=int, help="Frequency of the waves")
parser.add_argument(
"--stdout", help="Write output image to stdout", action="store_true"
)
args = parser.parse_args()
print(args)
if not args.offset:
args.offset = math.pi / random.choice([1, 2, 4])
if not args.frequency:
args.frequency = random.randint(10, 40)
output = create_wpotd(
args.width,
args.height,
dark=args.dark,
wave_offset=args.offset,
frequency=args.frequency,
)
if args.stdout:
import sys
output.write_to_png(sys.stdout.buffer)
else:
output.write_to_png("out/waves.png")
2 months ago
3 years ago
2 months ago
if __name__ == "__main__":
3 years ago
main()