-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient colored breeze.py
268 lines (232 loc) · 8.02 KB
/
gradient colored breeze.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import random
import sys
from math import floor
from ctypes import c_int64
import time
import numpy as np
import numba
import pygame
GRADIENTS2 = np.array([
5, 2, 2, 5,
-5, 2, -2, 5,
5, -2, 2, -5,
-5, -2, -2, -5,
], dtype=np.int64)
GRADIENTS3 = np.array([
-11, 4, 4, -4, 11, 4, -4, 4, 11,
11, 4, 4, 4, 11, 4, 4, 4, 11,
-11, -4, 4, -4, -11, 4, -4, -4, 11,
11, -4, 4, 4, -11, 4, 4, -4, 11,
-11, 4, -4, -4, 11, -4, -4, 4, -11,
11, 4, -4, 4, 11, -4, 4, 4, -11,
-11, -4, -4, -4, -11, -4, -4, -4, -11,
11, -4, -4, 4, -11, -4, 4, -4, -11,
], dtype=np.int64)
GRADIENTS4 = np.array([
3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3,
-3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3,
3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3,
-3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3,
3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3,
-3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3,
3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3,
-3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3,
3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3,
-3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3,
3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3,
-3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3,
3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3,
-3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3,
3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3,
-3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3,
], dtype=np.int64)
STRETCH_CONSTANT2 = -0.211324865405187
SQUISH_CONSTANT2 = 0.366025403784439
STRETCH_CONSTANT3 = -1.0 / 6
SQUISH_CONSTANT3 = 1.0 / 3
STRETCH_CONSTANT4 = -0.138196601125011
SQUISH_CONSTANT4 = 0.309016994374947
NORM_CONSTANT2 = 47
NORM_CONSTANT3 = 103
NORM_CONSTANT4 = 30
@numba.njit()
def extrapolate2(perm, xsb, ysb, dx, dy):
index = perm[(perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E
g1, g2 = GRADIENTS2[index : index + 2]
return g1 * dx + g2 * dy
@numba.njit()
def generatekey(x, y, perm):
stretch_offset = (x + y) * STRETCH_CONSTANT2
xs = x + stretch_offset
ys = y + stretch_offset
xsb = floor(xs)
ysb = floor(ys)
squish_offset = (xsb + ysb) * SQUISH_CONSTANT2
xb = xsb + squish_offset
yb = ysb + squish_offset
xins = xs - xsb
yins = ys - ysb
in_sum = xins + yins
dx0 = x - xb
dy0 = y - yb
value = 0
dx1 = dx0 - 1 - SQUISH_CONSTANT2
dy1 = dy0 - 0 - SQUISH_CONSTANT2
attn1 = 2 - dx1 * dx1 - dy1 * dy1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate2(perm, xsb + 1, ysb + 0, dx1, dy1)
dx2 = dx0 - 0 - SQUISH_CONSTANT2
dy2 = dy0 - 1 - SQUISH_CONSTANT2
attn2 = 2 - dx2 * dx2 - dy2 * dy2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate2(perm, xsb + 0, ysb + 1, dx2, dy2)
if in_sum <= 1:
zins = 1 - in_sum
if zins > xins or zins > yins:
if xins > yins:
xsv_ext = xsb + 1
ysv_ext = ysb - 1
dx_ext = dx0 - 1
dy_ext = dy0 + 1
else:
xsv_ext = xsb - 1
ysv_ext = ysb + 1
dx_ext = dx0 + 1
dy_ext = dy0 - 1
else:
xsv_ext = xsb + 1
ysv_ext = ysb + 1
dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT2
dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT2
else:
zins = 2 - in_sum
if zins < xins or zins < yins:
if xins > yins:
xsv_ext = xsb + 2
ysv_ext = ysb + 0
dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT2
dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT2
else:
xsv_ext = xsb + 0
ysv_ext = ysb + 2
dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT2
dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT2
else:
dx_ext = dx0
dy_ext = dy0
xsv_ext = xsb
ysv_ext = ysb
xsb += 1
ysb += 1
dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT2
dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT2
attn0 = 2 - dx0 * dx0 - dy0 * dy0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate2(perm, xsb, ysb, dx0, dy0)
attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext
if attn_ext > 0:
attn_ext *= attn_ext
value += attn_ext * attn_ext * extrapolate2(perm, xsv_ext, ysv_ext, dx_ext, dy_ext)
return value / NORM_CONSTANT2
def overflow(x):
return c_int64(x).value
def getseed(seed):
perm = np.zeros(256, dtype=np.int64)
perm_grad_index3 = np.zeros(256, dtype=np.int64)
source = np.arange(256)
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
for i in range(255, -1, -1):
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
r = int((seed + 31) % (i + 1))
if r < 0:
r += i + 1
perm[i] = source[r]
perm_grad_index3[i] = int((perm[i] % (len(GRADIENTS3) / 3)) * 3)
source[r] = source[i]
return perm
pygame.init()
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)
c1 = [0, 0, 0]
c2 = [0, 0, 0]
rseed = getseed(random.randint(0, 9999))
gseed = getseed(random.randint(0, 9999))
bseed = getseed(random.randint(0, 9999))
clock = pygame.time.Clock()
def fill_gradient(surface, color, gradient, rect=None, vertical=True, forward=True):
"""fill a surface with a gradient pattern
Parameters:
color -> starting color
gradient -> final color
rect -> area to fill; default is surface's rect
vertical -> True=vertical; False=horizontal
forward -> True=forward; False=reverse
Pygame recipe: http://www.pygame.org/wiki/GradientCode
"""
if rect is None:
rect = surface.get_rect()
x1,x2 = rect.left, rect.right
y1,y2 = rect.top, rect.bottom
if vertical:
h = y2-y1
else:
h = x2-x1
if forward:
a, b = color, gradient
else:
b, a = color, gradient
rate = (
float(b[0]-a[0])/h,
float(b[1]-a[1])/h,
float(b[2]-a[2])/h
)
fn_line = pygame.draw.line
if vertical:
for line in range(y1,y2):
color = (
min(max(a[0]+(rate[0]*(line-y1)),0),255),
min(max(a[1]+(rate[1]*(line-y1)),0),255),
min(max(a[2]+(rate[2]*(line-y1)),0),255)
)
fn_line(surface, color, (x1,line), (x2,line))
else:
for col in range(x1,x2):
color = (
min(max(a[0]+(rate[0]*(col-x1)),0),255),
min(max(a[1]+(rate[1]*(col-x1)),0),255),
min(max(a[2]+(rate[2]*(col-x1)),0),255)
)
fn_line(surface, color, (col,y1), (col,y2))
start = time.perf_counter()
now_time = 0
d = 255/2
x = 15
while True:
#screen.fill([c1[0], c2[0], c3[0]])
c1 = [
d*(1+generatekey(now_time/100, 0, rseed)),
d*(1+generatekey(now_time/100, 0, gseed)),
d*(1+generatekey(now_time/100, 0, bseed))]
c2 = [
d*(1+generatekey((now_time+x)/100, 0, rseed)),
d*(1+generatekey((now_time+x)/100, 0, gseed)),
d*(1+generatekey((now_time+x)/100, 0, bseed))]
fill_gradient(
screen,
c2,
c1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.flip()
clock.tick(60)
now_time = time.perf_counter()-start