Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Scripts/make-icon.swift3// Description: Draws the Local Chat icon (graphite tile, aurora gradient spark inside a chat bubble) and writes the PNG sizes both asset catalogs need.4import AppKit56func render(_ pixels: Int, inset: Bool) -> Data {7 let size = CGFloat(pixels)8 let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: pixels, pixelsHigh: pixels, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,9 colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!10 NSGraphicsContext.saveGraphicsState()11 NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)12 let ctx = NSGraphicsContext.current!.cgContext13 // macOS icons sit in a rounded tile with a margin; iOS icons are full-bleed squares (the system masks them)14 let margin = inset ? size * 0.098 : 015 let tile = CGRect(x: margin, y: margin, width: size - 2 * margin, height: size - 2 * margin)16 let radius = inset ? tile.width * 0.2237 : 017 let path = CGPath(roundedRect: tile, cornerWidth: radius, cornerHeight: radius, transform: nil)18 ctx.addPath(path); ctx.clip()19 let space = CGColorSpaceCreateDeviceRGB()20 let background = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.105, green: 0.11, blue: 0.14, alpha: 1), CGColor(red: 0.04, green: 0.043, blue: 0.055, alpha: 1)] as CFArray, locations: [0, 1])!21 ctx.drawLinearGradient(background, start: CGPoint(x: tile.minX, y: tile.maxY), end: CGPoint(x: tile.maxX, y: tile.minY), options: [])22 // soft aurora glow23 let glow = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.52, green: 0.53, blue: 1, alpha: 0.55), CGColor(red: 0.52, green: 0.53, blue: 1, alpha: 0)] as CFArray, locations: [0, 1])!24 ctx.drawRadialGradient(glow, startCenter: CGPoint(x: tile.midX - tile.width * 0.12, y: tile.midY + tile.height * 0.1), startRadius: 0,25 endCenter: CGPoint(x: tile.midX, y: tile.midY), endRadius: tile.width * 0.62, options: [])26 // chat bubble27 let bubble = CGRect(x: tile.minX + tile.width * 0.2, y: tile.minY + tile.height * 0.27, width: tile.width * 0.6, height: tile.height * 0.5)28 let bubblePath = CGMutablePath()29 bubblePath.addRoundedRect(in: bubble, cornerWidth: bubble.height * 0.3, cornerHeight: bubble.height * 0.3)30 bubblePath.move(to: CGPoint(x: bubble.minX + bubble.width * 0.2, y: bubble.minY + 1))31 bubblePath.addLine(to: CGPoint(x: bubble.minX + bubble.width * 0.14, y: bubble.minY - bubble.height * 0.2))32 bubblePath.addLine(to: CGPoint(x: bubble.minX + bubble.width * 0.4, y: bubble.minY + 1))33 bubblePath.closeSubpath()34 ctx.saveGState(); ctx.addPath(bubblePath); ctx.clip()35 let aurora = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.43, green: 0.47, blue: 1, alpha: 1), CGColor(red: 0.66, green: 0.48, blue: 1, alpha: 1), CGColor(red: 0.26, green: 0.85, blue: 0.89, alpha: 1)] as CFArray,36 locations: [0, 0.55, 1])!37 ctx.drawLinearGradient(aurora, start: CGPoint(x: bubble.minX, y: bubble.maxY), end: CGPoint(x: bubble.maxX, y: bubble.minY - bubble.height * 0.2), options: [])38 ctx.restoreGState()39 // four-point spark40 func spark(center: CGPoint, r: CGFloat) {41 let p = CGMutablePath(); let k = r * 0.242 p.move(to: CGPoint(x: center.x, y: center.y + r))43 p.addQuadCurve(to: CGPoint(x: center.x + r, y: center.y), control: CGPoint(x: center.x + k, y: center.y + k))44 p.addQuadCurve(to: CGPoint(x: center.x, y: center.y - r), control: CGPoint(x: center.x + k, y: center.y - k))45 p.addQuadCurve(to: CGPoint(x: center.x - r, y: center.y), control: CGPoint(x: center.x - k, y: center.y - k))46 p.addQuadCurve(to: CGPoint(x: center.x, y: center.y + r), control: CGPoint(x: center.x - k, y: center.y + k))47 ctx.addPath(p); ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 0.96)); ctx.fillPath()48 }49 spark(center: CGPoint(x: bubble.midX - bubble.width * 0.06, y: bubble.midY), r: bubble.height * 0.3)50 spark(center: CGPoint(x: bubble.midX + bubble.width * 0.26, y: bubble.midY + bubble.height * 0.2), r: bubble.height * 0.12)51 NSGraphicsContext.restoreGraphicsState()52 return rep.representation(using: .png, properties: [:])!53}5455let root = URL(fileURLWithPath: CommandLine.arguments[1])56let mac = root.appendingPathComponent("Apps/LocalChatMac/Assets.xcassets/AppIcon.appiconset")57let ios = root.appendingPathComponent("Apps/LocalChatIOS/Assets.xcassets/AppIcon.appiconset")58try FileManager.default.createDirectory(at: mac, withIntermediateDirectories: true)59try FileManager.default.createDirectory(at: ios, withIntermediateDirectories: true)60var images: [String] = []61for (points, scale) in [(16, 1), (16, 2), (32, 1), (32, 2), (128, 1), (128, 2), (256, 1), (256, 2), (512, 1), (512, 2)] {62 let name = "icon_\(points)x\(points)\(scale == 2 ? "@2x" : "").png"63 try render(points * scale, inset: true).write(to: mac.appendingPathComponent(name))64 images.append("{\"idiom\":\"mac\",\"size\":\"\(points)x\(points)\",\"scale\":\"\(scale)x\",\"filename\":\"\(name)\"}")65}66try "{\"images\":[\(images.joined(separator: ","))],\"info\":{\"version\":1,\"author\":\"xcode\"}}".write(to: mac.appendingPathComponent("Contents.json"), atomically: true, encoding: .utf8)67try render(1024, inset: false).write(to: ios.appendingPathComponent("icon_1024.png"))68try "{\"images\":[{\"idiom\":\"universal\",\"platform\":\"ios\",\"size\":\"1024x1024\",\"filename\":\"icon_1024.png\"}],\"info\":{\"version\":1,\"author\":\"xcode\"}}".write(to: ios.appendingPathComponent("Contents.json"), atomically: true, encoding: .utf8)69print("icons written")70