#!/usr/bin/env python
import png, math, os, functools, operator
def draw_pixel(pixelsss, x, y, rrggbb):
pixelsss[y][x][0] = int(rrggbb[-6:-4], 16)
pixelsss[y][x][1] = int(rrggbb[-4:-2], 16)
pixelsss[y][x][2] = int(rrggbb[-2: ], 16)
def write_png(path, pixelsss, width, height=None, alpha=False):
print('> %s' % (path))
f_out = png.Writer(width=width, height=width if height is None else height, alpha=alpha)
return f_out.write_array(open(path, 'wb'),
functools.reduce(operator.iconcat,
functools.reduce(operator.iconcat, pixelsss, []), []))
def compute_colour(fraction, deg):
return colours[int((deg + 720 - 360 * fraction + 0.5) / 60) % 6]
n_frame = 60
w_frame = 400
h_frame = 200
radius = 99
w_frame2 = w_frame // 2
diameter = radius * 2
colours = ['ff0000','cc9900','00cc00','3399ff','6666ff','cc33ff']
basename = os.path.splitext(os.path.basename(__file__))[0]
cumulsss = [[[255 for c in range(3)] for x in range(diameter)]
for y in range(diameter)]
for i_frame in range(n_frame + 1): ## 0 = prepare cumulative image
framesss = [[[255 for c in range(3)] for x in range(w_frame)]
for y in range(h_frame)]
for y in range(-radius, radius):
out_y = y + radius
out_y_end = (diameter - 1) * i_frame // n_frame
for x in range(-radius, radius):
if x * x + y * y < radius * radius:
out_x = x + radius
deg = math.degrees(math.atan2(y, x))
colour_cumul = compute_colour((y + radius + 1.0) / (diameter - 1), deg)
colour_disc = compute_colour(float(i_frame) / n_frame , deg)
if i_frame > 0: draw_pixel(framesss, out_x + 1, out_y, colour_disc )
else: draw_pixel(cumulsss, out_x , out_y, colour_cumul)
if out_y <= out_y_end: framesss[out_y][w_frame2:w_frame2 + diameter] = cumulsss[out_y]
for dy in [-1, 0]:
framesss[out_y_end + dy][1] = framesss[out_y_end + dy][w_frame - 2] = [0,0,0]
for dy in [-2, 1]:
framesss[out_y_end + dy][1:w_frame - 1] = [[0,0,0] for i in range(w_frame - 2)]
if i_frame > 0:
write_png(r'F:\%s\%02d.png' % (basename, i_frame), framesss, w_frame, h_frame)
else:
write_png(r'F:\%s\00.png' % (basename), cumulsss, diameter, diameter)