home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d049 / plot.lha / Plot / palette2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-11  |  8.9 KB  |  276 lines

  1. /***********************************************************************
  2. *  palette.c
  3. *     This program is useful for designing color palettes.
  4. * It may be used stand-alone (I would be setting the 'default map',
  5. * if preferences had a complete color map...), or you can use it
  6. * as a subroutine with minor modifications.
  7. *     One of the unique features of this program is that it prints
  8. * the hex value of the selected color, so you use it to generate
  9. * numeric values for use in programs.
  10. *     (c) 1985 by Charlie Heath of Microsmiths, this program is in
  11. * the public domain.  Not to be used commercially without permission
  12. * from MicroSmiths.
  13. ***********************************************************************/
  14.  
  15. #define I_REV   29
  16. #define G_REV   29
  17.  
  18. #define FAST register      /* Register type variable  */
  19.  
  20. #define DPTH 2       /* Raster dimensions */
  21. #define WDTH 640
  22. #define HGHT 200
  23.  
  24. #define BCOL 1          /* Background color for menus, etc   */
  25.  
  26. #define CTSIZ  4       /* In spite of myself, color table size   */
  27.  
  28. /****    Variables Initialized by init_colors()  **********/
  29. struct ColorMap *p_Co;
  30. USHORT *p_ct;           /* Color table as words */
  31. extern struct ViewPort *vp; 
  32. USHORT default_color[CTSIZ];     /* Save a copy of the defaults   */
  33.  
  34.  
  35. init_colors()
  36. {
  37. FAST int i;
  38.  
  39.    p_Co = (struct ColorMap *)GetColorMap(CTSIZ);
  40.    p_ct = (USHORT *)p_Co->ColorTable;
  41.  
  42.    for ( i=0; i<CTSIZ; i++)
  43.       default_color[i] = p_ct[i];
  44.    default_color[0] = 0x0a0; default_color[1] = 0x98b;
  45.    default_color[2] = 0x002; default_color[3] = 0xf80;
  46.    set_defmap();
  47. }
  48.  
  49. clear_colors()
  50. {
  51.    FreeColorMap(p_Co);
  52. }
  53.  
  54.  
  55. /****************************************************************
  56. * set_defmap()
  57. *    This routine sets up a 'default' color map.  It assumes
  58. * you have initialized the variable p_ct to point to a color
  59. * table, and that default_color has the colors you want.       */
  60. set_defmap()
  61. {
  62. FAST int i;
  63.  
  64.    for (i=0; i<CTSIZ ; i++)
  65.       *(p_ct+i) = default_color[i];
  66.  
  67.    set_cmap();
  68. }
  69.  
  70.  
  71.  
  72. /****************************************************************
  73. * set_cmap()
  74. *     This routine loads an updated p_ct color table into the
  75. * viewport.  You must have initialized the variables p_ct and
  76. * vp to point at color table and ViewPort                     */
  77. set_cmap()
  78. {
  79.    LoadRGB4(vp,p_ct,CTSIZ);
  80. }
  81.  
  82.  
  83. /***   palette modifier routines follow    ************************/
  84.  
  85. #define PLX 200      /* Palette Window Size  */
  86. #define PLY 132
  87.  
  88. #define PGAD 0             /* You can offset the gadgets here  */
  89. #define PGHI   PGAD+CTSIZ  /* Offset frm color selectors       */
  90.  
  91. struct IntuiText rtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"R",NL};
  92. struct IntuiText gtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"G",NL};
  93. struct IntuiText btxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"B",NL};
  94.  
  95. struct Image    r_img, g_img, b_img;
  96. struct PropInfo r_prop,g_prop,b_prop;
  97.  
  98. struct Gadget blue_gad = {
  99.    NL, 17,112, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  100.    PROPGADGET,(APTR)&b_img, NL,
  101.    &btxt, NL,(APTR)&b_prop, PGHI+3, NL };
  102.  
  103. struct Gadget green_gad = {
  104.    &blue_gad, 17,97, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  105.    PROPGADGET,(APTR)&g_img, NL,
  106.    >xt, NL,(APTR)&g_prop, PGHI+4, NL };
  107.  
  108. struct Gadget red_gad = {
  109.    &green_gad, 17,82, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  110.    PROPGADGET,(APTR)&r_img, NL,
  111.    &rtxt, NL,(APTR)&r_prop, PGHI+5, NL };
  112.  
  113. struct IntuiText oktxt = {3,2,JAM2,8,2,NL,(UBYTE *)" OK ",NL};
  114. struct IntuiText cntxt = {3,2,JAM2,0,2,NL,(UBYTE *)"Cancel",NL};
  115. struct IntuiText retxt = {3,2,JAM2,4,2,NL,(UBYTE *)"Reset",NL};
  116.  
  117. struct IntuiText mstxt={2,2,JAM1,0,0,NL,(UBYTE *)"MicroSmiths Palette",NL};
  118.  
  119. char frog[] = "hex";
  120. struct IntuiText hxtxt = {3,2,JAM2,136,31,NL,(UBYTE *)frog,NL};
  121.  
  122. struct Gadget re_gad = {
  123.    &red_gad, 110,112, 54,11, GADGHCOMP, RELVERIFY,
  124.    BOOLGADGET, NL, NL,
  125.    &retxt, NL,NL, PGHI+2, NL };
  126. struct Gadget cn_gad = {
  127.    &re_gad, 110,97, 54,11, GADGHCOMP, RELVERIFY,
  128.    BOOLGADGET, NL, NL,
  129.    &cntxt, NL,NL, PGHI+1, NL };
  130. struct Gadget ok_gad = {
  131.    &cn_gad, 110,82, 54,11, GADGHCOMP, RELVERIFY,
  132.    BOOLGADGET, NL, NL,
  133.    &oktxt, NL,NL, PGHI, NL };
  134.  
  135. struct Image m3C[CTSIZ] = {
  136.    {0,0,16,16,1, NL ,0,0,NL },   /* Colors   */
  137.    {0,0,16,16,1, NL ,0,1,NL },
  138.    {0,0,16,16,1, NL ,0,2,NL },
  139.    {0,0,16,16,1, NL ,0,3,NL } };
  140.  
  141. struct Gadget palg[CTSIZ] = {
  142.    { &palg[1], 3, 3,  16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  143.       BOOLGADGET,  (APTR)&m3C[0], NL,NL,NL,NL, PGAD+0, NL},
  144.    { &palg[2], 19,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  145.       BOOLGADGET,  (APTR)&m3C[1], NL,NL,NL,NL, PGAD+1, NL},
  146.    { &palg[3], 35,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  147.       BOOLGADGET,  (APTR)&m3C[2], NL,NL,NL,NL, PGAD+2, NL},
  148.    { &ok_gad, 51,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  149.       BOOLGADGET,  (APTR)&m3C[3], NL,NL,NL,NL, PGAD+3, NL} };
  150.  
  151.  
  152. /* Used to open a Window   */
  153. struct NewWindow NewCmod = {
  154.    40,30, PLX,PLY, 2,BCOL, NL,    /* IDCMP set up AFTER CALL */
  155.    ACTIVATE | SMART_REFRESH,
  156.    &palg[0],NL, NL,    /* FirstGadget, CheckMark, Title  */
  157.    NL,NL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
  158.    PLX,PLY,PLX,PLY, CUSTOMSCREEN }; /* MinW, MinH, MaxW, MaxH */
  159.  
  160.  
  161. /********************************************************************
  162. * palette(window)
  163. *    This is the meat. This routine opens the palette window and
  164. * retains control until the user selects the OK or CANCEL gadget.
  165. *     The calling arguement is a window pointer.  That window
  166. * is expected to have opened an IDCMP port, which palette uses.
  167. ********************************************************************/
  168. palette(calling_window)
  169. struct Window *calling_window;
  170. {
  171. struct Window *cW;      /* Palette window handle   */
  172.  
  173. FAST struct IntuiMessage *imsg;
  174. FAST struct Gadget *igad;
  175.  
  176. FAST int class,cursel;
  177. BOOL keepon,munge;
  178.  
  179. USHORT backup[CTSIZ];         /* This table restores calling colors... */
  180.  
  181. static char hxtab[16] = { '0','1','2','3','4','5','6','7','8',
  182.    '9','a','b','c','d','e','f' };
  183.  
  184.    for ( cursel=CTSIZ-1; cursel >= 0; cursel--)
  185.       backup[cursel] = p_ct[cursel];
  186.  
  187.    r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  188.    r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  189.  
  190.    /* Get screen from calling window   */
  191.    NewCmod.Screen = calling_window->WScreen;  /* NEED TO SET SCREEN!!! */
  192.    if ( ! (cW = (struct Window *)OpenWindow(&NewCmod)) ) {
  193.       return(FALSE); /* Oops...  */
  194.       }
  195.  
  196.    PrintIText(cW->RPort,&mstxt,7,71);
  197.  
  198.    cW->UserPort = calling_window->UserPort;
  199.    ModifyIDCMP(cW, GADGETUP | GADGETDOWN | MENUVERIFY );
  200.  
  201.    for ( munge = keepon = TRUE; keepon; ) {
  202.  
  203.       if ( munge ) {
  204.          SetAPen(cW->RPort,cursel);
  205.          RectFill(cW->RPort,133,3,PLX-5,66);
  206.  
  207.          /* RJ will proably cringe if he see's this, but it is proably */
  208.          /* safe to modify the HorizPot values this way, UNLESS the    */
  209.          /* gadgets were being fiddled with when you do it. Here that  */
  210.          /* is quite unlikely, since another gadget was just activated */
  211.          /* to cause the munge flag to be set...                       */
  212.  
  213.          r_prop.HorizPot = (p_ct[cursel] & 0xf00) << 4;
  214.          g_prop.HorizPot = (p_ct[cursel] & 0x0f0) << 8;
  215.          b_prop.HorizPot = p_ct[cursel] << 12;
  216.          RefreshGadgets(&red_gad,cW,NL);
  217.          munge = FALSE;
  218.          hxtxt.FrontPen = cursel ^ 15;
  219.          hxtxt.BackPen  = cursel;
  220.          }
  221.  
  222.       while ( ! (imsg=(struct IntuiMessage *)GetMsg(cW->UserPort)) ) {
  223.          class = p_ct[cursel] = ((r_prop.HorizPot >> 4) & 0xf00) +
  224.             ((g_prop.HorizPot >> 8) & 0xf0) + (b_prop.HorizPot >>12);
  225.          set_cmap();
  226.  
  227.          frog[0] = hxtab[class >> 8];
  228.          frog[1] = hxtab[ (class >> 4) & 0x0f];
  229.          frog[2] = hxtab[class & 0x0f];
  230.          PrintIText(cW->RPort,&hxtxt,0,0);
  231.  
  232.          /* Proably, should do a WaitPort(cW->UserPort); */
  233.          /* I didn't, so the color updates faster.       */
  234.          /* In a multitasking environment, the system is */
  235.          /* being gronked...                             */
  236.          }
  237.  
  238.       igad =(struct Gadget *) imsg->IAddress;
  239.       if ( (class = imsg->Class) == MENUVERIFY ) {
  240.          imsg->Code = MENUCANCEL;      /* Don't Let Em Out! */
  241.          DisplayBeep(NL);
  242.          }
  243.       ReplyMsg(imsg);
  244.  
  245.       switch ( class ) {
  246.          case GADGETUP:
  247.          case GADGETDOWN:
  248.             if ((igad->GadgetID >= PGAD)&&(igad->GadgetID < PGAD+CTSIZ)) {
  249.                cursel = igad->GadgetID - PGAD;
  250.                munge = TRUE;
  251.                }
  252.             else switch ( igad->GadgetID ) {
  253.                case PGHI+1:      /* Cancel   */
  254.                   for ( class =0 ; class<CTSIZ; class++)
  255.                      p_ct[class] = backup[class];
  256.                   set_cmap();
  257.                case PGHI:        /* OK */
  258.                   keepon = FALSE;
  259.                   break;
  260.  
  261.                case PGHI+2:      /* Reset */
  262.                   set_defmap();
  263.                   munge = TRUE;
  264.                   break;
  265.                }
  266.          }
  267.       }
  268.  
  269.    cW->UserPort = NL;
  270.    CloseWindow(cW);
  271.    return(TRUE);
  272. }
  273.  
  274.  
  275. /*************************************************************/
  276.