"""Disegna l'icona di Neuronal (un neurone con le sue sinapsi e il tricolore) senza librerie esterne.
Scrive web/icona.svg, web/icona.png (256 px) e windows/neuronal.ico (256, 64, 48, 32, 16)."""
import math, os, struct, zlib
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
CYAN, WHITE = (92, 225, 230), (235, 250, 255)
GREEN, RED = (0, 146, 70), (206, 43, 55)
# geometria su una tela 256x256
CENTER = (128, 112)
ARMS = [(-150, 64), (-90, 50), (-30, 66), (30, 58), (90, 66), (150, 52)] # (angolo, lunghezza)
NODES = [(CENTER[0] + l * math.cos(math.radians(a)), CENTER[1] + l * math.sin(math.radians(a))) for a, l in ARMS]
def svg():
lines = "".join(f'' for x, y in NODES)
dots = "".join(f'' for x, y in NODES)
return f'''
'''
def seg_dist(px, py, ax, ay, bx, by):
dx, dy = bx - ax, by - ay
t = max(0.0, min(1.0, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy)))
return math.hypot(px - ax - t * dx, py - ay - t * dy)
def render(size):
ss = 3 # sovracampionamento per bordi morbidi
n = size * ss
k = 256 / n
img = bytearray()
for j in range(size):
row = []
for i in range(size):
acc = [0.0, 0.0, 0.0, 0.0]
for sj in range(ss):
for si in range(ss):
x, y = (i * ss + si + 0.5) * k, (j * ss + sj + 0.5) * k
# angoli arrotondati
cx, cy = min(max(x, 52), 204), min(max(y, 52), 204)
if math.hypot(x - cx, y - cy) > 52:
continue
d = math.hypot(x - 128, y - 108) / 180
c = [15 * (1 - d) + 5 * d, 37 * (1 - d) + 7 * d, 48 * (1 - d) + 11 * d]
def mix(col, a):
for q in range(3):
c[q] = c[q] * (1 - a) + col[q] * a
for nx, ny in NODES: # sinapsi
dd = seg_dist(x, y, CENTER[0], CENTER[1], nx, ny)
if dd < 4: mix(CYAN, 0.85)
for nx, ny in NODES: # neuroni vicini
if math.hypot(x - nx, y - ny) < 9: mix(CYAN, 1)
r = math.hypot(x - CENTER[0], y - CENTER[1]) # il neurone che si accende
if r < 46: mix(CYAN, 0.9 * (1 - r / 46) ** 1.5)
if r < 20: mix(WHITE, 1)
if 206 <= y < 220 and 74 <= x < 182: # tricolore
mix(GREEN if x < 110 else (244, 245, 240) if x < 146 else RED, 1)
acc[0] += c[0]; acc[1] += c[1]; acc[2] += c[2]; acc[3] += 255
m = ss * ss
row.append(bytes(int(v / m) for v in acc))
img += b"\0" + b"".join(row)
return img
def png(size):
raw = render(size)
def chunk(t, d):
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0)) \
+ chunk(b"IDAT", zlib.compress(bytes(raw), 9)) + chunk(b"IEND", b"")
def ico(pngs):
head = struct.pack("