make_logo_data.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Generates src/ui/LogoData.h: the LYNX SHARE logo (by w3teal) embedded in the exe: the app icon (the mark on its dark
  2. # tile) for the window/taskbar icon, LYNX-drawn title bars and the About box, and the mark alone for the plugin-editor
  3. # header button.
  4. # Source of truth: resources/brand/lynx-share-logo.svg (+ its small-size form lynx-share-logo-small.svg);
  5. # tools/make_logo.py renders them to the PNGs read here. Embedded: the icon at 512 px, its small-size form at 128 px with
  6. # the stem (drawn at 21-32 physical pixels) and without it (20 px and less), the mark at 512 px and its small-size form
  7. # at 128 px, and where the mark's ring sits in the mark pictures.
  8. # Usage: python3 tools/make_logo_data.py (from the repository root). scripts/check-logo.py fails when the header drifts.
  9. import hashlib, io
  10. from PIL import Image
  11. SRC = "resources/lynx_badge.png"
  12. SMALL = "resources/brand/lynx-share-logo-small.png"
  13. TINY = "resources/brand/lynx-share-logo-tiny.png"
  14. MARK = "resources/lynx_logo.png"
  15. MARK_SMALL = "resources/brand/lynx-share-mark-small.png"
  16. def png(path, size):
  17. img = Image.open(path).convert("RGBA").resize((size, size), Image.LANCZOS)
  18. out = io.BytesIO()
  19. img.save(out, "PNG", optimize=True)
  20. return out.getvalue()
  21. def array(name, data):
  22. lines = ["inline constexpr unsigned char %s[] = {" % name]
  23. for i in range(0, len(data), 24):
  24. lines.append(", ".join(str(b) for b in data[i:i + 24]) + ",")
  25. return lines + ["};"]
  26. def sha(path):
  27. return hashlib.sha256(open(path, "rb").read()).hexdigest()
  28. # the mark's ring: its left, right and bottom edges are the circle's (the ears and the stem rise above it)
  29. a = Image.open(MARK).convert("RGBA").getchannel("A")
  30. x0, y0, x1, y1 = a.point(lambda v: 255 if v >= 128 else 0).getbbox()
  31. r = (x1 - x0) / 2.0
  32. disc = ((x0 + x1) / 2.0 / a.width, (y1 - r) / a.height, r / a.width)
  33. lines = ["#pragma once", "",
  34. "// LYNX SHARE logo by w3teal. png512: the app icon %s (512 px); pngSmall / pngTiny: its small-size form" % SRC,
  35. "// %s with and without the stem (128 px); markPng512: the mark without the tile %s (512 px);" % (SMALL, MARK),
  36. "// markSmall: its small-size form %s (128 px). All with transparency." % MARK_SMALL,
  37. "// Generated by tools/make_logo_data.py - do not edit by hand (scripts/check-logo.py verifies it).",
  38. "// source-sha256: " + sha(SRC),
  39. "// small-sha256: " + sha(SMALL),
  40. "// tiny-sha256: " + sha(TINY),
  41. "// mark-sha256: " + sha(MARK),
  42. "// mark-small-sha256: " + sha(MARK_SMALL), "",
  43. "namespace lynx::logo", "{",
  44. "// where the mark's ring sits in markPng512 and markSmall (fractions of the side): centre x, centre y, radius",
  45. "inline constexpr float discX = %.4ff, discY = %.4ff, discR = %.4ff;" % disc]
  46. sizes = {}
  47. for name, path, size in (("png512", SRC, 512), ("pngSmall", SMALL, 128), ("pngTiny", TINY, 128),
  48. ("markPng512", MARK, 512), ("markSmall", MARK_SMALL, 128)):
  49. data = png(path, size)
  50. sizes[name] = len(data)
  51. lines += array(name, data)
  52. lines += ["} // namespace lynx::logo", ""]
  53. open("src/ui/LogoData.h", "w").write("\n".join(lines))
  54. print(sizes, "disc", disc)