Created
January 7, 2019 10:42
-
-
Save nst/b0ec0dddbf4f60710fed36ebfe70cef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Nicolas Seriot, 2018-11-14 | |
# According to... | |
# Sample output: ... | |
import cairo | |
import random | |
import math | |
def rainbow(pct, offset = 0.0): | |
r = math.sin(2 * math.pi * (offset + pct) + 4) * 0.5 + 0.5 | |
g = math.sin(2 * math.pi * (offset + pct) + 2) * 0.5 + 0.5 | |
b = math.sin(2 * math.pi * (offset + pct) + 0) * 0.5 + 0.5 | |
return (r, g, b) | |
WIDTH = 800 | |
HEIGHT = 600 | |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) | |
c = cairo.Context(surface) | |
c.set_source_rgb(0.0, 0.0, 0.0) | |
c.paint() | |
for col in xrange(WIDTH): | |
color = rainbow(1.0 * col / WIDTH, offset = 0.5) | |
y = random.randint(0, HEIGHT) | |
alpha = random.uniform(0.4, 0.9) | |
g = cairo.LinearGradient(0, 0, 1, y) | |
g.add_color_stop_rgba(0, 0, 0, 0, 0.0) | |
g.add_color_stop_rgba(1, color[0], color[1], color[2], 1.0) | |
c.rectangle(col, 0, 1, y) | |
c.set_source(g) | |
c.fill() | |
surface.write_to_png("rainbow_rain.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment