Scripts/make-app-icons.swift

2ad5d8160338d289a65d6a4d1c214a02bf916f1f
domain-dig/Scripts/make-app-icons.swift history · blame · raw

155 lines · 5292 bytes · executable

5 symbols in this file
  1#!/usr/bin/env swift
  2
  3// Renders the app icon set, one per accent theme.
  4//
  5// The artwork is generated rather than drawn by hand so it is reproducible and
  6// reviewable as source. The mark is the namespace itself: the DNS is a tree
  7// from the root down, which is what the app reads, and no other category of app
  8// uses it — a magnifying glass would have said "search", which is not the same
  9// thing and is every other icon in the category.
 10//
 11// Only the ground colour changes between themes, so the four read as one app.
 12//
 13// Usage: swift Scripts/make-app-icons.swift <output-directory>
 14//
 15// Writes <name>-1024.png per theme. iOS masks the corners itself, so these are
 16// full-bleed squares with no rounding and no alpha.
 17
 18import CoreGraphics
 19import Foundation
 20import ImageIO
 21import UniformTypeIdentifiers
 22
 23let side = 1024
 24let size = CGFloat(side)
 25
 26struct Theme {
 27    let name: String
 28    /// Top and bottom of the ground gradient. The bottom carries the same hue
 29    /// darker, so the mark keeps its contrast across the whole square.
 30    let top: (CGFloat, CGFloat, CGFloat)
 31    let bottom: (CGFloat, CGFloat, CGFloat)
 32}
 33
 34let themes = [
 35    Theme(name: "AppIcon", top: (0.00, 0.25, 0.87), bottom: (0.00, 0.10, 0.45)),
 36    Theme(name: "AppIconGraphite", top: (0.34, 0.36, 0.40), bottom: (0.16, 0.17, 0.20)),
 37    Theme(name: "AppIconForest", top: (0.16, 0.48, 0.32), bottom: (0.06, 0.22, 0.15)),
 38    Theme(name: "AppIconPlum", top: (0.50, 0.24, 0.52), bottom: (0.24, 0.10, 0.26))
 39]
 40
 41struct Failure: LocalizedError {
 42    let message: String
 43    init(_ message: String) { self.message = message }
 44    var errorDescription: String? { message }
 45}
 46
 47private func dot(_ context: CGContext, _ centre: CGPoint, _ radius: CGFloat) {
 48    context.addEllipse(in: CGRect(
 49        x: centre.x - radius,
 50        y: centre.y - radius,
 51        width: radius * 2,
 52        height: radius * 2
 53    ))
 54    context.fillPath()
 55}
 56
 57/// Root, two labels, four leaves — the shape of a hostname read right to left.
 58private func drawNamespaceTree(_ context: CGContext) {
 59    let nodeRadius = size * 0.056
 60    let edgeWidth = size * 0.034
 61
 62    let root = CGPoint(x: size * 0.5, y: size * 0.775)
 63    let labels = [
 64        CGPoint(x: size * 0.305, y: size * 0.515),
 65        CGPoint(x: size * 0.695, y: size * 0.515)
 66    ]
 67    let leaves = [
 68        CGPoint(x: size * 0.215, y: size * 0.265),
 69        CGPoint(x: size * 0.395, y: size * 0.265),
 70        CGPoint(x: size * 0.605, y: size * 0.265),
 71        CGPoint(x: size * 0.785, y: size * 0.265)
 72    ]
 73
 74    // Edges first, slightly under full white so the nodes stay the emphasis.
 75    context.setStrokeColor(CGColor(red: 1, green: 1, blue: 1, alpha: 0.82))
 76    context.setLineWidth(edgeWidth)
 77    context.setLineCap(.round)
 78    for label in labels {
 79        context.move(to: root)
 80        context.addLine(to: label)
 81        context.strokePath()
 82    }
 83    for (index, leaf) in leaves.enumerated() {
 84        context.move(to: labels[index / 2])
 85        context.addLine(to: leaf)
 86        context.strokePath()
 87    }
 88
 89    context.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1))
 90    dot(context, root, nodeRadius * 1.28)
 91    for label in labels { dot(context, label, nodeRadius) }
 92    for leaf in leaves { dot(context, leaf, nodeRadius * 0.80) }
 93}
 94
 95func render(_ theme: Theme, to url: URL) throws {
 96    let space = CGColorSpaceCreateDeviceRGB()
 97    guard let context = CGContext(
 98        data: nil,
 99        width: side,
100        height: side,
101        bitsPerComponent: 8,
102        bytesPerRow: 0,
103        space: space,
104        bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue
105    ) else {
106        throw Failure("could not create a bitmap context")
107    }
108
109    let colors = [
110        CGColor(red: theme.top.0, green: theme.top.1, blue: theme.top.2, alpha: 1),
111        CGColor(red: theme.bottom.0, green: theme.bottom.1, blue: theme.bottom.2, alpha: 1)
112    ] as CFArray
113    guard let gradient = CGGradient(colorsSpace: space, colors: colors, locations: [0, 1]) else {
114        throw Failure("could not create the gradient")
115    }
116    context.drawLinearGradient(
117        gradient,
118        start: CGPoint(x: 0, y: size),
119        end: CGPoint(x: 0, y: 0),
120        options: []
121    )
122
123    drawNamespaceTree(context)
124
125    guard let image = context.makeImage() else {
126        throw Failure("could not render \(theme.name)")
127    }
128    guard let destination = CGImageDestinationCreateWithURL(
129        url as CFURL,
130        UTType.png.identifier as CFString,
131        1,
132        nil
133    ) else {
134        throw Failure("could not open \(url.path) for writing")
135    }
136    CGImageDestinationAddImage(destination, image, nil)
137    guard CGImageDestinationFinalize(destination) else {
138        throw Failure("could not write \(url.path)")
139    }
140}
141
142let arguments = CommandLine.arguments
143guard arguments.count == 2 else {
144    FileHandle.standardError.write(Data("usage: make-app-icons.swift <output-directory>\n".utf8))
145    exit(2)
146}
147
148let outputDirectory = URL(fileURLWithPath: arguments[1], isDirectory: true)
149try FileManager.default.createDirectory(at: outputDirectory, withIntermediateDirectories: true)
150
151for theme in themes {
152    let url = outputDirectory.appendingPathComponent("\(theme.name)-1024.png")
153    try render(theme, to: url)
154    print("wrote \(url.lastPathComponent)")
155}