App Icon Generator — Rust 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 Rust 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 web icon set.
//!
//! Language: Rust (std only — no external crates).
//! 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 module ports the deterministic geometry that decides where the source
//! image lands on every square canvas. `f64::round` rounds half away from
//! zero, which matches JS `Math.round` on the all-positive values this
//! geometry produces.
//!
//! display source — part of CosmoDev's polyglot tool pages.
/// The standard size set every web app is asked for (the island's SIZES).
pub const DEFAULT_SIZES: [u32; 5] = [16, 32, 180, 192, 512];
/// One export target: the square canvas edge and the centered contain-fit
/// rect the source image is drawn into.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IconRect {
/// Canvas edge (px × px) this rect was fitted into.
pub size: u32,
/// Top-left offset: centers the fitted image.
pub x: i64,
pub y: i64,
/// Fitted image size, at least 1×1.
pub w: u32,
pub h: u32,
}
/// 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.
pub fn icon_set_plan(src_w: u32, src_h: u32, sizes: &[u32]) -> Vec<IconRect> {
sizes
.iter()
.map(|&size| {
let px = f64::from(size);
let scale = f64::min(px / f64::from(src_w), px / f64::from(src_h));
let w = (f64::round(f64::from(src_w) * scale) as u32).max(1);
let h = (f64::round(f64::from(src_h) * scale) as u32).max(1);
IconRect {
size,
x: f64::round((px - f64::from(w)) / 2.0) as i64, // letterbox on transparency
y: f64::round((px - f64::from(h)) / 2.0) as i64,
w,
h,
}
})
.collect()
}
/// Convenience wrapper over the default export set.
pub fn icon_set_plan_default(src_w: u32, src_h: u32) -> Vec<IconRect> {
icon_set_plan(src_w, src_h, &DEFAULT_SIZES)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn contain_fit_is_centered_and_clamped_at_one() {
// Portrait 3x5 into 16px: scale = 16/5 = 3.2, h = 16, w = round(9.6)
// = 10, centered x = round(6/2) = 3.
let portrait = icon_set_plan(3, 5, &[16]);
assert_eq!(
portrait,
vec![IconRect { size: 16, x: 3, y: 0, w: 10, h: 16 }]
);
// 7x5 into 16px lands h on an odd 11, so the letterbox offset is an
// exact .5 — it must round up (3), the way JS Math.round does.
let odd = icon_set_plan(7, 5, &[16]);
assert_eq!(odd[0].w, 16);
assert_eq!(odd[0].h, 11);
assert_eq!(odd[0].y, 3);
// Extreme 1000x1 banner: the thin side rounds to 0 and clamps to 1.
let banner = icon_set_plan(1000, 1, &[16]);
assert_eq!(banner[0], IconRect { size: 16, x: 0, y: 8, w: 16, h: 1 });
// Full default set keeps one rect per export size.
assert_eq!(icon_set_plan_default(64, 64).len(), DEFAULT_SIZES.len());
}
}
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 →