Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Scripts/make-icon.swift3// Description: Draws the VQuant app icon (the sparkle mark of the web app, same path and gradient) at every macOS size into the asset catalogue.4// Usage: swift Scripts/make-icon.swift Resources/Assets.xcassets/AppIcon.appiconset5import AppKit6import CoreGraphics78// shared/vqLogo.ts of the backend repository (viewBox 0 0 64 64)9let main = "M27 9 C28.9 23.6 33.4 30.1 51 35 C33.4 39.9 28.9 46.4 27 61 C25.1 46.4 20.6 39.9 3 35 C20.6 30.1 25.1 23.6 27 9 Z"10let small = "M50 3 C50.8 9.1 52.9 11.9 60 14 C52.9 16.1 50.8 18.9 50 25 C49.2 18.9 47.1 16.1 40 14 C47.1 11.9 49.2 9.1 50 3 Z"1112func path(_ d: String) -> CGPath {13 let p = CGMutablePath()14 // "M27 9 C28.9 …": the command letter is glued to its first number15 var spaced = ""16 for ch in d { if ch.isLetter { spaced += " \(ch) " } else { spaced.append(ch) } }17 let tokens = spaced.split(separator: " ").map(String.init)18 var i = 019 func num() -> CGFloat { defer { i += 1 }; return CGFloat(Double(tokens[i])!) }20 while i < tokens.count {21 let t = tokens[i]; i += 122 switch t {23 case "M": let x = num(), y = num(); p.move(to: CGPoint(x: x, y: y))24 case "C": let a = num(), b = num(), c = num(), d = num(), e = num(), f = num(); p.addCurve(to: CGPoint(x: e, y: f), control1: CGPoint(x: a, y: b), control2: CGPoint(x: c, y: d))25 case "Z": p.closeSubpath()26 default: fatalError("unexpected token \(t)")27 }28 }29 return p30}3132func render(_ px: Int) -> Data {33 let cs = CGColorSpace(name: CGColorSpace.sRGB)!34 let ctx = CGContext(data: nil, width: px, height: px, bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!35 let s = CGFloat(px)36 // macOS icon grid: the tile occupies 824/1024 of the canvas, corner radius 185.4/102437 let inset = s * 100 / 1024, side = s - 2 * inset38 let tile = CGPath(roundedRect: CGRect(x: inset, y: inset, width: side, height: side), cornerWidth: s * 185.4 / 1024, cornerHeight: s * 185.4 / 1024, transform: nil)39 ctx.saveGState(); ctx.addPath(tile); ctx.clip()40 let bg = CGGradient(colorsSpace: cs, colors: [CGColor(red: 0.075, green: 0.078, blue: 0.106, alpha: 1), CGColor(red: 0.027, green: 0.027, blue: 0.043, alpha: 1)] as CFArray, locations: [0, 1])!41 ctx.drawLinearGradient(bg, start: CGPoint(x: 0, y: s), end: CGPoint(x: 0, y: 0), options: [])42 // the mark: 64-unit artboard centred in the tile, flipped (SVG y grows downward)43 let scale = side * 0.66 / 6444 ctx.translateBy(x: inset + side * 0.17, y: inset + side * 0.83)45 ctx.scaleBy(x: scale, y: -scale)46 ctx.addPath(path(main)); ctx.addPath(path(small)); ctx.clip()47 let g = CGGradient(colorsSpace: cs, colors: [CGColor(red: 1, green: 0.541, blue: 0.165, alpha: 1), CGColor(red: 1, green: 0.239, blue: 0.431, alpha: 1), CGColor(red: 0.659, green: 0.333, blue: 0.969, alpha: 1)] as CFArray, locations: [0, 0.55, 1])!48 ctx.drawLinearGradient(g, start: CGPoint(x: 6, y: 8), end: CGPoint(x: 58, y: 58), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])49 ctx.restoreGState()50 return NSBitmapImageRep(cgImage: ctx.makeImage()!).representation(using: .png, properties: [:])!51}5253let out = URL(fileURLWithPath: CommandLine.arguments[1])54var images: [String] = []55for (pt, scales) in [(16, [1, 2]), (32, [1, 2]), (128, [1, 2]), (256, [1, 2]), (512, [1, 2])] {56 for sc in scales {57 let name = "icon_\(pt)x\(pt)\(sc == 2 ? "@2x" : "").png"58 try! render(pt * sc).write(to: out.appendingPathComponent(name))59 images.append(#"{ "idiom" : "mac", "size" : "\#(pt)x\#(pt)", "scale" : "\#(sc)x", "filename" : "\#(name)" }"#)60 }61}62try! Data(("{ \"images\" : [\n " + images.joined(separator: ",\n ") + "\n], \"info\" : { \"author\" : \"xcode\", \"version\" : 1 } }\n").utf8).write(to: out.appendingPathComponent("Contents.json"))63print("icon written: \(images.count) images")64