Friday, September 23, 2011

256 colors

 
 

Sent to you by Danny via Google Reader:

 
 

via Hsian Studio by Anati on 3/4/10

在unix系統上,本來只有16色的,不過漸漸的,許多的應用程式都支援256色了,像是常用的xterm, urxvt, screen等都支援256色表示,此篇來介紹一下如何打造一個256色的環境。

1. xterm, rxvt-unicode, screen 支援256色
要達成預期的效果,當然是軟體需要重新編譯進加上支援256色的設定,如果你已安裝了,可以透過portupgrade使用-m的參數將256色支援的選項傳給ports來重新編譯。

軟體已安裝的人請用以下的指令
# portupgrade -ufv x11/xterm -m WITH_256_COLOR=yes
# portupgrade -ufv x11/rxvt-unicode -m WITH_XTERM_COLOR=yes
# cd /usr/ports/sysutils/screen && make config (選取256色支援)
# portupgrade -ufv sysutils/screen

未安裝的人請用以下的指令由ports安裝
# cd /usr/ports/x11/xterm && make WITH_256_COLOR=yes install clean
# cd /usr/ports/x11/rxvt-unicode && make WITH_XTERM_COLOR=yes install clean
# cd /usr/ports/sysutils/screen && make config (選取256色支援) && make install clean

2. 完成軟體支援之後先測試一下是不是可以正常支援256色了
下載 http://www.frexx.de/xterm-256-notes/data/256colors2.pl 是個perl程式
# perl ./256color2.pl 如果有支援的話,那應該會出現256種顏色

3. 之後就可以開始使用256種顏色了
可以參考以下兩個網頁的教學
http://www.frexx.de/xterm-256-notes/
http://www.pixelbeat.org/docs/terminal_colours/
使用256色的指令碼就像使用ansii color 16色一樣是由一些跳脫字元表示
\033[38;5;0~255m 設定前景色
\033[48;5;0~255m 設定背景色

以上適用於程式做printf出來使用,如果使用要使用在prompt或是用在motd的話需要在文字中直接寫入跳脫字元,以vim為例,要插入跳脫字元\033為先按下ctrl+v之後按下ESC鍵,即可輸入\033跳脫字元,而在編輯器中看到大概會是這樣
^[[38;5;0~255m 要上色的文字 ^[[m

如我的prompt就使用了256色來表示


 
 

Things you can do from here:

 
 

No comments: