Linux下C 更改字符在终端的显示颜色

使用\033[01;04;32;41m之类的配色方案在需要输出显示的文本之前,可以改变应用程序输出文本的颜色或者背景颜色。


比如:

#include <stdio.h>


int main(void) {
printf("\033[01;34m Hello World!\033[0m\n");

return 0;
}

上面的01表示加粗,34表示是蓝色,后面\033[0m表示恢复所有的属性为原来的默认值。也可以把上述的\033字符用\e替换。可以采用多种配色方案,比如上面提到的\033[01;04;32;41m,04表示下划线,32表示前景色是绿色,然后41表示背景色是红色。

由于使用的是Linux系统为终端提供的配色方案,所以该程序不具备移植性。可以看到,该程序在Windows会打印一些奇怪的符号。 

 

Some fun

You can paste the following into a terminal to get a stupid matrix like screensaver, or for an infinitely better example, see cmatrix.

tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock |
GREP_COLOR="1;32" grep --color "[^ ]"

For a silly 256 colour demo here is a command line to cycle through all shades of gray

yes "$(seq 232 255;seq 254 -1 233)" |
while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done

Note also the venerable game 0verkill, and the Colour AsCii Art library, which is included with mplayer for example:

mplayer -vo caca video.mpg

 

 

 

posted on 2014-04-08 22:21  Stomach_ache  阅读(323)  评论(0编辑  收藏  举报

导航