home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / linux / old-src / ncurses-1.8.5 / color.c < prev    next >
C/C++ Source or Header  |  1993-11-27  |  3KB  |  160 lines

  1.  
  2. /*
  3.  * Demo zur Curses-Color-Unterstuetzung
  4.  */
  5. #include <ncurses.h>
  6. short int  i=0,f, b, R, B, G;
  7.  
  8. #define FORGROUND 1
  9. #define BACKGROUND 0
  10.  
  11. void modf(int setting);        
  12. void fillwin(void);
  13. void process(void);
  14. void setfb(char setting);
  15. void modfb(int setting);        
  16.  
  17.  
  18. void main(int argc, char ** argv) 
  19. {
  20.     initscr();
  21.     keypad(stdscr, TRUE);
  22.     cbreak();
  23.     noecho();
  24.     scrollok(stdscr, TRUE);
  25.     
  26.     if(!has_colors()) {
  27.                 endwin();
  28.         fprintf(stderr, "\ncolor not supported \n");
  29.         exit(1);
  30.     }
  31.     
  32.     start_color();
  33.     init_pair(1, COLOR_WHITE, COLOR_BLACK);
  34.     attron(COLOR_PAIR(1));
  35.     
  36.     process();
  37.     endwin();
  38. }
  39.  
  40. void fillwin(void)
  41. {
  42.     register int y, x;
  43.     
  44.     move(0,0);
  45.     for(y = 0; y < LINES; y++)
  46.         for(x = 0; x < COLS; x++)
  47.             addch(' ');
  48. }
  49.  
  50. void process(void)
  51. {
  52.     while(1) {
  53.         fillwin();
  54.         mvprintw(8, 15, "Terminal supports %d Colors", COLORS);
  55.         mvprintw(10, 15, "Function keys are:");
  56.         mvprintw(12, 15, "F1 = Set Foreground");
  57.         mvprintw(13, 15, "F2 = Set Background");
  58.         mvprintw(14, 15, "F3 = Modify RED content");
  59.         mvprintw(15, 15, "F4 = Modify GREEN content");
  60.         mvprintw(16, 15, "F5 = Modify BLUE content");
  61.         mvprintw(17, 15, "F8 = Quit Program");
  62.         refresh();
  63.         
  64.         switch(getch()) {
  65.         case KEY_F(1):
  66.             setfb(FORGROUND);
  67.             break;
  68.         case KEY_F(2):
  69.             setfb(BACKGROUND);
  70.             break;
  71.         case KEY_F(3):
  72.             modfb(COLOR_RED);
  73.             break;
  74.         case KEY_F(4):
  75.             modfb(COLOR_GREEN);
  76.             break;
  77.         case KEY_F(5):
  78.             modfb(COLOR_BLUE);
  79.             break;
  80.         case KEY_F(8):
  81.             return;
  82.         default:
  83.             beep();
  84.         }
  85.     }
  86. }
  87.  
  88. void setfb(char setting)
  89. {
  90.     while(1) {
  91.         fillwin();
  92.         touchwin(stdscr);
  93.         mvprintw(12, 10, "Use <--  --> Keys");
  94.         mvprintw(13, 10, "F1 to select");
  95.         refresh();
  96.         switch(getch()) {
  97.         case KEY_LEFT:
  98.             if(setting == FORGROUND)
  99.                 f = f == 0 ? COLORS - 1: f - 1;
  100.             else
  101.                 b = b == 0 ? COLORS - 1: b - 1;
  102.             break;
  103.         case KEY_RIGHT:
  104.             if(setting == FORGROUND)
  105.                 f = f == COLORS - 1 ? 0: f + 1;
  106.             else
  107.                 b = b == COLORS - 1 ? 0: b + 1;
  108.             break;
  109.         case KEY_F(1):
  110.             return;
  111.         default:
  112.             beep();
  113.         }
  114.         init_pair(++i, f, b);
  115.         attrset(COLOR_PAIR(i));
  116.         if(i==2) i=0;
  117.         
  118.     }
  119. }
  120.  
  121. void modfb(int setting)        
  122. {
  123.     if(!can_change_color()) {
  124.         beep();
  125.         return;
  126.     }
  127.         
  128.     fillwin();
  129.     mvprintw(12, 10, "Use <--  --> Keys");
  130.     mvprintw(13, 10, "F1 to select");
  131.     refresh();
  132.     while(1) {
  133.         switch(getch()) {
  134.         case KEY_LEFT:
  135.             if(setting == COLOR_RED)
  136.                 R = R == 0 ? 1000 : R - 1;
  137.             else if(setting == COLOR_GREEN)
  138.                 G = G == 0 ? 1000 : G - 1;
  139.             else    
  140.                 B = B == 0 ? 1000 : B - 1;
  141.             break;
  142.         case KEY_RIGHT:
  143.             if(setting == COLOR_RED)
  144.                 R = R == 1000 ? 0 : R + 1;
  145.             else if(setting == COLOR_GREEN)
  146.                 G = G == 1000 ? 0 : G + 1;
  147.             else    
  148.                 B = B == 1000 ? 0 : B + 1;
  149.             break;
  150.         case KEY_F(1):
  151.             return;
  152.         default:
  153.             beep();
  154.         }
  155.         init_color(f, R, G, B);
  156.     }
  157. }
  158.                     
  159.  
  160.