Files
git-heatmap/generate-icons.ps1
2026-06-23 21:57:49 +08:00

251 lines
11 KiB
PowerShell

# Generate icons for GitHub HeatMap and Gitea HeatMap widgets
# Run once before building: .\generate-icons.ps1
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Drawing
$GITHUB_PATH = "M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"
$GITEA_TEABAG = "M395.9,484.2l-126.9-61c-12.5-6-17.9-21.2-11.8-33.8l61-126.9c6-12.5,21.2-17.9,33.8-11.8c17.2,8.3,27.1,13,27.1,13l-0.1-109.2l16.7-0.1l0.1,117.1c0,0,57.4,24.2,83.1,40.1c3.7,2.3,10.2,6.8,12.9,14.4c2.1,6.1,2,13.1-1,19.3l-61,126.9C423.6,484.9,408.4,490.3,395.9,484.2z"
$GITEA_BODY = "M622.7,149.8c-4.1-4.1-9.6-4-9.6-4s-117.2,6.6-177.9,8c-13.3,0.3-26.5,0.6-39.6,0.7c0,39.1,0,78.2,0,117.2c-5.5-2.6-11.1-5.3-16.6-7.9c0-36.4-0.1-109.2-0.1-109.2c-29,0.4-89.2-2.2-89.2-2.2s-141.4-7.1-156.8-8.5c-9.8-0.6-22.5-2.1-39,1.5c-8.7,1.8-33.5,7.4-53.8,26.9C-4.9,212.4,6.6,276.2,8,285.8c1.7,11.7,6.9,44.2,31.7,72.5c45.8,56.1,144.4,54.8,144.4,54.8s12.1,28.9,30.6,55.5c25,33.1,50.7,58.9,75.7,62c63,0,188.9-0.1,188.9-0.1s12,0.1,28.3-10.3c14-8.5,26.5-23.4,26.5-23.4s12.9-13.8,30.9-45.3c5.5-9.7,10.1-19.1,14.1-28c0,0,55.2-117.1,55.2-231.1C633.2,157.9,624.7,151.8,622.7,149.8z M125.6,353.9c-25.9-8.5-36.9-18.7-36.9-18.7S69.6,321.8,60,295.4c-16.5-44.2-1.4-71.2-1.4-71.2s8.4-22.5,38.5-30c13.8-3.7,31-3.1,31-3.1s7.1,59.4,15.7,94.2c7.2,29.2,24.8,77.7,24.8,77.7S142.5,359.9,125.6,353.9z M425.9,461.5c0,0-6.1,14.5-19.6,15.4c-5.8,0.4-10.3-1.2-10.3-1.2s-0.3-0.1-5.3-2.1l-112.9-55c0,0-10.9-5.7-12.8-15.6c-2.2-8.1,2.7-18.1,2.7-18.1L322,273c0,0,4.8-9.7,12.2-13c0.6-0.3,2.3-1,4.5-1.5c8.1-2.1,18,2.8,18,2.8l110.7,53.7c0,0,12.6,5.7,15.3,16.2c1.9,7.4-0.5,14-1.8,17.2C474.6,363.8,425.9,461.5,425.9,461.5z"
$GITEA_DETAIL = "M326.8,380.1c-8.2,0.1-15.4,5.8-17.3,13.8c-1.9,8,2,16.3,9.1,20c7.7,4,17.5,1.8,22.7-5.4c5.1-7.1,4.3-16.9-1.8-23.1l24-49.1c1.5,0.1,3.7,0.2,6.2-0.5c4.1-0.9,7.1-3.6,7.1-3.6c4.2,1.8,8.6,3.8,13.2,6.1c4.8,2.4,9.3,4.9,13.4,7.3c0.9,0.5,1.8,1.1,2.8,1.9c1.6,1.3,3.4,3.1,4.7,5.5c1.9,5.5-1.9,14.9-1.9,14.9c-2.3,7.6-18.4,40.6-18.4,40.6c-8.1-0.2-15.3,5-17.7,12.5c-2.6,8.1,1.1,17.3,8.9,21.3c7.8,4,17.4,1.7,22.5-5.3c5-6.8,4.6-16.3-1.1-22.6c1.9-3.7,3.7-7.4,5.6-11.3c5-10.4,13.5-30.4,13.5-30.4c0.9-1.7,5.7-10.3,2.7-21.3c-2.5-11.4-12.6-16.7-12.6-16.7c-12.2-7.9-29.2-15.2-29.2-15.2s0-4.1-1.1-7.1c-1.1-3.1-2.8-5.1-3.9-6.3c4.7-9.7,9.4-19.3,14.1-29c-4.1-2-8.1-4-12.2-6.1c-4.8,9.8-9.7,19.7-14.5,29.5c-6.7-0.1-12.9,3.5-16.1,9.4c-3.4,6.3-2.7,14.1,1.9,19.8C343.2,346.5,335,363.3,326.8,380.1z"
$ICON_SIZES = @(16, 24, 32, 48, 64, 128, 256)
function Get-Brush([string]$hex) {
[System.Windows.Media.BrushConverter]::new().ConvertFromString($hex)
}
function Save-AsPng($rtb, [string]$path) {
$enc = New-Object System.Windows.Media.Imaging.PngBitmapEncoder
$enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($rtb))
$fs = [System.IO.File]::OpenWrite($path)
$enc.Save($fs)
$fs.Close()
}
function Rtb-ToBitmap($rtb) {
$enc = New-Object System.Windows.Media.Imaging.PngBitmapEncoder
$ms = New-Object System.IO.MemoryStream
$enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($rtb))
$enc.Save($ms)
$ms.Position = 0
$bmp = New-Object System.Drawing.Bitmap($ms)
$ms.Dispose()
return $bmp
}
function Resize-Bitmap([System.Drawing.Bitmap]$source, [int]$size) {
$dest = New-Object System.Drawing.Bitmap($size, $size, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
$g = [System.Drawing.Graphics]::FromImage($dest)
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$g.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$g.Clear([System.Drawing.Color]::Transparent)
$g.DrawImage($source, 0, 0, $size, $size)
$g.Dispose()
return $dest
}
function Convert-BitmapToIcoDib([System.Drawing.Bitmap]$bitmap) {
$size = $bitmap.Width
$ms = New-Object System.IO.MemoryStream
$bw = New-Object System.IO.BinaryWriter($ms)
$bw.Write([int32]40)
$bw.Write([int32]$size)
$bw.Write([int32]($size * 2))
$bw.Write([int16]1)
$bw.Write([int16]32)
$bw.Write([int32]0)
$bw.Write([int32]0)
$bw.Write([int32]0)
$bw.Write([int32]0)
$bw.Write([int32]0)
$bw.Write([int32]0)
for ($y = $size - 1; $y -ge 0; $y--) {
for ($x = 0; $x -lt $size; $x++) {
$c = $bitmap.GetPixel($x, $y)
$bw.Write([byte]$c.B)
$bw.Write([byte]$c.G)
$bw.Write([byte]$c.R)
$bw.Write([byte]$c.A)
}
}
$rowBytes = [Math]::Ceiling($size / 8.0)
$rowBytes = ($rowBytes + 3) -band (-bnot 3)
for ($y = 0; $y -lt $size; $y++) {
for ($xb = 0; $xb -lt $rowBytes; $xb++) {
$val = 0
for ($bit = 0; $bit -lt 8; $bit++) {
$x = ($xb * 8) + $bit
if ($x -lt $size) {
$a = $bitmap.GetPixel($x, $size - 1 - $y).A
if ($a -lt 128) { $val = $val -bor (1 -shl (7 - $bit)) }
}
}
$bw.Write([byte]$val)
}
}
$bw.Close()
return $ms.ToArray()
}
function Save-MultiResIco([string]$icoPath, [System.Drawing.Bitmap]$master) {
$entries = @()
foreach ($size in $ICON_SIZES) {
$scaled = Resize-Bitmap $master $size
$entries += @{
Size = $size
Data = (Convert-BitmapToIcoDib $scaled)
Bitmap = $scaled
}
}
$ms = New-Object System.IO.MemoryStream
$bw = New-Object System.IO.BinaryWriter($ms)
$bw.Write([uint16]0)
$bw.Write([uint16]1)
$bw.Write([uint16]$entries.Count)
$offset = 6 + (16 * $entries.Count)
foreach ($entry in $entries) {
$size = $entry.Size
if ($size -ge 256) {
$bw.Write([byte]0)
$bw.Write([byte]0)
} else {
$bw.Write([byte]$size)
$bw.Write([byte]$size)
}
$bw.Write([byte]0)
$bw.Write([byte]0)
$bw.Write([uint16]1)
$bw.Write([uint16]32)
$bw.Write([uint32]$entry.Data.Length)
$bw.Write([uint32]$offset)
$offset += $entry.Data.Length
}
foreach ($entry in $entries) {
$bw.Write($entry.Data, 0, $entry.Data.Length)
$entry.Bitmap.Dispose()
}
$bw.Close()
[System.IO.File]::WriteAllBytes($icoPath, $ms.ToArray())
$ms.Dispose()
}
function Render-RtbAt([int]$px, [scriptblock]$drawBlock) {
$dv = New-Object System.Windows.Media.DrawingVisual
[System.Windows.Media.RenderOptions]::SetBitmapScalingMode($dv, [System.Windows.Media.BitmapScalingMode]::HighQuality)
[System.Windows.Media.RenderOptions]::SetEdgeMode($dv, [System.Windows.Media.EdgeMode]::Aliased)
$ctx = $dv.RenderOpen()
& $drawBlock $ctx $px
$ctx.Close()
$rtb = New-Object System.Windows.Media.Imaging.RenderTargetBitmap(
$px, $px, 96, 96, [System.Windows.Media.PixelFormats]::Pbgra32)
$rtb.Render($dv)
$rtb
}
function Get-IconCornerRadius([int]$px) {
return [Math]::Max(2, [Math]::Round($px * 0.22))
}
function Draw-IconBackground($ctx, [int]$px, $bgBrush) {
$radius = Get-IconCornerRadius $px
$bg = New-Object System.Windows.Media.RectangleGeometry(
[System.Windows.Rect]::new(0, 0, $px, $px), $radius, $radius)
$ctx.DrawGeometry($bgBrush, $null, $bg)
}
function Export-IconSet([string]$outDir, [scriptblock]$drawBlock) {
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$masterRtb = Render-RtbAt 256 $drawBlock
$masterBmp = Rtb-ToBitmap $masterRtb
Save-AsPng $masterRtb "$outDir\icon.png"
Write-Host " $outDir\icon.png"
foreach ($entry in @(@{px=32;suf="32x32"}, @{px=128;suf="128x128"}, @{px=256;suf="128x128@2x"})) {
$scaled = Resize-Bitmap $masterBmp $entry.px
$scaled.Save("$outDir\$($entry.suf).png", [System.Drawing.Imaging.ImageFormat]::Png)
$scaled.Dispose()
Write-Host " $outDir\$($entry.suf).png"
}
Save-MultiResIco "$outDir\icon.ico" $masterBmp
Write-Host " $outDir\icon.ico (multi-size BMP)"
$masterBmp.Dispose()
}
function Render-GithubIcon([string]$outDir) {
$bgBrush = Get-Brush "#0d1117"
$white = [System.Windows.Media.Brushes]::White
$draw = {
param($ctx, [int]$px)
Draw-IconBackground $ctx $px $bgBrush
$pad = $px * 0.18
$scale = ($px - 2 * $pad) / 24.0
$xf = New-Object System.Windows.Media.TransformGroup
$xf.Children.Add((New-Object System.Windows.Media.ScaleTransform($scale, $scale)))
$xf.Children.Add((New-Object System.Windows.Media.TranslateTransform($pad, $pad)))
$ctx.PushTransform($xf)
$ctx.DrawGeometry($white, $null, [System.Windows.Media.Geometry]::Parse($GITHUB_PATH))
$ctx.Pop()
}
Export-IconSet $outDir $draw
}
function Render-GiteaIcon([string]$outDir) {
$bgBrush = Get-Brush "#609926"
$white = [System.Windows.Media.Brushes]::White
$paths = @($GITEA_BODY, $GITEA_DETAIL, $GITEA_TEABAG)
$draw = {
param($ctx, [int]$px)
Draw-IconBackground $ctx $px $bgBrush
$pad = $px * 0.14
$scale = ($px - 2 * $pad) / 640.0
$xf = New-Object System.Windows.Media.TransformGroup
$xf.Children.Add((New-Object System.Windows.Media.ScaleTransform($scale, $scale)))
$xf.Children.Add((New-Object System.Windows.Media.TranslateTransform($pad, $pad)))
$ctx.PushTransform($xf)
foreach ($pathStr in $paths) {
$ctx.DrawGeometry($white, $null, [System.Windows.Media.Geometry]::Parse($pathStr))
}
$ctx.Pop()
}
Export-IconSet $outDir $draw
}
Set-Location $PSScriptRoot
Write-Host ""
Write-Host "Generating GitHub HeatMap icons..."
Render-GithubIcon ".\github\src-tauri\icons"
Write-Host ""
Write-Host "Generating Gitea HeatMap icons..."
Render-GiteaIcon ".\gitea\src-tauri\icons"
Write-Host ""
Write-Host "Done. Rebuild apps to refresh shortcut icons:"
Write-Host " .\build.bat"