你可以通过写\e]12;COLOR\a或\033]12;COLOR\007来改变颜色,它们都一样,这里是一个简单的例子:
#include <stdio.h>
#include <unistd.h>
void cursor_set_color_string(const char *color) {
printf("\e]12;%s\a", color);
fflush(stdout);
}
int main(int argc, char **argv) {
cursor_set_color_string("yellow"); sleep(1);
cursor_set_color_string("gray"); sleep(1);
cursor_set_color_string("blue"); sleep(1);
cursor_set_color_string("red"); sleep(1);
cursor_set_color_string("brown"); sleep(1);
return 0;
}
以下是颜色名称列表:Xterm Colors。
看起来你也可以使用\e]12;#XXXXXX\a形式的RGB颜色:
#include <stdio.h>
#include <unistd.h>
void cursor_set_color_rgb(unsigned char red,
unsigned char green,
unsigned char blue) {
printf("\e]12;#%.2x%.2x%.2x\a", red, green, blue);
fflush(stdout);
}
int main(int argc, char **argv) {
cursor_set_color_rgb(0xff, 0xff, 0xff); sleep(1);
cursor_set_color_rgb(0xff, 0xff, 0x00); sleep(1);
cursor_set_color_rgb(0xff, 0x00, 0xff); sleep(1);
cursor_set_color_rgb(0x00, 0xff, 0xff); sleep(1);
return 0;
}