"""The "Phosphore" skin of WinZik: an old green-phosphor computer terminal. A beige plastic bezel around a near-black
glass, everything on the glass drawn in glowing green (bright pixels, a dimmer 1px halo for the bloom, faint scanlines
every other row), the buttons as bracketed terminal labels ([ > ] [ || ] [ # ]) in a hand-made 5x7 pixel font, and a
blinking-cursor underscore here and there. Every picture is drawn here pixel by pixel (no font file, no copied picture,
no logo) and keeps the size and the place of the pictures of the other skins.
Run: python3 -I tools/skins/phosphore.py assets/skins/phosphore
"""
import os
import sys
from PIL import Image, ImageDraw, ImageFilter

OUT = sys.argv[1] if len(sys.argv) > 1 else 'assets/skins/phosphore'
os.makedirs(OUT, exist_ok=True)

# the glass and the phosphor
GLASS = (6, 12, 8)
SCAN = (2, 6, 3)                 # the darker row of the scanlines (odd rows)
PLAIN = (4, 10, 6)               # the glass where it may be stretched (no scanline)
GHOST = (12, 34, 17)             # the unlit segments
DIM = (24, 84, 40)               # the frames
HALO = (20, 78, 36)              # the 1px bloom around the bright pixels
HALO2 = (10, 36, 18)             # the faint outer bloom
MID = (52, 176, 82)
HI = (120, 255, 150)
WHITE = (214, 255, 222)
# the plastic of the case
BEIGE = (190, 182, 156)
BEIGE_HI = (226, 220, 198)
BEIGE_LO = (146, 138, 114)
BEIGE_DK = (88, 82, 66)
CASE = (30, 29, 26)

# --- a 5x7 pixel font (drawn here, one string of 5 characters per row) -------------------------------------------
F5 = {
    'A': ['.###.', '#...#', '#...#', '#####', '#...#', '#...#', '#...#'],
    'B': ['####.', '#...#', '#...#', '####.', '#...#', '#...#', '####.'],
    'C': ['.###.', '#...#', '#....', '#....', '#....', '#...#', '.###.'],
    'D': ['####.', '#...#', '#...#', '#...#', '#...#', '#...#', '####.'],
    'E': ['#####', '#....', '#....', '####.', '#....', '#....', '#####'],
    'F': ['#####', '#....', '#....', '####.', '#....', '#....', '#....'],
    'G': ['.###.', '#...#', '#....', '#.###', '#...#', '#...#', '.####'],
    'H': ['#...#', '#...#', '#...#', '#####', '#...#', '#...#', '#...#'],
    'I': ['.###.', '..#..', '..#..', '..#..', '..#..', '..#..', '.###.'],
    'K': ['#...#', '#..#.', '#.#..', '##...', '#.#..', '#..#.', '#...#'],
    'L': ['#....', '#....', '#....', '#....', '#....', '#....', '#####'],
    'M': ['#...#', '##.##', '#.#.#', '#.#.#', '#...#', '#...#', '#...#'],
    'N': ['#...#', '#...#', '##..#', '#.#.#', '#..##', '#...#', '#...#'],
    'O': ['.###.', '#...#', '#...#', '#...#', '#...#', '#...#', '.###.'],
    'P': ['####.', '#...#', '#...#', '####.', '#....', '#....', '#....'],
    'R': ['####.', '#...#', '#...#', '####.', '#.#..', '#..#.', '#...#'],
    'S': ['.####', '#....', '#....', '.###.', '....#', '....#', '####.'],
    'T': ['#####', '..#..', '..#..', '..#..', '..#..', '..#..', '..#..'],
    'U': ['#...#', '#...#', '#...#', '#...#', '#...#', '#...#', '.###.'],
    'V': ['#...#', '#...#', '#...#', '#...#', '#...#', '.#.#.', '..#..'],
    'W': ['#...#', '#...#', '#...#', '#.#.#', '#.#.#', '#.#.#', '.#.#.'],
    'X': ['#...#', '#...#', '.#.#.', '..#..', '.#.#.', '#...#', '#...#'],
    'Y': ['#...#', '#...#', '.#.#.', '..#..', '..#..', '..#..', '..#..'],
    'Z': ['#####', '....#', '...#.', '..#..', '.#...', '#....', '#####'],
    '0': ['.###.', '#...#', '#..##', '#.#.#', '##..#', '#...#', '.###.'],
    '1': ['..#..', '.##..', '..#..', '..#..', '..#..', '..#..', '.###.'],
    '2': ['.###.', '#...#', '....#', '...#.', '..#..', '.#...', '#####'],
    '8': ['.###.', '#...#', '#...#', '.###.', '#...#', '#...#', '.###.'],
    '?': ['.###.', '#...#', '....#', '...#.', '..#..', '.....', '..#..'],
    '_': ['.....', '.....', '.....', '.....', '.....', '.....', '#####'],
    '-': ['.....', '.....', '.....', '#####', '.....', '.....', '.....'],
    '+': ['.....', '..#..', '..#..', '#####', '..#..', '..#..', '.....'],
    '>': ['#....', '.#...', '..#..', '...#.', '..#..', '.#...', '#....'],
    ':': ['.....', '..#..', '..#..', '.....', '..#..', '..#..', '.....'],
    '.': ['.....', '.....', '.....', '.....', '.....', '..#..', '..#..'],
    ' ': ['.....'] * 7,
}

# --- a 3x5 pixel font for the small labels ------------------------------------------------------------------------
F3 = {
    'A': ['.#.', '#.#', '###', '#.#', '#.#'], 'B': ['##.', '#.#', '##.', '#.#', '##.'],
    'C': ['.##', '#..', '#..', '#..', '.##'], 'D': ['##.', '#.#', '#.#', '#.#', '##.'],
    'E': ['###', '#..', '##.', '#..', '###'], 'F': ['###', '#..', '##.', '#..', '#..'],
    'G': ['.##', '#..', '#.#', '#.#', '.##'], 'H': ['#.#', '#.#', '###', '#.#', '#.#'],
    'I': ['###', '.#.', '.#.', '.#.', '###'], 'J': ['..#', '..#', '..#', '#.#', '.#.'], 'K': ['#.#', '#.#', '##.', '#.#', '#.#'],
    'L': ['#..', '#..', '#..', '#..', '###'], 'M': ['#.#', '###', '###', '#.#', '#.#'],
    'N': ['##.', '#.#', '#.#', '#.#', '#.#'], 'O': ['.#.', '#.#', '#.#', '#.#', '.#.'],
    'P': ['##.', '#.#', '##.', '#..', '#..'], 'R': ['##.', '#.#', '##.', '#.#', '#.#'],
    'S': ['.##', '#..', '.#.', '..#', '##.'], 'T': ['###', '.#.', '.#.', '.#.', '.#.'],
    'U': ['#.#', '#.#', '#.#', '#.#', '###'], 'V': ['#.#', '#.#', '#.#', '#.#', '.#.'],
    'W': ['#.#', '#.#', '###', '###', '#.#'], 'X': ['#.#', '#.#', '.#.', '#.#', '#.#'],
    'Y': ['#.#', '#.#', '.#.', '.#.', '.#.'], 'Z': ['###', '..#', '.#.', '#..', '###'],
    '0': ['###', '#.#', '#.#', '#.#', '###'], '1': ['.#.', '##.', '.#.', '.#.', '###'],
    '2': ['##.', '..#', '.#.', '#..', '###'], '3': ['##.', '..#', '.#.', '..#', '##.'],
    '4': ['#.#', '#.#', '###', '..#', '..#'], '5': ['###', '#..', '##.', '..#', '##.'],
    '6': ['.##', '#..', '###', '#.#', '###'], '7': ['###', '..#', '.#.', '.#.', '.#.'],
    '8': ['###', '#.#', '###', '#.#', '###'], '9': ['###', '#.#', '###', '..#', '##.'],
    '>': ['#..', '.#.', '..#', '.#.', '#..'], '_': ['...', '...', '...', '...', '###'],
    '-': ['...', '...', '###', '...', '...'], '.': ['...', '...', '...', '...', '.#.'],
    ':': ['...', '.#.', '...', '.#.', '...'], '/': ['..#', '..#', '.#.', '#..', '#..'],
    ' ': ['...'] * 5,
}


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 glass(im, box=None, scan=True, color=GLASS):
    """The glass of the screen: near black, a darker line every other row (the scanlines)."""
    d = ImageDraw.Draw(im)
    x0, y0, x1, y1 = box or (0, 0, im.width, im.height)
    for y in range(y0, y1):
        d.line([(x0, y), (x1 - 1, y)], fill=SCAN if scan and y % 2 else color)


def text_width(s, font):
    cw = len(next(iter(font.values()))[0])
    return len(s) * (cw + 1) - 1


def pixels(d, x, y, s, font, color):
    """Draws a string of the pixel font, its top left corner at x, y."""
    cw = len(next(iter(font.values()))[0])
    for ch in s:
        rows = font.get(ch.upper(), font[' '])
        for j, row in enumerate(rows):
            for i, c in enumerate(row):
                if c == '#':
                    d.point((x + i, y + j), fill=color)
        x += cw + 1


def glow(im, draw, color=HI, halo=HALO, outer=HALO2, at=(0, 0)):
    """Phosphor: draws something bright on a layer, then lays a dim 1px halo (and a fainter one) under it."""
    layer = new(im.width, im.height)
    draw(ImageDraw.Draw(layer), color)
    mask = layer.getchannel('A')
    if outer:
        far = mask.filter(ImageFilter.MaxFilter(5))
        im.alpha_composite(Image.composite(new(im.width, im.height, outer + (150,)), new(im.width, im.height), far))
    if halo:
        near = mask.filter(ImageFilter.MaxFilter(3))
        im.alpha_composite(Image.composite(new(im.width, im.height, halo + (255,)), new(im.width, im.height), near))
    im.alpha_composite(layer)


def frame(d, x0, y0, x1, y1, color=DIM):
    d.rectangle([x0, y0, x1, y1], outline=color)


def box(im, x0, y0, x1, y1, scan=True, color=DIM):
    """A screen area: a thin phosphor frame (x0..x1, y0..y1 is the frame), the inside cleared."""
    glass(im, (x0 + 1, y0 + 1, x1, y1), scan)
    frame(ImageDraw.Draw(im), x0, y0, x1, y1, color)


def bracket(d, x, y, h, color, right=False):
    """A terminal bracket [ or ]: a stem and two serifs."""
    s = -1 if right else 1
    d.line([(x, y), (x, y + h - 1)], fill=color)
    d.line([(x, y), (x + 2 * s, y)], fill=color)
    d.line([(x, y + h - 1), (x + 2 * s, y + h - 1)], fill=color)


def symbol(d, kind, cx, cy, color, small=False):
    """The transport symbols, pixel by pixel, centred on cx, cy."""
    k = 1 if small else 0
    if kind == 'play':
        for i in range(4 - k):
            d.line([(cx - 2 + i, cy - 3 + k + i), (cx - 2 + i, cy + 3 - k - i)], fill=color)
    elif kind == 'pause':
        d.rectangle([cx - 3 + k, cy - 3 + k, cx - 2 + k, cy + 3 - k], fill=color)
        d.rectangle([cx + 1, cy - 3 + k, cx + 2, cy + 3 - k], fill=color)
    elif kind == 'stop':
        d.rectangle([cx - 3 + k, cy - 3 + k, cx + 3 - k, cy + 3 - k], fill=color)
    elif kind in ('prev', 'next'):
        s = 1 if kind == 'next' else -1
        n = 3 if small else 4
        bar = cx + s * (n + 1 if small else 5)
        for t in (0, 1):
            base = cx - s * (n if t == 0 else 0) - (0 if small else s)
            for i in range(n):
                x = base + s * i
                d.line([(x, cy - (n - 1) + i), (x, cy + (n - 1) - i)], fill=color)
        d.line([(bar, cy - (n - 1)), (bar, cy + (n - 1))], fill=color)
    elif kind == 'down':
        for i in range(4):
            d.line([(cx - 3 + i, cy - 2 + i), (cx + 3 - i, cy - 2 + i)], fill=color)
    elif kind == 'up':
        for i in range(4):
            d.line([(cx - 3 + i, cy + 1 - i), (cx + 3 - i, cy + 1 - i)], fill=color)
    elif kind == 'bin':
        d.line([(cx - 4, cy - 3), (cx + 4, cy - 3)], fill=color)
        d.line([(cx - 1, cy - 4), (cx + 1, cy - 4)], fill=color)
        d.rectangle([cx - 3, cy - 2, cx + 3, cy + 4], outline=color)
        d.line([(cx - 1, cy - 1), (cx - 1, cy + 3)], fill=color)
        d.line([(cx + 1, cy - 1), (cx + 1, cy + 3)], fill=color)
    elif kind == 'tray':
        d.line([(cx - 3, cy + 3), (cx + 3, cy + 3)], fill=color)
        for i in range(3):
            d.line([(cx - 2 + i, cy - 2 + i), (cx + 2 - i, cy - 2 + i)], fill=color)
    elif kind == 'note':
        d.line([(cx + 1, cy - 3), (cx + 1, cy + 2)], fill=color)
        d.line([(cx + 2, cy - 3), (cx + 3, cy - 2)], fill=color)
        d.rectangle([cx - 2, cy + 1, cx, cy + 3], fill=color)


def key(w, h, label=None, kind=None, inverse=False, small=False, font=F5, top=None, height=None):
    """A terminal button: [ label ] in phosphor on the glass; inverse = reverse video (the option is on)."""
    im = new(w, h)
    glass(im)
    y = top if top is not None else (2 if h >= 15 else 1)
    bh = height or (h - 2 * y)
    inset = 3 if w >= 25 else 2
    if inverse:
        ImageDraw.Draw(im).rectangle([inset + 2, y + 2, w - inset - 3, y + bh - 3], fill=MID)

    def draw(d, color):
        bracket(d, inset, y, bh, color)
        bracket(d, w - 1 - inset, y, bh, color, right=True)

    glow(im, draw, MID, HALO, None)
    cy = y + bh // 2
    ink = GLASS if inverse else HI

    def content(d, color):
        if kind:
            symbol(d, kind, w // 2, cy, color, small)
        if label:
            tw = text_width(label, font)
            fh = len(next(iter(font.values())))
            pixels(d, (w - tw) // 2, cy - fh // 2, label, font, color)

    if inverse:
        content(ImageDraw.Draw(im), ink)
    else:
        glow(im, content, ink)
    return im


# --- the player (main.bmp, 258 x 128: 99 shown, 128 opened) -------------------------------------------------------
main = new(258, 128, GLASS + (255,))
glass(main, (0, 0, 258, 99))
d = ImageDraw.Draw(main)

box(main, 46, 0, 210, 13)                         # the scrolling title (Text4: 48, 1)
box(main, 26, 16, 224, 29, scan=False)            # the bargraph
for x in list(range(28, 120, 4)) + list(range(133, 224, 4)):
    d.rectangle([x, 19, x + 2, 26], fill=GHOST)   # the unlit segments
for y in range(18, 28, 2):
    d.point((126, y), fill=DIM)
    d.point((127, y + 1), fill=DIM)
box(main, -1, 30, 70, 44)                         # "Volume : xx %"
box(main, 111, 30, 144, 44)                       # kbps
box(main, 185, 30, 258, 44)                       # "Balance : x %"
box(main, -1, 80, 56, 98)                         # the counter (0, 81)
for x in (15, 33):                                # the colons of hh:mm:ss
    d.rectangle([x, 85, x, 86], fill=MID)
    d.rectangle([x, 91, x, 92], fill=MID)
# the ring of the light between the two sliders (the light itself is drawn by WinZik at 123..131, 48..56)
d.ellipse([120, 45, 134, 59], outline=DIM)

# the small texts of the glass
def labels(dd, color):
    pixels(dd, 148, 35, 'KBPS', F3, color)
    pixels(dd, 28, 5, 'A>', F3, color)
glow(main, labels, MID, HALO2, None)

# a little trace of the oscilloscope between Volume and kbps
def wave(dd, color):
    import math
    pts = [(x, 37 + round(4 * math.sin((x - 74) / 34 * 2 * math.pi) * (0.4 + 0.6 * math.sin((x - 74) / 34 * math.pi)))) for x in range(74, 109)]
    dd.line(pts, fill=color)
glow(main, wave, MID, HALO2, None)

# the cursor, blinking (it is lit in the picture): right of the title and right of the buttons
def cursors(dd, color):
    dd.rectangle([214, 90, 222, 91], fill=color)
glow(main, cursors, HI)
# a prompt in front of the second cursor
glow(main, lambda dd, c: pixels(dd, 213, 81, '>', F3, c), MID, HALO2, None)

# the button that opens the bottom (Image3, 232, 76: no picture, it is part of main.bmp)
main.alpha_composite(key(25, 18, kind='down'), (232, 76))

# the bottom part: the beige plastic chin of the terminal
d.rectangle([0, 99, 257, 127], fill=BEIGE)
for y in range(101, 127):
    t = (y - 101) / 26
    c = tuple(int(BEIGE[i] * (1 - t * .18)) for i in range(3))
    d.line([(1, y), (256, y)], fill=c)
d.line([(0, 99), (257, 99)], fill=BEIGE_DK)
d.line([(0, 100), (257, 100)], fill=BEIGE_HI)
d.line([(0, 127), (257, 127)], fill=BEIGE_DK)
# the vents
for x in range(8, 66, 5):
    d.rectangle([x, 106, x + 1, 120], fill=BEIGE_DK)
    d.line([(x, 121), (x + 1, 121)], fill=BEIGE_HI)
    d.line([(x + 2, 106), (x + 2, 120)], fill=BEIGE_HI)
# the embossed name plate
name_x = 118 - text_width('PHOSPHORE', F5) // 2
pixels(d, name_x + 1, 106, 'PHOSPHORE', F5, BEIGE_HI)
pixels(d, name_x, 105, 'PHOSPHORE', F5, BEIGE_DK)
sub = 'TERMINAL 80 COLONNES'
pixels(d, 118 - text_width(sub, F3) // 2, 117, sub, F3, BEIGE_DK)
# the two knobs (luminosite, contraste)
for cx, notch in ((185, (-2, -3)), (205, (3, -1))):
    d.ellipse([cx - 6, 107, cx + 6, 119], fill=BEIGE_DK)
    d.ellipse([cx - 5, 107, cx + 5, 117], fill=BEIGE_HI)
    d.ellipse([cx - 4, 108, cx + 4, 117], fill=BEIGE)
    d.line([(cx, 112), (cx + notch[0], 112 + notch[1])], fill=BEIGE_DK)
pixels(d, 180, 121, 'LUM', F3, BEIGE_DK)
pixels(d, 200, 121, 'CTR', F3, BEIGE_DK)
# the power light, lit green
d.rectangle([231, 109, 242, 117], fill=BEIGE_DK)
d.line([(231, 118), (242, 118)], fill=BEIGE_HI)
d.rectangle([232, 110, 241, 116], fill=CASE)
glow(main, lambda dd, c: dd.rectangle([234, 112, 239, 114], fill=c), HI, MID, HALO)
pixels(d, 231, 121, 'PWR', F3, BEIGE_DK)
# the plastic rim around the glass
for x in (0, 257):
    d.line([(x, 0), (x, 99)], fill=BEIGE_LO)
d.line([(0, 0), (257, 0)], fill=BEIGE_LO)
save(main, 'main')

# the buttons of the player
save(key(25, 17, 'H'), 'hd')
save(key(25, 17, 'H', inverse=True), 'he')
save(key(25, 17, '?'), 'info')
save(key(15, 15, '_'), 'petit')
save(key(17, 15, 'X'), 'close')
save(key(17, 15, kind='tray'), 'tray')
save(key(17, 15, 'L'), 'lyr')
for name, kind in [('barr', 'prev'), ('bpause', 'pause'), ('bplay', 'play'), ('bstop', 'stop'), ('bav', 'next')]:
    save(key(25, 18, kind=kind), name)
save(key(25, 18, 'PL', font=F3), 'bpl')


# the sliders: their grooves (opaque, on the glass) and their knobs (a reverse-video block with its bloom)
def groove(w, ticks):
    im = new(w, 10)
    glass(im)
    dd = ImageDraw.Draw(im)
    ticks(dd)
    return im


def vol_ticks(dd):
    for x in range(4, 112, 3):
        hgt = 1 + (x - 4) * 5 // 108
        dd.line([(x, 8 - hgt), (x, 8)], fill=DIM if x < 90 else MID)
    dd.line([(2, 9), (113, 9)], fill=GHOST)


def bal_ticks(dd):
    for x in range(4, 112, 3):
        hgt = 1 + abs(x - 57) * 4 // 54
        dd.line([(x, 8 - hgt), (x, 8)], fill=DIM)
    dd.line([(57, 1), (57, 8)], fill=MID)
    dd.line([(2, 9), (113, 9)], fill=GHOST)


def pos_ticks(dd):
    dd.line([(3, 5), (251, 5)], fill=DIM)
    for x in range(6, 252, 6):
        dd.point((x, 4), fill=GHOST)
        dd.point((x, 6), fill=GHOST)
    for x in range(8, 252, 24):
        dd.line([(x, 2), (x, 8)], fill=DIM)
    bracket(dd, 1, 1, 9, MID)
    bracket(dd, 253, 1, 9, MID, right=True)


save(groove(116, vol_ticks), 'fbson')
save(groove(116, bal_ticks), 'fbbal')
save(groove(255, pos_ticks), 'fpos')


def knob(color, notch=True):
    im = new(17, 9)

    def draw(dd, c):
        dd.rectangle([5, 1, 11, 7], fill=c)
    glow(im, draw, color, HALO, None)
    if notch:
        ImageDraw.Draw(im).line([(8, 2), (8, 6)], fill=GLASS)
    return im


save(knob(HI), 'bson')
save(knob(HI), 'bbal')
save(knob(WHITE), 'bpos')

# --- the play list (mainpl.bmp, 258 x 362: the top as it is, 296..336 stretched, the last 26 pixels as they are) ---
pl = new(258, 362, GLASS + (255,))
glass(pl, (0, 0, 258, 46))
glass(pl, (0, 348, 258, 362))
dd = ImageDraw.Draw(pl)
# the list: a plain glass (no scanline: its middle is stretched), framed, and the track of its scroll bar
dd.rectangle([1, 46, 248, 347], fill=PLAIN)
frame(dd, 1, 46, 248, 347)
dd.rectangle([249, 46, 256, 347], fill=PLAIN)
dd.line([(249, 46), (256, 46)], fill=DIM)
dd.line([(249, 347), (256, 347)], fill=DIM)
dd.line([(257, 46), (257, 347)], fill=DIM)
# a soft glow on the inner edges of the list (columns only: it stretches well)
for i, c in enumerate([(9, 22, 12), (6, 15, 8)]):
    dd.line([(2 + i, 47), (2 + i, 346)], fill=c)
    dd.line([(247 - i, 47), (247 - i, 346)], fill=c)


def pl_texts(d2, color):
    pixels(d2, 29, 6, 'AJOUTER', F3, color)
    pixels(d2, 229 - text_width('RETIRER', F3), 6, 'RETIRER', F3, color)
    pixels(d2, 29, 22, 'CHARGER', F3, color)
    pixels(d2, 229 - text_width('SAUVER', F3), 22, 'SAUVER', F3, color)
    pixels(d2, 149, 22, 'EFFACER', F3, color)
glow(pl, pl_texts, MID, HALO2, None)
title = 'LISTE DE LECTURE'
tw = text_width(title, F5)
tx = (258 - tw - 8) // 2
glow(pl, lambda d2, c: pixels(d2, tx, 36, title, F5, c), HI)
glow(pl, lambda d2, c: d2.rectangle([tx + tw + 3, 42, tx + tw + 8, 43], fill=c), WHITE)   # the cursor
for x0, x1 in ((4, tx - 6), (tx + tw + 13, 253)):
    dd.line([(x0, 39), (x1, 39)], fill=DIM)
    dd.line([(x0, 41), (x1, 41)], fill=DIM)


# the grips of the bottom corners (16 x 12): a triangle of phosphor dots
def grips(d2, color):
    for j in range(3):
        for i in range(j + 1):
            d2.rectangle([3 + i * 3, 352 + j * 3, 4 + i * 3, 353 + j * 3], fill=color)
            d2.rectangle([253 - i * 3, 352 + j * 3, 254 - i * 3, 353 + j * 3], fill=color)
glow(pl, grips, MID, HALO2, None)
for x in (0, 257):
    dd.line([(x, 0), (x, 45)], fill=BEIGE_LO)
    dd.line([(x, 348), (x, 361)], fill=BEIGE_LO)
dd.line([(0, 46), (0, 347)], fill=BEIGE_LO)
dd.line([(0, 0), (257, 0)], fill=BEIGE_LO)
dd.line([(0, 361), (257, 361)], fill=BEIGE_LO)
save(pl, 'mainpl')

save(key(25, 17, '+'), 'plp')
save(key(25, 17, '-'), 'plm')
save(key(25, 17, 'L'), 'plload')
save(key(25, 17, 'S'), 'plsave')
save(key(25, 17, kind='bin'), 'plrem')
save(key(82, 16, 'MODE EDITION'), 'pledit')
save(key(82, 16, 'MODE LISTE', inverse=True), 'pledit2')

# the scroll bar: its track (stretched by WinZik: columns only) and its thumb (a reverse-video block)
track = new(8, 296, PLAIN + (255,))
td = ImageDraw.Draw(track)
td.line([(0, 0), (0, 295)], fill=DIM)
td.line([(4, 1), (4, 294)], fill=GHOST)
td.line([(7, 0), (7, 295)], fill=DIM)
save(track, 'posplay')
thumb = new(8, 17)
glow(thumb, lambda d2, c: d2.rectangle([2, 1, 6, 15], fill=c), HI, HALO, None)
ImageDraw.Draw(thumb).line([(3, 8), (5, 8)], fill=MID)
save(thumb, 'btnplay')


# the icons of the lines: the kind of file (a framed tag), the lyrics (a note, lit or dim)
def tag(label, color, inverse=False):
    im = new(17, 9)
    d2 = ImageDraw.Draw(im)
    if inverse:
        d2.rectangle([0, 0, 16, 8], fill=color)
    else:
        d2.rectangle([0, 0, 16, 8], outline=DIM)
    tw2 = text_width(label, F3)
    pixels(d2, (17 - tw2) // 2, 2, label, F3, GLASS if inverse else color)
    return im


save(tag('MP3', HI), 'infomp3')
save(tag('WAV', MID), 'infowav')
save(tag('MID', MID), 'infomid')
save(tag('CD', MID, inverse=True), 'infocd')
save(tag('VID', MID), 'infovid')
lyr_ok = new(17, 9)
glow(lyr_ok, lambda d2, c: symbol(d2, 'note', 8, 4, c), HI, HALO, None)
save(lyr_ok, 'lyrok')
lyr_no = new(17, 9)
symbol(ImageDraw.Draw(lyr_no), 'note', 8, 4, GHOST)
save(lyr_no, 'lyrno')

# --- the compact bar (mainp.bmp, 112 x 12) and its buttons (17 x 13, one every 16 pixels) ---------------------------
mp = new(112, 12, BEIGE + (255,))
md = ImageDraw.Draw(mp)
md.line([(0, 0), (111, 0)], fill=BEIGE_HI)
md.line([(0, 11), (111, 11)], fill=BEIGE_DK)
save(mp, 'mainp')


def small(kind=None, label=None):
    im = new(17, 13)
    glass(im, (0, 0, 16, 13))
    ImageDraw.Draw(im).line([(16, 0), (16, 12)], fill=BEIGE_LO)

    def draw(d2, color):
        for x, right in ((1, False), (14, True)):
            s = -1 if right else 1
            d2.line([(x, 2), (x, 10)], fill=color)
            d2.line([(x, 2), (x + 2 * s, 2)], fill=color)
            d2.line([(x, 10), (x + 2 * s, 10)], fill=color)
    glow(im, draw, MID, None, None)

    def content(d2, color):
        if kind:
            symbol(d2, kind, 8, 6, color, small=True)
        if label:
            pixels(d2, 7, 4, label, F3, color)
    glow(im, content, HI, HALO, None)
    return im


for name, kind, label in [('arrp', 'prev', None), ('pausep', 'pause', None), ('playp', 'play', None), ('avp', 'next', None),
                          ('petitp', 'up', None), ('helpp', None, '?'), ('lyrp', None, 'L')]:
    if label == '?':
        im = small()
        glow(im, lambda d2, c: [d2.point((7 + i, 4 + j), fill=c) for j, row in enumerate(['##.', '..#', '.#.', '...', '.#.']) for i, ch in enumerate(row) if ch == '#'], HI, HALO, None)
        save(im, name)
    else:
        save(small(kind, label), name)

# --- the icon: a little beige terminal with a green prompt --------------------------------------------------------
ico = new(32, 32)
d2 = ImageDraw.Draw(ico)
d2.rounded_rectangle([1, 2, 30, 25], 3, fill=BEIGE, outline=BEIGE_DK)
d2.rectangle([4, 5, 27, 20], fill=GLASS)
d2.polygon([(6, 25), (25, 25), (28, 30), (3, 30)], fill=BEIGE_LO, outline=BEIGE_DK)
glow(ico, lambda dd3, c: (pixels(dd3, 6, 8, '>', F3, c), dd3.rectangle([10, 12, 13, 12], fill=c)), HI, HALO, None)
d2.point((25, 23), fill=HI)
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)


colors = [HI, (88, 224, 116), (88, 224, 116), (88, 224, 116), (150, 255, 170), (150, 255, 170), (150, 255, 170),
          (46, 170, 78), (46, 170, 78), (120, 255, 150), (120, 255, 150), (214, 255, 222), (214, 255, 222), WHITE]
rows = [(110, 240, 140) if i % 2 == 0 else (64, 190, 98) for i in range(23)]
lines = ['[INF]', 'Phosphore'] + [vb(c) for c in colors] + [vb(c) for c in rows]
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('8 102 57 21\n')
