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.

88 lines
2.3 KiB
Python

3 years ago
#!/usr/bin/env python3
import cairo
import math
from utils import random_color
# dimensions of the output image
WIDTH, HEIGHT = 1920, 1080
# numbers of waves
WAVES = 12
# how much should the phases be offset?
WAVE_OFFSET = 0 - (math.pi/4)#-1.9
# amplitude of the sine wave
AMPLITUDE = 50
# 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
# background black? White otherwise:
DARK_BG = True
3 years ago
# precision of the calculation
PRECISION = 10000
3 years ago
def todays_color():
import datetime
import colorsys
import calendar
# a day between 1 and 365 (inclusive)
today = datetime.datetime.now().timetuple().tm_yday
year = datetime.datetime.now().year
days_in_year = 365 + calendar.isleap(year)
# between 0 and 1, how far through the year are we?
progress = today/days_in_year
return colorsys.hsv_to_rgb(progress, 1, 1)
3 years ago
def main():
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas
wave_height = 1/WAVES
3 years ago
step_size = 1/PRECISION
lastpoints = [(x/PRECISION, 0) for x in range(PRECISION+1)]
if DARK_BG:
# make bg black
ctx.rectangle(0, 0, 1, 1)
ctx.set_source_rgb(0, 0, 0)
ctx.fill()
3 years ago
if MONOCHROME:
3 years ago
r, g, b = random_color()
alpha_step = 1/WAVES
for num in range(WAVES+1):
if not MONOCHROME:
r, g, b = random_color()
3 years ago
points = []
x = 0
while x < 1:
y = math.sin(x*AMPLITUDE + (num * WAVE_OFFSET) ) * 0.1
3 years ago
points.append((x, ( (y/4) + ((0.5+num)*wave_height))))
3 years ago
x += step_size
print(f'Draw {len(points)} points for curve {num}')
if not MONOCHROME:
ctx.set_source_rgb(r, g, b)
else:
ctx.set_source_rgba(r, g, b, 1 - (alpha_step * num)) # make more transparent toward bottom
3 years ago
# draw waves
3 years ago
ctx.move_to(*points[0])
for p in points[1:]:
ctx.line_to(*p)
for p in reversed(lastpoints):
ctx.line_to(*p)
ctx.line_to(*points[0])
ctx.fill()
3 years ago
lastpoints = points
3 years ago
surface.write_to_png("out/waves.png") # Output to PNG
if __name__ == '__main__':
main()