SPB Git

Repository Public

icon.swift 5.5 KB 93 lines swift

1// ORIX Sonata2// File: Scripts/icon.swift3// Description: Draws the ORIX Sonata icon (a five-line staff that becomes a travelling wave) with4// Core Graphics and writes every size of the macOS app icon set. Original artwork, no external asset.5// Usage: swift Scripts/icon.swift Apps/ORIXSonata/Resources/Assets.xcassets/AppIcon.appiconset6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AppKit10import CoreGraphics1112func draw(size: Int) -> Data {13    let s = CGFloat(size)14    let space = CGColorSpace(name: CGColorSpace.sRGB)!15    let ctx = CGContext(data: nil, width: size, height: size, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!16    // macOS icon grid: the plate occupies 824/1024 of the canvas.17    let inset = s * 100 / 102418    let plate = CGRect(x: inset, y: inset, width: s - 2 * inset, height: s - 2 * inset)19    let radius = plate.width * 0.223720    let path = CGPath(roundedRect: plate, cornerWidth: radius, cornerHeight: radius, transform: nil)21    ctx.saveGState()22    ctx.setShadow(offset: CGSize(width: 0, height: -s * 0.012), blur: s * 0.03, color: CGColor(red: 0, green: 0, blue: 0, alpha: 0.35))23    ctx.addPath(path)24    ctx.setFillColor(CGColor(red: 0.07, green: 0.06, blue: 0.16, alpha: 1))25    ctx.fillPath()26    ctx.restoreGState()27    ctx.saveGState()28    ctx.addPath(path)29    ctx.clip()30    let bg = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.10, green: 0.08, blue: 0.26, alpha: 1), CGColor(red: 0.04, green: 0.04, blue: 0.11, alpha: 1)] as CFArray, locations: [0, 1])!31    ctx.drawLinearGradient(bg, start: CGPoint(x: plate.minX, y: plate.maxY), end: CGPoint(x: plate.maxX, y: plate.minY), options: [])32    let glow = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.45, green: 0.30, blue: 0.95, alpha: 0.55), CGColor(red: 0.45, green: 0.30, blue: 0.95, alpha: 0)] as CFArray, locations: [0, 1])!33    ctx.drawRadialGradient(glow, startCenter: CGPoint(x: plate.midX + plate.width * 0.18, y: plate.midY), startRadius: 0, endCenter: CGPoint(x: plate.midX + plate.width * 0.18, y: plate.midY), endRadius: plate.width * 0.62, options: [])3435    // Five lines: straight staff on the left, each bending into a wave whose amplitude grows to the right.36    let left = plate.minX + plate.width * 0.1337    let right = plate.maxX - plate.width * 0.1338    let gap = plate.height * 0.08539    let colors: [(CGFloat, CGFloat, CGFloat)] = [(0.42, 0.86, 1.00), (0.50, 0.70, 1.00), (0.62, 0.55, 1.00), (0.78, 0.48, 1.00), (0.98, 0.50, 0.86)]40    for line in 0..<5 {41        let baseY = plate.midY + (CGFloat(line) - 2) * gap42        let p = CGMutablePath()43        let steps = 40044        for i in 0...steps {45            let t = CGFloat(i) / CGFloat(steps)46            let x = left + (right - left) * t47            // Smooth onset after the first third; phase shifts per line so the five lines braid.48            let onset = max(0, (t - 0.30) / 0.70)49            let env = onset * onset * (3 - 2 * onset)50            let amp = plate.height * 0.155 * env * (1 - 0.35 * abs(CGFloat(line) - 2) / 2)51            let phase = CGFloat(line) * 0.4252            let y = baseY + amp * sin(2 * .pi * 2.1 * onset + phase) - (CGFloat(line) - 2) * gap * 0.30 * env53            if i == 0 { p.move(to: CGPoint(x: x, y: y)) } else { p.addLine(to: CGPoint(x: x, y: y)) }54        }55        let c = colors[line]56        ctx.addPath(p)57        ctx.setLineCap(.round)58        ctx.setLineJoin(.round)59        ctx.setLineWidth(max(1, plate.width * 0.022))60        ctx.replacePathWithStrokedPath()61        ctx.clip()62        let g = CGGradient(colorsSpace: space, colors: [CGColor(red: 0.86, green: 0.88, blue: 1, alpha: 0.80), CGColor(red: c.0, green: c.1, blue: c.2, alpha: 1)] as CFArray, locations: [0.25, 0.75])!63        ctx.drawLinearGradient(g, start: CGPoint(x: left, y: 0), end: CGPoint(x: right, y: 0), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])64        ctx.resetClip()65        ctx.addPath(path)66        ctx.clip()67    }68    // A note head sitting on the staff where the wave begins.69    let head = CGRect(x: left + (right - left) * 0.24, y: plate.midY - gap * 0.62, width: gap * 1.55, height: gap * 1.2)70    ctx.saveGState()71    ctx.translateBy(x: head.midX, y: head.midY)72    ctx.rotate(by: 0.38)73    ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 0.96))74    ctx.fillEllipse(in: CGRect(x: -head.width / 2, y: -head.height / 2, width: head.width, height: head.height))75    ctx.restoreGState()76    ctx.restoreGState()77    let image = ctx.makeImage()!78    let rep = NSBitmapImageRep(cgImage: image)79    return rep.representation(using: .png, properties: [:])!80}8182let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : ".")83try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)84var images: [[String: String]] = []85for (points, scale) in [(16, 1), (16, 2), (32, 1), (32, 2), (128, 1), (128, 2), (256, 1), (256, 2), (512, 1), (512, 2)] {86    let name = "icon_\(points)x\(points)\(scale == 2 ? "@2x" : "").png"87    try draw(size: points * scale).write(to: out.appendingPathComponent(name))88    images.append(["idiom": "mac", "size": "\(points)x\(points)", "scale": "\(scale)x", "filename": name])89}90let contents: [String: Any] = ["images": images, "info": ["author": "xcode", "version": 1]]91try JSONSerialization.data(withJSONObject: contents, options: [.prettyPrinted, .sortedKeys]).write(to: out.appendingPathComponent("Contents.json"))92print("wrote \(images.count) icon images to \(out.path)")93
Files main
Menu

Theme