#!/usr/bin/env swift // Renders the tvOS brand assets (layered app icon + top shelf images). // Run: swift tools/make-assets.swift (from the repo root) import AppKit import CoreGraphics import CoreText import Foundation let root = FileManager.default.currentDirectoryPath let catalog = "\(root)/Resources/Assets.xcassets" func makeContext(_ width: Int, _ height: Int) -> CGContext { let ctx = CGContext( data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue )! ctx.setAllowsAntialiasing(true) ctx.interpolationQuality = .high return ctx } func write(_ ctx: CGContext, to path: String) { let image = ctx.makeImage()! let rep = NSBitmapImageRep(cgImage: image) let data = rep.representation(using: .png, properties: [:])! try! FileManager.default.createDirectory( atPath: (path as NSString).deletingLastPathComponent, withIntermediateDirectories: true ) try! data.write(to: URL(fileURLWithPath: path)) print("wrote \(path)") } func drawText( _ ctx: CGContext, _ text: String, size: CGFloat, color: NSColor, center: CGPoint, weight: NSFont.Weight = .heavy ) { let font = NSFont.systemFont(ofSize: size, weight: weight) let attrs: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color] let line = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: attrs)) let bounds = CTLineGetBoundsWithOptions(line, .useOpticalBounds) ctx.textPosition = CGPoint(x: center.x - bounds.width / 2 - bounds.origin.x, y: center.y - bounds.height / 2 - bounds.origin.y) CTLineDraw(line, ctx) } let ink = NSColor(srgbRed: 0.06, green: 0.06, blue: 0.08, alpha: 1) let accent = NSColor(srgbRed: 1.0, green: 0.87, blue: 0.16, alpha: 1) /// Back: flat dark field with a soft vignette. func drawBack(_ ctx: CGContext, w: Int, h: Int) { ctx.setFillColor(ink.cgColor) ctx.fill(CGRect(x: 0, y: 0, width: w, height: h)) let colors = [ NSColor(srgbRed: 0.16, green: 0.13, blue: 0.20, alpha: 1).cgColor, NSColor(srgbRed: 0.05, green: 0.05, blue: 0.07, alpha: 1).cgColor, ] as CFArray let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB)!, colors: colors, locations: [0, 1])! ctx.drawRadialGradient( gradient, startCenter: CGPoint(x: Double(w) * 0.3, y: Double(h) * 0.75), startRadius: 0, endCenter: CGPoint(x: Double(w) * 0.5, y: Double(h) * 0.5), endRadius: Double(max(w, h)) * 0.8, options: [] ) } /// Middle: the accent disc that gets the parallax lift. func drawMiddle(_ ctx: CGContext, w: Int, h: Int) { ctx.clear(CGRect(x: 0, y: 0, width: w, height: h)) let d = Double(h) * 0.62 let rect = CGRect(x: Double(w) / 2 - d / 2, y: Double(h) / 2 - d / 2, width: d, height: d) ctx.setFillColor(accent.withAlphaComponent(0.18).cgColor) ctx.fillEllipse(in: rect.insetBy(dx: -d * 0.12, dy: -d * 0.12)) ctx.setFillColor(accent.cgColor) ctx.fillEllipse(in: rect) } /// Front: the wordmark that floats on top. func drawFront(_ ctx: CGContext, w: Int, h: Int) { ctx.clear(CGRect(x: 0, y: 0, width: w, height: h)) drawText(ctx, "\u{042F}", size: Double(h) * 0.42, color: ink, center: CGPoint(x: Double(w) / 2, y: Double(h) * 0.53)) } let iconSizes: [(String, Int, Int)] = [ ("App Icon.imagestack", 400, 240), ("App Icon - App Store.imagestack", 1280, 768), ] let layers: [(String, (CGContext, Int, Int) -> Void)] = [ ("Back", drawBack), ("Middle", drawMiddle), ("Front", drawFront), ] func imagesetContents(_ entries: [(String, String)]) -> String { let images = entries.map { """ { "idiom" : "tv", "filename" : "\($0.1)", "scale" : "\($0.0)" } """ }.joined(separator: ",\n") return """ { "images" : [ \(images) ], "info" : { "author" : "xcode", "version" : 1 } } """ } func writeJSON(_ text: String, to path: String) { try! FileManager.default.createDirectory( atPath: (path as NSString).deletingLastPathComponent, withIntermediateDirectories: true ) try! text.write(toFile: path, atomically: true, encoding: .utf8) } let brand = "\(catalog)/App Icon & Top Shelf Image.brandassets" for (stack, w, h) in iconSizes { let isStore = stack.contains("App Store") for (layer, draw) in layers { let layerDir = "\(brand)/\(stack)/\(layer).imagestacklayer" writeJSON(""" { "info" : { "author" : "xcode", "version" : 1 } } """, to: "\(layerDir)/Contents.json") var entries: [(String, String)] = [] let scales = isStore ? [1] : [1, 2] for scale in scales { let name = "\(layer.lowercased())@\(scale)x.png" let ctx = makeContext(w * scale, h * scale) draw(ctx, w * scale, h * scale) write(ctx, to: "\(layerDir)/Content.imageset/\(name)") entries.append(("\(scale)x", name)) } writeJSON(imagesetContents(entries), to: "\(layerDir)/Content.imageset/Contents.json") } writeJSON(""" { "layers" : [ { "filename" : "Front.imagestacklayer" }, { "filename" : "Middle.imagestacklayer" }, { "filename" : "Back.imagestacklayer" } ], "info" : { "author" : "xcode", "version" : 1 } } """, to: "\(brand)/\(stack)/Contents.json") } /// Top shelf art: the same language, stretched wide. func drawShelf(_ ctx: CGContext, w: Int, h: Int) { drawBack(ctx, w: w, h: h) let d = Double(h) * 0.5 let cx = Double(w) * 0.34 let rect = CGRect(x: cx - d / 2, y: Double(h) / 2 - d / 2, width: d, height: d) ctx.setFillColor(accent.cgColor) ctx.fillEllipse(in: rect) drawText(ctx, "\u{042F}", size: d * 0.62, color: ink, center: CGPoint(x: cx, y: Double(h) * 0.515)) drawText(ctx, "YaMusek", size: Double(h) * 0.19, color: .white, center: CGPoint(x: cx + d * 0.72 + Double(h) * 0.42, y: Double(h) * 0.5), weight: .bold) } let shelves: [(String, Int, Int)] = [ ("Top Shelf Image", 1920, 720), ("Top Shelf Image Wide", 2320, 720), ] for (name, w, h) in shelves { let dir = "\(brand)/\(name).imageset" var entries: [(String, String)] = [] for scale in [1, 2] { let file = "\(name.replacingOccurrences(of: " ", with: "-").lowercased())@\(scale)x.png" let ctx = makeContext(w * scale, h * scale) drawShelf(ctx, w: w * scale, h: h * scale) write(ctx, to: "\(dir)/\(file)") entries.append(("\(scale)x", file)) } writeJSON(imagesetContents(entries), to: "\(dir)/Contents.json") } writeJSON(""" { "assets" : [ { "filename" : "App Icon - App Store.imagestack", "idiom" : "tv", "role" : "primary-app-icon", "size" : "1280x768" }, { "filename" : "App Icon.imagestack", "idiom" : "tv", "role" : "primary-app-icon", "size" : "400x240" }, { "filename" : "Top Shelf Image Wide.imageset", "idiom" : "tv", "role" : "top-shelf-image-wide", "size" : "2320x720" }, { "filename" : "Top Shelf Image.imageset", "idiom" : "tv", "role" : "top-shelf-image", "size" : "1920x720" } ], "info" : { "author" : "xcode", "version" : 1 } } """, to: "\(brand)/Contents.json") writeJSON(""" { "info" : { "author" : "xcode", "version" : 1 } } """, to: "\(catalog)/Contents.json") /// Launch image shown while the app boots. let launchDir = "\(catalog)/LaunchImage.imageset" var launchEntries: [(String, String)] = [] for scale in [1, 2] { let ctx = makeContext(1920 * scale, 1080 * scale) drawBack(ctx, w: 1920 * scale, h: 1080 * scale) let d = Double(1080 * scale) * 0.22 let rect = CGRect(x: Double(1920 * scale) / 2 - d / 2, y: Double(1080 * scale) / 2 - d / 2, width: d, height: d) ctx.setFillColor(accent.cgColor) ctx.fillEllipse(in: rect) drawText(ctx, "\u{042F}", size: d * 0.6, color: ink, center: CGPoint(x: Double(1920 * scale) / 2, y: Double(1080 * scale) * 0.503)) let file = "launch@\(scale)x.png" write(ctx, to: "\(launchDir)/\(file)") launchEntries.append(("\(scale)x", file)) } writeJSON(imagesetContents(launchEntries), to: "\(launchDir)/Contents.json") print("assets done")