-
Notifications
You must be signed in to change notification settings - Fork 2
/
stars-monochrome
executable file
·85 lines (77 loc) · 2.32 KB
/
stars-monochrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
#+
# A random scattering of stars against a black background.
#
# Copyright 2015 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#-
import sys
import os
import math
import array
import qahirah as qah
from qahirah import \
CAIRO, \
Colour, \
Matrix, \
Rect, \
Vector
class RubbishRandom :
"a very crude random-number generator. The purpose of using this is to" \
" ensure all runs of this sample program will produce exactly the same output."
def __init__(self, seed) :
self.seed = seed
#end __init__
def random(self) :
"returns a random float in the range [0 .. 1)."
# algorithm is that of the good old BSD linear-congruential pseudo-random number generator.
self.seed = (1103515245 * self.seed + 12345) % (1 << 31)
return \
self.seed / (1 << 31)
#end random
def randrange(self, upper) :
"returns a random integer in range(upper)."
return \
math.trunc(self.random() * upper)
#end randrange
def choice(self, seq) :
"returns a random item from a sequence."
return \
seq[self.randrange(len(seq))]
#end choice
#end RubbishRandom
random = RubbishRandom(999) # want same sequence every time
pat_size = 1024
patbytes = array.array("B", (0,) * (pat_size * pat_size))
for i in range(1000) :
pos = Vector(random.randrange(pat_size), random.randrange(pat_size))
brightness = random.random() ** 3 # dimmer ones are more common
patbytes[pos.x + pos.y * pat_size] = round(brightness * 255)
#end for
pix = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_RGB24,
dimensions = (pat_size, pat_size)
)
(qah.Context.create(pix)
.set_source_colour(Colour.grey(0))
.set_operator(CAIRO.OPERATOR_SOURCE)
.paint()
.set_source_colour(Colour.grey(1))
.mask
(
qah.Pattern.create_for_surface
(
qah.ImageSurface.create_for_array
(
arr = patbytes,
format = CAIRO.FORMAT_A8,
dimensions = (pat_size, pat_size),
stride = pat_size
)
)
)
)
pix.flush().write_to_png("%s.png" % os.path.basename(sys.argv[0]))