Files
hrynco-kswitcher/tools/generate-icon.py
T

162 lines
7.4 KiB
Python

"""Regenerate the native multi-resolution icons (development only; requires Pillow)."""
from argparse import ArgumentParser
from pathlib import Path
from PIL import Image, ImageDraw
root = Path(__file__).resolve().parents[1] / 'resources'
icon_sizes = [(size, size) for size in (16, 20, 24, 32, 40, 48, 64, 128, 256)]
def draw_files_icon(size):
image = Image.new('RGBA', (size, size))
draw = ImageDraw.Draw(image)
def block(left, top, right, bottom, color):
scale = size / 32
draw.rectangle((round(left * scale), round(top * scale),
round(right * scale) - 1, round(bottom * scale) - 1), fill=color)
# Draw each size on its own pixel grid so the pane divider stays sharp.
frame = '#3DDDEB'
panel = '#101E89'
block(2, 2, 30, 30, '#10163D')
block(2, 2, 30, 26, frame)
block(4, 4, 28, 24, panel)
divider = max(1, round(size / 16))
center = size // 2 - divider // 2
draw.rectangle((center, round(size / 8), center + divider - 1, round(size * 3 / 4) - 1), fill=frame)
block(6, 6, 12, 8, '#FFE368')
block(19, 6, 25, 8, '#FFE368')
for y in (10, 18, 22):
block(6, y, 12, y + 2, frame)
for y, end in ((10, 26), (14, 24), (18, 26)):
block(19, y, end, y + 2, frame)
block(4, 14, 15, 16, '#5EEAD4')
for left in (4, 10, 16, 22):
block(left, 28, left + 4, 30, '#1AA3AD')
return image
def draw_window_icon(kind):
scale = 32
image = Image.new('RGBA', (32 * scale, 32 * scale))
draw = ImageDraw.Draw(image)
indigo, white, mint = '#4F46E5', '#FFFFFF', '#5EEAD4'
def box(left, top, right, bottom, radius, color, outline=None, width=1):
draw.rounded_rectangle(tuple(round(v * scale) for v in (left, top, right, bottom)),
radius=round(radius * scale), fill=color, outline=outline,
width=round(width * scale))
if kind == 'donate':
# The steamed cup and saucer from resources/support/donate-en.svg,
# with a thicker stroke to remain legible in the 16 px tray menu.
def cup_line(points):
draw.line([(round(x * scale), round(y * scale)) for x, y in points],
fill=indigo, width=round(2.2 * scale), joint='curve')
box(18, 13, 28, 21, 4, None, indigo, 2.2)
box(6, 12, 22, 25, 5, white, indigo, 2.2)
draw.rectangle((6 * scale, 12 * scale, 22 * scale, 17 * scale), fill=white)
cup_line([(6.9, 17), (6.9, 12.8), (21.1, 12.8), (21.1, 17)])
cup_line([(4, 28), (24, 28)])
for x in (10, 14, 18):
cup_line([(x, 5), (x, 8)])
return image.resize((256, 256), Image.Resampling.LANCZOS)
box(1, 1, 31, 31, 7, indigo)
if kind == 'history':
box(8, 8, 24, 26, 3, indigo, white, 2.5)
box(11.5, 5.5, 20.5, 11.5, 2, mint)
box(12, 14, 20, 16.2, 1.1, white)
box(12, 18.5, 18, 20.7, 1.1, white)
elif kind == 'screenshot':
box(7, 7, 24, 24, 2, indigo, white, 2.5)
draw.polygon([(round(x * scale), round(y * scale)) for x, y in
[(14, 22), (24, 12), (29, 17), (19, 27), (12, 29)]], fill=indigo)
draw.polygon([(round(x * scale), round(y * scale)) for x, y in
[(16, 23), (24, 15), (27, 18), (19, 26), (15, 27)]], fill=mint)
else:
for color, points in (
(white, [(5, 8), (11, 8), (16, 11), (21, 8), (27, 8), (27, 23),
(21, 23), (16, 26), (11, 23), (5, 23)]),
(indigo, [(7.5, 10.5), (11, 10.5), (14.8, 13), (14.8, 22.3),
(11.5, 20.5), (7.5, 20.5)]),
(indigo, [(17.2, 13), (21, 10.5), (24.5, 10.5), (24.5, 20.5),
(20.5, 20.5), (17.2, 22.3)]),
):
draw.polygon([(round(x * scale), round(y * scale)) for x, y in points], fill=color)
box(19, 14, 23, 16, 1, mint)
box(19, 17.5, 23, 19.4, 1, mint)
return image.resize((256, 256), Image.Resampling.LANCZOS)
parser = ArgumentParser(description=__doc__)
mode = parser.add_mutually_exclusive_group()
mode.add_argument('--files-only', action='store_true', help='Regenerate only the main Files icon.')
mode.add_argument('--window-icons-only', action='store_true', help='Regenerate the window and tray menu icons.')
args = parser.parse_args()
if not args.files_only:
for kind in ('history', 'screenshot', 'help', 'donate'):
draw_window_icon(kind).save(root / f'kswitcher-{kind}.ico', sizes=icon_sizes)
if args.window_icons_only:
raise SystemExit
frames = [draw_files_icon(size) for size, _ in icon_sizes]
frames[-1].save(root / 'kswitcher-files.ico', sizes=icon_sizes, append_images=frames[:-1])
if args.files_only:
raise SystemExit
# Keep the two pause bars aligned to whole pixels at each tray size.
def draw_paused_icon(size):
scale = 8
tile = Image.new('RGBA', (size * scale, size * scale))
unit = size * scale / 32
ImageDraw.Draw(tile).rounded_rectangle(
(unit, unit, 31 * unit - 1, 31 * unit - 1), radius=7 * unit, fill='#575E68')
tile = tile.resize((size, size), Image.Resampling.LANCZOS)
draw = ImageDraw.Draw(tile)
top = round(size / 4)
left = round(size / 4)
width = round(size * 3 / 16)
for x in (left, size - left - width):
draw.rectangle((x, top, x + width - 1, size - top - 1), fill='white')
return tile
paused_frames = [draw_paused_icon(size) for size, _ in icon_sizes]
paused_frames[-1].save(root / 'kswitcher-paused.ico', sizes=icon_sizes, append_images=paused_frames[:-1])
# Geometric K: white stem/upper arm, mint lower arm on an indigo keycap.
polygons = [('#FFFFFF', [(8, 7), (12, 7), (12, 25), (8, 25)]),
('#FFFFFF', [(11, 15), (22, 6), (25, 9), (14, 18)]),
('#5EEAD4', [(11, 14), (25, 24), (22, 27), (10, 18)])]
svg = '<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 32 32">\n'
svg += '<rect x="1" y="1" width="30" height="30" rx="7" fill="#4F46E5"/>\n'
for color, points in polygons:
svg += '<polygon fill="' + color + '" points="' + ' '.join(f'{x},{y}' for x,y in points) + '"/>\n'
(root / 'kswitcher.svg').write_text(svg + '</svg>\n', encoding='utf-8')
scale = 32
im = Image.new('RGBA', (32*scale, 32*scale))
draw = ImageDraw.Draw(im)
draw.rounded_rectangle((scale, scale, 31*scale, 31*scale), radius=7*scale, fill='#4F46E5')
for color, points in polygons:
draw.polygon([(x*scale,y*scale) for x,y in points], fill=color)
im = im.resize((256,256),Image.Resampling.LANCZOS)
im.save(root / 'kswitcher.png')
im.save(root / 'kswitcher.ico', sizes=icon_sizes)
for mode, color in [('editor', '#4F46E5'), ('viewer', '#087C73')]:
image = Image.new('RGBA', (32*scale, 32*scale))
draw = ImageDraw.Draw(image)
draw.rounded_rectangle((scale, scale, 31*scale, 31*scale), radius=7*scale, fill=color)
if mode == 'editor':
draw.polygon([(x*scale, y*scale) for x, y in
[(7, 25), (8, 19), (20, 7), (23, 7), (26, 10), (26, 12), (14, 24)]], fill='white')
draw.line([(18*scale, 9*scale), (24*scale, 15*scale)], fill=color, width=2*scale)
else:
draw.polygon([(x*scale, y*scale) for x, y in
[(5, 16), (8, 12), (12, 9), (16, 8), (20, 9), (24, 12),
(27, 16), (24, 20), (20, 23), (16, 24), (12, 23), (8, 20)]], fill='white')
draw.ellipse((12*scale, 12*scale, 20*scale, 20*scale), fill=color)
image.resize((256, 256), Image.Resampling.LANCZOS).save(root / f'kswitcher-files-{mode}.ico', sizes=icon_sizes)