【问题标题】:List of all colors available for PowerShell?PowerShell 可用的所有颜色列表?
【发布时间】:2013-12-30 17:50:33
【问题描述】:

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

例如-foregroundcolor

write-host "hello world" -foregroundcolor "red"

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    漂亮的网格

    $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 实际上并没有那么暗,也不是很黄。这是一个错误吗?
    • DarkYellowDarkMagenta 背景在 powershell 6.1+ 中看起来非常不同(但正确)。
    【解决方案2】:

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

    [Enum]::GetValues([System.ConsoleColor])
    

    或者只是

    [Enum]::GetValues([ConsoleColor])
    

    【讨论】:

    • 只是扩展 mjolinor 的代码:[Enum]::GetValues([System.ConsoleColor]) | foreach { Write-Host "$_" -ForegroundColor $_} 这将显示该颜色中的所有颜色名称
    • 在 PowerShell 5-7 中我不得不使用: [System.Enum]::GetValues([System.ConsoleColor]) | foreach { 写主机 "$_" -ForegroundColor $_}
    【解决方案3】:

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

    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
      }
    }
    

    【讨论】:

      【解决方案4】:

      查看帮助怎么样?像这样,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}]
      

      【讨论】:

      • 我将 get-gelp 用于 forgroundcolor,这将我带到了 powershell 配置文件。还是谢谢你
      【解决方案5】:

      我不会写下全部约 700 万(which you can apparently use now 如果您的终端可以显示它们),但这里是主要的,all named for you
      我还添加了其他内容,例如“bold”、underlinenegative

      只需像这样称呼它们(foregroundfgbackgroundbgbf/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

      【讨论】:

      • 遗憾的是,这在 Win8.1 上的 powershell 6.1.1 中根本不起作用。
      • 对不起,5.1.18362.752 (build 10.0.18362.752) 在 Win10 上
      • 我已经阅读了链接的材料,但不知何故,我看不到如何实现我想要的。我想 Write-Host -ForegroundColor ??? 以便文本是例如粉色的。事实上,我想指定我个人的整数三元组以使用自定义 RGB 颜色。您的回答似乎表明这是可能的,但我还没有掌握它的方法。
      【解决方案6】:

      这是一个显示背景色和前景色的所有颜色组合的示例。

      $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.
      【解决方案7】:

      不必那么难。标签完成是你的朋友。在-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)下的文档中:

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7#parameters

      【讨论】:

        【解决方案8】:

        如果您从 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
        
        


        您可以在此处阅读更多信息:

        【讨论】:

        • UPDATE:显然 PresentationFramework 在执行 GetTypes 方法之前不可用,所以为了做到我们必须像这样添加-PassThruAdd-Type -AssemblyName PresentationFramework -PassThru | Out-Null。见discussion
        猜你喜欢
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-04
        • 2012-09-01
        相关资源
        最近更新 更多