Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / build-icon.swift

Raw 1.6 KB on f7455f5645a899a3879cf752f3536fe6de471c90
1import AppKit
2import Foundation
3
4let directory = URL(fileURLWithPath: CommandLine.arguments[1])
5let artworkURL = URL(fileURLWithPath: "assets/app-icon/PulseCanvas-Octagon.png")
6guard let artwork = NSImage(contentsOf: artworkURL) else {
7 fatalError("Missing app icon artwork: \(artworkURL.path)")
8}
9try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
10for (name, pixels) in [("icon_16x16",16),("icon_16x16@2x",32),("icon_32x32",32),("icon_32x32@2x",64),("icon_128x128",128),("icon_128x128@2x",256),("icon_256x256",256),("icon_256x256@2x",512),("icon_512x512",512),("icon_512x512@2x",1024)] {
11 // Explicit pixel backing keeps Retina displays from doubling icon dimensions.
12 let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: pixels,
13 pixelsHigh: pixels, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
14 isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
15 let context = NSGraphicsContext(bitmapImageRep: bitmap)!
16 NSGraphicsContext.saveGraphicsState()
17 NSGraphicsContext.current = context
18 context.imageInterpolation = .high
19 let size = CGFloat(pixels)
20 let frame = NSRect(x: size * 0.06, y: size * 0.06,
21 width: size * 0.88, height: size * 0.88)
22 NSBezierPath(roundedRect: frame, xRadius: size * 0.2,
23 yRadius: size * 0.2).addClip()
24 artwork.draw(in: frame, from: .zero, operation: .copy, fraction: 1)
25 NSGraphicsContext.restoreGraphicsState()
26 try bitmap.representation(using: .png, properties: [:])!
27 .write(to: directory.appendingPathComponent(name + ".png"))
28}