SPB Git

Repository Public

make-icon.swift 4.7 KB 70 lines swift

1// SP Lecture Studio2// File: scripts/make-icon.swift3// Description: Draws the app icon with Core Graphics (iOS 1024 px opaque + macOS rounded variant). Run: swift scripts/make-icon.swift <output dir>4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import CoreGraphics9import ImageIO10import UniformTypeIdentifiers1112func draw(size: Int, mac: Bool) -> CGImage {13    let s = CGFloat(size)14    let ctx = CGContext(data: nil, width: size, height: size, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!,15                        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!16    // macOS icons sit inside a rounded square with a margin; iOS icons are full-bleed (the system applies the mask).17    let inset: CGFloat = mac ? s * 0.098 : 018    let rect = CGRect(x: inset, y: inset, width: s - inset * 2, height: s - inset * 2)19    let u = rect.width20    if mac { ctx.addPath(CGPath(roundedRect: rect, cornerWidth: u * 0.225, cornerHeight: u * 0.225, transform: nil)); ctx.clip() }21    let colors = [CGColor(red: 0.10, green: 0.16, blue: 0.36, alpha: 1), CGColor(red: 0.16, green: 0.36, blue: 0.86, alpha: 1)] as CFArray22    let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB)!, colors: colors, locations: [0, 1])!23    ctx.drawLinearGradient(gradient, start: CGPoint(x: rect.minX, y: rect.minY), end: CGPoint(x: rect.maxX, y: rect.maxY), options: [])24    // Slide.25    let slide = CGRect(x: rect.minX + u * 0.15, y: rect.minY + u * 0.27, width: u * 0.70, height: u * 0.46)26    ctx.setShadow(offset: CGSize(width: 0, height: -u * 0.012), blur: u * 0.04, color: CGColor(gray: 0, alpha: 0.35))27    ctx.setFillColor(CGColor(gray: 1, alpha: 1))28    ctx.addPath(CGPath(roundedRect: slide, cornerWidth: u * 0.035, cornerHeight: u * 0.035, transform: nil)); ctx.fillPath()29    ctx.setShadow(offset: .zero, blur: 0, color: nil)30    // Title band and text lines.31    ctx.saveGState()32    ctx.addPath(CGPath(roundedRect: slide, cornerWidth: u * 0.035, cornerHeight: u * 0.035, transform: nil)); ctx.clip()33    ctx.setFillColor(CGColor(red: 0.16, green: 0.36, blue: 0.86, alpha: 1))34    ctx.fill(CGRect(x: slide.minX, y: slide.maxY - slide.height * 0.2, width: slide.width, height: slide.height * 0.2))35    ctx.restoreGState()36    ctx.setFillColor(CGColor(gray: 0.78, alpha: 1))37    for (i, w) in [0.50, 0.42, 0.34].enumerated() {38        let y = slide.maxY - slide.height * (0.38 + 0.17 * CGFloat(i))39        ctx.addPath(CGPath(roundedRect: CGRect(x: slide.minX + slide.width * 0.08, y: y, width: slide.width * w, height: slide.height * 0.055), cornerWidth: u * 0.008, cornerHeight: u * 0.008, transform: nil))40        ctx.fillPath()41    }42    // Ink stroke.43    ctx.setStrokeColor(CGColor(red: 0.90, green: 0.16, blue: 0.18, alpha: 1)); ctx.setLineWidth(u * 0.03); ctx.setLineCap(.round); ctx.setLineJoin(.round)44    ctx.move(to: CGPoint(x: slide.minX + slide.width * 0.46, y: slide.minY + slide.height * 0.22))45    ctx.addCurve(to: CGPoint(x: slide.minX + slide.width * 0.92, y: slide.minY + slide.height * 0.52),46                 control1: CGPoint(x: slide.minX + slide.width * 0.62, y: slide.minY + slide.height * 0.62), control2: CGPoint(x: slide.minX + slide.width * 0.76, y: slide.minY + slide.height * 0.08))47    ctx.strokePath()48    // Record dot.49    let dot = CGRect(x: rect.maxX - u * 0.29, y: rect.minY + u * 0.115, width: u * 0.17, height: u * 0.17)50    ctx.setFillColor(CGColor(gray: 1, alpha: 1)); ctx.fillEllipse(in: dot.insetBy(dx: -u * 0.018, dy: -u * 0.018))51    ctx.setFillColor(CGColor(red: 0.90, green: 0.16, blue: 0.18, alpha: 1)); ctx.fillEllipse(in: dot)52    return ctx.makeImage()!53}5455func write(_ image: CGImage, _ url: URL, opaque: Bool) {56    var img = image57    if opaque {      // App Store icons must not have an alpha channel58        let c = CGContext(data: nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!,59                          bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!60        c.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height)); img = c.makeImage()!61    }62    let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)!63    CGImageDestinationAddImage(dest, img, nil); CGImageDestinationFinalize(dest)64}6566let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : ".")67write(draw(size: 1024, mac: false), out.appendingPathComponent("icon-ios-1024.png"), opaque: true)68for size in [16, 32, 64, 128, 256, 512, 1024] { write(draw(size: size, mac: true), out.appendingPathComponent("icon-mac-\(size).png"), opaque: false) }69print("icons written to \(out.path)")70
Files main
Menu

Theme