aboutsummaryrefslogtreecommitdiffstats
path: root/brand/make_logo.py
blob: 658e921d87e5ceca21a74b3dbb35c5ae0680001f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
"""Generates the 100cIA brand kit. Text is converted to curves with Inkscape,
so the SVGs do not depend on the font being installed."""
import os, re, subprocess, tempfile
import xml.etree.ElementTree as ET

HERE = os.path.dirname(os.path.abspath(__file__))

BG     = "#0B0F1A"
POINTS = "#243050"
WHITE_C = "#FFFFFF"
AMBER  = "#FFD166"
TRACK  = "#1B2233"
C_GRAY   = "#8D99AE"

FONT_NAME = "Roboto Black"
TRACKING = -0.035      # em


def curve_list(parts, size=400, tracking=TRACKING):
    """parts = [(text, color), ...] -> (inner_svg, x, y, w, h) already as curves."""
    tspans = "".join(
        f'<tspan fill="{c}"{f" dx=\'{0.045 * size:g}\'" if i else ""}>{t}</tspan>'
        for i, (t, c) in enumerate(parts))
    src = (f'<svg xmlns="http://www.w3.org/2000/svg" width="6000" height="1200" '
           f'viewBox="0 0 6000 1200"><text id="w" x="60" y="800" '
           f'font-family="{FONT_NAME}" font-weight="900" font-size="{size}" '
           f'letter-spacing="{tracking * size:g}">{tspans}</text></svg>')
    with tempfile.TemporaryDirectory() as d:
        a, b = os.path.join(d, "a.svg"), os.path.join(d, "b.svg")
        open(a, "w", encoding="utf-8").write(src)
        subprocess.run(["inkscape", "--export-text-to-path", "--export-plain-svg",
                        f"--export-filename={b}", a],
                       stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
        raw = open(b, encoding="utf-8").read()
        q = {k: float(subprocess.run(["inkscape", "--query-id=w", f"--query-{k}", b],
                                     capture_output=True, text=True).stdout.strip())
             for k in ("x", "y", "width", "height")}

    NS = "http://www.w3.org/2000/svg"
    ET.register_namespace("", NS)
    root = ET.fromstring(raw)
    node = next(e for e in root.iter() if e.get("id") == "w")
    for e in node.iter():
        e.attrib.pop("id", None)
        e.attrib.pop("aria-label", None)
    body_obj = ET.tostring(node, encoding="unicode")
    body_obj = body_obj.replace(f' xmlns="{NS}"', "")
    if not body_obj.lstrip().startswith("<g"):
        body_obj = f"<g>{body_obj}</g>"
    return body_obj, q["x"], q["y"], q["width"], q["height"]


def place(g, bx, by, bw, bh, cx, cy, width_px):
    s = width_px / bw
    tx = cx - s * (bx + bw / 2)
    ty = cy - s * (by + bh / 2)
    return f'<g transform="translate({tx:.3f},{ty:.3f}) scale({s:.5f})">{g}</g>'


def point_list(id_, sep=118, r=4.2, color=POINTS, op=0.55):
    return (f'<pattern id="{id_}" width="{sep}" height="{sep}" '
            f'patternUnits="userSpaceOnUse">'
            f'<circle cx="{sep/2}" cy="{sep/2}" r="{r}" fill="{color}" '
            f'opacity="{op}"/></pattern>')


def arc_m(cx, cy, r, since, until, color, w):
    """Arc in degrees, 0 = up, clockwise."""
    import math
    def p(a):
        t = math.radians(a - 90)
        return cx + r * math.cos(t), cy + r * math.sin(t)
    x0, y0 = p(since)
    x1, y1 = p(until)
    largo = 1 if (until - since) % 360 > 180 else 0
    return (f'<path d="M {x0:.2f} {y0:.2f} A {r} {r} 0 {largo} 1 {x1:.2f} {y1:.2f}" '
            f'fill="none" stroke="{color}" stroke-width="{w}" stroke-linecap="round"/>')


# ---------------------------------------------------------------- avatar
def avatar(file_path, chip=False, pct=0.62):
    D = 1024
    R = D / 2
    ring_r, ring_w = R - 26, 20
    if chip:
        g1, x1, y1, w1, h1 = curve_list([("100c", WHITE_C)])
        g2, x2, y2, w2, h2 = curve_list([("IA", "#0B0F1A")])
    else:
        g1, x1, y1, w1, h1 = curve_list([("100c", WHITE_C), ("IA", AMBER)])

    body_obj = [
        f'<circle cx="{R}" cy="{R}" r="{R}" fill="{BG}"/>',
        f'<circle cx="{R}" cy="{R}" r="{R}" fill="url(#dots)"/>',
        f'<circle cx="{R}" cy="{R}" r="{ring_r}" fill="none" '
        f'stroke="{TRACK}" stroke-width="{ring_w}"/>',
        arc_m(R, R, ring_r, 0, 360 * pct, AMBER, ring_w),
    ]
    if chip:
        total_width = 760
        # the "IA" chip takes ~38% of the width, like the chips in the videos
        w_text = total_width * 0.60
        body_obj.append(place(g1, x1, y1, w1, h1, R - total_width / 2 + w_text / 2, R, w_text))
        scale_f = w_text / w1
        cw, ch = w2 * scale_f * 1.0 + 70, h1 * scale_f + 62
        cxc = R - total_width / 2 + w_text + 26 + cw / 2
        body_obj.append(f'<rect x="{cxc - cw/2:.1f}" y="{R - ch/2:.1f}" width="{cw:.1f}" '
                      f'height="{ch:.1f}" rx="{ch*0.30:.1f}" fill="{AMBER}"/>')
        body_obj.append(place(g2, x2, y2, w2, h2, cxc, R, w2 * scale_f))
    else:
        body_obj.append(place(g1, x1, y1, w1, h1, R, R, 740))

    svg = (f'<svg xmlns="http://www.w3.org/2000/svg" width="{D}" height="{D}" '
           f'viewBox="0 0 {D} {D}"><defs>{point_list("dots")}</defs>'
           + "".join(body_obj) + '</svg>')
    open(os.path.join(HERE, file_path), "w", encoding="utf-8").write(svg)
    return file_path


# ---------------------------------------------------------------- wordmark
# The chip around "IA" is the brand: it goes on every variant except the
# "flat" one, kept as an alternative.
MODES = {
    # ink         chip background  chip letter
    "oscuro": (WHITE_C,     AMBER,           "#0B0F1A"),
    "claro":  ("#0B0F1A",  "#D98C00",       "#FFFFFF"),
    "mono":   (WHITE_C,     WHITE_C,          None),        # knocked-out letters
}


def _lockup(mode, alto=520):
    """Returns (svg_pieces, width, height) of the '100c' lockup + 'IA' chip."""
    ink, chip_bg, letter = MODES[mode]
    g1, x1, y1, w1, h1 = curve_list([("100c", ink)])
    g2, x2, y2, w2, h2 = curve_list([("IA", letter or "#000000")])

    scale_f = alto / h1
    wt = w1 * scale_f
    cw = w2 * scale_f + 0.68 * alto
    ch = alto + 0.60 * alto
    gap_m = 0.13 * alto
    W = wt + gap_m + cw
    cy = ch / 2
    rx = ch * 0.28
    cx_chip = wt + gap_m + cw / 2
    chip_x = wt + gap_m

    pieces = [place(g1, x1, y1, w1, h1, wt / 2, cy, wt)]
    if letter is None:                       # a single ink: the IA is knocked out
        mascara = (f'<mask id="cala">'
                   f'<rect x="{chip_x:.1f}" y="0" width="{cw:.1f}" height="{ch:.1f}" '
                   f'rx="{rx:.1f}" fill="#FFFFFF"/>'
                   + place(g2, x2, y2, w2, h2, cx_chip, cy, w2 * scale_f)
                   + '</mask>')
        pieces.append(f'<defs>{mascara}</defs>')
        pieces.append(f'<rect x="{chip_x:.1f}" y="0" width="{cw:.1f}" height="{ch:.1f}" '
                      f'rx="{rx:.1f}" fill="{chip_bg}" mask="url(#cala)"/>')
    else:
        pieces.append(f'<rect x="{chip_x:.1f}" y="0" width="{cw:.1f}" height="{ch:.1f}" '
                      f'rx="{rx:.1f}" fill="{chip_bg}"/>')
        pieces.append(place(g2, x2, y2, w2, h2, cx_chip, cy, w2 * scale_f))
    return pieces, W, ch


def wordmark(file_path, mode="oscuro", margin=0.10):
    pieces, W0, H0 = _lockup(mode)
    m = margin * H0
    W, H = int(round(W0 + 2 * m)), int(round(H0 + 2 * m))
    svg = (f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" '
           f'viewBox="0 0 {W} {H}"><g transform="translate({m:.1f},{m:.1f})">'
           + "".join(pieces) + '</g></svg>')
    open(os.path.join(HERE, file_path), "w", encoding="utf-8").write(svg)
    return file_path


def wordmark_plano(file_path):
    """Chip-less alternative: '100cIA' in one run, the IA in amber."""
    g, x, y, w, h = curve_list([("100c", WHITE_C), ("IA", AMBER)])
    W = 2400
    width_px = W * 0.88
    H = int(round(h / w * width_px + 2 * 0.06 * W))
    svg = (f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" '
           f'viewBox="0 0 {W} {H}">'
           + place(g, x, y, w, h, W / 2, H / 2, width_px) + '</svg>')
    open(os.path.join(HERE, file_path), "w", encoding="utf-8").write(svg)
    return file_path


# ---------------------------------------------------------------- banner
def banner(file_path, tagline="ciencia en 40 segundos"):
    W, H = 1920, 1080
    g, x, y, w, h = curve_list([("100c", WHITE_C)])
    t, tx, ty, tw, th = curve_list([(tagline, "#C9D3E5")], size=200, tracking=0.012)
    body_obj = [
        f'<rect width="{W}" height="{H}" fill="{BG}"/>',
        f'<rect width="{W}" height="{H}" fill="url(#dots)"/>',
        f'<rect x="0" y="0" width="{W}" height="12" fill="{TRACK}"/>',
        f'<rect x="0" y="0" width="{int(W*0.62)}" height="12" fill="{AMBER}"/>',
        None,