PowerShell可用的所有颜色列表?

Ray*_*and 14 powershell powershell-2.0 powershell-3.0

我正在搜索我可以在PowerShell中使用的所有颜色的列表.由于我们需要提供名称而不是hexnumber,因此很难确定是否存在颜色,至少如果您不知道如何:))

例如,as -foregroundcolor

write-host "hello world" -foregroundcolor "red"
Run Code Online (Sandbox Code Playgroud)

mjo*_*nor 25

控制台颜色位于名为[System.ConsoleColor]的枚举中.您可以使用[Enum]的GetValues静态方法列出所有值

[Enum]::GetValues([System.ConsoleColor])
Run Code Online (Sandbox Code Playgroud)

要不就

[Enum]::GetValues([ConsoleColor])
Run Code Online (Sandbox Code Playgroud)

  • 只是扩展mjolinor的代码:[Enum] :: GetValues([System.ConsoleColor])| foreach {Write-Host"$ _" - FieldColor $ _}这将显示所述颜色的所有颜色名称 (6认同)

Tim*_*ell 21

漂亮的网格

$colors = [enum]::GetValues([System.ConsoleColor])
Foreach ($bgcolor in $colors){
    Foreach ($fgcolor in $colors) { Write-Host "$fgcolor|"  -ForegroundColor $fgcolor -BackgroundColor $bgcolor -NoNewLine }
    Write-Host " on $bgcolor"
}
Run Code Online (Sandbox Code Playgroud)

多彩输出的截图

https://gist.github.com/timabell/cc9ca76964b59b2a54e91bda3665499e

  • 这段代码很好地说明了DarkYellow实际上不是那么黑,也不是非常黄。这是错误吗? (2认同)

Emp*_*LII 18

我发现使用简单的辅助函数预览控制台颜色的显示方式很有用:

function Show-Colors( ) {
  $colors = [Enum]::GetValues( [ConsoleColor] )
  $max = ($colors | foreach { "$_ ".Length } | Measure-Object -Maximum).Maximum
  foreach( $color in $colors ) {
    Write-Host (" {0,2} {1,$max} " -f [int]$color,$color) -NoNewline
    Write-Host "$color" -Foreground $color
  }
}
Run Code Online (Sandbox Code Playgroud)


von*_*ryz 6

检查帮助怎么样?像这样,get-help write-host会告诉你:

[-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]
[-ForegroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]
Run Code Online (Sandbox Code Playgroud)


Has*_*own 6

我不会写下所有约 700 万(如果您的终端可以显示它们,您现在显然可以使用它们),但这里是主要的,全部以您的名字命名,
我已经包括了其他内容,例如“ boldunderline、 和negative

只需像这样称呼它们(fgfor foregroundbgforbackgroundbf/bg用于“明亮的”前景/背景。default重置,还有fg.default+bg.default用于单独重置这些)

$style.fg.green + 'Im green!'
'I feel a little ',$style.bg.black,' moody' -join ''
"Math is pretty $($style.negative)$(191 * 7)$($style.default) too"
Run Code Online (Sandbox Code Playgroud)

颜色!

那些 24 位颜色被提及?$style.bg.rgb -f 120,32,230. 也许您在 linux 上运行 pwsh 或者来自 'nix 背景?$style.fg.x -f 30xterm 颜色会让您有宾至如归的感觉。

#console colours (VT escape sequences)
$style=@(
    0, 'default', 'bold',
    4, 'underline',
    24, 'nounderline',
    7, 'negative',
    27, 'positive',
    '_fg',
        30, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
        39, 'default',
    '_bg',
        'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
        49, 'default',
    '_bf', 90, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
    '_bb', 100, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
) `
| &{
    Begin {
        $sequence="$([char]27)[{0}m"
        $style=@{
            fg=@{
                rgb=$sequence -f '38;2;{0};{1};{2}'
                x=$sequence -f '38;5;{0}'
            };
            bg=@{
                rgb=$sequence -f '48;2;{0};{1};{2}'
                x=$sequence -f '48;5;{0}'
            };
            bf=@{};
            bb=@{};
        }
        $current=$style
        $index=$null
    }
    Process {
        Switch -regex ($_) {
            '\d' { $index=$_ }
            '^_' { $current=$style[$_ -replace '^.',''] }
            Default {
                $current[$_]=$sequence -f $index++
#comment me out
"$($current[$_])$($_)`t" | Out-Host
            }
        }
    }
    End {
        $style
#more demonstrations
@(($style.bg.rgb -f 120,32,230), ($style.fg.x -f 30), 'hello', $style.default) -join '' | Out-Host
    }
}
Run Code Online (Sandbox Code Playgroud)

显然您也可以设置超链接和截断文本!
https://github.com/PowerShell/PowerShell/issues/7744


小智 5

下面是显示背景色和前景色的所有颜色组合的示例。

$FGcolors = [enum]::GetValues([System.ConsoleColor])
$BGcolors = [enum]::GetValues([System.ConsoleColor])

Foreach ($FGcolor in $FGcolors)
{
    Foreach ($BGcolor in $BGcolors)
    {
        Write-Host ("Foreground: $FGColor BackGround: $BGColor")  -ForegroundColor $FGcolor -BackgroundColor $BGcolor
    }
}
Run Code Online (Sandbox Code Playgroud)