App Icon Generator — Kotlin 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 Kotlin 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: Kotlin (1.9, standard library only)
// Source: CosmoDev polyglot showcase port, from the authored TypeScript
// snippet src/tool-sources/app-icon-generator/typescript.ts.
import kotlin.math.roundToInt
/** One export target: the square canvas edge and the centered contain-fit
* rect the source image is drawn into. */
data class IconRect(val size: Int, val x: Int, val y: Int, val w: Int, val h: Int)
object AppIconGeometry {
val defaultSizes: IntArray = intArrayOf(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. roundToInt rounds ties toward positive infinity,
* matching JS Math.round on these non-negative values. */
fun iconSetPlan(srcW: Int, srcH: Int, sizes: IntArray = defaultSizes): List<IconRect> =
sizes.map { px ->
val scale = minOf(px.toDouble() / srcW, px.toDouble() / srcH)
val w = (srcW * scale).roundToInt().coerceAtLeast(1)
val h = (srcH * scale).roundToInt().coerceAtLeast(1)
IconRect(
px,
((px - w) / 2.0).roundToInt(), // letterbox on transparency
((px - h) / 2.0).roundToInt(),
w,
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 →