如何列出可用的颜色名称?

dou*_*ack 26 colors terminal

我正在尝试为我的 git 配置添加一些颜色,我想知道终端上可用的颜色名称。

  • 我只想按名称使用颜色,以便其他人更容易理解
  • 我不想添加任何新颜色 - 我只想从预定义的名称中进行选择
  • 我想要一个适用于所有发行版的解决方案,但主要是 Debian
  • 很高兴看到名称所指示的颜色

许多在线参考资料经常谈论我的系统上未定义的颜色名称,因此我只需要一种方法来查看我的默认选项是什么。

gol*_*cks 28

Many online references often talk about color names that are not defined on my system

Those probably are defined, but they are X11 colors; once upon a time you could find them in /lib[64]/X11/rgb.txt. In any case, this is a mapping of strings (e.g., dimgray) to 24-bit RGB colors (e.g. 0xff8800 or #ff8800, which would be orange). A 24-bit space is ~16 million colors, obviously X11 does not give them all names (CSS 3 uses X11 names, BTW).

The 24-bit space is used by your GUI; transparency is implemented by increasing this to a 32-bit space. However, git isn't a GUI (G = graphical) tool, it's a TUI (T = terminal) tool, and it is limited to the colors available on a normal terminal.

I would like a solution that works for all distros, but primarily Debian

If you want this to be properly portable, you should rely only on the eight standard ANSI colors:

  • black
  • blue
  • green
  • yellow
  • cyan
  • white
  • magenta
  • red

A little disappointing next to the X11 list, but better than nothing at all! These also have a "bold" or "bright" version that is standard, making 16 colors, which you may be able to specify as, e.g., "brightyellow" (or conversely, "darkyellow").

Most GUI terminals1 have 256 color support and some terminal apps can exploit this. To test, you first need to set the $TERM variable appropriately:

export $TERM=xterm-256color
Run Code Online (Sandbox Code Playgroud)

Your terminal emulator may also have a configuration option for this. Colors under the xterm 256 color protocol are indexed:

xterm 颜色图表

索引号位于左下角。请注意,此图表底部的集合 (0-15) 是 16 种基本(明暗)ANSI 颜色。要在标准下引用这些颜色之一,请使用color+ 索引号,例如。color40.


1.“GUI终端”是在GUI上下文中运行的终端模拟器,例如xterm、GNOME终端等。但是,这不会使在GUI终端中运行的TUI应用程序(例如git)成为GUI应用程序。 它们仍然是 TUI 应用程序,并受该上下文的约束。

  • @doub1ejack:你猜怎么着?我昨天注意到**在 256 色终端上有一个命名颜色的标准,** 并且有一些 TUI 应用程序使用它,尽管我没有检查 git。无论如何,我已经编辑了一些关于此的内容。 (2认同)