home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0151.lha / SCT / sct.c < prev    next >
C/C++ Source or Header  |  1987-06-15  |  6KB  |  176 lines

  1. /*  SetColorTable - CLI invoked ColorTable fiddler.
  2.     Copyright 1988 aklevin.  All rights reserved.
  3.  
  4.     Manx: cc +L, ln -lc32
  5. */
  6.  
  7. #include <intuition/intuitionbase.h>
  8. #include <graphics/gfxbase.h>
  9. #include <stdio.h>
  10.  
  11. #define INTUITION_REV    33
  12. #define GRAPHICS_REV    33
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15. struct GfxBase *GfxBase;
  16.  
  17. /*  Error severity values:  */
  18. #define EXIT_LOW    0
  19. #define EXIT_MED    10
  20. #define EXIT_HIGH    20
  21.  
  22.  
  23. /*  L E A V E  - Prints the error message (if any),
  24.                  closes any open libraries, and exits.
  25. */
  26. void
  27. leave(string, command_name, exit_val)
  28. char *string, *command_name;
  29. int exit_val;
  30. {
  31.     if (strlen(string)) fprintf(stderr, "%s: %s\n", command_name, string);
  32.     if (GfxBase) CloseLibrary(GfxBase);
  33.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  34.     exit(exit_val);
  35. }    /* end leave() */
  36.  
  37.  
  38. /*  U S A G E  - Gives the program parameter sumary.  */
  39. void
  40. usage(command_name, exit_val)
  41. char *command_name;
  42. int exit_val;
  43. {
  44. fprintf(stderr, "SetColorTable - Copyright 1988 aklevin.  All rights reserved.\n");
  45. fprintf(stderr, "\nUsage:\n");
  46. fprintf(stderr, "\n%s [-t \"Screen Title\"] Xrgb Xrgb Xrgb...\n", command_name);
  47. fprintf(stderr, "    Displays the current ColorTable settings for the Workbench Screen,\n");
  48. fprintf(stderr, "    (or the screen whose title is \"Screen Title\" if `-t' option is used)\n");
  49. fprintf(stderr, "    then sets the ColorTable to the values given by Xrgb Xrgb Xrgb... .\n");
  50. fprintf(stderr, "\n%s <colorfile [-t \"Screen Title\"]\n", command_name);
  51. fprintf(stderr, "    As above, but RGB values are in file 'colorfile'.\n");
  52. fprintf(stderr, "\n    Use \" >NIL:\" to prevent color values from being displayed.\n");
  53. fprintf(stderr, "    Use \" >file\" to save color values for later restoration.\n");
  54. fprintf(stderr, "    Any redirection ( <file  >file or >NIL:) MUST follow command name.\n");
  55. fprintf(stderr, "\nX r g b <- Amount of Blue  in this color (0 to f).\n");
  56. fprintf(stderr, "| | +----- Amount of Green in this color (0 to f).\n");
  57. fprintf(stderr, "| +------- Amount of Red   in this color (0 to f).\n");
  58. fprintf(stderr, "+--------- 0=Use value, 1=Skip value, 2=Skip this and all further values.\n\n");
  59. leave("", "", exit_val);
  60. }    /* end usage() */
  61.  
  62.  
  63. /*  S E T   C O L O R   T A B L E  - Accepts RGB values from either stdin
  64.                                      or the command line, and uses those
  65.                                      values to alter the ColorTable for
  66.                                      the named screen.
  67. */
  68.  
  69. /*  The largest ColorTable size.  */
  70. #define TABLE_SIZE    32
  71.  
  72. main(argc, argv)
  73. int argc;
  74. char *argv[];
  75. {
  76.  
  77. /*
  78.   Variables you should know:
  79.       from_cli- 0 if RGB values will come from stdin, 1 if from command line.
  80.     max_colors- Maximum number of colors for this screen.
  81.     num_colors- Counter for current number of colors.
  82.          title- Screen Title.
  83. */
  84. int argp=1, i, from_cli, max_colors=2, num_colors=0, scan_ok;
  85. char title[81];
  86. UWORD table[TABLE_SIZE], *tmp;
  87. struct Screen *screen;
  88. struct ViewPort *viewport=NULL;
  89.  
  90. /*  Set the default window title.  */
  91. strcpy (title, "Workbench Screen");
  92.  
  93. /*  Argument parsing.  */
  94. if (argc > 1) {
  95.     /*  Give usage if first argument is a '?'.  */
  96.     if (argv[argp][0] == '?') usage(argv[0], EXIT_LOW);
  97.     if (argv[argp][0] == '-') {
  98.         /*  Found an option.  Is it valid ('t' or 'T')?  */
  99.         if (tolower(argv[argp][1]) == 't') {
  100.             /*  Valid option, title is part of this argument.  */
  101.             if (strlen(argv[argp]) > 2) strcpy(title, &argv[argp++][2]);
  102.             else {
  103.                 /*  Valid option, title is next argument.  */
  104.                 if (argc > ++argp) strcpy(title, argv[argp++]);
  105.                 /*  No title given.  */
  106.                 else usage(argv[0], EXIT_MED);
  107.             }  /* end else */
  108.         }  /* end if (tolower */
  109.         else usage(argv[0], EXIT_MED);  /*  Wasn't a valid option.  */
  110.     }  /* end if (argv */
  111. }  /* end if (argc */
  112.  
  113. /*  If there are still command line arguments
  114.     to be parsed, then they must be RGB values.
  115. */
  116. from_cli = (argp < argc) ? 1 : 0;
  117.  
  118. /*  Open intuition and graphics libraries.  */
  119. IntuitionBase=(struct IntuitionBase *)
  120.     OpenLibrary("intuition.library", INTUITION_REV);
  121. if (IntuitionBase==NULL )
  122.     leave("Couldn't open intuition.library.", argv[0], EXIT_HIGH);
  123.  
  124. GfxBase=(struct GfxBase *)
  125.     OpenLibrary("graphics.library", GRAPHICS_REV);
  126. if (GfxBase==NULL)
  127.     leave("Couldn't open graphics.library.", argv[0], EXIT_HIGH);
  128.  
  129. /*  Find the screen with the desired title.  */
  130. for (screen=IntuitionBase->FirstScreen; screen; screen=screen->NextScreen) 
  131.     if (strcmp(screen->Title, title)==0) {
  132.         viewport = &(screen->ViewPort);
  133.         break;
  134.     }
  135.  
  136. /*  Quit if the screen couldn't be found.  */
  137. if (viewport==NULL) leave("Couldn't find that screen.", argv[0], EXIT_MED);
  138.  
  139. /*  Find out the maximum number of colors available.  Many thanks to
  140.     Bryce Nesbitt and Eugene Mortimore for opening my eyes to this.  */
  141. for (i=(int)screen->BitMap.Depth; i>1 ; i--) max_colors*=2;
  142.  
  143. /*  Print the current values, before they get changed.  */
  144. for (i=0; i<max_colors; i++)
  145.     printf("%04x%c", GetRGB4(viewport->ColorMap, i),
  146.         ((i<max_colors-1) ? ' ' : '\n'));
  147.  
  148. /*  Scanf the RGB values into the array 'table'.
  149.     There are four conditions which break out of this loop:
  150.     1) Current entry to be altered >= max_colors.
  151.     2) EOF reached on stdin or illegal value entered.
  152.     3) RGB values were being read from argv, and there are no more.
  153.     4) RGB value >= 0x2000, meaning "no more entries should be altered".
  154. */
  155. while ((num_colors < max_colors) && ( ! from_cli || (argp < argc))) {
  156.     if (from_cli) scan_ok = sscanf(argv[argp++], "%04x", &tmp);
  157.     else scan_ok = scanf("%04x", &tmp);
  158. /*  If EOF was reached or input wasn't legal,
  159.        don't alter this entry, or following ones.  */
  160.     if (scan_ok == EOF || scan_ok == NULL) break;
  161. /*  If first digit is >= 2, don't alter this entry, or following ones.  */
  162.     if (tmp >= 0x2000) break;
  163. /*  If first digit is >= 1, don't alter this entry.  */
  164.     if (tmp >= 0x1000) tmp = GetRGB4(viewport->ColorMap, num_colors);
  165.     table[num_colors++] = tmp;
  166. }  /* end while */
  167.  
  168. /*  Alter the ColorTable.  */
  169. LoadRGB4(viewport, &table[0], num_colors);
  170.  
  171. /*  All done.  */
  172. leave("", "", EXIT_LOW);
  173.  
  174. }    /* end main() */
  175.  
  176.