Last active
April 30, 2023 18:44
-
-
Save smoonlee/248a8b761f9f7aab8f4217b479b608d9 to your computer and use it in GitHub Desktop.
New-HexColourSwatch.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load the System.Drawing assembly | |
Add-Type -AssemblyName System.Drawing | |
# Prompt the user for a hex color code | |
$hex = Read-Host "Enter a hex color code (e.g. #FF0000)" | |
# Remove the '#' character from the beginning of the hex code, if present | |
if ($hex.StartsWith("#")) { | |
$hex = $hex.Substring(1) | |
} | |
# Convert the hex code to a color object | |
$color = [System.Drawing.Color]::FromArgb( | |
[System.Convert]::ToInt32($hex.Substring(0,2), 16), | |
[System.Convert]::ToInt32($hex.Substring(2,2), 16), | |
[System.Convert]::ToInt32($hex.Substring(4,2), 16) | |
) | |
# Create a new 32x32 bitmap with the specified color | |
$bitmap = New-Object System.Drawing.Bitmap(32, 32) | |
for ($x = 0; $x -lt $bitmap.Width; $x++) { | |
for ($y = 0; $y -lt $bitmap.Height; $y++) { | |
$bitmap.SetPixel($x, $y, $color) | |
} | |
} | |
# Save the bitmap as a PNG file with the specified filename | |
$filename = "image.png" | |
$bitmap.Save($filename, [System.Drawing.Imaging.ImageFormat]::Png) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment