下面我通過我自己的電腦上的實驗(Ubuntu 14.04,python 3)計算出來的。
- 有256種顏色(由前8位定義)。
- 其他位用於其他屬性,如突出顯示。
- 傳遞數字-1作爲顏色回落到默認的背景和前景色。
- 顏色對0(mod 256)固定在(-1,-1)上。
- 顏色0到15是終端調色板的顏色。
請考慮以下測試代碼。 添加到您的.bashrc
:
# Set proper $TERM if we are running gnome-terminal
if [ "$COLORTERM" == "gnome-terminal" ]
then
TERM=xterm-256color
fi
在Python文件將這個並運行它。
import curses
def main(stdscr):
curses.start_color()
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
try:
for i in range(0, 255):
stdscr.addstr(str(i), curses.color_pair(i))
except curses.ERR:
# End of screen reached
pass
stdscr.getch()
curses.wrapper(main)
運行它將產生以下輸出。
正如所看到的,顏色對1-16是用於前景顏色終端調色板。
你確定這是該屏幕截圖的代碼嗎?在我的系統(Ubuntu 12.04)中,'curses.COLORS'是8,而不是256,並且任何嘗試使用上面引發異常'_curses.error:init_pair()返回ERR'的顏色初始化對的嘗試。你正在使用什麼'curses'模塊,它是Python的stdlib中的默認模塊? – MestreLion
是的,我確定這是正確的屏幕截圖。在我的ubuntu 14.04(使用python3)上再次運行它會得到相同的輸出。 'curses.COLORS'對我來說是256。 –
Gnome終端默認使用'TERM = xterm-256color'也是如此?大!還是你手動添加到你的'〜/ .profile' /'〜/ .bashrc'?小心檢查這些文件是否有任何與「TERM」相關的代碼? – MestreLion