更改

跳到导航 跳到搜索

ANSI转义序列

添加59,073字节2019年9月20日 (五) 20:07
创建页面,内容为“'''ANSI转义序列'''是一种带内信号转义序列标准,用于控制视频文本终端上的光标位置、颜色和其他选项。在…”
'''ANSI转义序列'''是一种[[带内信号]]的[[转义序列]]标准,用于控制视频文本[[終端|终端]]上的光标位置、颜色和其他选项。在文本中嵌入确定的字节序列,大部分以<code>[[退出键|ESC]]</code>[[转义字符]]和"["字符开始,终端会把这些字节序列解释为相应的指令,而不是普通的[[字符编码]]。

ANSI序列是在二十世纪七十年代引入的标准,用以取代特定于终端供应商的序列,并在二十世纪八十年代早期开始在计算机设备市场上广泛使用。与早期缺少光标移动功能的系统相比,新生的[[电子公告板系统]]使用ANSI序列改进其显示。正是因为这个原因,ANSI序列变成了所有制造商共同采用的标准。

在21世纪,尽管硬件文本终端已经越来越少了,但ANSI标准依然存在,因为大多数终端模拟器会对部分ANSI转义序列进行解释。一个值得注意的例外是,在微软[[Windows 10]]更新TH2之前,[[Windows操作系统]]的[[Win32控制台]]是不支持ANSI转义序列的。

== 历史 ==
最初,几乎每个视频终端制造商都各自添加了特定的转义序列用于执行一些特殊操作,比如把光标置于屏幕上的某个位置。举例来说,{{Tsl|en|VT52}}终端允许通过发送<code>ESC</code>字符、<code>y</code>字符,后面跟上两个等于x,y位置的数值加上32的字符(这是为了从ASCII空格字符开始,并避开控制字符),将光标置于屏幕上的x,y位置。

由于这些序列对于不同的终端并不一样,因此人们不得不开发了一些复杂的库(比如{{Tsl|en|termcap}})和实用程序(比如{{Tsl|en|tput}}),以便程序可以使用同一套[[API]]应对各种终端。另外,在很多终端中需要借助字符的二进制值发送数字(如行和列)。对于某些编程语言,以及内部不使用ASCII的系统来说,把数字转换为正确的字符常常是有困难的,甚至完全做不到。

ANSI标准试图解决这些问题。标准制订了一种所有终端共用的指令集,并要求用ASCII的数字字符传递所有数值信息。该系列的第一个标准是1976年通过的ECMA-48。它是一系列字符编码标准的延续,其中第一个是从1965年的{{Tsl|en|ECMA-6}},一个7[[位元|位]]标准,[[ISO 646]]就源自此标准。“ANSI转义序列”的名称可以追溯到1979年[[ANSI]]采用ANSI X3.64。此外,ANSI X3L2委员会与[[ECMA]]委员会TC 1合作制订了一个几乎一模一样的标准。以上两个标准合并为ISO 6429的国际标准<ref>[http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-48,%202nd%20Edition,%20August%201979.pdf Historical version of ECMA-48]</ref>。1994年,ANSI取消了其标准,以支持国际标准。

第一个支持这个标准的流行视频终端是1978年推出的[[迪吉多|Digital]] {{Tsl|en|VT100}}<ref>{{cite web |url=http://vt100.net/vt_history |title=Digital's Video Terminals |publisher=VT100.net |author-first=Paul |author-last=Williams |date=2006 |access-date=2011-08-17 }}{{dead link|date=2018年2月 |bot=InternetArchiveBot |fix-attempted=yes }}</ref>。这个终端在市场上非常成功,引发了各种各样的仿制品,其中最早和最流行的是1979年的{{tsl|en|Zenith Z-89|Zenith Z-19}}<ref>{{cite web |url=http://www.pestingers.net/Computer_history/Computers_79.htm |title=Heathkit Catalog 1979 |author=Heathkit Company |date=1979 |publisher=Heathkit Company |access-date=2011-11-04 |dead-url=yes |archive-url=https://web.archive.org/web/20120113230301/http://www.pestingers.net/Computer_history/Computers_79.htm |archive-date=2012-01-13}}</ref><!-- Document at http://www.retroarchive.org/hardware/heathkit/H19CODES.DOC shows that you "Enter ANSI mode" with ESC <, but does not document the result --><!-- H-19 Esc < switches into ANSI mode, requiring a proprietary ANSI extended escape code to return to VT52 (Heathkit) mode. -->。其他品牌还有{{Tsl|en|Qume}} QVT-108,{{Tsl|en|Televideo}} TVI-970,{{Tsl|en|Wyse}} WY-99GT。另外,许多其他品牌的终端也不同程度地兼容可选的“VT100”、“VT103”或“ANSI”模式。 随着越来越多的软件(尤其是[[BBS|BBS系统]])普及,越来越多的软件依赖转义序列起作用,导致几乎所有新的终端和终端模拟器都支持了此标准。

1981年,ANSI X3.64被美国政府采用([[联邦资料处理标准|FIPS]] 86)。后来,美国政府停止复制行业标准,所以FIPS 86又被撤回了<ref>[https://www.nist.gov/itl/upload/Withdrawn-FIPS-by-Numerical-Order-Index2.pdf Withdrawn FIPS Listed by Number]</ref>。

ECMA-48已经经历了多次更新换代,目前是从1991年开始的第5版。它也被[[ISO]]和[[IEC]]用作标准'''ISO/IEC 6429'''。

==平台支持==
随着诸多[[BBS]]和线上服务广泛使用ANSI,到20世纪80年代中期,ANSI几乎得到了全平台支持。尽管许多操作系统在标准文本输出中越来越多地支持ANSI,但大多数情况下是以终端模拟器的形式(例如[[Unix]]上的[[xterm]],或MacOS上的[[终端 (macOS)|OS X Terminal]]或{{Tsl|en|ZTerm}},以及IBM PC上的许多通信程序)。

[[Unix]]和[[AmigaOS]]都在操作系统中包含了对ANSI的一些支持,导致在这些平台上运行的程序广泛使用ANSI。 [[类Unix操作系统]]可以通过像{{Tsl|en|termcap}}和{{Tsl|en|curses (programming library)|curses函式庫}}之类的库来生成ANSI代码,许多软件使用这些库升级显示方式。这些库也应该支持非ANSI终端,但是现在很少有人测试,所以很可能已经不起作用了{{citation needed|date=July 2014}}。许多游戏和shell脚本直接输出ANSI序列(如彩色的提示信息),因此无法在不支持ANSI的终端上运行。

AmigaOS不仅支持输出到屏幕上的文本使用ANSI序列,打印机驱动程序也支持(用AmigaOS的专有扩展),并将它们转换为与特定打印机实际通信所需的代码<ref>{{cite web |url=http://wiki.amigaos.net/wiki/Printer_Device#Printer_Command_Definitions |title=Amiga Printer Command Definitions |publisher=Commodore |access-date=2013-07-10}}</ref>。

尽管ANSI很普及,却并没有得到全平台支持。比如原始的[[Classic Mac OS|“经典”Mac OS]]就没有内置对ANSI的支持<!-- Does it even have text output??? -->,再比如Atari ST使用的是VT52改编的命令系统,用一些扩展程序支持颜色显示<ref>[https://books.google.com/books?id=Z0ejBQAAQBAJ&pg=PA88 "Using C-Kermit"], p. 88.</ref>。

===Windows和DOS===
MS-DOS 1.x不支持ANSI或任何其他转义序列,只有少数[[控制字符]](BEL、CR、LF、BS)可以由底层BIOS<!-- INT 10h/AH=0Eh -->解释,所以几乎<ref group="nb">屏幕显示可以通过从底部绘制整个新屏幕的内容来替代,滚动上一屏幕以充分擦除所有旧文本内容。用户会看到滚动,硬件[[光标]]会留在最底部。 一些早期的[[批处理文件]]以这种方式实现了基本的“全屏”显示。</ref>不可能做出任何全屏应用程序。所有显示效果都必须通过BIOS调用,或者直接控制IBM PC硬件来完成,调用速度非常慢。

DOS 2.0引入了添加[[设备驱动程序]]来支持ANSI转义序列的功能(事实上的标准是ANSI.SYS,但也使用了ANSI.COM<ref name="ANSI.COM">{{cite web |url=https://www.pcmag.com/article2/0,2817,5343,00.asp |title=ANSI.com: Download It Here |journal={{tsl|en|PC Magazine}} |author-first=Michael |author-last=Mefford |date=1989-02-07 |access-date=2011-08-10}}</ref>、NANSI.SYS<ref name="NANSI.SYS">{{cite web |url=http://www.kegel.com/nansi/ |title=Nansi and NNansi – ANSI Drivers for MS-DOS |publisher=Dan Kegel's Web Hostel |author-first1=Dan |author-last1=Kegel |author-first2=Eric |author-last2=Auer |date=1999-02-28 |access-date=2011-08-10}}</ref>和ANSIPLUS.EXE等其他程序。因为绕过了BIOS,所以这些程序的速度比以前快了不少)。但由于实际运行速度仍然比较慢,以及默认并没有安装,所以还是很少得到利用。应用程序往往还是继续用直接控制硬件的方式来显示所需的文本{{Citation needed|date=August 2011}}。ANSI.SYS和类似的驱动程序继续在[[Windows 9x]]上工作,直到[[Windows Me]],在NT衍生系统中用于在{{Tsl|en|NTVDM}}下执行的16位传统程序。

[[Win32控制台]]完全不支持ANSI转义序列。不过有一些控制台的替代品或者附加软件具有解释程序输出的ANSI转义序列的功能,例如JP Software的TCC(以前的4NT)、Michael J. Mefford的ANSI.COM、Jason Hood的ANSICON<ref name="Ansicon">{{cite web |url=https://github.com/adoxa/ansicon |title=Process ANSI escape sequences for Windows console programs |publisher=Jason Hood's Home page
|author-first=Jason |author-last=Hood |date=2005 |access-date=2013-05-09}}</ref>和Maximus5的ConEmu。有一个[[Python]]软件包<ref>{{cite web |url=https://pypi.python.org/pypi/colorama |title=colorama 0.2.5 : |work=Python Package Index |access-date=2013-08-17}}</ref>在内部解释了打印文本中的ANSI转义序列,将它们转换为系统调用来操纵颜色和光标位置,以便更容易地将使用ANSI的Python代码移植到Windows。

2016年,在[[Windows 10]]发布“Threshold 2”<ref name="win10th2">{{cite web |url=http://www.nivot.org/blog/post/2016/02/04/Windows-10-TH2-(v1511)-Console-Host-Enhancements |title=Windows 10 TH2 (v1511) Console Host Enhancements |author-first=Oisin |author-last=Grehan |date=2016-02-04 |access-date=2016-02-10}}</ref>时,微软开始在控制台应用程序中支持ANSI转义序列,使得从Unix移植软件或者远程访问Unix变得更容易。

==转义序列==
序列具有不同的长度。所有序列都以ASCII字符<code>'''[[退出键|ESC]]'''</code>(27 / [[十六进制]] 0x1B)开头,第二个字节则是0x40–0x5F(ASCII <code>@A–Z[\]^_</code>)范围内的字符。<ref name="ecma48"/>{{Rp|5.3.a}}

标准规定,在8位环境中,这两个字节的序列可以合并为0x80-0x9F范围内的单个字节(详情请参阅[[C0与C1控制字符#C1控制字符集|C1控制字符集]])。但是,在现代设备上,这些代码通常用于其他目的,例如[[UTF-8]]的一部分或[[CP-1252]]字符,因此并不使用这种合并的方式。

除ESC之外的其他C0代码(通常是BEL,BS,CR,LF,FF,TAB,VT,SO和SI)在输出时也可能会产生与某些控制序列相似或相同的效果。

{| class="wikitable"
|+ 一些ANSI转义序列(不完整列表)
|-
! 序列 !! C1 !! 名称 !! 作用
|- id=SS2
| {{nobr|ESC N}} || 0x8e || {{nobr|SS2 – Single Shift Two}} || rowspan=2 | 从其中一个替代字符集中选择一个字符。在xterm中,SS2选择G2字符集,SS3选择G3字符集。<ref name="xtc"/>
|- id=SS3
| {{nobr|ESC O}} || 0x8f || {{nobr|SS3 – Single Shift Three}}
|- id=DCS
| {{nobr|ESC P}} || 0x90 || {{nobr|DCS – 设备控制字符串(Device Control String)}} || 控制设备。在xterm中,这个序列的使用包括定义用户自定义的密钥,以及请求或设置Termcap/Terminfo数据。<ref name="xtc"/>
|- id=CSI
| {{nobr|ESC [}} || 0x9b || {{nobr|CSI - 控制序列导入器(Control Sequence Introducer)}} || 大部分有用的序列,请参阅下一节。结束于ASCII 64到126 (<code>@</code>到<code>~</code>/十六进制0x40到0x7E).<ref name="ecma48"/>
|- id=ST
| {{nobr|ESC \}} || 0x9c || {{nobr|ST – 字符串终止(String Terminator)}} || 终止其他控件(包括APC,DCS,OSC,PM和SOS)中的字符串。<ref name="ecma48">{{cite web |url=http://www.ecma-international.org/publications/standards/Ecma-048.htm |title=Standard ECMA-48: Control Functions for Coded Character Sets |edition=Fifth |date=June 1991 |institution=[[Ecma International]]}}</ref>{{Rp|8.3.143}}
|- id=OSC
| {{nobr|ESC ]}} || 0x9d || {{nobr|OSC – 操作系统命令(Operating System Command)}} || 启动操作系统使用的控制字符串。OSC序列与CSI序列相似,但不限于整数参数。通常,这些控制序列由ST终止<ref name="ecma48"/>{{Rp|8.3.89}}。在xterm中,它们也可能被BEL终止<ref name="xtc"/>。例如,在xterm中,窗口标题可以这样设置:<code>OSC 0;this is the window title BEL</code>。
|- id=SOS
| {{nobr|ESC X}} || 0x98 || {{nobr|SOS – 字符串开始(Start of String)}} || rowspan=3 | 引用由ST终止的一串文本的参数。这些字符串控制序列的用途由应用程序<ref name="ecma48"/>{{Rp|8.3.2,8.3.128}}或私有规则来定义<ref name="ecma48"/>{{Rp|8.3.94}}。这些函数没有实现,参数被xterm忽略<ref name="xtc"/>。
|- id=PM
| {{nobr|ESC ^}} || 0x9e || {{nobr|PM – 私有消息(Privacy Message)}}
|- id=APC
| {{nobr|ESC _}} || 0x9f || {{nobr|APC – 应用程序命令(Application Program Command)}}
|- id=RIS
| {{nobr|ESC c}} || || {{nobr|RIS – 重置为初始状态(Reset to Initial State)}} ||将设备重置为原始状态。可能包括(如果适用的话):重置图形格式,清除制表符,重置为默认字体等等。

|}

按下键盘上的特殊键,以及输出xterm CSI、DCS或OSC序列,常常用于产生从终端发送到计算机的CSI,DCS或OSC序列,就像用户使用键盘输入的一样。

<!-- Parked a number of anchors here which are used in redirects to this page but are not yet discussed inhere. Move them a better place when the corresponding info gets incorporated here at a later stage -->
{{anchor|CBT|CCH|CHT|CSI|CTC|CVT}}
{{anchor|DA|DAQ|DCH|DL|DMI|EA|ECH|EF|EMI}}
{{anchor|EPA|ESA|FNT|GSM|GSS|HPA|HPR|HTJ|HTS|ICH}}
{{anchor|IL|IND|INT|JFY|MC|MW|NEL|NP|OSC|PLD}}
{{anchor|PLU|PP|PU1|PU2|QUAD|REP|RI|RIS|RM}}
{{anchor|SEM|SL|SM|SPA|SPI|SR|SSA}}
{{anchor|STS|TBC|TSS|VPA|VPR|VTS}}
{{anchor|SKR|KR}}

== CSI序列 ==
CSI序列由<code>ESC [</code>、若干个(包括0个)“参数字节”、若干个“中间字节”,以及一个“最终字节”组成。各部分的字符范围如下:

{| class="wikitable"
|+ {{nobr|CSI序列在<code>ESC [</code>之后各个组成部分的字符范围<ref name="ecma48"/>{{Rp|5.4}}}}
! 组成部分 !! 字符范围 !! ASCII
|-
| 参数字节 || 0x30–0x3F || <code>0–9:;<=>?</code>
|-
| 中间字节 || 0x20–0x2F || 空格、<code>!"#$%&'()*+,-./</code>
|-
| 最终字节 || 0x40–0x7E || <code><nowiki>@A–Z[\]^_`a–z{|}~</nowiki></code>
|}

所有常见的序列都只是把参数用作一系列分号分隔的数字,如<code>1;2;3</code>。缺少的数字视为0(如<code>1;;3</code>相当于中间的数字是0,<code>ESC[m</code>这样没有参数的情况相当于参数为0)。某些序列(如CUU)把0视为1,以使缺少参数的情况下有意义{{Rp|F.4.2}}。

一部分字符定义是“私有”的,以便终端制造商可以插入他们自己的序列而不与标准相冲突。包括参数字节<code><=>?</code>的使用,或者最终字节0x70–0x7F(<code><nowiki>p–z{|}~</nowiki></code>)例如{{tsl|en|VT320}}序列<code>CSI?25h</code>和<code>CSI?25l</code>的作用是打开和关闭光标的显示。

当CSI序列含有超出0x20–0x7E范围的字符时,其行为是未定义的。这些非法字符包括C0控制字符(范围0–0x1F)、DEL(0x7F),以及高位字节。

{| class="wikitable"
|+ 一些ANSI控制序列(不完整列表)
|-
! 代码 !! 名称 !! 作用
{{anchor|CUU}}
|-
| CSI <code>n</code> A || CUU – 光标上移(Cursor Up) || rowspan=4 | 光标向指定的方向移动<math>n</math>(默认1)格。如果光标已在屏幕边缘,则无效。
{{anchor|CUD}}
|-
| CSI <code>n</code> B || CUD – 光标下移(Cursor Down)
{{anchor|CUF}}
|-
| CSI <code>n</code> C || CUF – 光标前移(Cursor Forward)
{{anchor|CUB}}
|-
| CSI <code>n</code> D || CUB – 光标后移(Cursor Back)
|- id=CNL
| CSI <code>n</code> E || CNL – 光标移到下一行(Cursor Next Line) || 光标移动到下面第<math>n</math>(默认1)行的开头。(非{{Tsl|en|ANSI.SYS}})
|- id=CPL
| CSI <code>n</code> F || CPL – 光标移到上一行(Cursor Previous Line) || 光标移动到上面第<math>n</math>(默认1)行的开头。(非ANSI.SYS)
|- id=CHA
| CSI <code>n</code> G || {{nobr|CHA – 光标水平绝对(Cursor Horizontal Absolute)}} || 光标移动到第<math>n</math>(默认1)列。(非ANSI.SYS)
{{anchor|CUP}}
|-
| {{nowrap|CSI <code>n</code> ; <code>m</code> H}} || CUP – 光标位置(Cursor Position) || 光标移动到第<math>n</math>行、第<math>m</math>列。值从1开始,且默认为<code>1</code>(左上角)。例如<code>CSI ;5H</code>和<code>CSI 1;5H</code>含义相同;<code>CSI 17;H</code>、<code>CSI 17H</code>和<code>CSI 17;1H</code>三者含义相同。
{{anchor|ED}}
|-
| CSI <code>n</code> J || ED – 擦除显示(Erase in Display) || 清除屏幕的部分区域。如果<math>n</math>是0(或缺失),则清除从光标位置到屏幕末尾的部分。如果<math>n</math>是1,则清除从光标位置到屏幕开头的部分。如果<math>n</math>是2,则清除整个屏幕(在DOS ANSI.SYS中,光标还会向左上方移动)。如果<math>n</math>是3,则清除整个屏幕,并删除回滚缓存区中的所有行(这个特性是[[xterm]]添加的,其他终端应用程序也支持)。
{{anchor|EL}}
|-
| CSI <code>n</code> K || EL – 擦除行(Erase in Line) || 清除行内的部分区域。如果<math>n</math>是0(或缺失),清除从光标位置到该行末尾的部分。如果<math>n</math>是1,清除从光标位置到该行开头的部分。如果<math>n</math>是2,清除整行。光标位置不变。
{{anchor|SU}}
|-
| CSI <code>n</code> S || SU – 向上滚动(Scroll Up) || 整页向上滚动<math>n</math>(默认1)行。新行添加到底部。(非ANSI.SYS)
{{anchor|SD}}
|-
| CSI <code>n</code> T || SD – 向下滚动(Scroll Down) || 整页向下滚动<math>n</math>(默认1)行。新行添加到顶部。(非ANSI.SYS)
{{anchor|HVP}}
|-
| {{nowrap|CSI <code>n</code> ; <code>m</code> f}} || {{nobr|HVP – 水平垂直位置(Horizontal Vertical Position)}} || 同CUP。
{{anchor|SGR}}
|-
| {{nowrap|CSI <code>n</code> m}} || SGR – 选择图形再现(Select Graphic Rendition) || 设置[[#选择图形再现(SGR)参数|SGR参数]],包括文字颜色。CSI后可以是0或者更多参数,用分号分隔。如果没有参数,则视为<code>CSI 0 m</code>(重置/常规)。
{{anchor|AUX}}
|-
| CSI 5i || 打开辅助端口 || 启用辅助串行端口,通常用于本地串行打印机
{{anchor|noAUX}}
|-
| CSI 4i || 关闭辅助端口 || 禁用辅助串行端口,通常用于本地串行打印机
{{anchor|DSR|CPR}}
|-
| CSI 6n || DSR – 设备状态报告(Device Status Report) || 以<code>ESC[n;mR</code>(就像在键盘上输入)向应用程序报告光标位置(CPR),其中<math>n</math>是行,<math>m</math>是列。
{{anchor|SCP}}
|-
| CSI s || SCP – 保存光标位置(Save Cursor Position) || 保存光标的当前位置。
{{anchor|RCP}}
|-
| CSI u || RCP – 恢复光标位置(Restore Cursor Position) || 恢复保存的光标位置。
|}

== 选择图形再现(SGR)参数 ==
{| class="wikitable"
! 代码 !! 作用 !! 备注
|-
| 0 || 重置/正常 || 关闭所有属性。
|-
| 1 || 粗体或增加强度 ||
|-
| 2 || 弱化(降低强度) || 未广泛支持。
|-
| 3 || 斜体 || 未广泛支持。有时视为反相显示。
|-
| 4 || 下划线 ||
|-
| 5 || 缓慢闪烁 || 低于每分钟150次。
|-
| 6 || 快速闪烁 || MS-DOS ANSI.SYS;每分钟150以上;未广泛支持。
|-
| 7 || 反显 || 前景色与背景色交换。
|-
| 8 || 隐藏 || 未广泛支持。
|-
| 9 || 划除 || 字符清晰,但标记为删除。未广泛支持。
|-
| 10 || 主要(默认)字体 ||
|-
| 11–19 || 替代字体 || 选择替代字体<math>n-10</math>。
|-
| 20 || [[德文尖角体|尖角体]] || 几乎无支持。
|-
| 21 || 关闭粗体或双下划线 || 关闭粗体未广泛支持;双下划线几乎无支持。
|-
| 22 || 正常颜色或强度 || 不强不弱。
|-
| 23 || 非斜体、非尖角体 ||
|-
| 24 || 关闭下划线 || 去掉单双下划线。
|-
| 25 || 关闭闪烁 ||
|-
| 27 || 关闭反显 ||
|-
| 28 || 关闭隐藏 ||
|-
| 29 || 关闭划除 ||
|-
| 30–37 || 设置前景色 || 参见下面的颜色表。
|-
| 38 || 设置前景色 || 下一个参数是<code>5;n</code>或<code>2;r;g;b</code>,见下。
|-
| 39 || 默认前景色 || 由具体实现定义(按照标准)。
|-
| 40–47 || 设置背景色 || 参见下面的颜色表。
|-
| 48 || 设置背景色 || 下一个参数是<code>5;n</code>或<code>2;r;g;b</code>,见下。
|-
| 49 || 默认背景色 || 由具体实现定义(按照标准)。
|-
| 51 || Framed ||
|-
| 52 || Encircled ||
|-
| 53 || 上划线 ||
|-
| 54 || Not framed or encircled ||
|-
| 55 || 关闭上划线 ||
|-
| 60 || 表意文字下划线或右边线 || rowspan="5"|几乎无支持。
|-
| 61 || 表意文字双下划线或双右边线
|-
| 62 || 表意文字上划线或左边线
|-
| 63 || 表意文字双上划线或双左边线
|-
| 64 || 表意文字着重标志
|-
| 65 || 表意文字属性关闭 || 重置<code>60</code>–<code>64</code>的所有效果。
|-
| 90–97 || 设置明亮的前景色 || aixterm(非标准)。
|-
| {{nobr|100–107}} || 设置明亮的背景色 || aixterm(非标准)。
|}
<!-- SGR 9x and 10x are not defined in ISO 6429, etc. They were defined for IBM's aixterm, and are used in xterm -->
<!-- The names given for the SGR codes are not normative from the ISO/IEC 6429, nor ECMA 48, nor ANSI X3.64. Rather, they are subjectively descriptive. Room for improvement. -->

== 颜色 ==

=== 3/4位 ===

初始的规格只有8种颜色,只给了它们的名字。SGR参数30-37选择前景色,40-47选择背景色。相当多的终端将“粗体”(SGR代码1)实现为更明亮的颜色而不是不同的字体,从而提供了8种额外的前景色,但通常情况下并不能用于背景色,虽然有时候反显(SGR代码7)可以允许这样。例如:在白色背景上显示黑色文字使用<code>ESC[30;47m</code>,显示红色文字用<code>ESC[31m</code>,显示明亮的红色文字用<code>ESC[1;31m</code>。重置为默认颜色用<code>ESC[39;49m</code>(某些终端不支持),重置所有属性用<code>ESC[0m</code>。后来的终端新增了功能,可以直接用90-97和100-107指定“明亮”的颜色。

当硬件开始使用8位[[数字模拟转换器|DAC]]时,多个软件为这些颜色名称分配了24位的代码。下面的图表显示了发送到DAC的一些常用硬件和软件的值。{{Citation needed|date=October 2010}}

{| class="wikitable"
|-
! 名称 || 前景色代码 || 背景色代码
! VGA<ref group="nb">在启动PC并将其保留在文本模式下时使用的典型颜色,使用16个条目的颜色表。EGA/VGA图形模式中的颜色不同。</ref>
! [[命令提示符|CMD]]<ref group="nb">从Windows XP起</ref>
! [[终端 (macOS)|Terminal.app]]
! [[PuTTY]]
! [[mIRC]]
! [[xterm]]
! {{tsl|en|X11 color names|X11颜色名称|X}}<ref group="nb">以上颜色名称来自X11 rgb.txt颜色数据库,用“light”作为明亮颜色的前缀。</ref>
! [[Ubuntu]]<ref group="nb">用于虚拟终端,来自/etc/vtrgb。</ref>
|-
| 黑 || 30 || 40
| colspan="7" style="background: #000; color: white" | 0,0,0
| style="background: #010101; color: white" | 1,1,1
|-
| 红 || 31 || 41
| style="background: #AA0000; color: white" | 170,0,0
| style="background: #800000; color: white" | 128,0,0
| style="background: #c23621; color: white" | 194,54,33
| style="background: #bb0000; color: white" | 187,0,0
| style="background: #7f0000; color: white" | 127,0,0
| style="background: #cd0000; color: white" | 205,0,0
| style="background: #ff0000; color: white" | 255,0,0
| style="background: #de382b; color: white" | 222,56,43
|-
| 绿 || 32 || 42
| style="background: #00AA00; color: white" | 0,170,0
| style="background: #008000; color: white" | 0,128,0
| style="background: #25bc26; color: white" | 37,188,36
| style="background: #00bb00; color: white" | 0,187,0
| style="background: #009300; color: white" | 0,147,0
| style="background: #00cd00; color: white" | 0,205,0
| style="background: #00ff00; color: black" | 0,255,0
| style="background: #39b54a; color: black" | 57,181,74
|-
| 黄 || 33 || 43
| style="background: #AA5500; color: white" | 170,85,0<ref group="nb">在基于[[彩色图形适配器|CGA]]兼容硬件(如在DOS上运行的ANSI.SYS)的终端上,正常强度的前景色呈现为橙色。 CGA RGBI显示器包含一些硬件,通过减少绿色成分将深黄色修改为橙色/棕色。</ref>
| style="background: #808000; color: white" | 128,128,0
| style="background: #adad27; color: white" | 173,173,39
| style="background: #bbbb00; color: black" | 187,187,0
| style="background: #fc7f00; color: white" | 252,127,0
| style="background: #cdcd00; color: black" | 205,205,0
| style="background: #ffff00; color: black" | 255,255,0
| style="background: #ffc706; color: black" | 255,199,6
|-
| 蓝 || 34 || 44
| style="background: #0000AA; color: white" | 0,0,170
| style="background: #000080; color: white" | 0,0,128
| style="background: #492ee1; color: white" | 73,46,225
| style="background: #0000bb; color: white" | 0,0,187
| style="background: #00007f; color: white" | 0,0,127
| style="background: #0000ee; color: white" | 0,0,238<ref>Changed from 0,0,205 in July 2004 {{cite web |url=http://invisible-island.net/xterm/xterm.log.html#xterm_192 |title=Patch #192 – 2004/7/12 – XFree86 4.4.99.9}}</ref>
| style="background: #0000ff; color: white" | 0,0,255
| style="background: #006fb8; color: white" | 0,111,184
|-
| 品红 || 35 || 45
| style="background: #AA00AA; color: white" | 170,0,170
| style="background: #800080; color: white" | 128,0,128
| style="background: #d338d3; color: white" | 211,56,211
| style="background: #bb00bb; color: white" | 187,0,187
| style="background: #9c009c; color: white" | 156,0,156
| style="background: #cd00cd; color: white" | 205,0,205
| style="background: #ff00ff; color: black" | 255,0,255
| style="background: #762671; color: white" | 118,38,113
|-
| 青 || 36 || 46
| style="background: #00AAAA; color: white" | 0,170,170
| style="background: #008080; color: white" | 0,128,128
| style="background: #33bbc8; color: white" | 51,187,200
| style="background: #00bbbb; color: black" | 0,187,187
| style="background: #009393; color: white" | 0,147,147
| style="background: #00cdcd; color: black" | 0,205,205
| style="background: #00ffff; color: black" | 0,255,255
| style="background: #2cb5e9; color: black" | 44,181,233
|-
| 白 || 37 || 47
| style="background: #AAAAAA; color: black" | 170,170,170
| style="background: #c0c0c0; color: black" | 192,192,192
| style="background: #cbcccd; color: black" | 203,204,205
| style="background: #bbbbbb; color: black" | 187,187,187
| style="background: #d2d2d2; color: black" | 210,210,210
| style="background: #e5e5e5; color: black" | 229,229,229
| style="background: #ffffff; color: black" | 255,255,255
| style="background: #cccccc; color: black" | 204,204,204
|-
| 亮黑(灰) || 90 || 100
| style="background: #555555; color: white" | 85,85,85
| style="background: #808080; color: black" | 128,128,128
| style="background: #818383; color: black" | 129,131,131
| style="background: #555555; color: white" | 85,85,85
| style="background: #7f7f7f; color: white" | 127,127,127
| style="background: #7f7f7f; color: white" | 127,127,127
|
| style="background: #808080; color: black" | 128,128,128
|-
| 亮红 || 91 || 101
| style="background: #FF5555; color: white" | 255,85,85
| style="background: #ff0000; color: white" | 255,0,0
| style="background: #fc391f; color: white" | 252,57,31
| style="background: #ff5555; color: white" | 255,85,85
| style="background: #ff0000; color: white" | 255,0,0
| style="background: #ff0000; color: white" | 255,0,0
|
| style="background: #ff0000; color: white" | 255,0,0
|-
|| 亮绿 || 92 || 102
| style="background: #55FF55; color: black" | 85,255,85
| style="background: #00ff00; color: black" | 0,255,0
| style="background: #31e722; color: black" | 49,231,34
| style="background: #55ff55; color: black" | 85,255,85
| style="background: #00fc00; color: black" | 0,252,0
| style="background: #00ff00; color: black" | 0,255,0
| style="background: #90ee90; color: black" | 144,238,144
| style="background: #00ff00; color: black" | 0,255,0
|-
|| 亮黄 || 93 || 103
| style="background: #FFFF55; color: black" | 255,255,85
| style="background: #ffff00; color: black" | 255,255,0
| style="background: #eaec23; color: black" | 234,236,35
| style="background: #ffff55; color: black" | 255,255,85
| style="background: #ffff00; color: black" | 255,255,0
| style="background: #ffff00; color: black" | 255,255,0
| style="background: #ffffe0; color: black" | 255,255,224
| style="background: #ffff00; color: black" | 255,255,0
|-
|| 亮蓝 || 94 || 104
| style="background: #5555FF; color: white" | 85,85,255
| style="background: #0000ff; color: white" | 0,0,255
| style="background: #5833ff; color: black" | 88,51,255
| style="background: #5555ff; color: black" | 85,85,255
| style="background: #0000fc; color: white" | 0,0,252
| style="background: #5c5cff; color: black" | 92,92,255<ref>Changed from 0,0,255 in July 2004 {{cite web |url=http://invisible-island.net/xterm/xterm.log.html#xterm_192 |title=Patch #192 – 2004/7/12 – XFree86 4.4.99.9}}</ref>
| style="background: #acd8e6; color: black" | 173,216,230
| style="background: #0000ff; color: white" | 0,0,255
|-
|| 亮品红 || 95 || 105
| style="background: #FF55FF; color: black" | 255,85,255
| style="background: #ff00ff; color: white" | 255,0,255
| style="background: #f935f8; color: black" | 249,53,248
| style="background: #ff55ff; color: black" | 255,85,255
| style="background: #ff00ff; color: white" | 255,0,255
| style="background: #ff00ff; color: white" | 255,0,255
|
| style="background: #ff00ff; color: white" | 255,0,255
|-
|| 亮青 || 96 || 106
| style="background: #55FFFF; color: black" | 85,255,255
| style="background: #00ffff; color: black" | 0,255,255
| style="background: #14f0f0; color: black" | 20,240,240
| style="background: #55ffff; color: black" | 85,255,255
| style="background: #00ffff; color: black" | 0,255,255
| style="background: #00ffff; color: black" | 0,255,255
| style="background: #e0ffff; color: black" | 224,255,255
| style="background: #00ffff; color: black" | 0,255,255
|-
|| 亮白 || 97 || 107
| style="background: #FFFFFF; color: black" | 255,255,255
| style="background: #ffffff; color: black" | 255,255,255
| style="background: #e9ebeb; color: black" | 233,235,235
| style="background: #ffffff; color: black" | 255,255,255
| style="background: #ffffff; color: black" | 255,255,255
| style="background: #ffffff; color: black" | 255,255,255
|
| style="background: #ffffff; color: black" | 255,255,255
|}

=== 8位 ===

随着256色查找表在显卡上越来越常见,相应的转义序列也增加了,以从预定义的256种颜色中选择:<ref name="moreColors"/>
<code>ESC[ … 38;5;<n> … m</code>选择前景色
<code>ESC[ … 48;5;<n> … m</code>选择背景色
0- 7:标准颜色(同<code>ESC &#91; 30–37 m</code>)
8- 15:高强度颜色(同<code>ESC &#91; 90–97 m</code>)
16-231:6 × 6 × 6 立方(216色): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
232-255:从黑到白的24阶灰度色

[[ITU]]的T.416信息技术-开放文档体系结构(ODA)和交换格式:字符内容体系结构<ref>{{cite web |url=https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.416-199303-I!!PDF-E&type=items |title=T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures}}</ref>使用“:”作为分隔符:
<code>ESC[ … 38:5:<n> … m</code>选择前景色
<code>ESC[ … 48:5:<n> … m</code>选择背景色
<!--
def fg(triad): return 'ffffff' if (triad[2:4] < 'a0') else '000000'
standards = ('000000 800000 008000 808000 000080 800080 008080 c0c0c0 ' +
'808080 ff0000 00ff00 ffff00 0000ff ff00ff 00ffff ffffff').split(' ')
rgbs = '00 5f 87 af d7 ff'.split(' ')
greys = '08 12 1c 26 30 3a 44 4e 58 62 6c 76 80 8a 94 9e a8 b2 bc c6 d0 da e4 ee'.split(' ')
fgs = '000000 ffffff'.split(' ')
code = 0
outs = [[] for i in range(3)]
out = '\n| style="color:#%s;background:#%s;" title="#%s" |%s'
for i in range(len(standards)):
if (i == len(standards) / 2.0): outs[0].append('\n| style="width:1em;"| ')
triad = standards[i]
outs[0].append(out % (fgs[i < len(standards) / 2], triad, triad,
code if (code > 9) else ' %d&nbsp;' % code))
code += 1
for r in range(len(rgbs)):
for g in range(len(rgbs)):
for b in range(len(rgbs)):
triad = rgbs[r] + rgbs[g] + rgbs[b]
outs[1].append(out % (fgs[g < len(rgbs) / 2], triad, triad, code))
code += 1
outs[1].append('\n|-\n')
for i in range(len(greys)):
triad = greys[i] * 3
outs[2].append(out % (fgs[i < len(greys) / 2], triad, triad, code))
code += 1
file_out = open(__file__[:__file__.rfind('.')] + '.htm', 'w')
file_out.write('''\
{| table class="collapsible" style="text-align:center;font-size:80%%;width:100%%;background:#f6f6f6;cursor:default;" cellpadding="0" cellspacing="1"
|-
! colspan="36" | 256-color mode — foreground: ESC[38;5;#m &nbsp; background: ESC[48;5;#m
|-
| colspan="18" | Standard colors
| colspan="18" | High-intensity colors
|-
| colspan="36" |
{| style="width:100%%;text-align:center;font-weight:bold;"
|-
%s
|-
|}
|-
| colspan="36" | 216 colors
|-
%s
|-
| colspan="36" | Grayscale colors
|-
| colspan="36" |
{| style="width:100%%;text-align:center;font-weight:bold;"
|-
%s
|-
|}
|}''' %
(''.join(outs[0]), ''.join(outs[1]), ''.join(outs[2])))
file_out.close()
-->
{| class="collapsible" style="text-align:center;font-size:80%;width:100%;background:#f6f6f6;cursor:default;" cellpadding="0" cellspacing="1"
|-
! colspan="36" | 256色模式 — 前景色:<code>ESC[38;5;#m</code>&nbsp;&nbsp;&nbsp;背景色:<code>ESC[48;5;#m</code>
|-
| colspan="18" |标准色
| colspan="18" |高强度色
|-
| colspan="36" |
{| style="width:100%;text-align:center;font-weight:bold;"
|-
| style="color:#ffffff;background:#000000;" title="#000000" |&nbsp;0&nbsp;
| style="color:#ffffff;background:#800000;" title="#800000" |&nbsp;1&nbsp;
| style="color:#ffffff;background:#008000;" title="#008000" |&nbsp;2&nbsp;
| style="color:#ffffff;background:#808000;" title="#808000" |&nbsp;3&nbsp;
| style="color:#ffffff;background:#000080;" title="#000080" |&nbsp;4&nbsp;
| style="color:#ffffff;background:#800080;" title="#800080" |&nbsp;5&nbsp;
| style="color:#ffffff;background:#008080;" title="#008080" |&nbsp;6&nbsp;
| style="color:#ffffff;background:#c0c0c0;" title="#c0c0c0" |&nbsp;7&nbsp;
| style="width:1em;" |
| style="color:#000000;background:#808080;" title="#808080" |&nbsp;8&nbsp;
| style="color:#000000;background:#ff0000;" title="#ff0000" |&nbsp;9&nbsp;
| style="color:#000000;background:#00ff00;" title="#00ff00" |10
| style="color:#000000;background:#ffff00;" title="#ffff00" |11
| style="color:#000000;background:#0000ff;" title="#0000ff" |12
| style="color:#000000;background:#ff00ff;" title="#ff00ff" |13
| style="color:#000000;background:#00ffff;" title="#00ffff" |14
| style="color:#000000;background:#ffffff;" title="#ffffff" |15
|}
|-
| colspan="36" |216色
|-
| style="color:#ffffff;background:#000000;" title="#000000" |16
| style="color:#ffffff;background:#00005f;" title="#00005f" |17
| style="color:#ffffff;background:#000087;" title="#000087" |18
| style="color:#ffffff;background:#0000af;" title="#0000af" |19
| style="color:#ffffff;background:#0000d7;" title="#0000d7" |20
| style="color:#ffffff;background:#0000ff;" title="#0000ff" |21
| style="color:#ffffff;background:#005f00;" title="#005f00" |22
| style="color:#ffffff;background:#005f5f;" title="#005f5f" |23
| style="color:#ffffff;background:#005f87;" title="#005f87" |24
| style="color:#ffffff;background:#005faf;" title="#005faf" |25
| style="color:#ffffff;background:#005fd7;" title="#005fd7" |26
| style="color:#ffffff;background:#005fff;" title="#005fff" |27
| style="color:#ffffff;background:#008700;" title="#008700" |28
| style="color:#ffffff;background:#00875f;" title="#00875f" |29
| style="color:#ffffff;background:#008787;" title="#008787" |30
| style="color:#ffffff;background:#0087af;" title="#0087af" |31
| style="color:#ffffff;background:#0087d7;" title="#0087d7" |32
| style="color:#ffffff;background:#0087ff;" title="#0087ff" |33
| style="color:#000000;background:#00af00;" title="#00af00" |34
| style="color:#000000;background:#00af5f;" title="#00af5f" |35
| style="color:#000000;background:#00af87;" title="#00af87" |36
| style="color:#000000;background:#00afaf;" title="#00afaf" |37
| style="color:#000000;background:#00afd7;" title="#00afd7" |38
| style="color:#000000;background:#00afff;" title="#00afff" |39
| style="color:#000000;background:#00d700;" title="#00d700" |40
| style="color:#000000;background:#00d75f;" title="#00d75f" |41
| style="color:#000000;background:#00d787;" title="#00d787" |42
| style="color:#000000;background:#00d7af;" title="#00d7af" |43
| style="color:#000000;background:#00d7d7;" title="#00d7d7" |44
| style="color:#000000;background:#00d7ff;" title="#00d7ff" |45
| style="color:#000000;background:#00ff00;" title="#00ff00" |46
| style="color:#000000;background:#00ff5f;" title="#00ff5f" |47
| style="color:#000000;background:#00ff87;" title="#00ff87" |48
| style="color:#000000;background:#00ffaf;" title="#00ffaf" |49
| style="color:#000000;background:#00ffd7;" title="#00ffd7" |50
| style="color:#000000;background:#00ffff;" title="#00ffff" |51
|-
| style="color:#ffffff;background:#5f0000;" title="#5f0000" |52
| style="color:#ffffff;background:#5f005f;" title="#5f005f" |53
| style="color:#ffffff;background:#5f0087;" title="#5f0087" |54
| style="color:#ffffff;background:#5f00af;" title="#5f00af" |55
| style="color:#ffffff;background:#5f00d7;" title="#5f00d7" |56
| style="color:#ffffff;background:#5f00ff;" title="#5f00ff" |57
| style="color:#ffffff;background:#5f5f00;" title="#5f5f00" |58
| style="color:#ffffff;background:#5f5f5f;" title="#5f5f5f" |59
| style="color:#ffffff;background:#5f5f87;" title="#5f5f87" |60
| style="color:#ffffff;background:#5f5faf;" title="#5f5faf" |61
| style="color:#ffffff;background:#5f5fd7;" title="#5f5fd7" |62
| style="color:#ffffff;background:#5f5fff;" title="#5f5fff" |63
| style="color:#ffffff;background:#5f8700;" title="#5f8700" |64
| style="color:#ffffff;background:#5f875f;" title="#5f875f" |65
| style="color:#ffffff;background:#5f8787;" title="#5f8787" |66
| style="color:#ffffff;background:#5f87af;" title="#5f87af" |67
| style="color:#ffffff;background:#5f87d7;" title="#5f87d7" |68
| style="color:#ffffff;background:#5f87ff;" title="#5f87ff" |69
| style="color:#000000;background:#5faf00;" title="#5faf00" |70
| style="color:#000000;background:#5faf5f;" title="#5faf5f" |71
| style="color:#000000;background:#5faf87;" title="#5faf87" |72
| style="color:#000000;background:#5fafaf;" title="#5fafaf" |73
| style="color:#000000;background:#5fafd7;" title="#5fafd7" |74
| style="color:#000000;background:#5fafff;" title="#5fafff" |75
| style="color:#000000;background:#5fd700;" title="#5fd700" |76
| style="color:#000000;background:#5fd75f;" title="#5fd75f" |77
| style="color:#000000;background:#5fd787;" title="#5fd787" |78
| style="color:#000000;background:#5fd7af;" title="#5fd7af" |79
| style="color:#000000;background:#5fd7d7;" title="#5fd7d7" |80
| style="color:#000000;background:#5fd7ff;" title="#5fd7ff" |81
| style="color:#000000;background:#5fff00;" title="#5fff00" |82
| style="color:#000000;background:#5fff5f;" title="#5fff5f" |83
| style="color:#000000;background:#5fff87;" title="#5fff87" |84
| style="color:#000000;background:#5fffaf;" title="#5fffaf" |85
| style="color:#000000;background:#5fffd7;" title="#5fffd7" |86
| style="color:#000000;background:#5fffff;" title="#5fffff" |87
|-
| style="color:#ffffff;background:#870000;" title="#870000" |88
| style="color:#ffffff;background:#87005f;" title="#87005f" |89
| style="color:#ffffff;background:#870087;" title="#870087" |90
| style="color:#ffffff;background:#8700af;" title="#8700af" |91
| style="color:#ffffff;background:#8700d7;" title="#8700d7" |92
| style="color:#ffffff;background:#8700ff;" title="#8700ff" |93
| style="color:#ffffff;background:#875f00;" title="#875f00" |94
| style="color:#ffffff;background:#875f5f;" title="#875f5f" |95
| style="color:#ffffff;background:#875f87;" title="#875f87" |96
| style="color:#ffffff;background:#875faf;" title="#875faf" |97
| style="color:#ffffff;background:#875fd7;" title="#875fd7" |98
| style="color:#ffffff;background:#875fff;" title="#875fff" |99
| style="color:#ffffff;background:#878700;" title="#878700" |100
| style="color:#ffffff;background:#87875f;" title="#87875f" |101
| style="color:#ffffff;background:#878787;" title="#878787" |102
| style="color:#ffffff;background:#8787af;" title="#8787af" |103
| style="color:#ffffff;background:#8787d7;" title="#8787d7" |104
| style="color:#ffffff;background:#8787ff;" title="#8787ff" |105
| style="color:#000000;background:#87af00;" title="#87af00" |106
| style="color:#000000;background:#87af5f;" title="#87af5f" |107
| style="color:#000000;background:#87af87;" title="#87af87" |108
| style="color:#000000;background:#87afaf;" title="#87afaf" |109
| style="color:#000000;background:#87afd7;" title="#87afd7" |110
| style="color:#000000;background:#87afff;" title="#87afff" |111
| style="color:#000000;background:#87d700;" title="#87d700" |112
| style="color:#000000;background:#87d75f;" title="#87d75f" |113
| style="color:#000000;background:#87d787;" title="#87d787" |114
| style="color:#000000;background:#87d7af;" title="#87d7af" |115
| style="color:#000000;background:#87d7d7;" title="#87d7d7" |116
| style="color:#000000;background:#87d7ff;" title="#87d7ff" |117
| style="color:#000000;background:#87ff00;" title="#87ff00" |118
| style="color:#000000;background:#87ff5f;" title="#87ff5f" |119
| style="color:#000000;background:#87ff87;" title="#87ff87" |120
| style="color:#000000;background:#87ffaf;" title="#87ffaf" |121
| style="color:#000000;background:#87ffd7;" title="#87ffd7" |122
| style="color:#000000;background:#87ffff;" title="#87ffff" |123
|-
| style="color:#ffffff;background:#af0000;" title="#af0000" |124
| style="color:#ffffff;background:#af005f;" title="#af005f" |125
| style="color:#ffffff;background:#af0087;" title="#af0087" |126
| style="color:#ffffff;background:#af00af;" title="#af00af" |127
| style="color:#ffffff;background:#af00d7;" title="#af00d7" |128
| style="color:#ffffff;background:#af00ff;" title="#af00ff" |129
| style="color:#ffffff;background:#af5f00;" title="#af5f00" |130
| style="color:#ffffff;background:#af5f5f;" title="#af5f5f" |131
| style="color:#ffffff;background:#af5f87;" title="#af5f87" |132
| style="color:#ffffff;background:#af5faf;" title="#af5faf" |133
| style="color:#ffffff;background:#af5fd7;" title="#af5fd7" |134
| style="color:#ffffff;background:#af5fff;" title="#af5fff" |135
| style="color:#ffffff;background:#af8700;" title="#af8700" |136
| style="color:#ffffff;background:#af875f;" title="#af875f" |137
| style="color:#ffffff;background:#af8787;" title="#af8787" |138
| style="color:#ffffff;background:#af87af;" title="#af87af" |139
| style="color:#ffffff;background:#af87d7;" title="#af87d7" |140
| style="color:#ffffff;background:#af87ff;" title="#af87ff" |141
| style="color:#000000;background:#afaf00;" title="#afaf00" |142
| style="color:#000000;background:#afaf5f;" title="#afaf5f" |143
| style="color:#000000;background:#afaf87;" title="#afaf87" |144
| style="color:#000000;background:#afafaf;" title="#afafaf" |145
| style="color:#000000;background:#afafd7;" title="#afafd7" |146
| style="color:#000000;background:#afafff;" title="#afafff" |147
| style="color:#000000;background:#afd700;" title="#afd700" |148
| style="color:#000000;background:#afd75f;" title="#afd75f" |149
| style="color:#000000;background:#afd787;" title="#afd787" |150
| style="color:#000000;background:#afd7af;" title="#afd7af" |151
| style="color:#000000;background:#afd7d7;" title="#afd7d7" |152
| style="color:#000000;background:#afd7ff;" title="#afd7ff" |153
| style="color:#000000;background:#afff00;" title="#afff00" |154
| style="color:#000000;background:#afff5f;" title="#afff5f" |155
| style="color:#000000;background:#afff87;" title="#afff87" |156
| style="color:#000000;background:#afffaf;" title="#afffaf" |157
| style="color:#000000;background:#afffd7;" title="#afffd7" |158
| style="color:#000000;background:#afffff;" title="#afffff" |159
|-
| style="color:#ffffff;background:#d70000;" title="#d70000" |160
| style="color:#ffffff;background:#d7005f;" title="#d7005f" |161
| style="color:#ffffff;background:#d70087;" title="#d70087" |162
| style="color:#ffffff;background:#d700af;" title="#d700af" |163
| style="color:#ffffff;background:#d700d7;" title="#d700d7" |164
| style="color:#ffffff;background:#d700ff;" title="#d700ff" |165
| style="color:#ffffff;background:#d75f00;" title="#d75f00" |166
| style="color:#ffffff;background:#d75f5f;" title="#d75f5f" |167
| style="color:#ffffff;background:#d75f87;" title="#d75f87" |168
| style="color:#ffffff;background:#d75faf;" title="#d75faf" |169
| style="color:#ffffff;background:#d75fd7;" title="#d75fd7" |170
| style="color:#ffffff;background:#d75fff;" title="#d75fff" |171
| style="color:#ffffff;background:#d78700;" title="#d78700" |172
| style="color:#ffffff;background:#d7875f;" title="#d7875f" |173
| style="color:#ffffff;background:#d78787;" title="#d78787" |174
| style="color:#ffffff;background:#d787af;" title="#d787af" |175
| style="color:#ffffff;background:#d787d7;" title="#d787d7" |176
| style="color:#ffffff;background:#d787ff;" title="#d787ff" |177
| style="color:#000000;background:#d7af00;" title="#d7af00" |178
| style="color:#000000;background:#d7af5f;" title="#d7af5f" |179
| style="color:#000000;background:#d7af87;" title="#d7af87" |180
| style="color:#000000;background:#d7afaf;" title="#d7afaf" |181
| style="color:#000000;background:#d7afd7;" title="#d7afd7" |182
| style="color:#000000;background:#d7afff;" title="#d7afff" |183
| style="color:#000000;background:#d7d700;" title="#d7d700" |184
| style="color:#000000;background:#d7d75f;" title="#d7d75f" |185
| style="color:#000000;background:#d7d787;" title="#d7d787" |186
| style="color:#000000;background:#d7d7af;" title="#d7d7af" |187
| style="color:#000000;background:#d7d7d7;" title="#d7d7d7" |188
| style="color:#000000;background:#d7d7ff;" title="#d7d7ff" |189
| style="color:#000000;background:#d7ff00;" title="#d7ff00" |190
| style="color:#000000;background:#d7ff5f;" title="#d7ff5f" |191
| style="color:#000000;background:#d7ff87;" title="#d7ff87" |192
| style="color:#000000;background:#d7ffaf;" title="#d7ffaf" |193
| style="color:#000000;background:#d7ffd7;" title="#d7ffd7" |194
| style="color:#000000;background:#d7ffff;" title="#d7ffff" |195
|-
| style="color:#ffffff;background:#ff0000;" title="#ff0000" |196
| style="color:#ffffff;background:#ff005f;" title="#ff005f" |197
| style="color:#ffffff;background:#ff0087;" title="#ff0087" |198
| style="color:#ffffff;background:#ff00af;" title="#ff00af" |199
| style="color:#ffffff;background:#ff00d7;" title="#ff00d7" |200
| style="color:#ffffff;background:#ff00ff;" title="#ff00ff" |201
| style="color:#ffffff;background:#ff5f00;" title="#ff5f00" |202
| style="color:#ffffff;background:#ff5f5f;" title="#ff5f5f" |203
| style="color:#ffffff;background:#ff5f87;" title="#ff5f87" |204
| style="color:#ffffff;background:#ff5faf;" title="#ff5faf" |205
| style="color:#ffffff;background:#ff5fd7;" title="#ff5fd7" |206
| style="color:#ffffff;background:#ff5fff;" title="#ff5fff" |207
| style="color:#ffffff;background:#ff8700;" title="#ff8700" |208
| style="color:#ffffff;background:#ff875f;" title="#ff875f" |209
| style="color:#ffffff;background:#ff8787;" title="#ff8787" |210
| style="color:#ffffff;background:#ff87af;" title="#ff87af" |211
| style="color:#ffffff;background:#ff87d7;" title="#ff87d7" |212
| style="color:#ffffff;background:#ff87ff;" title="#ff87ff" |213
| style="color:#000000;background:#ffaf00;" title="#ffaf00" |214
| style="color:#000000;background:#ffaf5f;" title="#ffaf5f" |215
| style="color:#000000;background:#ffaf87;" title="#ffaf87" |216
| style="color:#000000;background:#ffafaf;" title="#ffafaf" |217
| style="color:#000000;background:#ffafd7;" title="#ffafd7" |218
| style="color:#000000;background:#ffafff;" title="#ffafff" |219
| style="color:#000000;background:#ffd700;" title="#ffd700" |220
| style="color:#000000;background:#ffd75f;" title="#ffd75f" |221
| style="color:#000000;background:#ffd787;" title="#ffd787" |222
| style="color:#000000;background:#ffd7af;" title="#ffd7af" |223
| style="color:#000000;background:#ffd7d7;" title="#ffd7d7" |224
| style="color:#000000;background:#ffd7ff;" title="#ffd7ff" |225
| style="color:#000000;background:#ffff00;" title="#ffff00" |226
| style="color:#000000;background:#ffff5f;" title="#ffff5f" |227
| style="color:#000000;background:#ffff87;" title="#ffff87" |228
| style="color:#000000;background:#ffffaf;" title="#ffffaf" |229
| style="color:#000000;background:#ffffd7;" title="#ffffd7" |230
| style="color:#000000;background:#ffffff;" title="#ffffff" |231
|-

|-
| colspan="36" |灰度色
|-
| colspan="36" |
{| style="width:100%;text-align:center;font-weight:bold;"
|-
| style="color:#ffffff;background:#080808;" title="#080808" |232
| style="color:#ffffff;background:#121212;" title="#121212" |233
| style="color:#ffffff;background:#1c1c1c;" title="#1c1c1c" |234
| style="color:#ffffff;background:#262626;" title="#262626" |235
| style="color:#ffffff;background:#303030;" title="#303030" |236
| style="color:#ffffff;background:#3a3a3a;" title="#3a3a3a" |237
| style="color:#ffffff;background:#444444;" title="#444444" |238
| style="color:#ffffff;background:#4e4e4e;" title="#4e4e4e" |239
| style="color:#ffffff;background:#585858;" title="#585858" |240
| style="color:#ffffff;background:#626262;" title="#626262" |241
| style="color:#ffffff;background:#6c6c6c;" title="#6c6c6c" |242
| style="color:#ffffff;background:#767676;" title="#767676" |243
| style="color:#000000;background:#808080;" title="#808080" |244
| style="color:#000000;background:#8a8a8a;" title="#8a8a8a" |245
| style="color:#000000;background:#949494;" title="#949494" |246
| style="color:#000000;background:#9e9e9e;" title="#9e9e9e" |247
| style="color:#000000;background:#a8a8a8;" title="#a8a8a8" |248
| style="color:#000000;background:#b2b2b2;" title="#b2b2b2" |249
| style="color:#000000;background:#bcbcbc;" title="#bcbcbc" |250
| style="color:#000000;background:#c6c6c6;" title="#c6c6c6" |251
| style="color:#000000;background:#d0d0d0;" title="#d0d0d0" |252
| style="color:#000000;background:#dadada;" title="#dadada" |253
| style="color:#000000;background:#e4e4e4;" title="#e4e4e4" |254
| style="color:#000000;background:#eeeeee;" title="#eeeeee" |255
|}
|}

=== 24位 ===

随着16位到24位颜色的“真彩色”显卡的普及,Xterm<ref name="xtc">{{cite web |url=http://invisible-island.net/xterm/ctlseqs/ctlseqs.html |title=XTerm Control Sequences |work=invisible-island.net |date=2014-01-13 |access-date=2014-04-13}}</ref>、KDE的[[Konsole]]<ref>{{cite web |url=https://quickgit.kde.org/?p=konsole.git&a=blob&f=tests%2Fcolor-spaces.pl |title=color-spaces.pl (a copy of 256colors2.pl from xterm dated 1999-07-11) |publisher=KDE |date=2006-12-06}}</ref>,以及所有基于libvte的终端<ref>{{cite web |url=https://bugzilla.gnome.org/show_bug.cgi?id=704449 |title=libvte's bug report and patches |publisher=GNOME Bugzilla |date=2014-04-04 |access-date=2016-06-05}}</ref>(包括{{tsl|en|GNOME Terminal|GNOME终端}})支持了ISO-8613-3的24位前景色和背景色设置。<ref name="moreColors">{{cite web |url=https://cgit.kde.org/konsole.git/tree/doc/user/README.moreColors |title=README.moreColors |publisher=KDE |date=2010-04-22}}</ref>
<code>ESC[ … 38;2;<r>;<g>;&lt;b> … m</code>选择RGB前景色
<code>ESC[ … 48;2;<r>;<g>;&lt;b> … m</code>选择RGB背景色

作为ISO / IEC国际标准8613-6采用的[[ITU]]的T.416信息技术-开放文档体系结构(ODA)和交换格式:字符内容体系结构<ref>{{cite web |url=https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.416-199303-I!!PDF-E&type=items |title=T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures}}</ref>给出了一个似乎不太受支持的替代版本:
<code>ESC[ … 38:2:<Color-Space-ID>:<r>:<g>:&lt;b>:<unused>:<CS tolerance>:<Color-Space: 0="CIELUV"; 1="CIELAB">m</code>选择RGB前景色
<code>ESC[ … 48:2:<Color-Space-ID>:<r>:<g>:&lt;b>:<unused>:<CS tolerance>:<Color-Space: 0="CIELUV"; 1="CIELAB">m</code>选择RGB背景色
请注意,这里使用了保留的“:”字符来分隔子选项,这可能是在实际实现中造成混淆的始作俑者。它还使用“3”作为第二个参数来指定使用青-品红-黄方案的方案,“4”用于青-品红-黄-黑的方案,后者使用上面标记为“unused”(“未使用”)的位置作为黑色组件。

还要注意,许多识别“:”作为分隔符的实现错误地忽视了色彩空间标识符参数,并因此改变了其余部分的位置。

== 示例 ==
<code>CSI 2 J</code> — 清除屏幕、(在某些设备上)把光标置于1,1位置(左上角)。

<code>CSI 32 m</code> — 使文字呈绿色。在MS-DOS上,一般绿色是暗淡的绿色,可以用<code>CSI 1 m</code>启用粗体使其变成明亮的绿色,或者将两者合并为<code>CSI 32 ; 1 m</code>。MS-DOS ANSI.SYS用粗体状态使字符变亮,闪烁状态(通过<code>INT 10, AX 1003h, BL 00h</code>)使背景色变成明亮模式。MS-DOS ANSI.SYS并不直接支持SGR代码90–97和100–107。

<code>CSI 0 ; 6 8 ; "DIR" ; 13 p</code> — 重新分配F10键的功能为发送字符串“DIR”和回车符到键盘缓存中,在DOS命令行里会显示当前目录的内容(仅MS-DOS ANSI.SYS)。这种序列有时用于“{{tsl|en|ANSI Bomb|ANSI炸弹}}”。这是一个私用编码(如字母p所示),用非标准的扩展使其包含一个字符串参数。如果按标准,会认为字母D是序列的末尾。

<code>CSI s</code> — 保存光标的位置。用序列<code>CSI u</code>会把光标重置回这个位置。假设当前的光标位置是7(y)、10(x)。序列<code>CSI s</code>会保存这两个数值。现在可以把光标移动到其他位置,比如用序列<code>CSI 20 ; 3 H</code>或<code>CSI 20 ; 3 f</code>把光标移动到20(y)、3(x)。现在如果用序列<code>CSI u</code>,光标会回到7(y)、10(x)。某些终端需要使用DEC序列<code>ESC 7</code>/<code>ESC 8</code>,这得到了更广泛的支持。

=== 使用Shell脚本的示例 ===

ANSI转移代码常常用于[[UNIX]]和类UNIX终端,以提供[[语法高亮]]功能。例如,在兼容的终端上,以下[[ls]]命令按类型对文件和目录的名称进行颜色编码。

<code>ls --color</code>

用户可以在脚本中使用转义码,将其作为[[标准输出]]或[[标准错误输出]]的一部分。例如,下面的GNU [[sed]]命令通过反显“WARN”开头的单词的行,以及使用暗红色背景色和亮黄色前景色显示以“ERR”开头的单词(字母大小写被忽略)的行来修饰make命令的输出。突出显示了设置ANSI代码的部分。<ref>{{cite web |url=http://www.debian.org/doc/manuals/debian-reference/ch09.en.html#_colorized_shell_echo |title=Chapter 9. System tips|work=debian.org}}</ref>
<span style="white-space:pre-wrap;">make 2&gt;&amp;1 | sed -e 's/.*\bWARN.*/<span style="border:1px solid grey;border-radius:5px;color:white;background:black;font-weight:bolder;">\x1b[7m</span>&<span style="border:1px solid grey;border-radius:5px;">\x1b[0m</span>/i' -e 's/.*\bERR.*/<span style="border:1px solid grey;border-radius:5px;color:yellow;background:maroon;font-weight:bolder;">\x1b[93;41&nbsp;m</span>&<span style="border:1px solid grey;border-radius:5px;">\x1b[0m</span>/i'</span>

以下Bash函数会使终端闪烁(通过交替发送反相和正常显示模式代码),直到用户按下任意键<ref>{{cite web |title=VT100.net: Digital VT100 User Guide |url=http://vt100.net/docs/vt100-ug/chapter3.html |access-date=2015-01-19}}</ref>。这个函数可以用于当一个冗长的命令终止时提醒用户,用法如<code>make; flasher</code><ref>{{cite web |title=bash – How to get a notification when my commands are done – Ask Different |url=http://apple.stackexchange.com/questions/9412/how-to-get-a-notification-when-my-commands-are-done |access-date=2015-01-19}}</ref>。

<span style="white-space:pre-wrap;">flasher () { while true; do printf <span style="border:1px solid grey;border-radius:5px;color:white;background:black;font-weight:bolder;">\\e[?5h</span>; sleep 0.1; printf <span style="border:1px solid grey;border-radius:5px;font-weight:bolder;">\\e[?5l</span>; read -s -n1 -t1 && break; done; }</span>

下面这个命令可以重置控制台,类似现代Linux系统的<code>reset</code>命令。然而,即使在较早的Linux系统和其他(非Linux)UNIX变体上,也应该能起作用。

printf \\033c

== 参见 ==
* [[控制字符]]



== 外部链接 ==
* [http://www.ecma-international.org/publications/standards/Ecma-048.htm Standard ECMA-48, Control Functions For Coded Character Sets]. (''5th edition, June 1991''), European Computer Manufacturers Association, Geneva 1991 (also published by ISO and IEC as standard ISO/IEC 6429)
* [http://vt100.net/docs/ vt100.net DEC Documents]
* {{Cite web |url=http://enterprise.aacc.cc.md.us/~rhs/ansi.html |title=ANSI.SYS -- ansi terminal emulation escape sequences |access-date=2018-01-12 |archive-url=https://web.archive.org/web/20060206022229/http://enterprise.aacc.cc.md.us/~rhs/ansi.html |archive-date=2006-02-06 |dead-url=yes }}
* [http://invisible-island.net/xterm/ctlseqs/ctlseqs.html Xterm / Escape Sequences]
* [http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds1%2Faixterm.htm AIXterm / Escape Sequences]
* [http://bjh21.me.uk/all-escapes/all-escapes.txt A collection of escape sequences for terminals that are vaguely compliant with ECMA-48 and friends.]
* [http://ascii-table.com/ansi-escape-sequences.php ANSI Escape Sequences]
* [https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.416-199303-I!!PDF-E&type=items ITU-T Rec. T.416 (03/93) Information technology – Open Document Architecture (ODA) and interchange format: Character content architectures]

导航菜单