|
| 1 | +function Convert-ColorToConsoleColor { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Convert a color ([System.Drawing.Color]) to the closest matching console color name ([System.ConsoleColor]). |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Convert a color ([System.Drawing.Color]) to the closest matching console color name ([System.ConsoleColor]). |
| 8 | +
|
| 9 | + .PARAMETER Color |
| 10 | + An [A,]R,G,B color value or object. |
| 11 | +
|
| 12 | + .EXAMPLE |
| 13 | + $Color = [System.Drawing.Color]::FromArgb(50, 55, 20) |
| 14 | + $ConsoleColor = Convert-ColorToConsoleColor -Color $Color |
| 15 | + Write-Output $ConsoleColor |
| 16 | +
|
| 17 | + .EXAMPLE |
| 18 | + Convert-ColorToConsoleColor -Color ([System.Drawing.Color]::FromArgb(50, 55, 20)) |
| 19 | +
|
| 20 | + .EXAMPLE |
| 21 | + Convert-ColorToConsoleColor -Color ([System.Drawing.Color]::DarkOrchid) |
| 22 | +
|
| 23 | + .EXAMPLE |
| 24 | + [System.Drawing.Color]::BurlyWood | Convert-ColorToConsoleColor |
| 25 | +
|
| 26 | + .EXAMPLE |
| 27 | + $psISE.Options.ConsolePaneTextBackgroundColor.ToString() | Convert-ColorToConsoleColor |
| 28 | +
|
| 29 | + Converts the value of the PowerShell ISE console pane text background color to the closest matching console color. |
| 30 | +
|
| 31 | + .OUTPUTS |
| 32 | + System.ConsoleColor |
| 33 | +
|
| 34 | + .NOTES |
| 35 | + Author: Sam Erde |
| 36 | + Version: 1.0.0 |
| 37 | + Modified: 2024-12-04 |
| 38 | +
|
| 39 | + To Do: Add tab-autocomplete for color names. |
| 40 | + #> |
| 41 | + [CmdletBinding()] |
| 42 | + [OutputType([System.ConsoleColor])] |
| 43 | + param ( |
| 44 | + [Parameter(Mandatory, Position = 0, ValueFromPipeline)] |
| 45 | + [ArgumentCompleter({ ColorArgumentCompleter @args })] |
| 46 | + [System.Drawing.Color] |
| 47 | + $Color |
| 48 | + ) |
| 49 | + |
| 50 | + $ConsoleColorList = [Enum]::GetValues([System.ConsoleColor]) |
| 51 | + $ClosestColor = [System.ConsoleColor]::Black |
| 52 | + $SmallestDistance = [double]::MaxValue |
| 53 | + |
| 54 | + # Loop through each console color and find the closest match to the input color. |
| 55 | + foreach ($ConsoleColor in $ConsoleColorList) { |
| 56 | + $ConsoleColorValue = [System.Drawing.Color]::FromName($ConsoleColor.ToString()) |
| 57 | + $Distance = [math]::Sqrt( |
| 58 | + [math]::Pow($color.R - $ConsoleColorValue.R, 2) + |
| 59 | + [math]::Pow($color.G - $ConsoleColorValue.G, 2) + |
| 60 | + [math]::Pow($color.B - $ConsoleColorValue.B, 2) |
| 61 | + ) |
| 62 | + |
| 63 | + if ($Distance -lt $SmallestDistance) { |
| 64 | + $SmallestDistance = $Distance |
| 65 | + $ClosestColor = [System.ConsoleColor]$ConsoleColor |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $ClosestColor |
| 70 | +} |
| 71 | + |
| 72 | +function ColorArgumentCompleter { |
| 73 | + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) |
| 74 | + |
| 75 | + $KnownColors = [System.Enum]::GetValues([System.Drawing.KnownColor]) |
| 76 | + $ColorNames = $KnownColors | ForEach-Object { |
| 77 | + [System.Drawing.Color]::FromKnownColor($_).Name |
| 78 | + } |
| 79 | + $ColorNames |
| 80 | +} |
0 commit comments