scripts/make-icon.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/scripts/make-icon.swift history · blame · raw

60 lines · 3583 bytes

 1// Draws orgstar's app icon: an outline's heading star over three indented lines, on a deep
 2// blue-to-violet square. Usage: swift scripts/make-icon.swift <dir.iconset> for the Mac's
 3// iconset, or swift scripts/make-icon.swift --ios <file.png> for the iOS app's 1024-pixel icon.
 4import AppKit
 5
 6/// The Mac's icon sits on the macOS grid, a rounded square inset in a clear canvas; the iOS
 7/// icon is the same square filling an opaque canvas, which iOS rounds itself.
 8func draw(_ size: CGFloat, fullBleed: Bool = false) -> NSBitmapImageRep {
 9    let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size), pixelsHigh: Int(size), bitsPerSample: 8,
10                               samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
11    NSGraphicsContext.saveGraphicsState()
12    NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)
13    let s = size / 1024
14    if fullBleed {
15        let transform = NSAffineTransform()
16        transform.scale(by: 1024 / 824)
17        transform.translateX(by: -100 * s, yBy: -100 * s)
18        transform.concat()
19    }
20    // The macOS icon grid: an 824-point rounded square in a 1024 canvas.
21    let square = NSRect(x: 100 * s, y: 100 * s, width: 824 * s, height: 824 * s)
22    let shape = fullBleed ? NSBezierPath(rect: square) : NSBezierPath(roundedRect: square, xRadius: 185 * s, yRadius: 185 * s)
23    NSGradient(colors: [NSColor(srgbRed: 0.13, green: 0.20, blue: 0.55, alpha: 1), NSColor(srgbRed: 0.40, green: 0.22, blue: 0.70, alpha: 1)])!
24        .draw(in: shape, angle: -60)
25    // The heading star.
26    let star = NSAttributedString(string: "✱", attributes: [
27        .font: NSFont.systemFont(ofSize: 330 * s, weight: .heavy), .foregroundColor: NSColor(srgbRed: 1, green: 0.78, blue: 0.30, alpha: 1),
28    ])
29    star.draw(at: NSPoint(x: 190 * s, y: 520 * s))
30    // Three outline lines, indented like a heading's children.
31    NSColor(white: 1, alpha: 0.92).setFill()
32    for (i, (x, width)) in [(470.0, 330.0), (300.0, 500.0), (380.0, 420.0)].enumerated() {
33        let y = (650 - Double(i) * 150) * s
34        let line = i == 0 ? NSRect(x: x * s, y: y, width: width * s, height: 70 * s) : NSRect(x: x * s, y: y, width: width * s, height: 56 * s)
35        NSBezierPath(roundedRect: line, xRadius: 28 * s, yRadius: 28 * s).fill()
36    }
37    NSGraphicsContext.restoreGraphicsState()
38    return rep
39}
40
41if CommandLine.arguments[1] == "--ios" {
42    // Without an alpha channel, as the App Store requires: drawn, then copied into an opaque image.
43    let drawn = draw(1024, fullBleed: true).cgImage!
44    let opaque = CGContext(data: nil, width: 1024, height: 1024, bitsPerComponent: 8, bytesPerRow: 0,
45                           space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!
46    opaque.draw(drawn, in: CGRect(x: 0, y: 0, width: 1024, height: 1024))
47    let file = URL(fileURLWithPath: CommandLine.arguments[2])
48    try NSBitmapImageRep(cgImage: opaque.makeImage()!).representation(using: .png, properties: [:])!.write(to: file)
49    exit(0)
50}
51
52let folder = URL(fileURLWithPath: CommandLine.arguments[1])
53try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
54for base in [16, 32, 128, 256, 512] {
55    for scale in [1, 2] {
56        let name = scale == 1 ? "icon_\(base)x\(base).png" : "icon_\(base)x\(base)@2x.png"
57        let data = draw(CGFloat(base * scale)).representation(using: .png, properties: [:])!
58        try data.write(to: folder.appendingPathComponent(name))
59    }
60}