| 1 | import Cocoa |
| 2 | |
| 3 | let source = URL(fileURLWithPath: CommandLine.arguments[1]) |
| 4 | let folder = CommandLine.arguments[2] |
| 5 | guard let image = NSImage(contentsOf: source) else { |
| 6 | fatalError("Cannot load icon source: \(source.path)") |
| 7 | } |
| 8 | try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true) |
| 9 | |
| 10 | for (name, size) 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 | let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: size, pixelsHigh: size, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)! |
| 12 | NSGraphicsContext.saveGraphicsState() |
| 13 | NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep) |
| 14 | NSGraphicsContext.current?.imageInterpolation = .high |
| 15 | let scale = CGFloat(size) / 1024 |
| 16 | let transform = NSAffineTransform() |
| 17 | transform.scale(by: scale) |
| 18 | transform.concat() |
| 19 | let bounds = NSRect(x: 60, y: 60, width: 904, height: 904) |
| 20 | NSBezierPath(roundedRect: bounds, xRadius: 210, yRadius: 210).addClip() |
| 21 | image.draw(in: bounds, from: .zero, operation: .sourceOver, fraction: 1) |
| 22 | NSGraphicsContext.restoreGraphicsState() |
| 23 | try rep.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: folder + "/" + name + ".png")) |
| 24 | } |