Another source code with ncurses.
This time the source code will create a window with all colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | /* color-demo.c */ #include <curses.h> #include <stdio.h> #include <stdlib.h> void init_colorpairs(void); short curs_color(int fg); int colornum(int fg, int bg); void setcolor(int fg, int bg); void unsetcolor(int fg, int bg); void init_colorpairs(void) { int fg, bg; int colorpair; for (bg = 0; bg <= 7; bg++) { for (fg = 0; fg <= 7; fg++) { colorpair = colornum(fg, bg); init_pair(colorpair, curs_color(fg), curs_color(bg)); } } } short curs_color(int fg) { switch (7 & fg) { /* RGB */ case 0: /* 000 */ return (COLOR_BLACK); case 1: /* 001 */ return (COLOR_BLUE); case 2: /* 010 */ return (COLOR_GREEN); case 3: /* 011 */ return (COLOR_CYAN); case 4: /* 100 */ return (COLOR_RED); case 5: /* 101 */ return (COLOR_MAGENTA); case 6: /* 110 */ return (COLOR_YELLOW); case 7: /* 111 */ return (COLOR_WHITE); } } int colornum(int fg, int bg) { int B, bbb, ffff; B = 1 << 7; bbb = (7 & bg) << 4; ffff = 7 & fg; return (B | bbb | ffff); } // set the color pair by colornum and bold/bright A_BOLD void setcolor(int fg, int bg) { attron(COLOR_PAIR(colornum(fg, bg))); attron(A_BOLD); } // unset the color pair by colornum and bold/bright A_BOLD void unsetcolor(int fg, int bg) { attroff(COLOR_PAIR(colornum(fg, bg))); attroff(A_BOLD); } // main function int main(void) { int fg, bg; // initialize curses initscr(); keypad(stdscr, TRUE); cbreak(); noecho(); // initialize colors if (has_colors() == FALSE) { endwin(); puts("Your terminal does not support color"); exit(1); } start_color(); init_colorpairs(); // draw test pattern if ((LINES < 24) || (COLS < 80)) { endwin(); puts("Your terminal needs to be at least 80x24"); exit(2); } mvaddstr(0, 35, " COLORS used in ncurses ! "); mvaddstr(2, 0, " low intensity from 0-7 "); mvaddstr(12, 0, " high intensity from 8-15 "); for (bg = 0; bg <= 7; bg++) { for (fg = 0; fg <= 7; fg++) { setcolor(fg, bg); mvaddstr(fg + 3, bg * 10, " COLOR "); unsetcolor(fg, bg); } for (fg = 8; fg <= 15; fg++) { setcolor(fg, bg); mvaddstr(fg + 5, bg * 10, " COLOR "); unsetcolor(fg, bg); } } mvaddstr(LINES - 1, 0, "You can press any key to quit"); // refresh the screen refresh(); // get key getch(); // close window endwin(); exit(0); } |
This is result of the source code: