为了账号安全,请及时绑定邮箱和手机立即绑定

ANSI颜色转义序列列表

ANSI颜色转义序列列表

在大多数终端上,可以使用\033ANSI转义序列对输出进行着色。我正在寻找所有受支持的颜色和选项(如明亮和闪烁)的列表。由于支持它们的终端之间可能存在差异,因此我主要对xterm兼容终端支持的序列感兴趣。
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

您要查找的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种颜色(插曲)

布伦特·柏林(Brent Berlin)和保罗·凯(Paul Kay)在他们的“基本颜色术语:它们的普遍性和演变”一书中,使用了从来自多种语言族的二十种不同语言中收集的数据来识别11种可能的基本颜色类别:白色,黑色,红色,绿色,黄色,蓝色,棕色,紫色,粉红色,橙色和灰色。


柏林和凯发现,在少于最多11种颜色类别的语言中,颜色遵循特定的进化模式。此模式如下:


所有语言均包含黑色(冷色)和白色(亮色)的术语。

如果一种语言包含三个术语,则它包含一个红色术语。

如果一种语言包含四个术语,则它包含一个绿色或黄色(但不能同时包含)的术语。

如果一种语言包含五个术语,则它同时包含绿色和黄色的术语。

如果一种语言包含六个词,则它包含一个蓝色词。

如果一种语言包含七个术语,则它包含一个棕色术语。

如果一种语言包含八个或更多的术语,则它包含紫色,粉红色,橙色或灰色的术语。

这可能就是为什么故事Beowulf仅包含黑色,白色和红色的原因。这也可能就是圣经不包含蓝色的原因。荷马的奥德赛包含黑色几乎200倍和白色大约100倍。红色出现15次,而黄色和绿色出现10次。(更多信息在这里)


语言之间的差异也很有趣:请注意,英语和汉语使用了大量不同颜色的单词。但是,深入研究这些语言表明每种语言都以不同的方式使用颜色。(更多信息)


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


一般来说,人类语言中颜色的命名,使用和分组是很有趣的。现在,回到表演。


8位(256)颜色

技术先进,并且提供了256种预选颜色的表格,如下所示。


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


使用上面的这些,您可以像这样制作粉色文本:


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

并使用


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

并且,当然,您可以将这些结合起来:


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

8位颜色的排列方式如下:


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

所有颜色

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


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

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

因此,您可以使用


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

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


以上大部分内容来自维基百科页面“ 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"


查看完整回答
反对 回复 2019-11-21
  • 3 回答
  • 0 关注
  • 1021 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信