3 回答

TA貢獻1799條經(jīng)驗 獲得超6個贊
您要查找的ANSI轉(zhuǎn)義序列是“選擇圖形渲染”子集。所有這些都具有以下形式
\033[XXXm
其中XXX是一系列用分號分隔的參數(shù)。
可以在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),最后部分清除所有這些內(nèi)容(0)。
如下表所述,您可以設(shè)置許多文本屬性,例如粗體,字體,下劃線和&c。(StackOverflow不允許您在答案中放置適當?shù)谋砀袷遣皇呛苡薮???/p>
字體效果
╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
║ 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位色彩
你已經(jīng)有了這個!
4位色彩
實現(xiàn)端子顏色的標準從有限的(4位)選項開始。下表列出了各種終端仿真器用于這些背景和前景顏色的RGB值:
各種終端仿真器實現(xiàn)的ANSI顏色表
使用上述方法,您可以使用以下方法在綠色背景上制作紅色文本(但是為什么呢?):
\033[31;42m
11種顏色(插曲)
布倫特·柏林(Brent Berlin)和保羅·凱(Paul Kay)在他們的“基本顏色術(shù)語:它們的普遍性和演變”一書中,使用了從來自多種語言族的二十種不同語言中收集的數(shù)據(jù)來識別11種可能的基本顏色類別:白色,黑色,紅色,綠色,黃色,藍色,棕色,紫色,粉紅色,橙色和灰色。
柏林和凱發(fā)現(xiàn),在少于最多11種顏色類別的語言中,顏色遵循特定的進化模式。此模式如下:
所有語言均包含黑色(冷色)和白色(亮色)的術(shù)語。
如果一種語言包含三個術(shù)語,則它包含一個紅色術(shù)語。
如果一種語言包含四個術(shù)語,則它包含一個綠色或黃色(但不能同時包含)的術(shù)語。
如果一種語言包含五個術(shù)語,則它同時包含綠色和黃色的術(shù)語。
如果一種語言包含六個詞,則它包含一個藍色詞。
如果一種語言包含七個術(shù)語,則它包含一個棕色術(shù)語。
如果一種語言包含八個或更多的術(shù)語,則它包含紫色,粉紅色,橙色或灰色的術(shù)語。
這可能就是為什么故事Beowulf僅包含黑色,白色和紅色的原因。這也可能就是圣經(jīng)不包含藍色的原因。荷馬的奧德賽包含黑色幾乎200倍和白色大約100倍。紅色出現(xiàn)15次,而黃色和綠色出現(xiàn)10次。(更多信息在這里)
語言之間的差異也很有趣:請注意,英語和漢語使用了大量不同顏色的單詞。但是,深入研究這些語言表明每種語言都以不同的方式使用顏色。(更多信息)
中文和英文顏色名稱。 圖片取自“ muyueh.com”
一般來說,人類語言中顏色的命名,使用和分組是很有趣的?,F(xiàn)在,回到表演。
8位(256)顏色
技術(shù)先進,并且提供了256種預(yù)選顏色的表格,如下所示。
ANSI轉(zhuǎn)義序列的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
并且,當然,您可以將這些結(jié)合起來:
\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
所有顏色
現(xiàn)在我們生活在未來,可以使用以下方式獲得完整的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
這里列出了對“真彩色”終端的支持。
以上大部分內(nèi)容來自維基百科頁面“ ANSI轉(zhuǎn)義代碼 ”。
方便自己的腳本
因為我經(jīng)常想記住什么顏色是什么,所以我有一個方便的腳本,叫做~/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"
添加回答
舉報