【发布时间】:2013-12-30 17:50:33
【问题描述】:
我正在搜索可以在 PowerShell 中使用的所有颜色的列表。由于我们需要提供名称而不是十六进制数字,因此很难确定颜色是否存在,至少如果您不知道如何:))
例如-foregroundcolor
write-host "hello world" -foregroundcolor "red"
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0
我正在搜索可以在 PowerShell 中使用的所有颜色的列表。由于我们需要提供名称而不是十六进制数字,因此很难确定颜色是否存在,至少如果您不知道如何:))
例如-foregroundcolor
write-host "hello world" -foregroundcolor "red"
【问题讨论】:
标签: powershell powershell-2.0 powershell-3.0
漂亮的网格
$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"
}
在较新的 powershell 中更新了颜色:
https://gist.github.com/timabell/cc9ca76964b59b2a54e91bda3665499e
【讨论】:
DarkYellow 和 DarkMagenta 背景在 powershell 6.1+ 中看起来非常不同(但正确)。
控制台颜色位于名为 [System.ConsoleColor] 的枚举中。您可以使用 [Enum] 的 GetValues 静态方法列出所有值
[Enum]::GetValues([System.ConsoleColor])
或者只是
[Enum]::GetValues([ConsoleColor])
【讨论】:
我发现使用简单的辅助函数预览控制台颜色的显示方式很有用:
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
}
}
【讨论】:
查看帮助怎么样?像这样,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}]
【讨论】:
我不会写下全部约 700 万(which you can apparently use now 如果您的终端可以显示它们),但这里是主要的,all named for you
我还添加了其他内容,例如“bold”、underline 和 negative。
只需像这样称呼它们(foreground 为fg,background 为bg,bf/bg 为“明亮的”前景/背景。default 进行重置,还有@987654336 @ + 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"
提到那些 24 位颜色? $style.bg.rgb -f 120,32,230。也许您是在 linux 上运行 pwsh 或者来自 'nix 背景? $style.fg.x -f 30 xterm 颜色会让您有宾至如归的感觉。
#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
}
}
显然你也可以设置超链接和截断文本!
https://github.com/PowerShell/PowerShell/issues/7744
【讨论】:
5.1.18362.752 (build 10.0.18362.752) 在 Win10 上
这是一个显示背景色和前景色的所有颜色组合的示例。
$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
}
}
【讨论】:
Write-Host "$FGColor on $BGColor " -background $BGColor -foreground $FGColor -nonewline.
不必那么难。标签完成是你的朋友。在-foregroundcolor(或任何唯一的缩写)之后按 Tab 键将列出它们。在 emacs 编辑模式下,它们会同时列出。
set-psreadlineoption -editmode emacs # put in your $profile
write-host hello world -f # press tab, it actually appears above it
Black Cyan DarkCyan DarkGreen DarkRed Gray Magenta White
Blue DarkBlue DarkGray DarkMagenta DarkYellow Green Red Yellow
它也在-Foregroundcolor(和-BackgroundColor)下的文档中:
【讨论】:
如果您从 Google 来到这里并想知道 Windows 中可用的颜色名称,您可以这样做:
Add-Type –assemblyName PresentationFramework; [System.Windows.Media.Colors] | Get-Member -static -Type Property |Select -Expand Name
(请注意您首先需要如何加载 PresentationFramework 程序集。)
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
Blue
BlueViolet
Brown
BurlyWood
CadetBlue
Chartreuse
...
尽管新控制台通过 ANSI 颜色转义序列和 @987654333 支持“所有”(24 位 TRUE)颜色,但只有其中一些可以在 Powershell 控制台中按其 名称 打印@ 已启用。(VT = 控制台虚拟终端支持)
要获取 (Write-Host) 可打印名称的列表,您可以这样做:
# Add-Type –assemblyName PresentationFramework
$colors = [System.Windows.Media.Colors] | Get-Member -static -Type Property |Select -Expand Name
Foreach ($col in $colors) { try { Write-Host "$col" -ForegroundColor $col -BackgroundColor Black } catch {} }
要获得所有 Windows定义颜色名称的完整列表,您可以运行以下魔法:
function argb2box { $c = ($args[0] -split '#..(.{2})(.{2})(.{2})'); $r,$g,$b = ("$c" -join(' ')).trim() -split ' '; return "`e[48;2;$([Int32]"0x$r");$([Int32]"0x$g");$([Int32]"0x$b")m `e[0m"; }
$sc=[System.Windows.Media.Colors]; $sc | Get-Member -static -Type Property |Select -Expand Name| ForEach { [pscustomobject] @{ ARGB = "$($sc::$_)"; MEOW = $(argb2box "$($sc::$_)") ; Color = $_}}
# You can sort on HEX values by adding:
# | sort -Property ARGB
您可以在此处阅读更多信息:
【讨论】:
-PassThru:Add-Type -AssemblyName PresentationFramework -PassThru | Out-Null。见discussion。