SPB Git

Repository Public

make-icon.swift 3.6 KB 69 lines swift

1// Draws the iOS application icon (original artwork: a hub and a ring of nodes, one of them stale, on a dark tile).2// 1024 × 1024 px, opaque (no alpha channel), full bleed — iOS applies its own mask.3// Usage: swift Scripts/make-icon.swift Resources/Assets.xcassets/AppIcon.appiconset4import CoreGraphics5import Foundation6import ImageIO7import UniformTypeIdentifiers89let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : ".")10func color(_ r: Double, _ g: Double, _ b: Double, _ a: Double = 1) -> CGColor { CGColor(srgbRed: r, green: g, blue: b, alpha: a) }11let px = 102412let s = CGFloat(px)13let space = CGColorSpace(name: CGColorSpace.sRGB)!14let ctx = CGContext(data: nil, width: px, height: px, bitsPerComponent: 8, bytesPerRow: 0, space: space,15                    bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!16// Background: design-token canvas colours, top lighter than bottom.17let gradient = CGGradient(colorsSpace: space, colors: [color(0.1137, 0.1294, 0.1529), color(0.0392, 0.0431, 0.0510)] as CFArray, locations: [0, 1])!18ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: s), end: CGPoint(x: 0, y: 0), options: [])1920let centre = CGPoint(x: s / 2, y: s / 2)21let radius = s * 0.3022let ok = color(0.2392, 0.8392, 0.5490), accent = color(0.4863, 0.7176, 1.0), stale = color(0.9608, 0.7216, 0.2588), dim = color(0.2314, 0.2588, 0.3059)23let ring: [CGColor] = [ok, ok, accent, ok, stale, ok, ok, dim]24// Links from the hub to each node.25ctx.setLineWidth(s * 0.018)26ctx.setLineCap(.round)27for (i, c) in ring.enumerated() {28    let a = CGFloat(i) / CGFloat(ring.count) * 2 * .pi + .pi / 229    let p = CGPoint(x: centre.x + cos(a) * radius, y: centre.y + sin(a) * radius)30    ctx.setStrokeColor(c.copy(alpha: 0.38)!)31    ctx.move(to: centre); ctx.addLine(to: p); ctx.strokePath()32}33// Outer ring.34ctx.setStrokeColor(color(0.2314, 0.2588, 0.3059))35ctx.setLineWidth(s * 0.012)36ctx.addEllipse(in: CGRect(x: centre.x - radius, y: centre.y - radius, width: radius * 2, height: radius * 2)); ctx.strokePath()37// Nodes.38let node = s * 0.05839for (i, c) in ring.enumerated() {40    let a = CGFloat(i) / CGFloat(ring.count) * 2 * .pi + .pi / 241    let p = CGPoint(x: centre.x + cos(a) * radius, y: centre.y + sin(a) * radius)42    ctx.setFillColor(color(0.0392, 0.0431, 0.0510))43    ctx.fillEllipse(in: CGRect(x: p.x - node * 1.35, y: p.y - node * 1.35, width: node * 2.7, height: node * 2.7))44    ctx.setFillColor(c)45    ctx.fillEllipse(in: CGRect(x: p.x - node, y: p.y - node, width: node * 2, height: node * 2))46}47// Hub: rounded square, accent colour.48let hub = s * 0.10549ctx.setFillColor(color(0.0392, 0.0431, 0.0510))50ctx.addPath(CGPath(roundedRect: CGRect(x: centre.x - hub * 1.3, y: centre.y - hub * 1.3, width: hub * 2.6, height: hub * 2.6), cornerWidth: hub * 0.62, cornerHeight: hub * 0.62, transform: nil)); ctx.fillPath()51ctx.setFillColor(accent)52ctx.addPath(CGPath(roundedRect: CGRect(x: centre.x - hub, y: centre.y - hub, width: hub * 2, height: hub * 2), cornerWidth: hub * 0.45, cornerHeight: hub * 0.45, transform: nil)); ctx.fillPath()5354let url = out.appendingPathComponent("icon-1024.png")55let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)!56CGImageDestinationAddImage(dest, ctx.makeImage()!, nil)57guard CGImageDestinationFinalize(dest) else { fatalError("cannot write \(url.path)") }58let contents = """59{60  "images" : [61    { "filename" : "icon-1024.png", "idiom" : "universal", "platform" : "ios", "size" : "1024x1024" }62  ],63  "info" : { "author" : "xcode", "version" : 1 }64}6566"""67try contents.write(to: out.appendingPathComponent("Contents.json"), atomically: true, encoding: .utf8)68print("wrote \(url.path)")69
Files main
Menu

Theme