ANSI颜色转义序列列表

盗贼大师

在大多数终端上,可以使用\033ANSI转义序列对输出进行着色

我正在寻找所有受支持的颜色和选项(如明亮和闪烁)的列表。

由于支持它们的终端之间可能存在差异,因此我主要对xterm兼容终端支持的序列感兴趣。

理查德

您要查找的ANSI转义序列是“选择图形渲染”子集。所有这些都具有以下形式

\033[XXXm

其中XXX是一系列用分号分隔的参数。

可以在C语言中用红色,粗体和带下划线的文字(下面将讨论许多其他选项)来写:

printf("\033[31;1;4mHello\033[0m");

在C ++中,您将使用

std::cout<<"\033[31;1;4mHello\033[0m";

在Python3中,您将使用

print("\033[31;1;4mHello\033[0m")

在Bash中,您将使用

echo -e "\033[31;1;4mHello\033[0m"

其中第一部分使文本为红色(31),粗体(1),带下划线的(4),最后一部分清除所有这些内容(0)。

如下表所述,您可以设置许多文本属性,例如粗体,字体,下划线和&c。StackOverflow不允许您在答案中放置适当的表格是不是很愚蠢?)

字体效果

╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
║  Code    ║             Effect             ║                                   Note                                  ║
╠══════════╬════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
║ 0        ║  Reset / Normal                ║  all attributes off                                                     ║
║ 1        ║  Bold or increased intensity   ║                                                                         ║
║ 2        ║  Faint (decreased intensity)   ║  Not widely supported.                                                  ║
║ 3        ║  Italic                        ║  Not widely supported. Sometimes treated as inverse.                    ║
║ 4        ║  Underline                     ║                                                                         ║
║ 5        ║  Slow Blink                    ║  less than 150 per minute                                               ║
║ 6        ║  Rapid Blink                   ║  MS-DOS ANSI.SYS; 150+ per minute; not widely supported                 ║
║ 7        ║  [[reverse video]]             ║  swap foreground and background colors                                  ║
║ 8        ║  Conceal                       ║  Not widely supported.                                                  ║
║ 9        ║  Crossed-out                   ║  Characters legible, but marked for deletion.  Not widely supported.    ║
║ 10       ║  Primary(default) font         ║                                                                         ║
║ 11–19    ║  Alternate font                ║  Select alternate font `n-10`                                           ║
║ 20       ║  Fraktur                       ║  hardly ever supported                                                  ║
║ 21       ║  Bold off or Double Underline  ║  Bold off not widely supported; double underline hardly ever supported. ║
║ 22       ║  Normal color or intensity     ║  Neither bold nor faint                                                 ║
║ 23       ║  Not italic, not Fraktur       ║                                                                         ║
║ 24       ║  Underline off                 ║  Not singly or doubly underlined                                        ║
║ 25       ║  Blink off                     ║                                                                         ║
║ 27       ║  Inverse off                   ║                                                                         ║
║ 28       ║  Reveal                        ║  conceal off                                                            ║
║ 29       ║  Not crossed out               ║                                                                         ║
║ 30–37    ║  Set foreground color          ║  See color table below                                                  ║
║ 38       ║  Set foreground color          ║  Next arguments are `5;<n>` or `2;<r>;<g>;<b>`, see below               ║
║ 39       ║  Default foreground color      ║  implementation defined (according to standard)                         ║
║ 40–47    ║  Set background color          ║  See color table below                                                  ║
║ 48       ║  Set background color          ║  Next arguments are `5;<n>` or `2;<r>;<g>;<b>`, see below               ║
║ 49       ║  Default background color      ║  implementation defined (according to standard)                         ║
║ 51       ║  Framed                        ║                                                                         ║
║ 52       ║  Encircled                     ║                                                                         ║
║ 53       ║  Overlined                     ║                                                                         ║
║ 54       ║  Not framed or encircled       ║                                                                         ║
║ 55       ║  Not overlined                 ║                                                                         ║
║ 60       ║  ideogram underline            ║  hardly ever supported                                                  ║
║ 61       ║  ideogram double underline     ║  hardly ever supported                                                  ║
║ 62       ║  ideogram overline             ║  hardly ever supported                                                  ║
║ 63       ║  ideogram double overline      ║  hardly ever supported                                                  ║
║ 64       ║  ideogram stress marking       ║  hardly ever supported                                                  ║
║ 65       ║  ideogram attributes off       ║  reset the effects of all of 60-64                                      ║
║ 90–97    ║  Set bright foreground color   ║  aixterm (not in standard)                                              ║
║ 100–107  ║  Set bright background color   ║  aixterm (not in standard)                                              ║
╚══════════╩════════════════════════════════╩═════════════════════════════════════════════════════════════════════════╝

2位色彩

你已经有了这个!

4位色彩

实现端子颜色的标准从有限的(4位)选项开始。下表列出了各种终端仿真器用于这些背景和前景颜色的RGB值:

各种终端仿真器实现的ANSI颜色表

使用上述方法,您可以使用以下方法在绿色背景上制作红色文本(但是为什么呢?):

\033[31;42m

11种颜色(插曲)

In their book "Basic Color Terms: Their Universality and Evolution", Brent Berlin and Paul Kay used data collected from twenty different languages from a range of language families to identify eleven possible basic color categories: white, black, red, green, yellow, blue, brown, purple, pink, orange, and gray.

Berlin and Kay found that, in languages with fewer than the maximum eleven color categories, the colors followed a specific evolutionary pattern. This pattern is as follows:

  1. All languages contain terms for black (cool colours) and white (bright colours).
  2. If a language contains three terms, then it contains a term for red.
  3. If a language contains four terms, then it contains a term for either green or yellow (but not both).
  4. If a language contains five terms, then it contains terms for both green and yellow.
  5. If a language contains six terms, then it contains a term for blue.
  6. If a language contains seven terms, then it contains a term for brown.
  7. If a language contains eight or more terms, then it contains terms for purple, pink, orange or gray.

This may be why story Beowulf only contains the colours black, white, and red. It may also be why the Bible does not contain the colour blue. Homer's Odyssey contains black almost 200 times and white about 100 times. Red appears 15 times, while yellow and green appear only 10 times. (More information here)

Differences between languages are also interesting: note the profusion of distinct colour words used by English vs. Chinese. However, digging deeper into these languages shows that each uses colour in distinct ways. (More information)

中文和英文颜色名称。 图片来自“ muyueh.com”

Generally speaking, the naming, use, and grouping of colours in human languages is fascinating. Now, back to the show.

8-bit (256) colours

Technology advanced, and tables of 256 pre-selected colours became available, as shown below.

ANSI转义序列的256位彩色模式

Using these above, you can make pink text like so:

\033[38;5;206m     #That is, \033[38;5;<FG COLOR>m

And make an early-morning blue background using

\033[48;5;57m      #That is, \033[48;5;<BG COLOR>m

And, of course, you can combine these:

\033[38;5;206;48;5;57m

The 8-bit colours are arranged like so:

0x00-0x07:  standard colors (same as the 4-bit colours)
0x08-0x0F:  high intensity colors
0x10-0xE7:  6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
0xE8-0xFF:  grayscale from black to white in 24 steps

#ALL THE COLOURS

Now we are living in the future, and the full RGB spectrum is available using:

\033[38;2;<r>;<g>;<b>m     #Select RGB foreground color
\033[48;2;<r>;<g>;<b>m     #Select RGB background color

So you can put pinkish text on a brownish background using

\033[38;2;255;82;197;48;2;155;106;0mHello

这里列出对“真彩色”终端的支持

以上大部分内容来自Wikipedia页面“ ANSI转义代码”。

方便自己的脚本

由于我经常想记住什么颜色是什么,所以我有一个方便的脚本,叫做~/bin/ansi_colours

#!/usr/bin/python

print "\\033[XXm"

for i in range(30,37+1):
    print "\033[%dm%d\t\t\033[%dm%d" % (i,i,i+60,i+60);

print "\033[39m\\033[39m - Reset colour"
print "\\033[2K - Clear Line"
print "\\033[<L>;<C>H OR \\033[<L>;<C>f puts the cursor at line L and column C."
print "\\033[<N>A Move the cursor up N lines"
print "\\033[<N>B Move the cursor down N lines"
print "\\033[<N>C Move the cursor forward N columns"
print "\\033[<N>D Move the cursor backward N columns"
print "\\033[2J Clear the screen, move to (0,0)"
print "\\033[K Erase to end of line"
print "\\033[s Save cursor position"
print "\\033[u Restore cursor position"
print " "
print "\\033[4m  Underline on"
print "\\033[24m Underline off"
print "\\033[1m  Bold on"
print "\\033[21m Bold off"

此打印

简单的ANSI颜色

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Ansi转义序列更改xterm的颜色?

运行Emacs Shell时可见的ANSI转义序列(这些不是ANSI颜色)

readline与ANSI转义序列混淆

ANSI转义序列中的vim和颜色:如何在vim中显示与cat命令显示的颜色相同的颜色

使用管道从ByteString过滤ANSI转义序列

支持 ANSI 转义序列光标移动

在 Dart 中解码 ANSI 转义序列

在zsh提示中使用ANSI转义序列

有没有一种方法可以改变颜色而无需在C ++中输出ANSI转义序列?

使用a2ps打印ansi颜色/转义码

C ++ Ansi转义码未向控制台显示颜色

为什么ANSI颜色转义以'm'而不是']'结尾?

ANSI转义序列,用于折叠/折叠文本(可能是分层的)

ANSI转义序列保存/恢复光标位置支持

Readline:将ANSI转义序列绑定到密钥

bash程序“监视”和输出中的ANSI转义序列

ANSI转义码序列“ ESC [> c”是什么?

如何发送 ctrl+shift+6 作为 ansi 转义序列?

从输出日志文件中排除 ANSI 转义序列

VT100 ANSI转义序列:获取屏幕尺寸,条件ANSI

使用sed时保留转义序列和颜色

如何使用echo命令输出颜色的转义序列

登录前是否可以发送颜色代码转义序列?

从终端读取ANSI转义

Lua:ANSI 转义模式

如何在Rails 4日志中关闭ANSI转义颜色代码

sqlcmd 可以配置支持 ANSI 转义序列(用于着色打印输出)

[?1049h和[?1h ANSI转义序列]有什么作用?

使用ANSI转义序列“就地”编辑终端内容的正确方法是什么?