App Icon Generator — Swift source
Turn one image into the full web app icon set — favicon, apple-touch-icon and manifest sizes as crisp PNGs. Letterboxed contain-fit, optional white background — runs entirely in your browser.
This is the Swift implementation — the same logic the interactive tool runs, in a shareable, citable form.
// App Icon Generator — contain-fit, centered export geometry for the
// 16/32/180/192/512 icon set.
//
// Language: Swift (5.9, standard library only)
// Source: CosmoDev polyglot showcase port, from the authored TypeScript
// snippet src/tool-sources/app-icon-generator/typescript.ts.
/// One export target: the square canvas edge and the centered contain-fit
/// rect the source image is drawn into.
struct IconRect: Equatable, Sendable {
let size: Int // canvas edge (px x px)
let x: Int, y: Int // top-left offset: centers the fitted image
let w: Int, h: Int // fitted image size, at least 1x1
}
enum AppIconGeometry {
static let defaultSizes = [16, 32, 180, 192, 512]
/// The deterministic half of the tool: per export size, the contain-fit
/// rect (scale = min(px/w, px/h), clamped to >= 1) plus the centered draw
/// offset — the geometry the TS pipeline feeds `ctx.drawImage` before
/// `canvas.toBlob` PNG-encodes each square. Bitmap decode and PNG encode
/// stay with the host. `.rounded()` rounds half away from zero, matching
/// JS `Math.round` on these non-negative values.
static func iconSetPlan(srcW: Int, srcH: Int, sizes: [Int]? = nil) -> [IconRect] {
let sizes = sizes ?? defaultSizes
return sizes.map { px in
let scale = min(Double(px) / Double(srcW), Double(px) / Double(srcH))
let w = max(1, (Double(srcW) * scale).rounded())
let h = max(1, (Double(srcH) * scale).rounded())
return IconRect(
size: px,
x: Int(((Double(px) - w) / 2).rounded()), // letterbox on transparency
y: Int(((Double(px) - h) / 2).rounded()),
w: Int(w),
h: Int(h))
}
}
}
Also available in 8 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →