Skip to content

App Icon Generator — Go 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 Go implementation — the same logic the interactive tool runs, in a shareable, citable form.

// Package appicongenerator is the Go twin of the deterministic half of
// CosmoDev's app-icon-generator island (src/tools/AppIconGenerator.tsx).
// Pure + deterministic, never panics.
//
// Language: Go (standard library only).
// CosmoDev polyglot showcase port of the `app-icon-generator` tool, ported
// from src/tools/AppIconGenerator.tsx.
//
// The island's interactive half — createImageBitmap decode, canvas drawImage,
// canvas.toBlob PNG encode — is browser-bound and stays there; this package
// ports the geometry that decides where the source image lands on every
// square canvas. math.Round rounds half away from zero, matching JS
// Math.round on the all-positive values this geometry produces.
//
// display source — part of CosmoDev's polyglot tool pages.
package appicongenerator

import "math"

// DefaultSizes is the standard size set every web app is asked for (the
// island's SIZES).
var DefaultSizes = []int{16, 32, 180, 192, 512}

// IconRect is one export target: the square canvas edge and the centered
// contain-fit rect the source image is drawn into.
type IconRect struct {
	Size int // canvas edge (px × px) this rect was fitted into
	X, Y int // top-left offset: centers the fitted image
	W, H int // fitted image size, at least 1×1
}

// IconSetPlan is 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 island feeds ctx.drawImage before
// canvas.toBlob PNG-encodes each square. Bitmap decode and PNG encode stay
// with the host. A nil sizes slice means DefaultSizes.
func IconSetPlan(srcW, srcH int, sizes []int) []IconRect {
	if sizes == nil {
		sizes = DefaultSizes
	}
	plan := make([]IconRect, 0, len(sizes))
	for _, px := range sizes {
		scale := math.Min(float64(px)/float64(srcW), float64(px)/float64(srcH))
		w := int(math.Max(1, math.Round(float64(srcW)*scale)))
		h := int(math.Max(1, math.Round(float64(srcH)*scale)))
		plan = append(plan, IconRect{
			Size: px,
			X:    int(math.Round(float64(px-w) / 2)), // letterbox on transparency
			Y:    int(math.Round(float64(px-h) / 2)),
			W:    w,
			H:    h,
		})
	}
	return plan
}

Also available in 13 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 →