Thi*_*ter 185 terminal colors ansi-escape
在大多数终端上(当然,除了Windows的cmd.exe之外),可以使用\033
ANSI转义序列对输出进行着色.
现在我正在寻找所有支持的颜色和选项列表(如明亮和闪烁).
由于支持它们的终端之间可能存在一些差异:我主要对xterm兼容终端支持的序列感兴趣.
Ric*_*ard 396
您正在寻找的ANSI转义序列是Select Graphic Rendition子集.所有这些都有形式
\033[XXXm
Run Code Online (Sandbox Code Playgroud)
其中XXX
是一系列以分号分隔的参数.
要说,你可以写下C中的文字红色,粗体和下划线(我们将在下面讨论下面的许多其他选项):
printf("\033[31;1;4mHello\033[0m");
Run Code Online (Sandbox Code Playgroud)
在C++中你会使用
std::cout<<"\033[31;1;4mHello\033[0m";
Run Code Online (Sandbox Code Playgroud)
在Python3中你会使用
print("\033[31;1;4mHello\033[0m")
Run Code Online (Sandbox Code Playgroud)
在Bash中你会使用
echo -e "\033[31;1;4mHello\033[0m"
Run Code Online (Sandbox Code Playgroud)
第一部分使文本为red(31
),bold(1
),underlined(4
),最后一部分使所有this(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) ?
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
你已经有了这个!
实现终端颜色的标准始于有限的(4位)选项.下表列出了各种终端仿真器使用的背景和前景颜色的RGB值:
使用上面的内容,你可以在绿色背景上制作红色文字(但为什么?)使用:
\033[31;42m
Run Code Online (Sandbox Code Playgroud)
在他们的书"基本颜色术语:他们的普遍性和进化"中,布伦特柏林和保罗凯使用从一系列语言家族的二十种不同语言收集的数据来识别十一种可能的基本颜色类别:白色,黑色,红色,绿色,黄色,蓝色,棕色,紫色,粉色,橙色和灰色.
柏林和凯发现,在最多十一种颜色类别的语言中,颜色遵循特定的进化模式.这种模式如下:
这可能就是故事Beowulf只包含黑色,白色和红色的原因.这也可能是圣经不包含蓝色的原因.荷马的奥德赛包含黑色近200倍,白色约100倍.红色出现15次,而黄色和绿色只出现10次.(更多信息在这里)
语言之间的差异也很有趣:请注意英语与中文使用的不同颜色词的丰富程度.然而,深入研究这些语言表明每种语言都以不同的方式使用颜色.(更多信息)
一般来说,人类语言中颜色的命名,使用和分组是非常有趣的.现在,回到节目.
技术先进,并提供256种预选颜色的表格,如下所示.
使用上面这些,您可以像这样制作粉红色文字:
\033[38;5;206m #That is, \033[38;5;<FG COLOR>m
Run Code Online (Sandbox Code Playgroud)
并使用清晨的蓝色背景
\033[48;5;57m #That is, \033[48;5;<BG COLOR>m
Run Code Online (Sandbox Code Playgroud)
当然,你可以结合这些:
\033[38;5;206;48;5;57m
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
现在我们生活在未来,完整的RGB光谱可用于:
\033[38;2;<r>;<g>;<b>m #Select RGB foreground color
\033[48;2;<r>;<g>;<b>m #Select RGB background color
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用粉红色文本在棕色背景上
\033[38;2;255;82;197;48;2;155;106;0mHello
Run Code Online (Sandbox Code Playgroud)
以上大部分内容来自维基百科页面" ANSI转义码 ".
Jak*_*erp 27
当您编写 ANSI 转义代码 时\033[<color>m
,请将 替换<color>
为下面的任何颜色代码。例如,\033[31m
文本颜色为红色:
颜色 | 例子 | 文本 | 背景 | 明亮的文字 | 明亮的背景 |
---|---|---|---|---|---|
黑色的 | ![]() |
30 | 40 | 90 | 100 |
红色的 | ![]() |
31 | 41 | 91 | 101 |
绿色的 | ![]() |
32 | 42 | 92 | 102 |
黄色的 | ![]() |
33 | 43 | 93 | 103 |
蓝色的 | ![]() |
34 | 44 | 94 | 104 |
品红 | ![]() |
35 | 45 | 95 | 105 |
青色 | ![]() |
36 | 46 | 96 | 106 |
白色的 | ![]() |
37 | 47 | 97 | 107 |
默认 | 39 | 49 | 99 | 109 |
\033[0m
另外,请记住在每次想要恢复为默认终端文本样式时使用。否则,任何颜色或样式都可能溢出并进入其他终端消息。
对于效果,代码是:
影响 | 在 | 离开 | 例子 |
---|---|---|---|
大胆的 | 1 | 21 | ![]() |
暗淡 | 2 | 22 | ![]() |
强调 | 4 | 24 | |
眨 | 5 | 25 | |
撤销 | 7 | 27 | |
隐藏 | 8 | 28 |
我推荐这些文章来进一步探索:
PS:坦白说,我是Colorist包的作者。Colorist是轻量级的,可以轻松地在许多终端中打印彩色文本。只需使用以下命令安装软件包并pip install colorist
输入:
from colorist import Color
print(f"Only {Color.CYAN}this part{Color.OFF} is in colour")
Run Code Online (Sandbox Code Playgroud)
此外,如果您的终端支持高级 ANSI 颜色,Colorist还支持定义为 RGB、HSL 或 Hex 的颜色:
from colorist import ColorRGB, BgColorRGB
dusty_pink = ColorRGB(194, 145, 164)
bg_steel_blue = BgColorRGB(70, 130, 180)
print(f"I want to use {dusty_pink}dusty pink{dusty_pink.OFF} and {bg_steel_blue}steel blue{bg_steel_blue.OFF} colors inside this paragraph")
Run Code Online (Sandbox Code Playgroud)
from colorist import ColorHSL, BgColorHSL
mustard_green = ColorHSL(60, 56, 43)
bg_steel_gray = BgColorHSL(190, 2, 49)
print(f"I want to use {mustard_green}mustard green{mustard_green.OFF} and {bg_steel_gray}steel blue{bg_steel_gray.OFF} colors inside this paragraph")
Run Code Online (Sandbox Code Playgroud)
from colorist import ColorHex, BgColorHex
watermelon_red = ColorHex("#ff5733")
bg_mint_green = BgColorHex("#99ff99")
print(f"I want to use {watermelon_red}watermelon pink{watermelon_red.OFF} and {bg_mint_green}mint green{bg_mint_green.OFF} colors inside this paragraph")
Run Code Online (Sandbox Code Playgroud)
Colorist的更多选项:
sin*_*law 12
怎么样:
ECMA-48 - 编码字符集的控制功能,第5版(1991年6月) - 定义颜色控制代码的标准,显然也是由xterm支持的.
SGR 38和48最初由ECMA-48保留,但几年后在国际电联,IEC和ISO联合标准中得到了充实,该标准有几个部分,其中(在很多其他事项中)记录了SGR 38/48 直接颜色和索引颜色的控制序列:
在维基百科页面上有一个用于ANSI转义码的 xterm列
还有一些更有趣的相关信息.
对于那些除上述语言之外无法获得正确结果的人,如果您使用 C# 将文本打印到控制台(终端)窗口,则应将“\033”替换为“\x1b ”。在 Visual Basic 中,它是Chrw(27)。
它与您的终端绝对相关。VTE不支持blink,根据VTE使用gnome-terminal
, tilda
, guake
, terminator
,xfce4-terminal
等,则不会出现blink。
如果您在 VTE 上使用或想要使用闪烁,则必须使用xterm
.
您可以使用带有终端名称的 infocmp 命令:
#infocmp vt100
#infocmp xterm
#infocmp vte
Run Code Online (Sandbox Code Playgroud)
例如 :
# 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,
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
157094 次 |
最近记录: |