~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
156 lines · 8.2 KB Raw History
#!/usr/bin/env swift
import AppKit
import CoreGraphics
import Foundation
import ImageIO
import UniformTypeIdentifiers

let canvasSize = 1024
let lightBackground = CGColor(srgbRed: 0.66, green: 0.25, blue: 0.15, alpha: 1)
let cream = CGColor(srgbRed: 0.98, green: 0.89, blue: 0.73, alpha: 1)
let lightCream = CGColor(srgbRed: 0.96, green: 0.80, blue: 0.59, alpha: 1)
let white = CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
let black = CGColor(srgbRed: 0, green: 0, blue: 0, alpha: 1)
let devBackground = CGColor(srgbRed: 0.12, green: 0.16, blue: 0.22, alpha: 1)
let safetyYellow = CGColor(srgbRed: 1, green: 0.76, blue: 0.23, alpha: 1)

func cowboyHat() -> CGPath {
    // A creased crown and an upturned brim form a cowboy hat.
    let hat = CGMutablePath()
    hat.move(to: CGPoint(x: 350, y: 558))
    hat.addCurve(to: CGPoint(x: 371, y: 365), control1: CGPoint(x: 350, y: 490), control2: CGPoint(x: 349, y: 393))
    hat.addCurve(to: CGPoint(x: 466, y: 379), control1: CGPoint(x: 393, y: 338), control2: CGPoint(x: 425, y: 359))
    hat.addCurve(to: CGPoint(x: 512, y: 391), control1: CGPoint(x: 484, y: 388), control2: CGPoint(x: 498, y: 391))
    hat.addCurve(to: CGPoint(x: 558, y: 379), control1: CGPoint(x: 526, y: 391), control2: CGPoint(x: 540, y: 388))
    hat.addCurve(to: CGPoint(x: 653, y: 365), control1: CGPoint(x: 599, y: 359), control2: CGPoint(x: 631, y: 338))
    hat.addCurve(to: CGPoint(x: 674, y: 558), control1: CGPoint(x: 675, y: 393), control2: CGPoint(x: 674, y: 490))
    hat.addCurve(to: CGPoint(x: 796, y: 510), control1: CGPoint(x: 738, y: 561), control2: CGPoint(x: 780, y: 540))
    hat.addCurve(to: CGPoint(x: 753, y: 632), control1: CGPoint(x: 813, y: 492), control2: CGPoint(x: 817, y: 584))
    hat.addCurve(to: CGPoint(x: 512, y: 680), control1: CGPoint(x: 700, y: 670), control2: CGPoint(x: 598, y: 680))
    hat.addCurve(to: CGPoint(x: 271, y: 632), control1: CGPoint(x: 426, y: 680), control2: CGPoint(x: 324, y: 670))
    hat.addCurve(to: CGPoint(x: 228, y: 510), control1: CGPoint(x: 207, y: 584), control2: CGPoint(x: 211, y: 492))
    hat.addCurve(to: CGPoint(x: 350, y: 558), control1: CGPoint(x: 244, y: 540), control2: CGPoint(x: 286, y: 561))
    hat.closeSubpath()
    return hat
}

func hardHat() -> CGPath {
    let hat = CGMutablePath()
    hat.move(to: CGPoint(x: 232, y: 565))
    hat.addQuadCurve(to: CGPoint(x: 260, y: 537), control: CGPoint(x: 232, y: 537))
    hat.addLine(to: CGPoint(x: 278, y: 537))
    hat.addCurve(to: CGPoint(x: 473, y: 354), control1: CGPoint(x: 289, y: 447), control2: CGPoint(x: 367, y: 370))
    hat.addLine(to: CGPoint(x: 473, y: 344))
    hat.addQuadCurve(to: CGPoint(x: 494, y: 323), control: CGPoint(x: 473, y: 323))
    hat.addLine(to: CGPoint(x: 530, y: 323))
    hat.addQuadCurve(to: CGPoint(x: 551, y: 344), control: CGPoint(x: 551, y: 323))
    hat.addLine(to: CGPoint(x: 551, y: 354))
    hat.addCurve(to: CGPoint(x: 746, y: 537), control1: CGPoint(x: 657, y: 370), control2: CGPoint(x: 735, y: 447))
    hat.addLine(to: CGPoint(x: 764, y: 537))
    hat.addQuadCurve(to: CGPoint(x: 792, y: 565), control: CGPoint(x: 792, y: 537))
    hat.addLine(to: CGPoint(x: 792, y: 611))
    hat.addQuadCurve(to: CGPoint(x: 769, y: 638), control: CGPoint(x: 792, y: 632))
    hat.addCurve(to: CGPoint(x: 255, y: 638), control1: CGPoint(x: 637, y: 666), control2: CGPoint(x: 387, y: 666))
    hat.addQuadCurve(to: CGPoint(x: 232, y: 611), control: CGPoint(x: 232, y: 632))
    hat.closeSubpath()

    // Cut out the grooves by the center ridge and above the brim.
    hat.addRoundedRect(in: CGRect(x: 451, y: 394, width: 24, height: 109), cornerWidth: 12, cornerHeight: 12)
    hat.addRoundedRect(in: CGRect(x: 549, y: 394, width: 24, height: 109), cornerWidth: 12, cornerHeight: 12)
    hat.addRoundedRect(in: CGRect(x: 285, y: 561, width: 454, height: 24), cornerWidth: 12, cornerHeight: 12)
    return hat
}

func makeArtwork(
    mark: CGPath, background: CGColor?, markColor: CGColor, markWidthFraction: CGFloat? = nil
) throws -> CGImage {
    let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
    let alphaInfo: CGImageAlphaInfo = background == nil ? .premultipliedLast : .noneSkipLast
    let bitmapInfo = CGBitmapInfo.byteOrder32Big.union(CGBitmapInfo(rawValue: alphaInfo.rawValue))
    guard
        let context = CGContext(
            data: nil, width: canvasSize, height: canvasSize, bitsPerComponent: 8,
            bytesPerRow: canvasSize * 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
    else {
        throw NSError(
            domain: "OldFellaIcon", code: 1, userInfo: [NSLocalizedDescriptionKey: "Could not create the image canvas."]
        )
    }
    context.translateBy(x: 0, y: CGFloat(canvasSize))
    context.scaleBy(x: 1, y: -1)
    if let background {
        context.setFillColor(background)
        context.fill(CGRect(x: 0, y: 0, width: canvasSize, height: canvasSize))
    }
    if let markWidthFraction {
        let bounds = mark.boundingBoxOfPath
        let scale = CGFloat(canvasSize) * markWidthFraction / bounds.width
        context.translateBy(x: CGFloat(canvasSize) / 2, y: CGFloat(canvasSize) / 2)
        context.scaleBy(x: scale, y: scale)
        context.translateBy(x: -bounds.midX, y: -bounds.midY)
    }
    context.setFillColor(markColor)
    context.addPath(mark)
    context.fillPath(using: .evenOdd)

    guard let image = context.makeImage() else {
        throw NSError(
            domain: "OldFellaIcon", code: 2, userInfo: [NSLocalizedDescriptionKey: "Could not create the icon image."])
    }
    return image
}

func writePNG(_ image: CGImage, to url: URL) throws {
    guard let destination = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)
    else {
        throw NSError(
            domain: "OldFellaIcon", code: 3,
            userInfo: [NSLocalizedDescriptionKey: "Could not create PNG output at \(url.path)."])
    }
    CGImageDestinationAddImage(destination, image, nil)
    guard CGImageDestinationFinalize(destination) else {
        throw NSError(
            domain: "OldFellaIcon", code: 4,
            userInfo: [NSLocalizedDescriptionKey: "Could not write PNG output at \(url.path)."])
    }
}

let scriptURL = URL(fileURLWithPath: #filePath)
let repositoryRoot = scriptURL.deletingLastPathComponent().deletingLastPathComponent()
let catalogURL = repositoryRoot.appendingPathComponent("Sources/OldFella/Assets.xcassets", isDirectory: true)
let appIconURL = catalogURL.appendingPathComponent("AppIcon.appiconset", isDirectory: true)
let devIconURL = catalogURL.appendingPathComponent("AppIconDev.appiconset", isDirectory: true)
let brandMarkURL = catalogURL.appendingPathComponent("BrandMark.imageset", isDirectory: true)

do {
    let cowboy = cowboyHat()
    let helmet = hardHat()
    try writePNG(
        makeArtwork(mark: cowboy, background: lightBackground, markColor: cream, markWidthFraction: 0.8),
        to: appIconURL.appendingPathComponent("AppIcon.png"))
    try writePNG(
        makeArtwork(mark: cowboy, background: nil, markColor: lightCream, markWidthFraction: 0.8),
        to: appIconURL.appendingPathComponent("AppIcon-Dark.png"))
    try writePNG(
        makeArtwork(mark: cowboy, background: black, markColor: white, markWidthFraction: 0.8),
        to: appIconURL.appendingPathComponent("AppIcon-Tinted.png"))
    try writePNG(
        makeArtwork(mark: cowboy, background: nil, markColor: lightBackground),
        to: brandMarkURL.appendingPathComponent("BrandMark.png"))
    try writePNG(
        makeArtwork(mark: cowboy, background: nil, markColor: lightCream),
        to: brandMarkURL.appendingPathComponent("BrandMark-Dark.png"))
    try writePNG(
        makeArtwork(mark: helmet, background: devBackground, markColor: safetyYellow, markWidthFraction: 0.78),
        to: devIconURL.appendingPathComponent("AppIconDev.png"))
    try writePNG(
        makeArtwork(mark: helmet, background: nil, markColor: safetyYellow, markWidthFraction: 0.78),
        to: devIconURL.appendingPathComponent("AppIconDev-Dark.png"))
    try writePNG(
        makeArtwork(mark: helmet, background: black, markColor: white, markWidthFraction: 0.78),
        to: devIconURL.appendingPathComponent("AppIconDev-Tinted.png"))
    print("Wrote icon and brand mark images to \(catalogURL.path)")
} catch {
    fputs("Icon generation failed: \(error.localizedDescription)\n", stderr)
    exit(1)
}