Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created September 29, 2026 14:34
Show Gist options
  • Select an option

  • Save dfinke/07bfcdcc093c3e33cf95e437637adc4e to your computer and use it in GitHub Desktop.

Select an option

Save dfinke/07bfcdcc093c3e33cf95e437637adc4e to your computer and use it in GitHub Desktop.
A few lines of PowerShell can turn a seed into a shareable little universe: glowing moon, scattered stars, and a new constellation every time you change the seed.
#requires -Version 7.0
# Pick a fixed seed so the cloud validation output stays reproducible.
$seed = 2718
$outputPath = Join-Path $PSScriptRoot 'night-sky.svg'
# Create a shareable SVG file using the generated PowerShell script.
& (Join-Path $PSScriptRoot 'solution.ps1') -Seed $seed -OutputPath $outputPath | Format-List
#requires -Version 7.0
[CmdletBinding()]
param(
# Set the seed so the same input creates the same sky every time.
[int] $Seed = 2718,
# Choose where to write the new SVG artwork.
[Parameter(Mandatory)]
[string] $OutputPath
)
# Use a local random generator so this script does not change global random state.
$random = [System.Random]::new($Seed)
$width = 720
$height = 420
$stars = [System.Collections.Generic.List[string]]::new()
# Generate a bounded set of tiny stars with deterministic positions and sizes.
for ($index = 0; $index -lt 72; $index++) {
$x = $random.Next(18, $width - 18)
$y = $random.Next(16, $height - 16)
$radius = [math]::Round($random.NextDouble() * 1.5 + 0.4, 2)
$opacity = [math]::Round($random.NextDouble() * 0.55 + 0.35, 2)
$stars.Add(('<circle cx="{0}" cy="{1}" r="{2}" fill="#f4f7ff" opacity="{3}"/>' -f $x, $y, $radius, $opacity))
}
# Create a small constellation from fixed points so the shape is easy to spot.
$constellation = @(
'<polyline points="120,275 190,230 245,270 305,205 360,230 425,160"'
'fill="none" stroke="#9fe7ff" stroke-width="2" opacity="0.85"/>'
'<circle cx="120" cy="275" r="4" fill="#a8e9ff"/>'
'<circle cx="190" cy="230" r="4" fill="#a8e9ff"/>'
'<circle cx="245" cy="270" r="4" fill="#a8e9ff"/>'
'<circle cx="305" cy="205" r="4" fill="#a8e9ff"/>'
'<circle cx="360" cy="230" r="4" fill="#a8e9ff"/>'
'<circle cx="425" cy="160" r="4" fill="#a8e9ff"/>'
) -join [Environment]::NewLine
# Build the SVG as plain text with a night gradient, moon, stars, and constellation.
$svg = @"
<svg xmlns="http://www.w3.org/2000/svg" width="$width" height="$height" viewBox="0 0 $width $height">
<defs>
<linearGradient id="sky" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#101a44"/>
<stop offset="55%" stop-color="#263d73"/>
<stop offset="100%" stop-color="#572f65"/>
</linearGradient>
<radialGradient id="moonGlow">
<stop offset="0%" stop-color="#fff8cf" stop-opacity="0.75"/>
<stop offset="100%" stop-color="#fff8cf" stop-opacity="0"/>
</radialGradient>
</defs>
<rect width="100%" height="100%" fill="url(#sky)"/>
<circle cx="575" cy="105" r="90" fill="url(#moonGlow)"/>
<circle cx="575" cy="105" r="34" fill="#fff3b0"/>
$($stars -join [Environment]::NewLine)
$constellation
<text x="30" y="390" fill="#dce8ff" font-family="sans-serif" font-size="16">A tiny sky, generated with PowerShell 7 ┬╖ seed $Seed</text>
</svg>
"@
# Create the selected output directory when it does not yet exist.
$parent = Split-Path -Parent $OutputPath
if (-not (Test-Path -LiteralPath $parent -PathType Container)) {
New-Item -ItemType Directory -Path $parent -Force | Out-Null
}
# Save the vector image as UTF-8 without a byte-order mark.
[System.IO.File]::WriteAllText($OutputPath, $svg, [System.Text.UTF8Encoding]::new($false))
# Return a short summary while leaving the image ready to open in a browser.
[pscustomobject]@{
File = [System.IO.Path]::GetFileName($OutputPath)
Seed = $Seed
StarCount = $stars.Count
Dimensions = ('{0}x{1}' -f $width, $height)
}
@dfinke

dfinke commented Sep 29, 2026

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment