ANSI颜色转义序列的列表

在大多数terminal(当然除了Windows的cmd.exe),可以使用\033 ANSI转义序列对输出进行着色。

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

由于支持它们的terminal之间可能存在一些差异:我主要感兴趣的是由xterm兼容terminal支持的那些序列。

这个页面有一个很好的总结:

ANSI转义序列

您正在查找的ANSI转义序列是Select Graphic Rendition子集。 所有这些都有forms

 \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位颜色

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

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

使用上面的,你可以使绿色背景上的红色文字(但为什么?)使用:

 \033[31;42m 

11种颜色(半严重插曲)

Brent Berlin和Paul Kay在他们的书“Basic Color Terms:他们的普遍性和演变”中使用了来自一系列公司的20个不同terminal模拟器收集的数据,以确定十一种可能的基本颜色类别:白色,黑色,红色,绿色,蓝色,棕色,紫色,粉红色,橙色和灰色。

柏林和凯发现,在颜色类别最多不超过11​​种的模拟器中,颜色遵循特定的演变模式。 这种模式如下:

  1. 所有模拟器都包含黑色(冷色)和白色(亮色)的术语。
  2. 如果一个模拟器包含三个术语,那么它包含一个红色术语。
  3. 如果仿真器包含四个术语,则它包含绿色或黄色(但不是两者)的术语。
  4. 如果模拟器包含五个术语,则它包含绿色和黄色的术语。
  5. 如果模拟器包含六个术语,则它包含一个蓝色的术语。
  6. 如果一个模拟器包含七个术语,那么它包含一个棕色术语。
  7. 如果模拟器包含八个或更多的术语,则它包含紫色,粉红色,橙色或灰色的术语。

这可能是为什么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 

此处列出了对“真彩色”terminal的支持。

以上大部分是从维基百科页面“ ANSI转义代码 ”中提取的。

怎么样:

ECMA-48 – 编码字符集的控制函数,第5版(1991年6月) – 定义颜色控制代码的一个标准,显然也受xterm支持。

ANSI转义代码在维基百科页面的表格中有一个xterm列

这与您的terminal绝对相关。 VTE不支持眨眼,如果您使用gnome-terminaltildaguaketerminatorxfce4-terminal等符合VTE,您将不会眨眼。
如果你想使用闪烁VTE,你必须使用xterm命令。
您可以使用带terminal名称的infocmp命令:

 #infocmp vt100 #infocmp xterm #infocmp vte 

例如 :

 # infocmp vte # Reconstructed via infocmp from file: /usr/share/terminfo/v/vte vte|VTE aka GNOME Terminal, am, bce, mir, msgr, xenl, colors#8, cols#80, it#8, lines#24, ncv#16, pairs#64, acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~, bel=^G, bold=\E[1m, civis=\E[?25l, clear=\E[H\E[2J, cnorm=\E[?25h, cr=^M, csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H, cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C, cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A, dch=\E[%p1%dP, dch1=\E[P, dim=\E[2m, dl=\E[%p1%dM, dl1=\E[M, ech=\E[%p1%dX, ed=\E[J, el=\E[K, enacs=\E)0, home=\E[H, hpa=\E[%i%p1%dG, ht=^I, hts=\EH, il=\E[%p1%dL, il1=\E[L, ind=^J, invis=\E[8m, is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H, kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\E[E, kbs=\177, kcbt=\E[Z, kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA, kdch1=\E[3~, kend=\EOF, kf1=\EOP, kf10=\E[21~, kf11=\E[23~, kf12=\E[24~, kf13=\E[1;2P, kf14=\E[1;2Q, kf15=\E[1;2R, kf16=\E[1;2S, kf17=\E[15;2~, kf18=\E[17;2~, kf19=\E[18;2~, kf2=\EOQ, kf20=\E[19;2~, kf21=\E[20;2~, kf22=\E[21;2~, kf23=\E[23;2~, kf24=\E[24;2~, kf25=\E[1;5P, kf26=\E[1;5Q, kf27=\E[1;5R, kf28=\E[1;5S, kf29=\E[15;5~, kf3=\EOR, kf30=\E[17;5~, kf31=\E[18;5~, kf32=\E[19;5~, kf33=\E[20;5~, kf34=\E[21;5~, kf35=\E[23;5~, kf36=\E[24;5~, kf37=\E[1;6P, kf38=\E[1;6Q, kf39=\E[1;6R, kf4=\EOS, kf40=\E[1;6S, kf41=\E[15;6~, kf42=\E[17;6~, kf43=\E[18;6~, kf44=\E[19;6~, kf45=\E[20;6~, kf46=\E[21;6~, kf47=\E[23;6~, kf48=\E[24;6~, kf49=\E[1;3P, kf5=\E[15~, kf50=\E[1;3Q, kf51=\E[1;3R, kf52=\E[1;3S, kf53=\E[15;3~, kf54=\E[17;3~, kf55=\E[18;3~, kf56=\E[19;3~, kf57=\E[20;3~, kf58=\E[21;3~, kf59=\E[23;3~, kf6=\E[17~, kf60=\E[24;3~, kf61=\E[1;4P, kf62=\E[1;4Q, kf63=\E[1;4R, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~, kfnd=\E[1~, khome=\EOH, kich1=\E[2~, kind=\E[1;2B, kmous=\E[M, knp=\E[6~, kpp=\E[5~, kri=\E[1;2A, kslt=\E[4~, meml=\El, memu=\Em, op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM, ritm=\E[23m, rmacs=^O, rmam=\E[?7l, rmcup=\E[2J\E[?47l\E8, rmir=\E[4l, rmkx=\E[?1l\E>, rmso=\E[m, rmul=\E[m, rs1=\Ec, rs2=\E7\E[r\E8\E[m\E[?7h\E[!p\E[?1;3;4;6l\E[4l\E>\E[?1000l\E[?25h, sc=\E7, setab=\E[4%p1%dm, setaf=\E[3%p1%dm, sgr=\E[0%?%p6%t;1%;%?%p2%t;4%;%?%p5%t;2%;%?%p7%t;8%;%?%p1%p3%|%t;7%;m%?%p9%t\016%e\017%;, sgr0=\E[0m\017, sitm=\E[3m, smacs=^N, smam=\E[?7h, smcup=\E7\E[?47h, smir=\E[4h, smkx=\E[?1h\E=, smso=\E[7m, smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n, u8=\E[?%[;0123456789]c, u9=\E[c, vpa=\E[%i%p1%dd,