-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplasma.py
executable file
·246 lines (215 loc) · 6.7 KB
/
plasma.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
#!/opt/homebrew/bin/python3
import math
import random
import colorsys
# Constants
UPDATE_PIXEL_COUNT = 12
WIDTH, HEIGHT = 16, 16
SCALE = 30
FPS = 30
UPDATE_DELAY = math.floor(1000 / FPS)
# Data structures
plasma_pixels = [[[0, 0, 0] for _ in range(WIDTH)] for _ in range(HEIGHT)]
error_values = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
top_error_positions = [i for i in range(256)]
display_pixels = [[[0, 0, 0] for _ in range(WIDTH)] for _ in range(HEIGHT)]
# ============================== #
# PURE FUNCTIONS (IMPORTABLE)
# ============================== #
from datetime import datetime
# 3x5 pixel representations of digits 0-9
DIGIT_MAP = {
'0': [
"cfc",
"f f",
"f f",
"f f",
"cfc"
],
'1': [
" f ",
"ff ",
" f ",
" f ",
"fff"
],
'2': [
"cfc",
" f",
"cfc",
"f ",
"fff"
],
'3': [
"ffc",
" f",
" ff",
" f",
"ffc"
],
'4': [
"c f",
"f f",
"fff",
" f",
" f"
],
'5': [
"fff",
"f ",
"ffc",
" f",
"ffc"
],
'6': [
"cff",
"f ",
"ffc",
"f f",
"cfc"
],
'7': [
"fff",
" f",
" f ",
" f ",
" f "
],
'8': [
"cfc",
"f f",
"cfc",
"f f",
"cfc"
],
'9': [
"cfc",
"f f",
"cff",
" f",
"ffc"
]
}
def clamp(value, min_val=0, max_val=255):
return max(min(value, max_val), min_val)
def draw_alpha_pixel(x, y, color, alpha):
plasma_pixels[y][x] = [
clamp(int((1 - alpha) * plasma_pixels[y][x][j] + alpha * color[j]))
for j in range(3)
]
def update_clock():
now = datetime.now()
time_string = now.strftime("%H%M")
start_x, start_y = 0, 2 # Position of the clock on the grid
#color = [0xff, 0xff, 0xff]
color = [0, 0, 0]
alpha = 1
for i, digit in enumerate(time_string):
x_offset = start_x + (i * 4) + (0 if i>1 else 1) # Space between digits
y_offset = start_y + (6 if i>1 else 0) # Space between digits
digit_pattern = DIGIT_MAP[digit]
for dy, row in enumerate(digit_pattern):
for dx, pixel in enumerate(row):
if pixel != " " :
pixel_value = int(pixel, 16)
#plasma_pixels[y_offset + dy][x_offset + dx] = [0, 0, 0]
draw_alpha_pixel(x_offset + dx, y_offset + dy, color, alpha*pixel_value/15.0)
progress_x = start_x + int((WIDTH-1)*(now.second / 59.0))
progress_y = HEIGHT - 1
draw_alpha_pixel(progress_x, progress_y, color, alpha)
error_values[progress_y][progress_x] += 100
def hsv_to_hex(h, s, v):
r, g, b = colorsys.hsv_to_rgb(h, s, v)
return '#%02x%02x%02x' % (int(r * 255), int(g * 255), int(b * 255))
def rgb_to_hex(rgb):
#return '#%02x%02x%02x' % (int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255))
return '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])
def update_plasma(t):
for y in range(HEIGHT):
for x in range(WIDTH):
color_value = (
math.sin(0.04*(-x + t)) +
math.sin(0.05*(y - t)) +
math.sin(0.06*(-(x + 0.5*y) + t)) +
math.sin(0.07*((0.5*x + y) - t))
)
normalized = (color_value + 4) / 8
hue = (normalized + t * 0.05) % 1.0
#r, g, b = colorsys.hsv_to_rgb(hue, 1, 1)
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1, 1)]
pixel = plasma_pixels[y][x]
pixel[0] = r
pixel[1] = g
pixel[2] = b
def partial_sort_error_positions():
#for i in range(len(top_error_positions) - 2, -1, -1):
for i in range(len(top_error_positions) - 1):
idx_a = top_error_positions[i]
idx_b = top_error_positions[i + 1]
y_a, x_a = divmod(idx_a, WIDTH)
y_b, x_b = divmod(idx_b, WIDTH)
if error_values[y_a][x_a] < error_values[y_b][x_b]:
top_error_positions[i], top_error_positions[i + 1] = top_error_positions[i + 1], top_error_positions[i]
def update_error():
for y in range(HEIGHT):
for x in range(WIDTH):
plasma_pixel = plasma_pixels[y][x]
display_pixel = display_pixels[y][x]
error_values[y][x] += sum(abs(plasma_pixel[i] - display_pixel[i]) for i in range(3)) + random.randint(0,10)
partial_sort_error_positions()
partial_sort_error_positions()
def update_display():
for i in range(UPDATE_PIXEL_COUNT):
p = top_error_positions[i]
y, x = divmod(p, WIDTH)
display_pixels[y][x][:] = plasma_pixels[y][x]
error_values[y][x] = 0
top_error_positions[:] = top_error_positions[UPDATE_PIXEL_COUNT:] + top_error_positions[:UPDATE_PIXEL_COUNT]
def max_error_value():
return max(max(row) for row in error_values) + 1
# ===================================== #
# TKINTER-DEPENDENT (NOT IMPORTABLE)
# ===================================== #
def main():
import tkinter as tk
root = tk.Tk()
root.title("Plasmaeffekt med Tkinter")
canvas = tk.Canvas(root, width=WIDTH * SCALE * 3, height=HEIGHT * SCALE, bg='black')
canvas.pack()
canvas_pixels = [[None for _ in range(WIDTH * 3)] for _ in range(HEIGHT)]
for y in range(HEIGHT):
for x in range(WIDTH * 3):
rect = canvas.create_rectangle(
x * SCALE, y * SCALE, (x + 1) * SCALE - 1, (y + 1) * SCALE - 1,
outline='', fill='#000000'
)
canvas_pixels[y][x] = rect
def show_plasma():
for y in range(HEIGHT):
for x in range(WIDTH):
pixel = plasma_pixels[y][x]
canvas.itemconfig(canvas_pixels[y][x], fill=rgb_to_hex(pixel))
def show_display():
for y in range(HEIGHT):
for x in range(WIDTH):
pixel = display_pixels[y][x]
canvas.itemconfig(canvas_pixels[y][x + 2 * WIDTH], fill=rgb_to_hex(pixel))
def show_error():
max_error = max_error_value()
for y in range(HEIGHT):
for x in range(WIDTH):
error = error_values[y][x] / max_error
canvas.itemconfig(canvas_pixels[y][x + WIDTH], fill=hsv_to_hex(1, error, 1))
def update(t):
update_plasma(t)
update_clock()
update_error()
update_display()
show_plasma()
show_error()
show_display()
root.after(UPDATE_DELAY, lambda: update(t + 0.01))
update(random.randrange(0, 1000000))
root.mainloop()
if __name__ == "__main__":
main()