ANSI 颜色转义序列列表
在大多数终端上,可以使用 \033
ANSI 转义序列对输出进行着色。
我正在寻找所有支持的颜色和选项(例如明亮和闪烁)的列表。
由于支持它们的终端之间可能存在差异,因此我主要对 xterm 兼容终端支持的序列感兴趣。
On most terminals it is possible to colorize output using the \033
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在寻找的 ANSI 转义序列是“选择图形呈现”子集。所有这些都具有以下形式
:
XXX
是一系列以分号分隔的参数。也就是说,在 C 语言中将文本设置为红色、粗体和下划线(我们将在下面讨论许多其他选项),您可以这样写:
在 C++ 中,您将使用
在 Python3 中
,您将使用 在 Bash 中,您将
在第一部分中 使用将文本设为红色 (
31
)、粗体 (1
)、下划线 (4
),最后一部分清除所有这些 (0 )。
如下表所述,您可以设置大量文本属性,例如粗体、字体、下划线等。
字体效果
n-10
5;
或2;;;
,请参见下面的5;
或2;;;
,见下文2 位颜色
您已经拥有了!
4 位颜色
实现终端颜色的标准始于有限的(4 位)选项。下表列出了各种终端模拟器使用的背景色和前景色的 RGB 值:
使用上面的内容,您可以在绿色背景上制作红色文本(但是为什么?)使用:
11 种颜色(插曲)
在《基本颜色术语:其普遍性和演变》一书中,Brent Berlin 和 Paul Kay 使用从一系列语系的 20 种不同语言收集的数据来识别 11 种可能的基本颜色类别:白色、黑色、红色、绿色、黄色、蓝色、棕色、紫色、粉色、橙色和灰色。
柏林和凯发现,在颜色类别少于最多十一种的语言中,颜色遵循特定的进化模式。该模式如下:
这可能就是为什么故事贝奥武夫只包含黑色、白色和红色。荷马的奥德赛包含大约200次黑色和大约100次白色。红色出现了 15 次,而黄色和绿色只出现了 10 次。 (更多信息请点击此处)
语言之间的差异也很有趣:请注意语言中使用的大量不同颜色词英语与汉语。然而,深入研究这些语言表明,每种语言都以不同的方式使用颜色。 (更多信息)
一般来说人类语言中颜色的命名、使用和分组令人着迷。现在,回到节目。
8 位 (256) 颜色
技术先进,并且可以使用 256 种预选颜色表,如下所示。
使用上面的这些,您可以像这样制作粉红色文本:
并使用制作清晨的蓝色背景
当然,您可以组合这些:
排列 8 位颜色像这样:
所有颜色
现在我们生活在未来,完整的 RGB 光谱可通过以下方式获得:
因此,您可以将粉红色文本放在 使用“真彩色”终端支持的棕色背景
此处列出了
。以上大部分内容取自维基百科页面“ANSI 转义码”。
提醒自己的方便脚本
由于我经常试图记住什么颜色是什么,所以我有一个方便的脚本,名为:
~/bin/ansi_colours
:这将打印
The ANSI escape sequences you're looking for are the Select Graphic Rendition subset. All of these have the form
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:
In C++ you'd use
In Python3 you'd use
and in Bash you'd use
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.
Font Effects
n-10
5;<n>
or2;<r>;<g>;<b>
, see below5;<n>
or2;<r>;<g>;<b>
, see below2-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:
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:
This may be why story Beowulf only contains the colours black, white, and red. 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:
And make an early-morning blue background using
And, of course, you can combine these:
The 8-bit colours are arranged like so:
ALL THE COLOURS
Now we are living in the future, and the full RGB spectrum is available using:
So you can put pinkish text on a brownish background using
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
:This prints
当您编写 ANSI 转义码
\033[m
时,请将
替换为下面的任何颜色代码。例如,\033[31m
将是红色文本颜色:另外,请记住每次要恢复到默认终端时使用
\033[0m
文本样式。否则,任何颜色或样式都可能溢出并进入其他终端消息。对于效果,代码为:
我推荐这些文章来进一步探索:
PS:完全公开,我是 Colorist 包。 Colorist 是轻量级的,可以轻松地在许多终端中打印彩色文本。只需使用
pip install colorist
安装软件包并输入:此外,Colorist 还支持定义为 RGB、HSL 或 Hex 的颜色,如果您的终端支持高级 ANSI 颜色:
调色师的更多选项:
< /a>
When you write a ANSI escape code
\033[<color>m
, replace the<color>
with any of the color codes below. For instance,\033[31m
would be red text color:Also, remember to use
\033[0m
every time you want to revert back to the default terminal text style. Otherwise, any color or styling may spill over and into other terminal messages.For effects, the codes are:
I recommend these articles to explore further:
PS: In full disclosure, I'm the author of the Colorist package. Colorist is lightweight and makes it easy to print colorful text in many terminals. Simply install the package with
pip install colorist
and type:Moreover, Colorist also supports color defined as RGB, HSL or Hex if your terminal supports advanced ANSI colors:
More options with Colorist:
怎么样:
ECMA-48 - 编码字符集的控制函数,第 5 版版(1991 年 6 月) -
定义颜色控制代码的标准,显然 xterm 也支持该标准。
SGR 38 和 48 最初由 ECMA-48 保留,但几年后在 ITU、IEC 和 ISO 联合标准中得到充实,该标准分为几个部分,并且(在很多其他内容中)记录了 SGR 直接颜色和索引颜色的38/48控制序列:
在 ANSI 转义码的 Wikipedia 页面上的此表中有一列 xterm
How about:
ECMA-48 - Control Functions for Coded Character Sets, 5th edition (June 1991) -
A standard defining the color control codes, that is apparently supported also by xterm.
SGR 38 and 48 were originally reserved by ECMA-48, but were fleshed out a few years later in a joint ITU, IEC, and ISO standard, which comes in several parts and which (amongst a whole lot of other things) documents the SGR 38/48 control sequences for direct colour and indexed colour:
There's a column for xterm in this table on the Wikipedia page for ANSI escape codes
对于那些无法获得上述语言以外的正确结果的人,如果您使用 C# 将文本打印到控制台(终端)窗口,则应将 "\033" 替换为 "\ x1b”。在 Visual Basic 中,它是 Chrw(27)。
For these who don't get proper results other than mentioned languages, if you're using C# to print a text into console(terminal) window you should replace "\033" with "\x1b". In Visual Basic it would be Chrw(27).
这和你的终端绝对有关系。 VTE 不支持眨眼,如果您使用
gnome-terminal
、tilda
、guake
、terminator
、xfce4-terminal
等等根据 VTE,你不会有眨眼。如果您在 VTE 上使用或想要使用眨眼,则必须使用
xterm
。您可以使用带有终端名称的 infocmp 命令:
例如:
It's related absolutely to your terminal. VTE doesn't support blink, If you use
gnome-terminal
,tilda
,guake
,terminator
,xfce4-terminal
and so on according to VTE, you won't have blink.If you use or want to use blink on VTE, you have to use
xterm
.You can use infocmp command with terminal name:
For example :
如果您使用 TCC shell(这只需要修改一两行即可与 CMD 一起使用,因为我使用 %@CHAR,这是 TCC 特定的),这里有一个方便的脚本,可让您通过方便的环境变量测试大多数 ansi 。这是我在 Windows 终端上的结果,它支持很多,但不是全部,包括双高和宽线:
If you're using TCC shell (and this only requires modifying a line or two to work with CMD, since i use %@CHAR, which is TCC-specific), here's a handy script that lets you test most ansi via convenient environment variables. Here's my results with Windows Terminal, which supports a lot, but not all, of this, including double-height and wide lines:
下面是一些代码,显示了与颜色有关的所有转义序列。您可能需要获取实际的转义字符才能使代码正常工作。
Here is some code that shows all escape sequences that have to do with color. You might need to get the actual escape character in order for the code to work.