Repository Public License
1// ORIX OS Automation2// File: Scripts/tools/make-icon.swift3// Description: Draws the app icon (graphite squircle, violet-to-blue orbit with three workflow nodes) at every macOS size into the asset catalog.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AppKit89func draw(_ px: Int) -> Data {10 let s = CGFloat(px)11 let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: px, pixelsHigh: px, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!12 rep.size = NSSize(width: s, height: s)13 NSGraphicsContext.saveGraphicsState()14 NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep)15 let ctx = NSGraphicsContext.current!.cgContext16 let inset = s * 0.117 let rect = CGRect(x: inset, y: inset, width: s - 2 * inset, height: s - 2 * inset)18 let path = CGPath(roundedRect: rect, cornerWidth: rect.width * 0.225, cornerHeight: rect.width * 0.225, transform: nil)19 ctx.saveGState()20 ctx.setShadow(offset: CGSize(width: 0, height: -s * 0.012), blur: s * 0.03, color: NSColor.black.withAlphaComponent(0.35).cgColor)21 ctx.addPath(path); ctx.setFillColor(NSColor(srgbRed: 0.07, green: 0.07, blue: 0.09, alpha: 1).cgColor); ctx.fillPath()22 ctx.restoreGState()23 ctx.saveGState(); ctx.addPath(path); ctx.clip()24 let cs = CGColorSpaceCreateDeviceRGB()25 let bg = CGGradient(colorsSpace: cs, colors: [NSColor(srgbRed: 0.13, green: 0.12, blue: 0.19, alpha: 1).cgColor, NSColor(srgbRed: 0.05, green: 0.05, blue: 0.07, alpha: 1).cgColor] as CFArray, locations: [0, 1])!26 ctx.drawLinearGradient(bg, start: CGPoint(x: rect.minX, y: rect.maxY), end: CGPoint(x: rect.maxX, y: rect.minY), options: [])27 let glow = CGGradient(colorsSpace: cs, colors: [NSColor(srgbRed: 0.56, green: 0.42, blue: 1, alpha: 0.45).cgColor, NSColor(srgbRed: 0.56, green: 0.42, blue: 1, alpha: 0).cgColor] as CFArray, locations: [0, 1])!28 ctx.drawRadialGradient(glow, startCenter: CGPoint(x: s * 0.36, y: s * 0.68), startRadius: 0, endCenter: CGPoint(x: s * 0.36, y: s * 0.68), endRadius: s * 0.5, options: [])29 ctx.restoreGState()30 // Orbit ring31 let c = CGPoint(x: s / 2, y: s / 2), r = s * 0.235, lw = s * 0.06232 ctx.saveGState()33 ctx.addArc(center: c, radius: r, startAngle: 0, endAngle: .pi * 2, clockwise: false)34 ctx.setLineWidth(lw); ctx.replacePathWithStrokedPath(); ctx.clip()35 let ring = CGGradient(colorsSpace: cs, colors: [NSColor(srgbRed: 0.62, green: 0.47, blue: 1, alpha: 1).cgColor, NSColor(srgbRed: 0.33, green: 0.55, blue: 1, alpha: 1).cgColor] as CFArray, locations: [0, 1])!36 ctx.drawLinearGradient(ring, start: CGPoint(x: c.x - r, y: c.y + r), end: CGPoint(x: c.x + r, y: c.y - r), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])37 ctx.restoreGState()38 // Three nodes on the orbit, linked to the centre: plan, act, verify39 for (i, a) in [CGFloat.pi * 0.5, .pi * 7 / 6, .pi * 11 / 6].enumerated() {40 let p = CGPoint(x: c.x + cos(a) * r, y: c.y + sin(a) * r)41 ctx.setStrokeColor(NSColor.white.withAlphaComponent(0.55).cgColor); ctx.setLineWidth(s * 0.012)42 ctx.move(to: c); ctx.addLine(to: p); ctx.strokePath()43 let nr = s * (i == 0 ? 0.062 : 0.052)44 ctx.setFillColor(NSColor(srgbRed: 0.07, green: 0.07, blue: 0.09, alpha: 1).cgColor)45 ctx.fillEllipse(in: CGRect(x: p.x - nr * 1.25, y: p.y - nr * 1.25, width: nr * 2.5, height: nr * 2.5))46 ctx.setFillColor(NSColor.white.cgColor)47 ctx.fillEllipse(in: CGRect(x: p.x - nr, y: p.y - nr, width: nr * 2, height: nr * 2))48 }49 ctx.setFillColor(NSColor.white.cgColor)50 let cr = s * 0.0451 ctx.fillEllipse(in: CGRect(x: c.x - cr, y: c.y - cr, width: cr * 2, height: cr * 2))52 NSGraphicsContext.restoreGraphicsState()53 return rep.representation(using: .png, properties: [:])!54}5556let out = URL(fileURLWithPath: CommandLine.arguments[1])57var images: [[String: String]] = []58for (pt, scale) in [(16, 1), (16, 2), (32, 1), (32, 2), (128, 1), (128, 2), (256, 1), (256, 2), (512, 1), (512, 2)] {59 let name = "icon_\(pt)x\(pt)\(scale == 2 ? "@2x" : "").png"60 try draw(pt * scale).write(to: out.appendingPathComponent(name))61 images.append(["idiom": "mac", "size": "\(pt)x\(pt)", "scale": "\(scale)x", "filename": name])62}63let json = try JSONSerialization.data(withJSONObject: ["images": images, "info": ["author": "xcode", "version": 1]], options: [.prettyPrinted, .sortedKeys])64try json.write(to: out.appendingPathComponent("Contents.json"))65print("wrote \(images.count) icon images")66