Skip to content

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

# frozen_string_literal: true

# App Icon Generator — contain-fit, centered export geometry for the
# 16/32/180/192/512 icon set.
#
# Language: Ruby (3.2, standard library only)
# Source:   CosmoDev polyglot showcase port of the App Icon Generator tool,
#           from the authored TypeScript snippet
#           src/tool-sources/app-icon-generator/typescript.ts.

module AppIcon
  # One export target: the square canvas edge and the centered contain-fit
  # rect the source image is drawn into.
  IconRect = Struct.new(:size, :x, :y, :w, :h, keyword_init: true)

  DEFAULT_SIZES = [16, 32, 180, 192, 512].freeze

  module_function

  # 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. Decoding the bitmap and encoding the PNG stay
  # with the host. Float#round halves up, matching JS Math.round on these
  # non-negative values.
  def icon_set_plan(src_w, src_h, sizes: DEFAULT_SIZES)
    sizes.map do |px|
      scale = [px.to_f / src_w, px.to_f / src_h].min
      w = [(src_w * scale).round, 1].max
      h = [(src_h * scale).round, 1].max
      IconRect.new(
        size: px,
        x: ((px - w) / 2.0).round, # letterbox on transparency
        y: ((px - h) / 2.0).round,
        w: w,
        h: h
      )
    end
  end
end

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 →