"""The "Néon 84" skin of WinZik: a synthwave night (a deep purple and indigo case, magenta and cyan neon tubes with
a soft glow, a sunset with a perspective grid on the bottom plate, chrome-pink lettering, dark LCDs with cyan
digits), drawn here pixel by pixel. Every picture has the name and the size of the other skins (see classique.py).
The light comes from the top left: the faces are lighter at the top, their lips darker at the bottom right.
Run: python3 tools/skins/neon84.py assets/skins/neon-84
"""
import os
import sys
from PIL import Image, ImageDraw, ImageFilter, ImageFont

OUT = sys.argv[1] if len(sys.argv) > 1 else 'assets/skins/neon-84'
os.makedirs(OUT, exist_ok=True)
FONTS = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'assets', 'fonts')

CASE_TOP = (46, 20, 84)
CASE_BOT = (18, 9, 44)
EDGE = (6, 2, 16)
MAGENTA = (255, 46, 196)
CYAN = (0, 236, 255)
CYAN_DIM = (0, 150, 190)
PINK = (255, 120, 220)
SUN_TOP = (255, 226, 110)
SUN_BOT = (255, 60, 150)
LCD = (9, 5, 24)
LCD_SHADE = (2, 0, 8)
SEG_OFF = (26, 16, 52)
FACE_TOP = (78, 40, 124)
FACE_BOT = (34, 15, 66)
FACE_HI = (150, 110, 205)
FACE_LIP = (10, 4, 24)
LIT_TOP = (200, 40, 170)
LIT_BOT = (110, 14, 110)


def mix(a, b, t):
    return tuple(int(round(a[i] * (1 - t) + b[i] * t)) for i in range(3))


def white(c, t=0.45):
    return mix(c, (255, 255, 255), t)


def font(size, bold=True):
    name = 'ui-bold.ttf' if bold else 'ui.ttf'
    try:
        return ImageFont.truetype(os.path.join(FONTS, name), size)
    except OSError:
        return ImageFont.load_default()


def new(w, h, color=(0, 0, 0, 0)):
    return Image.new('RGBA', (w, h), color)


def save(im, name):
    im.save(os.path.join(OUT, name + '.png'))


def vgrad(im, box, c0, c1):
    d = ImageDraw.Draw(im)
    x0, y0, x1, y1 = box
    for y in range(y0, y1 + 1):
        t = (y - y0) / max(1, y1 - y0)
        d.line([(x0, y), (x1, y)], fill=mix(c0, c1, t) + (255,))


def neon(im, paint, color, radius=1.4, gain=1.5, core=0.45, halo=0):
    """A neon tube: the shape blurred in its colour (the glow), then the shape itself, whiter (the hot core).
    halo > 0 also lights the pixels next to the tube (alpha 0..255), for the tubes that must read at a glance."""
    glow = new(*im.size)
    paint(ImageDraw.Draw(glow), color + (255,))
    if halo:
        near = glow.filter(ImageFilter.MaxFilter(3))
        near.putalpha(near.getchannel('A').point(lambda v: v * halo // 255))
        im.alpha_composite(near)
    glow = glow.filter(ImageFilter.GaussianBlur(radius))
    glow.putalpha(glow.getchannel('A').point(lambda v: min(255, int(v * gain))))
    im.alpha_composite(glow)
    tube = new(*im.size)
    paint(ImageDraw.Draw(tube), white(color, core) + (255,))
    im.alpha_composite(tube)


def text(d, xy, s, size=8, fill=(255, 255, 255), anchor='la', bold=True, crisp=True):
    d.fontmode = '1' if crisp else 'L'
    d.text(xy, s, font=font(size, bold), fill=fill, anchor=anchor)


PIXFONT = {
    'K': ['#.#', '##.', '#..', '##.', '#.#'], 'B': ['##.', '#.#', '##.', '#.#', '##.'],
    'P': ['##.', '#.#', '##.', '#..', '#..'], 'S': ['###', '#..', '###', '..#', '###'],
    'N': ['#..#', '##.#', '#.##', '#..#', '#..#'], 'E': ['###', '#..', '##.', '#..', '###'],
    'O': ['###', '#.#', '#.#', '#.#', '###'], '8': ['###', '#.#', '###', '#.#', '###'],
    '4': ['#.#', '#.#', '###', '..#', '..#'], '3': ['###', '..#', '.##', '..#', '###'],
    'M': ['#...#', '##.##', '#.#.#', '#...#', '#...#'], 'W': ['#...#', '#...#', '#.#.#', '##.##', '#...#'],
    'A': ['.#.', '#.#', '###', '#.#', '#.#'], 'V': ['#.#', '#.#', '#.#', '#.#', '.#.'],
    'I': ['###', '.#.', '.#.', '.#.', '###'], 'D': ['##.', '#.#', '#.#', '#.#', '##.'],
    'C': ['###', '#..', '#..', '#..', '###'], ' ': ['.', '.', '.', '.', '.'],
}


def pix(d, x, y, s, color):
    """Small capitals of 3 x 5 pixels, a pixel apart: crisp at the size of the labels of the case."""
    for ch in s:
        rows = PIXFONT[ch]
        for j, row in enumerate(rows):
            for i, v in enumerate(row):
                if v == '#':
                    d.point((x + i, y + j), fill=color)
        x += len(rows[0]) + 1


def lcd(im, x, y, w, h, frame=CYAN_DIM, fill=LCD):
    """A dark LCD window behind a thin cyan neon frame; its inside is shaded at the top left (it is sunk)."""
    neon(im, lambda d, c: d.rectangle([x - 1, y - 1, x + w, y + h], outline=c), frame, radius=1.2, gain=1.2, core=0.15)
    d = ImageDraw.Draw(im)
    d.rectangle([x, y, x + w - 1, y + h - 1], fill=fill)
    d.line([(x, y), (x + w - 1, y)], fill=LCD_SHADE)
    d.line([(x, y), (x, y + h - 1)], fill=LCD_SHADE)


def chrome(im, xy, s, size, anchor='la', glow=MAGENTA):
    """Chrome-pink lettering: a pale top, a dark horizon in the middle, a pink bottom, a dark outline, a glow."""
    mask = new(*im.size)
    md = ImageDraw.Draw(mask)
    md.fontmode = 'L'
    md.text(xy, s, font=font(size), fill=(255, 255, 255, 255), anchor=anchor)
    a = mask.getchannel('A')
    box = a.getbbox()
    if box is None:
        return
    x0, y0, x1, y1 = box
    # the glow and the outline, under the letters
    halo = Image.new('RGBA', im.size, glow + (0,))
    halo.putalpha(a.filter(ImageFilter.GaussianBlur(2.2)).point(lambda v: min(255, int(v * 1.6))))
    im.alpha_composite(halo)
    edge = Image.new('RGBA', im.size, EDGE + (0,))
    edge.putalpha(a.filter(ImageFilter.MaxFilter(3)))
    im.alpha_composite(edge)
    face = new(*im.size)
    fd = ImageDraw.Draw(face)
    mid = y0 + (y1 - y0) * 0.52
    for y in range(y0, y1):
        if y < mid:
            c = mix((255, 246, 252), (255, 150, 222), (y - y0) / max(1, mid - y0))
        else:
            c = mix((150, 22, 120), (255, 170, 236), (y - mid) / max(1, y1 - mid))
        fd.line([(x0, y), (x1, y)], fill=c + (255,))
    face.putalpha(a)
    im.alpha_composite(face)


# --- the glyphs, drawn column by column so their edges fall on whole pixels -------------------------------------
def tri(d, x0, cy, hh, direction, step, color):
    """A triangle: its base is the column x0 (2 * hh + 1 high), its tip hh * step columns away."""
    for j in range(hh * step + 1):
        h = hh - j // step
        x = x0 + j * direction
        d.line([(x, cy - h), (x, cy + h)], fill=color)


def glyph(d, kind, cx, cy, color, small=False):
    hh = 3 if small else 4
    if kind == 'play':
        tri(d, cx - hh, cy, hh, 1, 2, color)
    elif kind == 'next':
        x = cx - (4 if small else 6)
        tri(d, x, cy, hh, 1, 1, color)
        tri(d, x + hh + 1, cy, hh, 1, 1, color)
        d.rectangle([x + 2 * hh + 2, cy - hh, x + 2 * hh + 3, cy + hh], fill=color)
    elif kind == 'prev':
        x = cx + (4 if small else 6)
        tri(d, x, cy, hh, -1, 1, color)
        tri(d, x - hh - 1, cy, hh, -1, 1, color)
        d.rectangle([x - 2 * hh - 3, cy - hh, x - 2 * hh - 2, cy + hh], fill=color)
    elif kind == 'pause':
        w = 1 if small else 2
        g = 2 if small else 2
        d.rectangle([cx - g - w, cy - hh, cx - g, cy + hh], fill=color)
        d.rectangle([cx + g, cy - hh, cx + g + w, cy + hh], fill=color)
    elif kind == 'stop':
        d.rectangle([cx - hh, cy - hh, cx + hh, cy + hh], fill=color)
    elif kind == 'list':
        for i, y in enumerate((cy - 3, cy, cy + 3)):
            d.rectangle([cx - 6, y, cx - 5, y + 1], fill=color)
            d.rectangle([cx - 2, y, cx + 6, y + 1], fill=color)
    elif kind == 'shuffle':
        # two crossing paths with their arrow heads on the right
        d.line([(cx - 7, cy - 3), (cx - 4, cy - 3), (cx + 2, cy + 3), (cx + 5, cy + 3)], fill=color)
        d.line([(cx - 7, cy + 3), (cx - 4, cy + 3), (cx + 2, cy - 3), (cx + 5, cy - 3)], fill=color)
        for y in (cy - 3, cy + 3):
            d.polygon([(cx + 5, y - 3), (cx + 5, y + 3), (cx + 8, y)], fill=color)
    elif kind == 'info':
        d.rectangle([cx - 1, cy - 5, cx, cy - 4], fill=color)
        d.rectangle([cx - 1, cy - 2, cx, cy + 4], fill=color)
        d.line([(cx - 2, cy - 2), (cx - 1, cy - 2)], fill=color)
        d.line([(cx - 2, cy + 4), (cx + 1, cy + 4)], fill=color)
    elif kind == 'min':
        d.rectangle([cx - 3, cy + 2, cx + 3, cy + 3], fill=color)
    elif kind == 'close':
        for k in range(-3, 4):
            d.rectangle([cx + k, cy + k, cx + k + 1, cy + k], fill=color)
            d.rectangle([cx + k, cy - k, cx + k + 1, cy - k], fill=color)
    elif kind == 'tray':
        for j in range(4):
            d.line([(cx - 3 + j, cy - 2 + j), (cx + 3 - j, cy - 2 + j)], fill=color)
        d.line([(cx - 3, cy + 3), (cx + 3, cy + 3)], fill=color)
    elif kind == 'lyrics':
        # a quaver: a stem, a flag, a note head
        d.rectangle([cx, cy - 4, cx, cy + 2], fill=color)
        d.line([(cx + 1, cy - 4), (cx + 3, cy - 2), (cx + 3, cy - 1)], fill=color)
        d.ellipse([cx - 3, cy + 1, cx, cy + 4], fill=color)
    elif kind == 'plus':
        d.rectangle([cx - 4, cy - 1, cx + 4, cy], fill=color)
        d.rectangle([cx - 1, cy - 4, cx, cy + 3], fill=color)
    elif kind == 'minus':
        d.rectangle([cx - 4, cy - 1, cx + 4, cy], fill=color)
    elif kind == 'load':
        # an open folder
        d.line([(cx - 6, cy - 3), (cx - 3, cy - 3), (cx - 2, cy - 2), (cx + 4, cy - 2), (cx + 4, cy - 1)], fill=color)
        d.line([(cx - 6, cy - 3), (cx - 6, cy + 4)], fill=color)
        d.polygon([(cx - 6, cy + 4), (cx - 4, cy), (cx + 6, cy), (cx + 4, cy + 4)], fill=color)
    elif kind == 'save':
        # a floppy disk
        d.rectangle([cx - 5, cy - 5, cx + 5, cy + 4], outline=color)
        d.rectangle([cx - 3, cy - 5, cx + 2, cy - 2], fill=color)
        d.rectangle([cx - 3, cy + 1, cx + 3, cy + 4], outline=color)
    elif kind == 'trash':
        d.line([(cx - 5, cy - 4), (cx + 5, cy - 4)], fill=color)
        d.line([(cx - 2, cy - 5), (cx + 2, cy - 5)], fill=color)
        d.rectangle([cx - 4, cy - 3, cx + 4, cy + 4], outline=color)
        for x in (cx - 2, cx, cx + 2):
            d.line([(x, cy - 1), (x, cy + 2)], fill=color)
    elif kind == 'chevrons':
        for y in (cy - 3, cy + 1):
            d.line([(cx - 4, y), (cx, y + 3), (cx + 4, y)], fill=color)
            d.line([(cx - 4, y - 1), (cx, y + 2), (cx + 4, y - 1)], fill=color)
    elif kind == 'eject':
        for j in range(4):
            d.line([(cx - j, cy - 3 + j), (cx + j, cy - 3 + j)], fill=color)
        d.line([(cx - 3, cy + 2), (cx + 3, cy + 2)], fill=color)


def body(im, x, y, w, h, lit=False, outline=CYAN_DIM):
    """The face of a button: lighter at the top (the light comes from above), a bright top edge, a dark lip at the
    bottom right, a thin neon rim."""
    d = ImageDraw.Draw(im)
    top, bot = (LIT_TOP, LIT_BOT) if lit else (FACE_TOP, FACE_BOT)
    for yy in range(y, y + h):
        t = (yy - y) / max(1, h - 1)
        d.line([(x, yy), (x + w - 1, yy)], fill=mix(top, bot, t) + (255,))
    d.line([(x + 1, y + 1), (x + w - 2, y + 1)], fill=(white(top, 0.35) if lit else FACE_HI) + (255,))
    d.line([(x + 1, y + 1), (x + 1, y + h - 3)], fill=mix(top, FACE_HI, 0.5) + (255,))
    d.line([(x + 1, y + h - 2), (x + w - 2, y + h - 2)], fill=FACE_LIP + (255,))
    d.line([(x + w - 2, y + 2), (x + w - 2, y + h - 2)], fill=FACE_LIP + (255,))
    d.rectangle([x, y, x + w - 1, y + h - 1], outline=(white(MAGENTA, 0.2) if lit else outline) + (255,))
    for px, py in ((x, y), (x + w - 1, y), (x, y + h - 1), (x + w - 1, y + h - 1)):
        im.putpixel((px, py), (0, 0, 0, 0))


def button(w, h, kind=None, label=None, size=8, lit=False, color=CYAN, small=False, outline=CYAN_DIM, cy=None):
    im = new(w, h)
    body(im, 0, 0, w, h, lit, outline)
    cx = (w - 1) // 2
    cy = (h - 2) // 2 if cy is None else cy
    ink = (255, 236, 250) if lit else color
    if kind:
        neon(im, lambda d, c: glyph(d, kind, cx, cy, c, small), ink, radius=1.0, gain=1.0, core=0.35)
    if label:
        neon(im, lambda d, c: text(d, (w / 2, h / 2 - 0.5), label, size, c, 'mm'), ink, radius=1.0, gain=1.0, core=0.3)
    return im


# --- the player (main.bmp, 258 x 128: 99 shown, 128 opened) -----------------------------------------------------
main = new(258, 128)
vgrad(main, (0, 0, 257, 98), CASE_TOP, CASE_BOT)
d = ImageDraw.Draw(main)

# the title bar: sunset stripes on both sides of the title LCD (thinner and pinker towards the bottom)
def stripes(d, c):
    for i, y in enumerate((2, 4, 6, 8, 10, 12)):
        col = mix(SUN_TOP, MAGENTA, i / 5) + (255,)
        for x0, x1 in ((27, 44), (212, 221)):
            d.line([(x0, y), (x1, y)], fill=col)
neon(main, stripes, MAGENTA, radius=1.0, gain=0.8, core=0.0)

# the dark socket of the light between the two sliders, the marks of the expand button
d = ImageDraw.Draw(main)
d.ellipse([121, 46, 133, 58], fill=EDGE + (255,))
d.ellipse([122, 47, 132, 57], fill=(40, 12, 40, 255))

# the case: a magenta tube all round the upper part, a cyan one under it (the line between the two parts)
def frame(d, c):
    d.rectangle([1, 1, 256, 97], outline=c)
neon(main, frame, MAGENTA, radius=1.5, gain=2.0, core=0.35, halo=80)
d = ImageDraw.Draw(main)
d.rectangle([0, 0, 257, 98], outline=EDGE + (255,))

# the LCDs (title, bargraph, volume, kbps, balance, counter)
lcd(main, 47, 1, 163, 12)
lcd(main, 27, 17, 197, 12)
d = ImageDraw.Draw(main)
for i in range(23):                             # the unlit segments of the bargraph, where WinZik lights them
    for x in (120 - (i + 1) * 4, 133 + i * 4):
        d.rectangle([x, 19, x + 2, 26], fill=SEG_OFF + (255,))
lcd(main, 0, 31, 70, 13)
lcd(main, 112, 31, 32, 13)
lcd(main, 186, 31, 70, 13)
lcd(main, 0, 81, 56, 17)
d = ImageDraw.Draw(main)
for x in (15, 33):                             # the colons of the counter
    d.rectangle([x, 87, x + 1, 88], fill=CYAN_DIM + (255,))
    d.rectangle([x, 92, x + 1, 93], fill=CYAN_DIM + (255,))
pix(d, 148, 35, 'KBPS', (190, 120, 235, 255))

# the expand button (no picture: it is part of main.bmp): a small neon plate with a down chevron
body(main, 233, 77, 22, 17)
neon(main, lambda d, c: glyph(d, 'chevrons', 243, 85, c), PINK, radius=1.0, gain=1.0, core=0.3)
# a little striped sun between the transport buttons and the expand button
mini = new(258, 128)
ImageDraw.Draw(mini).ellipse([213, 79, 228, 94], fill=(255, 255, 255, 255))
md = ImageDraw.Draw(mini)
for y in (87, 89, 91):
    md.line([(200, y), (240, y)], fill=(0, 0, 0, 0))
md.rectangle([200, 92, 240, 99], fill=(0, 0, 0, 0))
minic = new(258, 128)
vgrad(minic, (0, 79, 257, 91), SUN_TOP, SUN_BOT)
minic.putalpha(mini.getchannel('A'))
mh = Image.new('RGBA', (258, 128), SUN_BOT + (0,))
mh.putalpha(mini.getchannel('A').filter(ImageFilter.GaussianBlur(2)).point(lambda v: int(v * 0.6)))
main.alpha_composite(mh)
main.alpha_composite(minic)
neon(main, lambda d, c: d.line([(211, 92), (230, 92)], fill=c), CYAN, radius=0.8, gain=0.8, core=0.3)

# the bottom part (opened with the expand button): a sunset over a perspective grid, the chrome name
HZ = 111                                        # the horizon
vgrad(main, (0, 99, 257, HZ - 1), (22, 6, 54), (120, 22, 116))
vgrad(main, (0, HZ + 1, 257, 127), (34, 4, 52), (12, 2, 28))
VX = 200                                        # the vanishing point, under the sun
sun = new(258, 128)
sd = ImageDraw.Draw(sun)
sd.ellipse([VX - 17, HZ - 10, VX + 17, HZ + 24], fill=(255, 255, 255, 255))
sunc = new(258, 128)
vgrad(sunc, (0, HZ - 10, 257, HZ), SUN_TOP, SUN_BOT)
for y, gap in ((HZ - 5, 1), (HZ - 3, 1), (HZ - 1, 1)):   # the cuts across the lower half of the sun
    sd.rectangle([0, y, 257, y + gap - 1], fill=(0, 0, 0, 0))
sd.rectangle([0, HZ, 257, 127], fill=(0, 0, 0, 0))
sunc.putalpha(sun.getchannel('A'))
halo = Image.new('RGBA', (258, 128), SUN_BOT + (0,))
halo.putalpha(sun.getchannel('A').filter(ImageFilter.GaussianBlur(3)).point(lambda v: int(v * 0.7)))
main.alpha_composite(halo)
main.alpha_composite(sunc)

# the grid, drawn 4 times bigger then reduced: its slanted lines come out smooth instead of in stairs
S4 = 4
big = new(258 * S4, 128 * S4)
bd = ImageDraw.Draw(big)
for dy in (1.5, 3.5, 6.5, 10.5):                # the cross lines get closer towards the horizon
    y = (HZ + dy) * S4
    bd.line([(0, y), (258 * S4, y)], fill=MAGENTA + (255,), width=S4)
for k in range(-30, 31):                        # the long lines run to the vanishing point
    x0, x1 = (VX + 0.5 + k * 2.2) * S4, (VX + 0.5 + k * 24) * S4
    bd.line([(x0, (HZ + 1) * S4), (x1, 127 * S4)], fill=MAGENTA + (255,), width=S4)
grid_core = big.resize((258, 128), Image.BOX)
grid_layer = new(258, 128)
g = grid_core.filter(ImageFilter.GaussianBlur(1.2))
g.putalpha(g.getchannel('A').point(lambda v: min(255, int(v * 0.9))))
grid_layer.alpha_composite(g)
grid_layer.alpha_composite(grid_core)
fade = Image.new('L', (258, 128), 0)
fd = ImageDraw.Draw(fade)
for y in range(HZ + 1, 128):                    # the grid fades into the haze near the horizon
    fd.line([(0, y), (257, y)], fill=int(255 * min(1, 0.25 + (y - HZ) / 10)))
masked = new(258, 128)
masked.paste(grid_layer, (0, 0), fade)
main.alpha_composite(masked)
neon(main, lambda d, c: d.line([(1, HZ), (256, HZ)], fill=c), CYAN, radius=1.2, gain=1.2, core=0.4)
chrome(main, (12, 113), 'WINZIK', 17, 'lm')
d = ImageDraw.Draw(main)
pix(d, 82, 104, 'NEON 84', white(CYAN, 0.3) + (255,))

def plate(d, c):
    d.line([(1, 99), (1, 126), (256, 126), (256, 99)], fill=c)
neon(main, plate, MAGENTA, radius=1.5, gain=2.0, core=0.35, halo=80)
d = ImageDraw.Draw(main)
d.line([(0, 99), (0, 127), (257, 127), (257, 99)], fill=EDGE + (255,))
# the cyan tube between the two parts (the last line of the closed player, the first of the opened bottom)
neon(main, lambda d, c: d.line([(2, 98), (255, 98)], fill=c), CYAN, radius=1.0, gain=1.0, core=0.3)
lcd(main, 0, 81, 56, 17)                         # again: the counter sits on the line
d = ImageDraw.Draw(main)
for x in (15, 33):
    d.rectangle([x, 87, x + 1, 88], fill=CYAN_DIM + (255,))
    d.rectangle([x, 92, x + 1, 93], fill=CYAN_DIM + (255,))
save(main, 'main')

# the buttons of the player
save(button(25, 17, 'shuffle'), 'hd')
save(button(25, 17, 'shuffle', lit=True), 'he')
save(button(25, 17, 'info'), 'info')
save(button(15, 15, 'min', color=PINK, outline=(150, 50, 140), cy=6), 'petit')
save(button(17, 15, 'close', color=PINK, outline=(150, 50, 140), cy=6), 'close')
save(button(17, 15, 'tray', color=PINK, outline=(150, 50, 140), cy=6), 'tray')
save(button(17, 15, 'lyrics', color=PINK, outline=(150, 50, 140), cy=6), 'lyr')
for name, kind in [('barr', 'prev'), ('bpause', 'pause'), ('bplay', 'play'), ('bstop', 'stop'), ('bav', 'next'), ('bpl', 'list')]:
    save(button(25, 18, kind), name)

# the sliders: a groove with a tube from cyan to magenta (volume), magenta at the ends of the balance
def groove(w, h, colors, glow=True):
    im = new(w, h)
    dd = ImageDraw.Draw(im)
    dd.rectangle([0, 2, w - 1, h - 3], fill=EDGE + (255,))
    dd.rectangle([1, 3, w - 2, h - 4], fill=LCD + (255,))
    dd.line([(1, 3), (w - 2, 3)], fill=LCD_SHADE + (255,))
    dd.line([(1, h - 3), (w - 2, h - 3)], fill=(90, 50, 140, 255))   # the lit lower edge of the groove
    tube = new(w, h)
    td = ImageDraw.Draw(tube)
    for x in range(2, w - 2):
        td.line([(x, 4), (x, 5)], fill=colors((x - 2) / (w - 5)) + (255,))
    if glow:
        g = tube.filter(ImageFilter.GaussianBlur(1.0))
        im.alpha_composite(g)
    im.alpha_composite(tube)
    return im

save(groove(116, 10, lambda t: mix(CYAN, MAGENTA, t)), 'fbson')
save(groove(116, 10, lambda t: mix(CYAN, MAGENTA, abs(t - .5) * 2)), 'fbbal')
pos = groove(255, 10, lambda t: mix((70, 30, 120), (120, 30, 130), t), glow=False)
pd = ImageDraw.Draw(pos)
for k in range(1, 4):                            # the quarters of the song
    x = 8 + round(k * 235 / 4)
    pd.line([(x, 3), (x, 6)], fill=CYAN_DIM + (255,))
save(pos, 'fpos')

def knob(top, bot, notch):
    im = new(17, 9)
    dd = ImageDraw.Draw(im)
    dd.rounded_rectangle([0, 0, 16, 8], 3, fill=EDGE + (255,))
    for y in range(1, 8):
        c = mix(top, bot, (y - 1) / 6) if y < 5 else mix(bot, top, (y - 5) / 5 * 0.6)
        dd.line([(2 if y in (1, 7) else 1, y), (14 if y in (1, 7) else 15, y)], fill=c + (255,))
    dd.line([(3, 1), (13, 1)], fill=(255, 255, 255, 255))
    dd.line([(8, 2), (8, 6)], fill=notch + (255,))
    return im

save(knob((255, 210, 242), (200, 40, 160), (90, 10, 70)), 'bson')
save(knob((255, 210, 242), (200, 40, 160), (90, 10, 70)), 'bbal')
save(knob((210, 255, 255), (0, 160, 210), (0, 60, 90)), 'bpos')

# --- the play list (mainpl.bmp, 258 x 362: the band 296..335 stretches, the foot 336..361 stays) ---------------
PH = 362
FOOT = PH - 26
pl = new(258, PH)
vgrad(pl, (0, 0, 257, 47), CASE_TOP, CASE_BOT)
vgrad(pl, (0, 48, 257, FOOT - 1), CASE_BOT, CASE_BOT)          # the band: no change from top to bottom
vgrad(pl, (0, FOOT, 257, PH - 1), CASE_BOT, (12, 6, 30))

def pl_stripes(d, c):
    for i, y in enumerate((3, 5, 7, 9, 11, 13)):
        col = mix(SUN_TOP, MAGENTA, i / 5) + (255,)
        for x0, x1 in ((28, 84), (174, 229)):
            d.line([(x0, y), (x1, y)], fill=col)
neon(pl, pl_stripes, MAGENTA, radius=1.0, gain=0.8, core=0.0)

def pl_frame(d, c):
    d.rectangle([1, 1, 256, PH - 2], outline=c)
neon(pl, pl_frame, MAGENTA, radius=1.5, gain=2.0, core=0.35, halo=80)
d = ImageDraw.Draw(pl)
d.rectangle([0, 0, 257, PH - 1], outline=EDGE + (255,))
# the title of the list, between two cyan tubes
neon(pl, lambda d, c: (d.line([(28, 25), (116, 25)], fill=c), d.line([(148, 25), (229, 25)], fill=c)), CYAN, radius=1.0,
     gain=1.0, core=0.3)
chrome(pl, (129, 40), 'PLAYLIST', 11, 'mm')
neon(pl, lambda d, c: (d.line([(10, 40), (84, 40)], fill=c), d.line([(174, 40), (247, 40)], fill=c)), MAGENTA,
     radius=1.0, gain=0.9, core=0.2)
# the list and the scroll track: plain dark (no grid: the band is stretched), down to the line above the status
LIST_END = FOOT + 11
lcd(pl, 3, 48, 252, LIST_END - 48 + 1)
d = ImageDraw.Draw(pl)
d.line([(249, 48), (249, LIST_END)], fill=CYAN_DIM + (255,))      # the list | the scroll track
d.rectangle([250, 48, 254, LIST_END], fill=(6, 3, 16, 255))
d.line([(250, 48), (250, LIST_END)], fill=LCD_SHADE + (255,))
d.line([(252, 48), (252, LIST_END)], fill=(60, 24, 90, 255))       # the rail of the thumb
# the status strip of the foot, and the two resize grips (ridges at the bottom corners)
d = ImageDraw.Draw(pl)
d.rectangle([40, FOOT + 14, 217, FOOT + 22], fill=(10, 4, 26, 255))
d.line([(40, FOOT + 14), (217, FOOT + 14)], fill=LCD_SHADE + (255,))
d.line([(40, FOOT + 23), (217, FOOT + 23)], fill=(70, 34, 110, 255))

def grips(d, c):
    for i in range(3):                              # three diagonal ridges in each bottom corner
        for k in range(i + 1):
            x, y = 4 + 3 * k, PH - 5 - 3 * (i - k)
            d.rectangle([x, y, x + 1, y + 1], fill=c)
            d.rectangle([253 - 3 * k - 1, y, 253 - 3 * k, y + 1], fill=c)
neon(pl, grips, CYAN, radius=0.8, gain=0.8, core=0.4)
save(pl, 'mainpl')

save(button(25, 17, 'plus'), 'plp')
save(button(25, 17, 'minus'), 'plm')
save(button(25, 17, 'load'), 'plload')
save(button(25, 17, 'save'), 'plsave')
save(button(25, 17, 'trash', color=PINK), 'plrem')
save(button(82, 16, label='MODE EDITION', size=8), 'pledit')
save(button(82, 16, label='MODE LISTE', size=8, lit=True), 'pledit2')

# the scroll bar: its track (stretched to the length of the list, so only vertical lines) and its thumb
# (the track is drawn at x 250 over the right side of the case: it is the very columns of mainpl.png there, the rail,
# the cyan frame, the magenta tube, the dark edge, so the case stays whole)
track = pl.crop((250, 300, 258, 301)).resize((8, 296), Image.NEAREST)
save(track, 'posplay')
thumb = new(8, 17)
tdd = ImageDraw.Draw(thumb)
tdd.rounded_rectangle([0, 0, 4, 16], 1, fill=EDGE + (255,))
for y in range(1, 16):
    tdd.line([(1, y), (3, y)], fill=mix((150, 255, 255), (0, 140, 190), (y - 1) / 14) + (255,))
tdd.line([(1, 1), (3, 1)], fill=(255, 255, 255, 255))
for y in (6, 8, 10):
    tdd.point((2, y), fill=(0, 70, 100, 255))
save(thumb, 'btnplay')

# the icons of the lines: the kind of file, the lyrics
for name, label, color in [('infomp3', 'MP3', CYAN), ('infowav', 'WAV', (255, 90, 210)), ('infomid', 'MID', SUN_TOP),
                           ('infocd', 'CD', (200, 180, 255))]:
    im = new(17, 9)
    dd = ImageDraw.Draw(im)
    dd.rectangle([0, 0, 16, 8], fill=mix(color, (0, 0, 0), 0.8) + (255,), outline=color + (255,))
    w = sum(len(PIXFONT[ch][0]) + 1 for ch in label) - 1
    pix(dd, (17 - w) // 2, 2, label, white(color, 0.3) + (255,))
    save(im, name)
im = new(17, 9)
neon(im, lambda d, c: d.ellipse([5, 1, 11, 7], fill=c), MAGENTA, radius=1.0, gain=1.2, core=0.3)
save(im, 'lyrok')
im = new(17, 9)
ImageDraw.Draw(im).ellipse([5, 1, 11, 7], outline=(80, 34, 90, 255))
save(im, 'lyrno')

# --- the compact bar (mainp.bmp, 112 x 12) and its buttons -----------------------------------------------------
mp = new(112, 12)
vgrad(mp, (0, 0, 111, 11), CASE_TOP, CASE_BOT)
ImageDraw.Draw(mp).line([(0, 11), (111, 11)], fill=MAGENTA + (255,))
save(mp, 'mainp')
for name, kind, color in [('arrp', 'prev', CYAN), ('pausep', 'pause', CYAN), ('playp', 'play', CYAN), ('avp', 'next', CYAN),
                          ('petitp', 'eject', PINK), ('helpp', 'info', PINK), ('lyrp', 'lyrics', PINK)]:
    save(button(17, 13, kind, color=color, small=True, cy=5), name)

# --- the icon -----------------------------------------------------------------------------------------------------
ico = new(32, 32)
dd = ImageDraw.Draw(ico)
dd.rounded_rectangle([0, 0, 31, 31], 6, fill=(22, 8, 48, 255))
mask = Image.new('L', (32, 32), 0)
ImageDraw.Draw(mask).rounded_rectangle([1, 1, 30, 30], 5, fill=255)
sky = new(32, 32)
vgrad(sky, (0, 0, 31, 18), (40, 10, 84), (170, 34, 140))
vgrad(sky, (0, 19, 31, 31), (30, 4, 46), (10, 2, 24))
ico.paste(sky, (0, 0), mask)
dd = ImageDraw.Draw(ico)
sunm = Image.new('L', (32, 32), 0)
smd = ImageDraw.Draw(sunm)
smd.ellipse([8, 5, 23, 20], fill=255)
for y in (13, 15, 17):
    smd.line([(0, y), (31, y)], fill=0)
smd.rectangle([0, 18, 31, 31], fill=0)
sunc = new(32, 32)
vgrad(sunc, (0, 5, 31, 18), SUN_TOP, SUN_BOT)
ico.paste(sunc, (0, 0), sunm)
big = new(128, 128)
bd = ImageDraw.Draw(big)
for y in (20.5, 23.5, 28.5):
    bd.line([(0, y * 4), (128, y * 4)], fill=MAGENTA + (255,), width=4)
for k in range(-6, 7):
    bd.line([((16 + k * 1.6) * 4, 19 * 4), ((16 + k * 8) * 4, 32 * 4)], fill=MAGENTA + (255,), width=4)
ico.paste(big.resize((32, 32), Image.BOX), (0, 0), Image.composite(big.resize((32, 32), Image.BOX).getchannel('A'),
                                                                  Image.new('L', (32, 32), 0), mask))
dd = ImageDraw.Draw(ico)
dd.line([(1, 18), (30, 18)], fill=white(CYAN, 0.3) + (255,))
dd.rounded_rectangle([0, 0, 31, 31], 6, outline=MAGENTA + (255,))
save(ico, 'ico')

# --- INFO.txt: the colours, as VB wrote them (&H00BBGGRR&) -------------------------------------------------------
def vb(rgb):
    r, g, b = rgb
    return '&H00%02X%02X%02X&' % (b, g, r)

DIGIT = (90, 246, 255)
colors = [PINK, CYAN, CYAN, CYAN, DIGIT, DIGIT, DIGIT,
          (0, 225, 255), (0, 225, 255), (190, 90, 255), (190, 90, 255), (255, 50, 170), (255, 50, 170), SUN_TOP]
lines = ['[INF]', 'Néon 84'] + [vb(c) for c in colors] + [vb(mix((90, 220, 255), (255, 120, 225), i / 22)) for i in range(23)]
with open(os.path.join(OUT, 'INFO.txt'), 'w', encoding='utf-8') as f:
    f.write('\n'.join(lines) + '\n')
print('skin written in', OUT)

# VIS.txt: where WinZik draws its visualizer (the spectrum of the sound), in the opened part of the player
with open(os.path.join(OUT, 'VIS.txt'), 'w') as f:
    f.write('120 103 130 20\n')
