SPB Git

Repository Public

make-icon.swift 2.8 KB 45 lines swift

1// Draws the application icon (original artwork: a 3×3 grid of nodes on a dark tile) at every macOS size.2// Usage: swift Scripts/make-icon.swift Resources/Assets.xcassets/AppIcon.appiconset3import AppKit45let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : ".")6func color(_ r: Double, _ g: Double, _ b: Double) -> CGColor { CGColor(srgbRed: r, green: g, blue: b, alpha: 1) }7let tile = color(0.0667, 0.0745, 0.0902), tileTop = color(0.1137, 0.1294, 0.1529)8let ok = color(0.2392, 0.8392, 0.5490), accent = color(0.4863, 0.7176, 1.0), stale = color(0.7176, 0.6118, 0.9490), dim = color(0.2314, 0.2588, 0.3059)9let cells: [CGColor] = [ok, ok, accent, ok, accent, ok, stale, ok, dim]1011func render(_ px: Int) -> Data {12    let s = CGFloat(px)13    let ctx = CGContext(data: nil, width: px, height: px, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!,14                        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!15    let inset = s * 0.116    let rect = CGRect(x: inset, y: inset, width: s - 2 * inset, height: s - 2 * inset)17    let path = CGPath(roundedRect: rect, cornerWidth: rect.width * 0.225, cornerHeight: rect.width * 0.225, transform: nil)18    ctx.addPath(path); ctx.clip()19    let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB)!, colors: [tileTop, tile] as CFArray, locations: [0, 1])!20    ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: s), end: CGPoint(x: 0, y: 0), options: [])21    let grid = rect.insetBy(dx: rect.width * 0.2, dy: rect.width * 0.2)22    let gap = grid.width * 0.09, cell = (grid.width - 2 * gap) / 323    for row in 0..<3 {24        for col in 0..<3 {25            let r = CGRect(x: grid.minX + CGFloat(col) * (cell + gap), y: grid.maxY - CGFloat(row + 1) * cell - CGFloat(row) * gap, width: cell, height: cell)26            ctx.addPath(CGPath(roundedRect: r, cornerWidth: cell * 0.26, cornerHeight: cell * 0.26, transform: nil))27            ctx.setFillColor(cells[row * 3 + col]); ctx.fillPath()28        }29    }30    let rep = NSBitmapImageRep(cgImage: ctx.makeImage()!)31    return rep.representation(using: .png, properties: [:])!32}3334var images: [[String: String]] = []35for (size, scales) in [(16, [1, 2]), (32, [1, 2]), (128, [1, 2]), (256, [1, 2]), (512, [1, 2])] {36    for scale in scales {37        let name = "icon_\(size)x\(size)\(scale == 2 ? "@2x" : "").png"38        try render(size * scale).write(to: out.appendingPathComponent(name))39        images.append(["size": "\(size)x\(size)", "idiom": "mac", "filename": name, "scale": "\(scale)x"])40    }41}42let json = try JSONSerialization.data(withJSONObject: ["images": images, "info": ["author": "xcode", "version": 1]], options: [.prettyPrinted, .sortedKeys])43try json.write(to: out.appendingPathComponent("Contents.json"))44print("wrote \(images.count) icons to \(out.path)")45
Files main
Menu

Theme