home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 1 / FREEWARE.BIN / gunix / cpc.c < prev    next >
C/C++ Source or Header  |  1989-10-17  |  4KB  |  180 lines

  1. /* 
  2.  *     cpc.c ----- change palette color.
  3.  * 
  4.  * 
  5.  *    Usage:  cpc   -{c,m} 
  6.  * 
  7.  *            -c : Resets to the dafault color of console. 
  8.  *            -m : Resets to the dafault color of MSDOS. 
  9.  * 
  10.  *              cpc     color b [r [g] ]
  11.  * 
  12.  *                       color : palette number ( 0 - 16 ).
  13.  *                      b :     Blue value     ( 0 - 255 ). 
  14.  *                      r :     Red  value     ( 0 - 255 ). 
  15.  *                      g :     Green value    ( 0 - 255 ). 
  16.  * 
  17.  */ 
  18.  
  19. #include <stdio.h>
  20. #include <dos.h>
  21. #include <ctype.h>
  22.  
  23. #define P_ADDR        0x0fd90
  24. #define P_BLUE        0x0fd92
  25. #define P_RED         0x0fd94
  26. #define P_GREEN     0x0fd96
  27.  
  28. #define    VAL0    0
  29. #define    VAL1    255
  30. #define    VAL2    64
  31. #define    VAL3    128
  32.  
  33. #define    FAIL    -1 
  34.  
  35. /* console default color */ 
  36. static    unsigned    char p0[16][3]= {
  37.      VAL2,   VAL2,   VAL2,    /* 0  gray */
  38.      VAL1,   VAL1,   VAL1,    /* 1  white */
  39.      VAL2,   VAL2,   VAL2,    /* 2  gray */
  40.      VAL1,   VAL1,   VAL1,    /* 3  white */
  41.      VAL0,   VAL1,   VAL1,    /* 4  yellow */
  42.      VAL0,   VAL1,   VAL1,    /* 5  yellow */
  43.      VAL0,   VAL1,   VAL1,    /* 6  yellow */
  44.      VAL0,   VAL0,   VAL0,    /* 7  black */
  45.  
  46.      VAL0,   VAL1,   VAL0,    /* 8  red */
  47.      VAL0,   VAL0,   VAL0,    /* 9  black */
  48.      VAL0,   VAL1,   VAL0,    /* 10 red */
  49.      VAL0,   VAL0,   VAL0,    /* 11 black */
  50.      VAL0,   VAL1,   VAL0,    /* 12 red */
  51.      VAL0,   VAL0,   VAL0,    /* 13 black */
  52.      VAL0,   VAL1,   VAL0,    /* 14 red */
  53.      VAL0,   VAL0,   VAL0    /* 15 black */
  54. };
  55.  
  56. /* MSDOS default color */ 
  57. static    unsigned    char p1[16][3]= {
  58.      VAL0,   VAL0,   VAL0,    /* 0  Black */
  59.      VAL3,   VAL0,   VAL0,    /* 1  Blue */
  60.      VAL0,   VAL3,   VAL0,    /* 2  Red */
  61.      VAL0,   VAL0,   VAL3,    /* 3  Green */
  62.      VAL0,   VAL3,   VAL3,    /* 4  Yellow */
  63.      VAL3,   VAL3,   VAL0,    /* 5  sky blue */
  64.      VAL3,   VAL0,   VAL3,    /* 6  cyian */
  65.      VAL3,   VAL3,   VAL3,    /* 7  White */
  66.  
  67.      VAL0,   VAL0,   VAL0,    /* 8  Black */
  68.      VAL1,   VAL0,   VAL0,    /* 9  Blue with hilight*/
  69.      VAL0,   VAL1,   VAL0,    /* 10 Red with hilight*/
  70.      VAL0,   VAL0,   VAL1,    /* 11 Green with hilight*/
  71.      VAL0,   VAL1,   VAL1,    /* 12 Yellow with hilight*/
  72.      VAL1,   VAL1,   VAL0,    /* 13 Sky Blue with hilight*/
  73.      VAL1,   VAL0,   VAL1,    /* 14 Cyian with hilight*/
  74.      VAL1,   VAL1,   VAL1    /* 15 White with hilight*/
  75. };
  76.  
  77. main( ac, av )
  78. char    **av; 
  79. {
  80.     int    color, b, r, g, i, c;
  81.  
  82.     switch( ac ){ 
  83.         case 2: 
  84.         if ( av[1][0] == '-' ){ 
  85.             c = tolower( av[1][1] ); 
  86.             if ( c == 'c' ){ 
  87.                 /* console dafault */ 
  88.                 for( i=0; i<16; i++ ){ 
  89.                     outp(P_ADDR,i);
  90.                     outp(P_BLUE, p0[i][0]);
  91.                     outp(P_RED,  p0[i][1]);
  92.                     outp(P_GREEN,p0[i][2]);
  93.                 } 
  94.                 exit(0); 
  95.             } 
  96.             else if ( c == 'm' ){ 
  97.                 /* msdos dafault */ 
  98.                 for( i=0; i<16; i++ ){ 
  99.                     outp(P_ADDR,i);
  100.                     outp(P_BLUE, p1[i][0]);
  101.                     outp(P_RED,  p1[i][1]);
  102.                     outp(P_GREEN,p1[i][2]);
  103.                 } 
  104.                 exit(0); 
  105.             } 
  106.         } 
  107.         break; 
  108.  
  109.         case 3: 
  110.         if ( (color=str_digit(av[1])) != FAIL && 
  111.              (b=str_digit(av[2])) != FAIL){
  112.             g = r = b; 
  113.             set_palette( color, b, r, g ); 
  114.             exit(0); 
  115.         } 
  116.         break; 
  117.  
  118.         case 4: 
  119.         if ( (color=str_digit(av[1])) != FAIL && 
  120.              (b=str_digit(av[2])) != FAIL &&
  121.              (r=str_digit(av[3])) != FAIL){
  122.             g = r; 
  123.             set_palette( color, b, r, g ); 
  124.             exit(0); 
  125.         } 
  126.         break; 
  127.  
  128.         case 5: 
  129.         if ( (color=str_digit(av[1])) != FAIL && 
  130.              (b=str_digit(av[2])) != FAIL &&
  131.              (r=str_digit(av[3])) != FAIL &&
  132.              (g=str_digit(av[4])) != FAIL){
  133.             set_palette( color, b, r, g ); 
  134.             exit(0); 
  135.         } 
  136.         break; 
  137.  
  138.         default: 
  139.         break; 
  140.     } 
  141.  
  142.     /* Start Interractive processing */ 
  143.     printf("Change Pallete Color  for DMYCON or Console (Gunix)\n"); 
  144.     printf("Copyright (C)  Panic Corp 1989. All rights reserved  ではありません\n"); 
  145.     printf("       ^C to quit\n"); 
  146.  
  147.     while(1){
  148.         printf("Color  : Coloe code    ( 0 --> 15 )\n"); 
  149.         printf("Blue   : Blue  Value   ( 0 --> 255 )\n"); 
  150.         printf("Red    : Red   Value   ( 0 --> 255 )\n"); 
  151.         printf("Green  : Green Value   ( 0 --> 255 )\n"); 
  152.         printf("Color, Blue, Red, Green ===>> "); 
  153.         scanf("%d %d %d %d", &color, &b, &r, &g); 
  154.         set_palette( color, b, r, g ); 
  155.     }
  156. }
  157.  
  158. str_digit( str ) 
  159. char    *str; 
  160.     char    *p=str; 
  161.     int    digit; 
  162.  
  163.     while( *p ){ 
  164.         if ( !isdigit( *p ) ) 
  165.             return FAIL; 
  166.         p++; 
  167.     } 
  168.     sscanf( str, "%d", &digit ); 
  169.     return    digit; 
  170.  
  171. set_palette( color, b, r, g ) 
  172.     outp(P_ADDR,(char)color);
  173.     outp(P_BLUE, (char)b);
  174.     outp(P_RED, (char)r);
  175.     outp(P_GREEN, (char)g);
  176.