home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 092.lha / ColorFull / palette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-21  |  16.8 KB  |  568 lines

  1. /***************************************************************************
  2. * palette.c  by: Keith Young                                      12/20/87
  3. *
  4. *   These routines were origionally written by: Charlie Heath of Microsmiths
  5. * (palette.c on Manx Aztec C examples disk) and were Heavily modified by me
  6. * ( Keith Young ) for use in my ShareWare terminal program: "Access!" and
  7. * then further modified for "portability" and infestation into the public.
  8. * (as FREEWARE).
  9. *  I'll not attemp to list all the modifications made (sounds to much like
  10. * work). Nor will I guarantee that they even work at all (but hey, they work
  11. * for me :-). 
  12. *
  13. *  You may Use these source files, modify 'em, throw 'em away, give 'em away,
  14. * without obligation. JUST DON'T SELL 'EM! (I doubt you'd get much anyway:-)
  15. *   If you do use 'em (in a noncomercial manner), I would appeciate a small
  16. * "mention" in the docs somewhere. (small print, lower left hand corner will
  17. * do )
  18. *  
  19. *  These routines now compile using AZTEC 3.4a using 16 bit ints.(horay!)
  20. * (see included makefile)
  21. *
  22. *   Any bug reports/suggestions/fixes/etc... would be appreciated.
  23. *   I can be reached at:              (703) 297-7639
  24. *
  25. * via    E-MAIL->  GEnie: K.Young
  26. *                 CA-AUG: Keith Young
  27. *           Deep Thought: Keith Young
  28. *             COMPUSERVE: 73170,307
  29. *
  30. * via Snail-Mail-> Keith Young
  31. *                  Rt.2 Box 261
  32. *                  Goodview, Va.   (yes, really :-)
  33. *                           24095
  34. *  
  35. ***************************************************************************/
  36.  
  37. #include "standard.h"
  38. #include <stdio.h>
  39.  
  40. #define FAST register       /* Register type variable  */
  41. #define CTSIZ  8L           /* Color table size   */
  42. #define PLX 230L            /* Palette Window Size  */
  43. #define PLY 95L
  44. #define PGAD 0L             /* You can offset the gadgets here  */
  45. #define PGHI PGAD+CTSIZ     /* Offset frm color selectors       */
  46.  
  47. extern struct Window       *OpenWindow();
  48. extern struct IntuiMessage *GetMsg();
  49.  
  50. /* these are "included" into "getfile.c" currently... */
  51. extern struct Image        okupImage,okdnImage,canupImage,candnImage,
  52.                            reupImage,rednImage,ldupImage,lddnImage,
  53.                            svupImage,svdnImage;
  54.  
  55. extern UBYTE  *get_fname();
  56. extern UBYTE  *prep_file();
  57.  
  58. static struct IntuiText rtxt = {3,2,JAM1,-10,2,NULL,(UBYTE *)"R",NULL};
  59. static struct IntuiText gtxt = {6,2,JAM1,-10,2,NULL,(UBYTE *)"G",NULL};
  60. static struct IntuiText btxt = {7,2,JAM1,-10,2,NULL,(UBYTE *)"B",NULL};
  61.  
  62. static char frog[] = "hex";
  63. static struct IntuiText hxtxt = {3,2,JAM2,180,19,NULL,(UBYTE *)frog,NULL};
  64. static char hxtab[16] = { '0','1','2','3','4','5','6','7',
  65.                           '8','9','a','b','c','d','e','f' };
  66.  
  67. static struct Image    r_img, g_img, b_img; 
  68. static struct PropInfo r_prop,g_prop,b_prop;
  69.  
  70.  
  71. /* border around color gadgets */
  72.  
  73. static SHORT oXY[] = {
  74.     0,0,
  75.     155,0,
  76.     155,21,
  77.     0,21,
  78.     0,0,
  79. };
  80.  
  81. static struct Border cbdr = {
  82.     0,0,
  83.     0,0,JAM1,
  84.     5,
  85.     oXY,
  86.     NULL
  87. };
  88.  
  89. /* border around selected color image */
  90.  
  91. static SHORT oXY1[] = {
  92.     0,0,
  93.     52,0,
  94.     52,21,
  95.     0,21,
  96.     0,0
  97. };
  98.  
  99. static struct Border sbdr = {
  100.     0,0,
  101.     0,0,JAM1,
  102.     5,
  103.     oXY1,
  104.     NULL
  105. };
  106.  
  107. static struct Gadget blue_gad = {
  108.    NULL, 17,61, 130,11, 
  109.    GADGHCOMP,                                 /* BLUE_PROP */
  110.    GADGIMMEDIATE | RELVERIFY,
  111.    PROPGADGET,
  112.    (APTR)&b_img, 
  113.    NULL,
  114.    &btxt, NULL,(APTR)&b_prop, PGHI+3, NULL };
  115.  
  116. static struct Gadget green_gad = {
  117.    &blue_gad, 17,49, 130,11, 
  118.    GADGHCOMP,                             /* GREEN_PROP */
  119.    GADGIMMEDIATE | RELVERIFY,
  120.    PROPGADGET,
  121.    (APTR)&g_img, 
  122.    NULL,
  123.    >xt, NULL,(APTR)&g_prop, PGHI+4, NULL };
  124.  
  125. static struct Gadget red_gad = {
  126.    &green_gad, 17,37, 130,11, 
  127.    GADGHCOMP,                              /* RED_PROP */
  128.    GADGIMMEDIATE | RELVERIFY,
  129.    PROPGADGET,
  130.    (APTR)&r_img, 
  131.    NULL,
  132.    &rtxt, NULL,(APTR)&r_prop, PGHI+5, NULL };
  133.  
  134. static struct Gadget ld_gad = {
  135.    &red_gad, PLX-75,39, 58,12, 
  136.    GADGIMAGE | GADGHIMAGE, 
  137.    RELVERIFY,                        /* load */
  138.    BOOLGADGET,
  139.    (APTR)&ldupImage, 
  140.    (APTR)&lddnImage,
  141.    NULL,NULL,NULL, PGHI+7, NULL };
  142.  
  143. static struct Gadget sv_gad = {
  144.    &ld_gad, PLX-75,57, 58,12, 
  145.    GADGIMAGE | GADGHIMAGE, 
  146.    RELVERIFY,                        /* save */
  147.    BOOLGADGET,
  148.    (APTR)&svupImage, 
  149.    (APTR)&svdnImage,
  150.    NULL,NULL,NULL, PGHI+6, NULL };
  151.  
  152.  
  153. static struct Gadget re_gad = {
  154.    &sv_gad, (PLX/2)-33,PLY-19, 58,12, 
  155.    GADGIMAGE | GADGHIMAGE, 
  156.    RELVERIFY,                        /* RESET */
  157.    BOOLGADGET,
  158.    (APTR)&reupImage, 
  159.    (APTR)&rednImage,
  160.    NULL,NULL,NULL, PGHI+2, NULL };
  161.  
  162. static struct Gadget cn_gad = {
  163.    &re_gad, PLX-75,PLY-19, 58,12, 
  164.    GADGIMAGE | GADGHIMAGE, 
  165.    RELVERIFY,                        /* CANCEL */
  166.    BOOLGADGET,
  167.    (APTR)&canupImage, 
  168.    (APTR)&candnImage,
  169.    NULL,NULL,NULL, PGHI+1, NULL };
  170.  
  171. static struct Gadget ok_gad = {
  172.    &cn_gad, 10,PLY-19, 58,12, 
  173.    GADGIMAGE | GADGHIMAGE, 
  174.    RELVERIFY,                        /* OKAY */
  175.    BOOLGADGET,
  176.    (APTR)&okupImage, 
  177.    (APTR)&okdnImage,
  178.    NULL,NULL,NULL, PGHI, NULL };
  179.  
  180. static struct Image m3C[CTSIZ] = {
  181.    {0,0,18,18,1, NULL ,0,0,NULL },        /* Color images */
  182.    {0,0,18,18,1, NULL ,0,1,NULL },
  183.    {0,0,18,18,1, NULL ,0,2,NULL },
  184.    {0,0,18,18,1, NULL ,0,3,NULL },
  185.    {0,0,18,18,1, NULL ,0,4,NULL },
  186.    {0,0,18,18,1, NULL ,0,5,NULL },
  187.    {0,0,18,18,1, NULL ,0,6,NULL },
  188.    {0,0,18,18,1, NULL ,0,7,NULL }
  189.  
  190. };
  191.  
  192. /* gadgets for Color images */
  193.    
  194. static struct Gadget palg[CTSIZ] = { 
  195.    { &palg[1], 8,15,  18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  196.       BOOLGADGET,  (APTR)&m3C[0], NULL,NULL,NULL,NULL, PGAD+0, NULL},
  197.    { &palg[2], 27,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  198.       BOOLGADGET,  (APTR)&m3C[1], NULL,NULL,NULL,NULL, PGAD+1, NULL},
  199.    { &palg[3], 46,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  200.       BOOLGADGET,  (APTR)&m3C[2], NULL,NULL,NULL,NULL, PGAD+2, NULL},
  201.    { &palg[4], 65,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  202.       BOOLGADGET,  (APTR)&m3C[3], NULL,NULL,NULL,NULL, PGAD+3, NULL},
  203.    { &palg[5], 84,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  204.       BOOLGADGET,  (APTR)&m3C[4], NULL,NULL,NULL,NULL, PGAD+4, NULL},
  205.    { &palg[6], 103,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  206.       BOOLGADGET,  (APTR)&m3C[5], NULL,NULL,NULL,NULL, PGAD+5, NULL},
  207.    { &palg[7], 122,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  208.       BOOLGADGET,  (APTR)&m3C[6], NULL,NULL,NULL,NULL, PGAD+6, NULL},
  209.    { &ok_gad, 141,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
  210.       BOOLGADGET,  (APTR)&m3C[7], NULL,NULL,NULL,NULL, PGAD+7, NULL}
  211.  };
  212.   
  213.  
  214. /* Used to open a Window   */
  215. static struct NewWindow NewCmod = {
  216.    200,30,
  217.    PLX,PLY,
  218.    7,4, NULL,                     /* IDCMP set up AFTER CALL */
  219.    ACTIVATE | SMART_REFRESH | WINDOWDRAG,
  220.    &palg[0],NULL,
  221.    (UBYTE *)"Sample Color Palette",              /*Title */
  222.    NULL,NULL,              /* MUST SET SCREEN AFTER CALL!!! */
  223.    PLX,PLY,PLX,PLY, 
  224.    CUSTOMSCREEN        
  225. };
  226.  
  227.  
  228. /****    Variables Initialized (needed) by init_palette()  **********/
  229.  
  230. extern struct ViewPort      *ViewPortAddress();
  231. static struct Window        *pW   = NULL;   /* Palette Window handle */
  232. static struct RastPort      *p_Rp;
  233. static struct ViewPort      *p_Vp;
  234. static USHORT               p_ct[CTSIZ];    /* color table */
  235. static USHORT               new_colors[CTSIZ];
  236. static USHORT               def_colors[CTSIZ] =
  237.    {
  238.     0x000,
  239.     0x999,
  240.     0x555,
  241.     0x900,      /* put your own colors in here */
  242.     0xD60,
  243.     0xF90,
  244.     0x071,
  245.     0x009
  246.    };
  247.  
  248. /********************************************************************
  249.  * these buffers are sent to and maintained by get_fname() 
  250.  * (see: getfile.c)
  251.  *******************************************************************/
  252.  
  253. UBYTE def_dir[80]="SYS:\0";
  254. UBYTE cfile[32]="Color.file\0";
  255.  
  256. /*********************** init_palette() *****************************/
  257.  
  258. void init_palette(win,screen)
  259.  struct Window *win;
  260. {
  261. FAST int i;
  262.  
  263.  p_Vp = (struct ViewPort *)ViewPortAddress(win);
  264.  for ( i =0; i<CTSIZ; i++)
  265.     *(p_ct+i) = new_colors[i] = def_colors[i];
  266.  
  267.  set_cmap(); /* initialize begining colors as def_colors[] */   
  268. }
  269.  
  270. /********************************************************************
  271. * palette(window,screen)
  272. *    This is the meat. This routine opens the palette window and
  273. * retains control until the user selects the OK or CANCEL gadget.
  274. *     The calling arguements are a window & a screen pointer . The window
  275. * is expected to have opened an IDCMP port, which palette uses.
  276. ********************************************************************/
  277.  
  278. void palette(calling_window,screen)
  279.  struct Window *calling_window;
  280.  struct Screen *screen;
  281. {
  282.  FAST struct IntuiMessage *imsg;
  283.  FAST struct Gadget       *egad;
  284.  FAST ULONG  i,class,cursel;        /* cursel = current selected color */
  285.  BOOL keepon,munge;
  286.  
  287.  USHORT backup[CTSIZ];    /* This table restores calling colors... */
  288.  
  289. /* if ( ! (fW = calling_window) )  return;*/
  290.  
  291.  for ( cursel = 0L; cursel<CTSIZ; cursel++) 
  292.       backup[cursel] = *(p_ct+cursel);     /* backup the current colors */
  293.  
  294.  cursel--;                                 /* Get Back into Palette range */
  295.  
  296.  r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  297.  r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  298.  
  299.  /* Set Color Palette's Screen */
  300.  
  301.  NewCmod.Screen = screen;
  302.       
  303.  if ( ! (pW = (struct Window *)OpenWindow(&NewCmod)) )  /* Open Window */
  304.    {
  305.      return; /* Oops...  */
  306.    }
  307.  
  308.  p_Rp = pW->RPort;                        /* get a handle for RastPort */
  309.  
  310.  pW->UserPort = calling_window->UserPort; /* use calling window's UserPort */
  311.        
  312.  ModifyIDCMP(pW,(ULONG)(GADGETUP | GADGETDOWN | MENUVERIFY ));  /* Flags */
  313.  
  314.  /* Fill in the window, then Refresh the Gadgets */
  315.  
  316.  SetAPen(p_Rp,2L);                 
  317.  RectFill(p_Rp,4L,11L,(ULONG)(PLX-5),(ULONG)(PLY-3));
  318.  DrawBorder(p_Rp,&cbdr,6L,13L); 
  319.  DrawBorder(p_Rp,&sbdr,165L,13L); 
  320.  
  321.  RefreshGadgets(&palg[0],pW,NULL); 
  322.  
  323.  for ( munge = keepon = TRUE; keepon; )     /* The BIG LOOP */
  324.     {
  325.       if ( munge ) 
  326.         {
  327.          SetAPen(p_Rp,cursel);
  328.          RectFill(p_Rp,169L,15L,(ULONG)(PLX-17),32L); /* selected color */
  329.  
  330.          /* RJ will proably cringe if he see's this, but it is proably */
  331.          /* safe to modify the HorizPot values this way, UNLESS the    */
  332.          /* gadgets were being fiddled with when you do it. Here that  */
  333.          /* is quite unlikely, since another gadget was just activated */
  334.          /* to cause the munge flag to be set...                       */
  335.  
  336.          /* Make the prop gadgets represent the current selected color */
  337.  
  338.          r_prop.HorizPot = (p_ct[cursel] & 0xf00) << 4;
  339.          g_prop.HorizPot = (p_ct[cursel] & 0x0f0) << 8;
  340.          b_prop.HorizPot = p_ct[cursel] << 12;
  341.  
  342.      RefreshGadgets(&red_gad,pW,NULL); 
  343.  
  344.      munge = FALSE;
  345.          hxtxt.FrontPen = cursel ^ 15;
  346.          hxtxt.BackPen  = cursel;
  347.         }
  348.  
  349.       while ( ! (imsg=(struct IntuiMessage *)GetMsg(pW->UserPort)) )
  350.         {
  351.          class = p_ct[cursel] = ((r_prop.HorizPot >> 4) & 0xf00) +
  352.          ((g_prop.HorizPot >> 8) & 0xf0) + (b_prop.HorizPot >>12);
  353.  
  354.       set_cmap(); 
  355.  
  356.          /* get HEX values for selected color */
  357.  
  358.          frog[0] = hxtab[class >> 8];
  359.          frog[1] = hxtab[ (class >> 4) & 0x0f];
  360.          frog[2] = hxtab[class & 0x0f];
  361.          PrintIText(p_Rp,&hxtxt,0L,0L); 
  362.           
  363.          /* Proably, should do a WaitPort(pW->UserPort); */
  364.          /* I didn't, so the color updates faster.       */
  365.          /* In a multitasking environment, the system is */
  366.          /* being gronked...                             */
  367.  
  368.         }
  369.       egad =(struct Gadget *) imsg->IAddress;
  370.       if ( (class = imsg->Class) == MENUVERIFY )
  371.         {
  372.          imsg->Code = MENUCANCEL;      /* Don't Let Em Out! */
  373.          DisplayBeep(NULL);
  374.         }
  375.       ReplyMsg(imsg);
  376.  
  377.       switch ( class ) 
  378.         {
  379.          case GADGETUP:
  380.          case GADGETDOWN:
  381.             if ((egad->GadgetID >= PGAD)&&(egad->GadgetID < PGAD+CTSIZ)) 
  382.                { /* must be one of the color gadgets... */
  383.                cursel = egad->GadgetID - PGAD;
  384.                munge = TRUE;
  385.                break;
  386.                }
  387.             else switch ( egad->GadgetID ) 
  388.                  { /* otherwise.... */
  389.                case PGHI+1:                                /* CANCEL */
  390.                   for ( i =0L ; i<CTSIZ; i++)
  391.                      *(p_ct+i) = backup[i];
  392.                   set_cmap();
  393.  
  394.                case PGHI:                                  /* OK */
  395.                   for (i =0L ; i<CTSIZ; i++)
  396.                     new_colors[i] = p_ct[i]; 
  397.                   keepon = FALSE;
  398.                   break;
  399.  
  400.                case PGHI+2:                                /* RESET */
  401.                   for (i =0L ; i<CTSIZ ; i++)
  402.                      *(p_ct+i) = def_colors[i];
  403.                   set_cmap();
  404.                   munge = TRUE;
  405.                   break;
  406.  
  407.                case PGHI+6:                                 /* SAVE */
  408.                   if(get_fname(calling_window,screen,
  409.                     "Save Color Palette filename",cfile,def_dir))
  410.                    {
  411.                     Save_colors(prep_file(cfile,def_dir));
  412.                    }
  413.                   break;
  414.  
  415.                case PGHI+7:                                /* LOAD */
  416.                   if(get_fname(calling_window,screen,
  417.                     "Load Color Palette filename",cfile,def_dir))
  418.                    {
  419.                     Load_colors(prep_file(cfile,def_dir));
  420.                     for (i =0 ; i<CTSIZ ; i++)
  421.                        *(p_ct+i) = new_colors[i];
  422.                     set_cmap();
  423.                     munge = TRUE;
  424.                    }
  425.                   break;
  426.         }
  427.      }
  428.   }
  429.  
  430.  pW->UserPort = NULL;
  431.  CloseWindow(pW);
  432.  return;
  433. }
  434.  
  435. /***************************************************************
  436. * prep_file() 
  437. *
  438. *   little routine to add file name and dir together
  439. ***************************************************************/     
  440.  
  441. UBYTE *prep_file(name,dir)
  442. UBYTE   *name,*dir;
  443. {
  444. FAST UBYTE *retval;
  445.  
  446.  if(index(name,':') == 0)
  447.    strcpy(retval,dir);
  448.  strcat(retval,name);
  449.  return (retval); 
  450. }
  451.  
  452. /****************************************************************
  453. * set_cmap()
  454. *     This routine loads an updated p_ct color table into the
  455. * viewport.  You must have initialized the variables p_ct and
  456. * p_Vp to point at color table and ViewPort                     
  457. *****************************************************************/
  458.  
  459. set_cmap()
  460. {
  461.   LoadRGB4(p_Vp,p_ct,CTSIZ);
  462. }
  463.  
  464. /**************************** Save_colors() ********************************/
  465.  
  466. Save_colors(name)
  467. UBYTE *name;
  468. {
  469.  FILE   *out;
  470.  FAST int i,error;
  471.  
  472.  if((out = fopen(name,"w")) == NULL)
  473.    {
  474.     /* print_failure_string_to_screen() */
  475.     return ;
  476.    }
  477.  
  478.  fprintf(out,"%s\n",  " ________________________________________________");
  479.  fprintf(out,"%s\n",  "|                                                ");
  480.  fprintf(out,"%s%s\n","| File: ",name                                    );
  481.  fprintf(out,"%s\n",  "| Palette Colors generated by: My Program        ");
  482.  fprintf(out,"%s\n",  "|________________________________________________");
  483.  
  484. /*
  485.  *  I use the '%' char. to tell the "Load_Colors()" routine that there are
  486.  * no more comments, so the next lines should be the colors it's looking
  487.  * for. 
  488.  */
  489.  
  490.  fprintf(out,"%c\n",'%');
  491.  
  492. /*
  493.  * This may not be the best way to do this but... I print the values out 
  494.  * as a HEX string (and load them in the same way). The following code   
  495.  * just makes sure that the HEX value takes up all three spaces (after   
  496.  * the leading "0x") for readability.                                        
  497.  * i.e. the colors are printed as: 0xf00
  498.  *                                 0x0f0
  499.  *                                 0x00f
  500.  *                                 0x909
  501.  *                                 etc...                                                                          
  502.  */
  503.  
  504.  for(i = 0; i < CTSIZ; i++)
  505.     { 
  506.      fprintf(out,"%s","0x");     
  507.  
  508.      if (*(p_ct+i) < 0x010)
  509.         fprintf(out,"%d%d",0,0);
  510.  
  511.      else if (*(p_ct+i) < 0x100)
  512.         fprintf(out,"%d",0);    
  513.  
  514.      fprintf(out,"%x\n",*(p_ct+i));
  515.     }
  516.  error = ferror(out);
  517.  fclose( out );
  518.  if( error )
  519.    {
  520.     /* print_error_message_to_screen() */
  521.     return ;
  522.    }
  523. }
  524.  
  525. /******************* Load_Colors() ***********************/
  526.  
  527. Load_colors(name)
  528. UBYTE *name;
  529. {
  530.  FAST int      i;
  531.  int           n;
  532.  char          c;
  533.  FILE          *lib;
  534.  USHORT        t[CTSIZ];
  535.     
  536.  
  537.  if((lib = fopen(name,"r")) == NULL) 
  538.   {
  539.    /* print_failure_string_to_screen() */
  540.    return ;                 
  541.   }
  542.  
  543. /* 
  544.  *  Again, probably not the best way to do this...
  545.  *  (but hey, it works for me :-)
  546.  */
  547.  n=0;
  548.  while(!n)
  549.      n = (getc(lib) == '%');   /* stip off chars. until '%' char */
  550.  
  551.  c = getc(lib);                /* strip off '\n' char. */
  552.    
  553.  fscanf(lib,"%x%x%x%x%x%x%x%x",&t[0],&t[1],&t[2],&t[3],&t[4],&t[5],&t[6],
  554.                                &t[7]);
  555.  fclose(lib);
  556.  
  557. /*
  558.  * should do some error checking in here before copying in the new colors,
  559.  * but I got lazy :-)
  560.  *
  561.  */
  562.  
  563.  for (i=0; i<CTSIZ; i++)
  564.     new_colors[i] = t[i];  /* copy the new colors into new_colors[] */
  565. }
  566.