| 1 | // Draws Base's app icon, ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png: |
| 2 | // Base's mark (the wordmark's rounded square, --mark-1 into --mark-2, with |
| 3 | // its bright edge) on the dark sky (--sky-1 to --sky-3 at 165°), lit as the |
| 4 | // web is: the warm beam from above and the teal and violet fog below. A |
| 5 | // square 1024 px master; bundle-mac.py derives the Mac's padded icon. |
| 6 | // |
| 7 | // swift native/scripts/make-icon.swift native/ios/Assets.xcassets/AppIcon.appiconset/AppIcon.png |
| 8 | import AppKit |
| 9 | |
| 10 | let size: CGFloat = 1024 |
| 11 | let out = URL(fileURLWithPath: CommandLine.arguments[1]) |
| 12 | func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1) -> CGColor { |
| 13 | CGColor(srgbRed: r / 255, green: g / 255, blue: b / 255, alpha: a) |
| 14 | } |
| 15 | let space = CGColorSpace(name: CGColorSpace.sRGB)! |
| 16 | guard let ctx = CGContext(data: nil, width: Int(size), height: Int(size), bitsPerComponent: 8, bytesPerRow: 0, |
| 17 | space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { fatalError("no context") } |
| 18 | // CoreGraphics' origin is the bottom left: y runs up. |
| 19 | // The sky: 165° (towards the bottom, a little left), --sky-1 0%, --sky-2 62%, --sky-3 100%. |
| 20 | let sky = CGGradient(colorsSpace: space, colors: [rgba(44, 52, 112), rgba(26, 31, 69), rgba(22, 25, 58)] as CFArray, locations: [0, 0.62, 1])! |
| 21 | let angle = 165.0 * Double.pi / 180 |
| 22 | let dx = CGFloat(sin(angle)), dy = CGFloat(-cos(angle)) |
| 23 | let half = (abs(dx) + abs(dy)) * size / 2 |
| 24 | let mid = CGPoint(x: size / 2, y: size / 2) |
| 25 | ctx.drawLinearGradient(sky, start: CGPoint(x: mid.x - dx * half, y: mid.y + dy * half), |
| 26 | end: CGPoint(x: mid.x + dx * half, y: mid.y - dy * half), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]) |
| 27 | // Fog: soft radial glows of --lagoon and --orchid low down, and the warm beam from above. |
| 28 | func glow(_ color: CGColor, at center: CGPoint, radius: CGFloat) { |
| 29 | let g = CGGradient(colorsSpace: space, colors: [color, color.copy(alpha: 0)!] as CFArray, locations: [0, 1])! |
| 30 | ctx.drawRadialGradient(g, startCenter: center, startRadius: 0, endCenter: center, endRadius: radius, options: []) |
| 31 | } |
| 32 | glow(rgba(47, 198, 192, 0.42), at: CGPoint(x: size * 0.18, y: size * 0.12), radius: size * 0.62) |
| 33 | glow(rgba(139, 92, 246, 0.42), at: CGPoint(x: size * 0.86, y: size * 0.22), radius: size * 0.6) |
| 34 | glow(rgba(244, 114, 182, 0.18), at: CGPoint(x: size * 0.6, y: size * 0.02), radius: size * 0.36) |
| 35 | glow(rgba(255, 214, 170, 0.30), at: CGPoint(x: size * 0.42, y: size * 1.02), radius: size * 0.62) |
| 36 | // The mark: a rounded square (16 px with 5 px corners on the web), 40% of the icon. |
| 37 | let side = size * 0.40 |
| 38 | let rect = CGRect(x: (size - side) / 2, y: (size - side) / 2, width: side, height: side) |
| 39 | let path = CGPath(roundedRect: rect, cornerWidth: side * 5 / 16, cornerHeight: side * 5 / 16, transform: nil) |
| 40 | ctx.saveGState() |
| 41 | ctx.setShadow(offset: CGSize(width: 0, height: -side * 0.06), blur: side * 0.22, color: rgba(8, 12, 22, 0.55)) |
| 42 | ctx.addPath(path); ctx.setFillColor(rgba(238, 242, 248, 0.5)); ctx.fillPath() |
| 43 | ctx.restoreGState() |
| 44 | ctx.saveGState() |
| 45 | ctx.addPath(path); ctx.clip() |
| 46 | // 160°: from the top, a little left, to the bottom: #FFFFFF into rgba(238,242,248,.35) over the sky. |
| 47 | let markAngle = 160.0 * Double.pi / 180 |
| 48 | let mdx = CGFloat(sin(markAngle)), mdy = CGFloat(-cos(markAngle)) |
| 49 | let mhalf = (abs(mdx) + abs(mdy)) * side / 2 |
| 50 | let mc = CGPoint(x: rect.midX, y: rect.midY) |
| 51 | let mark = CGGradient(colorsSpace: space, colors: [rgba(255, 255, 255), rgba(238, 242, 248, 0.35)] as CFArray, locations: [0, 1])! |
| 52 | ctx.drawLinearGradient(mark, start: CGPoint(x: mc.x - mdx * mhalf, y: mc.y + mdy * mhalf), |
| 53 | end: CGPoint(x: mc.x + mdx * mhalf, y: mc.y - mdy * mhalf), options: []) |
| 54 | // Its edge: inset 0 0 0 1px rgba(255,255,255,.5), scaled. |
| 55 | ctx.addPath(path); ctx.setStrokeColor(rgba(255, 255, 255, 0.5)); ctx.setLineWidth(side / 24); ctx.strokePath() |
| 56 | ctx.restoreGState() |
| 57 | guard let image = ctx.makeImage(), let png = NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]) else { fatalError("no png") } |
| 58 | try png.write(to: out) |
| 59 | |