ANSI 颜色转义序列列表 [英] List of ANSI color escape sequences

查看:44
本文介绍了ANSI 颜色转义序列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

您要查找的 ANSI 转义序列是 Select Graphic Rendition 子集.所有这些都有形式

33[XXXm

其中 XXX 是一系列以分号分隔的参数.

比如说,在 C 中将文本设为红色、粗体和下划线(我们将在下面讨论许多其他选项),您可能会这样写:

printf("33[31;1;4mHello33[0m");

在 C++ 中你会使用

std::cout<<"33[31;1;4mHello33[0m";

在 Python3 中你会使用

print("33[31;1;4mHello33[0m")

在 Bash 中你会使用

echo -e "33[31;1;4mHello33[0m";

其中第一部分使文本变为红色 (31)、粗体 (1)、下划线 (4) 和最后一部分清除所有这些(0).

如下表所述,您可以设置大量文本属性,例如粗体、字体、下划线、&c.(是不是

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

33[31;42m

11 种颜色(插曲)

在他们的基本颜色术语:它们的普遍性和演变"一书中,Brent Berlin 和 Paul Kay 使用从一系列语言家族的 20 种不同语言收集的数据来确定 11 种可能的基本颜色类别:白色、黑色、红色、绿色、黄色、蓝色、棕色、紫色、粉色、橙色和灰色.

Berlin 和 Kay 发现,在少于最多 11 种颜色类别的语言中,颜色遵循特定的进化模式.这种模式如下:

  1. 所有语言都包含黑色(冷色)和白色(亮色)的术语.
  2. 如果一种语言包含三个术语,那么它包含一个表示红色的术语.
  3. 如果一种语言包含四个术语,那么它包含一个表示绿色或黄色(但不能同时表示两者)的术语.
  4. 如果一种语言包含五个术语,那么它包含绿色和黄色的术语.
  5. 如果一种语言包含六个术语,则它包含一个表示蓝色的术语.
  6. 如果一种语言包含七个术语,那么它包含一个表示棕色的术语.
  7. 如果一种语言包含八个或更多术语,则它包含紫色、粉红色、橙色或灰色术语.

这可能就是故事 Beowulf 只包含黑色、白色和红色的原因.这也可能是圣经不包含蓝色的原因.荷马的奥德赛包含黑色近 200 次,白色约 100 次.红色出现了 15 次,而黄色和绿色只出现了 10 次.(

总的来说,人类语言中颜色​​的命名、使用和分组很有趣.现在,回到节目.

8 位 (256) 色

技术进步,256种预选颜色的表格可用,如下图.

使用上面的这些,你可以像这样制作粉红色的文字:

33[38;5;206m #即33[38;5;m

并使用

制作清晨的蓝色背景

33[48;5;57m #即33[48;5;<BG COLOR>m

当然,您可以组合这些:

33[38;5;206;48;5;57m

8 位颜色的排列如下:

0x00-0x07:标准色(同4位色)0x08-0x0F:高强度颜色0x10-0xE7:6×6×6立方体(216色):16+36×r+6×g+b(0≤r,g,b≤5)0xE8-0xFF:24步灰度从黑到白

所有颜色

现在我们生活在未来,使用以下方法可以获得完整的 RGB 光谱:

33[38;2;;;m #选择RGB前景色33[48;2;<r>;<g>;<b>m #选择RGB背景色

因此您可以使用

在棕色背景上放置粉红色文本

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

支持真彩色"

On most terminals it is possible to colorize output using the 33 ANSI escape sequence.

I'm looking for a list of all supported colors and options (like bright and blinking).

As there are probably differences between the terminals supporting them, I'm mainly interested in sequences supported by xterm-compatible terminals.

解决方案

The ANSI escape sequences you're looking for are the Select Graphic Rendition subset. All of these have the form

33[XXXm

where XXX is a series of semicolon-separated parameters.

To say, make text red, bold, and underlined (we'll discuss many other options below) in C you might write:

printf("33[31;1;4mHello33[0m");

In C++ you'd use

std::cout<<"33[31;1;4mHello33[0m";

In Python3 you'd use

print("33[31;1;4mHello33[0m")

and in Bash you'd use

echo -e "33[31;1;4mHello33[0m"

where the first part makes the text red (31), bold (1), underlined (4) and the last part clears all this (0).

As described in the table below, there are a large number of text properties you can set, such as boldness, font, underlining, &c. (Isn't it silly that StackOverflow doesn't allow you to put proper tables in answers?)

Font Effects

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-bit Colours

You've got this already!

4-bit Colours

The standards implementing terminal colours began with limited (4-bit) options. The table below lists the RGB values of the background and foreground colours used for these by a variety of terminal emulators:

Using the above, you can make red text on a green background (but why?) using:

33[31;42m

11 Colours (An Interlude)

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)

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.

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

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

And make an early-morning blue background using

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

And, of course, you can combine these:

33[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:

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

So you can put pinkish text on a brownish background using

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

Support for "true color" terminals is listed here.

Much of the above is drawn from the Wikipedia page "ANSI escape code".

A Handy Script to Remind Yourself

Since I'm often in the position of trying to remember what colours are what, I have a handy script called: ~/bin/ansi_colours:

#!/usr/bin/python

print "\033[XXm"

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

print "33[39m\033[49m - 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"

This prints

这篇关于ANSI 颜色转义序列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆