其他分享
首页 > 其他分享> > C printf ANSI控制码之字体颜色、背景颜色

C printf ANSI控制码之字体颜色、背景颜色

作者:互联网

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define COLOR_NONE              "\033[0m"
 5 #define COLOR_HIGHLIGHT         "\033[1m"
 6 #define COLOR_NON_BOLD          "\033[22m"
 7 #define COLOR_UNDERLINE         "\033[4m"
 8 #define COLOR_NON_UNDERLINE     "\033[24m"
 9 #define COLOR_FLICKER           "\033[5m"
10 #define COLOR_NON_FLICKER       "\033[25m"
11 #define COLOR_REVERSE_VIDEO     "\033[7m"
12 #define COLOR_NON_REVERSE_VIDEO "\033[27m"
13 #define COLOR_BLINK             "\033[8m"
14 
15 #define FONT_COLOR_BLACK        "\033[30m"
16 #define FONT_COLOR_RED          "\033[31m"
17 #define FONT_COLOR_GREEN        "\033[32m"
18 #define FONT_COLOR_YELLOW       "\033[33m"
19 #define FONT_COLOR_BULE         "\033[34m"
20 #define FONT_COLOR_PURPLE       "\033[35m"
21 #define FONT_COLOR_DARKGREEN    "\033[36m"
22 #define FONT_COLOR_WHITE        "\033[37m"
23 
24 #define BACKGROUND_COLOR_BLACK      "\033[40m"
25 #define BACKGROUND_COLOR_DARKRED    "\033[41m"
26 #define BACKGROUND_COLOR_GREEN      "\033[42m"
27 #define BACKGROUND_COLOR_YELLOW     "\033[43m"
28 #define BACKGROUND_COLOR_BULE       "\033[44m"
29 #define BACKGROUND_COLOR_PURPLE     "\033[45m"
30 #define BACKGROUND_COLOR_DARKGREEN  "\033[46m"
31 #define BACKGROUND_COLOR_WHITE      "\033[47m"
32 
33 static char *font_color[] = {
34     FONT_COLOR_BLACK, FONT_COLOR_RED, FONT_COLOR_GREEN, FONT_COLOR_YELLOW, 
35     FONT_COLOR_BULE, FONT_COLOR_PURPLE, FONT_COLOR_DARKGREEN, FONT_COLOR_WHITE
36 };
37 
38 static char *background_color[] = {
39     BACKGROUND_COLOR_BLACK, BACKGROUND_COLOR_DARKRED, BACKGROUND_COLOR_GREEN, BACKGROUND_COLOR_YELLOW, 
40     BACKGROUND_COLOR_BULE, BACKGROUND_COLOR_PURPLE, BACKGROUND_COLOR_DARKGREEN, BACKGROUND_COLOR_WHITE
41 };
42 
43 static char *control_color[] = {
44     COLOR_HIGHLIGHT, COLOR_NON_BOLD, COLOR_UNDERLINE, COLOR_NON_UNDERLINE, COLOR_FLICKER, 
45     COLOR_NON_FLICKER, COLOR_REVERSE_VIDEO, COLOR_NON_REVERSE_VIDEO, COLOR_BLINK
46 };
47 
48 int main(void)
49 {
50     int i, j, z;
51     char buf[256] = {0};
52 
53     for (i = 0; i < 9; ++i) {
54         for (j = 0; j < 8; ++j) {
55             for (z = 0; z < 8; ++z) {
56                 strncat(buf, control_color[i], strlen(control_color[i]));
57                 strncat(buf, font_color[j], strlen(font_color[j]));
58                 strncat(buf, background_color[z], strlen(background_color[z]));
59                 strncat(buf, "123abc456def\n", strlen("123abc456def\n"));
60                 strncat(buf, COLOR_NONE, strlen(COLOR_NONE));
61                 printf("i: %d, j: %d, z: %d\n", i, j, z);
62 
63                 printf("%s", buf);
64                 memset(buf, 0, 256);
65             }
66         }
67     }
68 
69     return 0;
70 }

 

标签:颜色,COLOR,ANSI,BACKGROUND,printf,033,FONT,color,define
来源: https://www.cnblogs.com/coolYuan/p/15745964.html