home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d126 / colour.lha / Colour / colour.c < prev    next >
C/C++ Source or Header  |  1988-01-02  |  7KB  |  288 lines

  1. /*
  2.  *  Colour.c - a program for manipulating the colours of screens.
  3.  *
  4.  *  Written because I like one set of colours in a CLI and another in
  5.  *  my terminal window, and can't always afford the memory for another
  6.  *  screen. Plus, I like a more direct way of changing colours than
  7.  *  twiddling a bunch of slider gadgets (although that way has its uses
  8.  *  too, so I include the capability). Lastly, Americans don't know
  9.  *  how to spell "colour" :-).
  10.  *
  11.  *  Features include:
  12.  *    - save all colours from a given screen
  13.  *    - load a set of colours into a given screen
  14.  *    - set a particular colour register in a given screen
  15.  *    - adjust screen colours interactively (OPTIONAL)
  16.  *
  17.  *  Copyright 1987, John Russell
  18.  *
  19.  *  5 Alderdice Place
  20.  *  St. John's, Newfoundland
  21.  *  Canada   A1B 2P8
  22.  *  (709) 726-7847
  23.  *
  24.  *  Freely redistributable.
  25.  */
  26.  
  27. /* Manx users compile with "+l" just in case */
  28.  
  29. /* These includes might not be enough, I use precompiled includes. */
  30. /* anything to do with screens & viewports may be required */
  31.  
  32. #include <intuition/intuition.h>
  33. #include <intuition/intuitionbase.h>
  34. #include <stdio.h>
  35. #include "ctype.h"
  36.  
  37. #define LOAD 1
  38. #define SAVE 2
  39. #define SET 3
  40. #define ADJUST 4
  41.  
  42. /* if "fancy" is defined, you have the option to adjust on-the-fly */
  43.  
  44. #define FANCY 1
  45.  
  46. /* debug = "puts" to enable tracing, leave blank to disable */
  47.  
  48. #define debug(x)
  49.  
  50. struct IntuitionBase *IntuitionBase=NULL;
  51. struct GfxBase *GfxBase=NULL;
  52.  
  53. char *usage = "\n\
  54. Bad syntax.\n\n\
  55. Usage 1: colour load   <screen | \"-null\"> [from] <filename>.\n\
  56. Usage 2: colour save   <screen | \"-null\"> [to] <filename>.\n\
  57. Usage 3: colour set    <screen | \"-null\"> index R G B.\n\
  58. Usage 4: colour adjust <screen | \"-null\">.\n\n\
  59. Note  -  Specify screen by first word of title (case doesn't matter).\n\
  60. Note  -  Load file is searched for as \"file\" and \"s:file.col\".\n";
  61.  
  62. main(argc,argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     struct Screen *screen;    /* which screen to consider */
  67.     struct ViewPort *vp;    /* controlling viewport for screen */
  68.     FILE *fp;            /* for loading & saving */
  69.     short good_args = 0;    /* right # arguments for syntax? */
  70.     short mode = 0;        /* what type of command is it? */
  71.     int r,g,b;
  72.     int i,n,fname,depth;    /* depth == number of colours in screen */
  73.     UWORD colour;        /* I only remember these typedefs */
  74.     struct ColorMap *map;      /* when reading includes :-) */
  75.     char name[80];
  76.  
  77.     /* check for valid arguments and commands */
  78.  
  79.     if (argc < 3)    /* always need at least 3 */
  80.     {
  81.     puts(usage);
  82.     exit(0);
  83.     }
  84.     else if (!compare(argv[1],"load"))
  85.     {
  86.     debug("Asked to load a colour set.");
  87.     mode = LOAD;
  88.     if (argc == 4 || (!compare(argv[3],"from") && argc == 5))
  89.         good_args = 1;
  90.     }
  91.     else if (!compare(argv[1],"save"))
  92.     {
  93.     debug("Asked to save a colour set.");
  94.     mode = SAVE;
  95.     if (argc == 4 || (!compare(argv[3],"to") && argc == 5))
  96.         good_args = 1;
  97.     }
  98.     else if (!compare(argv[1],"set"))
  99.     {
  100.     debug("Asked to set a single colour.");
  101.     mode = SET;
  102.     if (argc == 7) good_args = 1;
  103.     }
  104.  
  105. #ifdef FANCY
  106.     else if (!compare(argv[1],"adjust"))
  107.     {
  108.     debug("Asked to adjust screen colours.");
  109.     mode = ADJUST;
  110.     if (argc == 3) good_args = 1;
  111.     }
  112. #endif
  113.  
  114.     fname = argc - 1;    /* argv[fname] = file for load & save */
  115.  
  116.     if (!good_args) {
  117.     puts(usage);
  118.     exit(0);
  119.     }
  120.  
  121.     debug("Arguments are legal.");
  122.  
  123.     if (! (IntuitionBase = (struct IntuitionBase *)
  124.         OpenLibrary("intuition.library",33L)))
  125.     {
  126.     puts("Please use Kickstart/Workbench V1.2 for latest Intuition.");
  127.     exit(0);
  128.     }
  129.  
  130.     if (! (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33L)))
  131.     {
  132.     puts("Please use Kickstart/Workbench V1.2 for latest Graphics.");
  133.     exit(0);
  134.     }
  135.  
  136.     screen = IntuitionBase->FirstScreen;
  137.  
  138. /* the target screen is found when the first word of the title matches the
  139.    2nd argument specified, OR if the 2nd argument is -null, the first
  140.    screen with NO title is matched (eg the DPaint screen) */
  141.  
  142.     LockIBase(NULL);    /* prevent window list from changing while in loop */
  143.  
  144.     while (screen)    /* do all screens */
  145.     {
  146.         if (!compare(argv[2],screen->Title) ||
  147.            ((screen->Title == NULL) && (!compare(argv[2],"-null"))))
  148.         {
  149.         UnlockIBase(NULL);
  150.  
  151.         debug("Found the indicated screen.");
  152.  
  153.         vp = &screen->ViewPort;     /* don't like 'em much myself */
  154.         map = vp->ColorMap;
  155.         depth = 1 << screen->BitMap.Depth;   /* no easier way? */
  156.  
  157.         switch (mode)
  158.         {
  159.  
  160.             case SET:
  161.  
  162.             i = atoi(argv[3]);  /* colour index */
  163.             r = atoi(argv[4]);  /* RGB values */
  164.             g = atoi(argv[5]);
  165.             b = atoi(argv[6]);
  166.  
  167.             if (i < depth)
  168.             {
  169.                 SetRGB4(vp,i,r,g,b);
  170.                 debug("Set single colour.");
  171.             }
  172.             else
  173.                 puts("Illegal colour index for this screen.");
  174.  
  175.             break;
  176.  
  177.             case LOAD:    /* (LA Law is on in the background) */
  178.  
  179.             /* search current directory and S: for file */
  180.  
  181.             /* look for for file as "file" and "s:file.col" */
  182.  
  183.  
  184.             if (!(fp = fopen(argv[fname],"r")))
  185.             {
  186.                 /* Boy do I hate C string functions! */
  187.  
  188.                 strcpy(name,"s:");
  189.                 strcat(name,argv[fname]);
  190.                 strcat(name,".col");
  191.  
  192.                 debug("Searching for file in S: directory.");
  193.                 fp = fopen(name,"r");
  194.             }
  195.  
  196.             if (!fp)
  197.             {
  198.                 puts("Unable to open load file!");
  199.                 goto QUIT;
  200.             }
  201.  
  202.             debug("Opened file successfully.");
  203.  
  204.             /* This is an *ugly* loop condition! */
  205.  
  206.             for (i=0;
  207.                  (i < depth)
  208.                 &&
  209.                  (fscanf(fp,"%d %d %d\n",&r,&g,&b)==3);
  210.                  i++)
  211.             {
  212.                 SetRGB4(vp,i,r,g,b);
  213.                 debug("Set a colour.");
  214.             }
  215.  
  216.             fclose(fp);
  217.             break;
  218.  
  219.             case SAVE:
  220.  
  221.             fp = fopen(argv[fname],"w");
  222.  
  223.             if (!fp) {
  224.                 puts("Unable to open save file!");
  225.                 goto QUIT;
  226.             }
  227.  
  228.             for (i=0; i < depth; i++)
  229.             {
  230.                 colour = GetRGB4(map,i);
  231.  
  232.                 /* each colour component encoded by 4 bits */
  233.  
  234.                 r = (colour & 0xf00) >> 8;
  235.                 g = (colour & 0x0f0) >> 4;
  236.                 b = (colour & 0x00f);
  237.  
  238.                 fprintf(fp,"%d %d %d\n",r,g,b);
  239.                 debug("Saved a colour.");
  240.             }
  241.  
  242.             fclose(fp);
  243.  
  244.             break;
  245.  
  246. #ifdef FANCY
  247.             case ADJUST:
  248.             ScreenToFront(screen);
  249.             DoColorWindow(screen);    /* I can't take credit for */
  250.             break;            /* this section */
  251. #endif
  252.  
  253.             default:
  254.  
  255.             debug("Illegal instruction mode.");
  256.             break;
  257.         }
  258.  
  259.         goto QUIT;    /* only match 1 screen */
  260.  
  261.         }
  262.  
  263.         screen = screen->NextScreen;
  264.  
  265.     }
  266.  
  267.     UnlockIBase(NULL);    /* didn't find the screen at all */
  268.     debug("Didn't find indicated screen.");
  269.  
  270. QUIT:
  271.     CloseLibrary(IntuitionBase);
  272.     CloseLibrary(GfxBase);
  273. }
  274.  
  275. /* case-insensitive string comparison, with \0 and space as end conditions */
  276.  
  277. compare(string1,string2)
  278. char *string1,*string2;
  279. {
  280.     while ((*string1 != '\0') && (*string1 != ' '))
  281.     {
  282.     if (toupper(*(string1++)) != toupper(*(string2++)))
  283.         return(1);
  284.     }
  285.     return(0);    /* return weird values like strcmp() */
  286. }
  287.  
  288.