home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * palette.c by: Keith Young 12/20/87
- *
- * These routines were origionally written by: Charlie Heath of Microsmiths
- * (palette.c on Manx Aztec C examples disk) and were Heavily modified by me
- * ( Keith Young ) for use in my ShareWare terminal program: "Access!" and
- * then further modified for "portability" and infestation into the public.
- * (as FREEWARE).
- * I'll not attemp to list all the modifications made (sounds to much like
- * work). Nor will I guarantee that they even work at all (but hey, they work
- * for me :-).
- *
- * You may Use these source files, modify 'em, throw 'em away, give 'em away,
- * without obligation. JUST DON'T SELL 'EM! (I doubt you'd get much anyway:-)
- *
- * If you do use 'em (in a noncomercial manner), I would appeciate a small
- * "mention" in the docs somewhere. (small print, lower left hand corner will
- * do )
- *
- * These routines now compile using AZTEC 3.4a using 16 bit ints.(horay!)
- * (see included makefile)
- *
- * Any bug reports/suggestions/fixes/etc... would be appreciated.
- *
- * I can be reached at: (703) 297-7639
- *
- * via E-MAIL-> GEnie: K.Young
- * CA-AUG: Keith Young
- * Deep Thought: Keith Young
- * COMPUSERVE: 73170,307
- *
- * via Snail-Mail-> Keith Young
- * Rt.2 Box 261
- * Goodview, Va. (yes, really :-)
- * 24095
- *
- ***************************************************************************/
-
- #include "standard.h"
- #include <stdio.h>
-
- #define FAST register /* Register type variable */
- #define CTSIZ 8L /* Color table size */
- #define PLX 230L /* Palette Window Size */
- #define PLY 95L
- #define PGAD 0L /* You can offset the gadgets here */
- #define PGHI PGAD+CTSIZ /* Offset frm color selectors */
-
- extern struct Window *OpenWindow();
- extern struct IntuiMessage *GetMsg();
-
- /* these are "included" into "getfile.c" currently... */
- extern struct Image okupImage,okdnImage,canupImage,candnImage,
- reupImage,rednImage,ldupImage,lddnImage,
- svupImage,svdnImage;
-
- extern UBYTE *get_fname();
- extern UBYTE *prep_file();
-
- static struct IntuiText rtxt = {3,2,JAM1,-10,2,NULL,(UBYTE *)"R",NULL};
- static struct IntuiText gtxt = {6,2,JAM1,-10,2,NULL,(UBYTE *)"G",NULL};
- static struct IntuiText btxt = {7,2,JAM1,-10,2,NULL,(UBYTE *)"B",NULL};
-
- static char frog[] = "hex";
- static struct IntuiText hxtxt = {3,2,JAM2,180,19,NULL,(UBYTE *)frog,NULL};
- static char hxtab[16] = { '0','1','2','3','4','5','6','7',
- '8','9','a','b','c','d','e','f' };
-
- static struct Image r_img, g_img, b_img;
- static struct PropInfo r_prop,g_prop,b_prop;
-
-
- /* border around color gadgets */
-
- static SHORT oXY[] = {
- 0,0,
- 155,0,
- 155,21,
- 0,21,
- 0,0,
- };
-
- static struct Border cbdr = {
- 0,0,
- 0,0,JAM1,
- 5,
- oXY,
- NULL
- };
-
- /* border around selected color image */
-
- static SHORT oXY1[] = {
- 0,0,
- 52,0,
- 52,21,
- 0,21,
- 0,0
- };
-
- static struct Border sbdr = {
- 0,0,
- 0,0,JAM1,
- 5,
- oXY1,
- NULL
- };
-
- static struct Gadget blue_gad = {
- NULL, 17,61, 130,11,
- GADGHCOMP, /* BLUE_PROP */
- GADGIMMEDIATE | RELVERIFY,
- PROPGADGET,
- (APTR)&b_img,
- NULL,
- &btxt, NULL,(APTR)&b_prop, PGHI+3, NULL };
-
- static struct Gadget green_gad = {
- &blue_gad, 17,49, 130,11,
- GADGHCOMP, /* GREEN_PROP */
- GADGIMMEDIATE | RELVERIFY,
- PROPGADGET,
- (APTR)&g_img,
- NULL,
- >xt, NULL,(APTR)&g_prop, PGHI+4, NULL };
-
- static struct Gadget red_gad = {
- &green_gad, 17,37, 130,11,
- GADGHCOMP, /* RED_PROP */
- GADGIMMEDIATE | RELVERIFY,
- PROPGADGET,
- (APTR)&r_img,
- NULL,
- &rtxt, NULL,(APTR)&r_prop, PGHI+5, NULL };
-
- static struct Gadget ld_gad = {
- &red_gad, PLX-75,39, 58,12,
- GADGIMAGE | GADGHIMAGE,
- RELVERIFY, /* load */
- BOOLGADGET,
- (APTR)&ldupImage,
- (APTR)&lddnImage,
- NULL,NULL,NULL, PGHI+7, NULL };
-
- static struct Gadget sv_gad = {
- &ld_gad, PLX-75,57, 58,12,
- GADGIMAGE | GADGHIMAGE,
- RELVERIFY, /* save */
- BOOLGADGET,
- (APTR)&svupImage,
- (APTR)&svdnImage,
- NULL,NULL,NULL, PGHI+6, NULL };
-
-
- static struct Gadget re_gad = {
- &sv_gad, (PLX/2)-33,PLY-19, 58,12,
- GADGIMAGE | GADGHIMAGE,
- RELVERIFY, /* RESET */
- BOOLGADGET,
- (APTR)&reupImage,
- (APTR)&rednImage,
- NULL,NULL,NULL, PGHI+2, NULL };
-
- static struct Gadget cn_gad = {
- &re_gad, PLX-75,PLY-19, 58,12,
- GADGIMAGE | GADGHIMAGE,
- RELVERIFY, /* CANCEL */
- BOOLGADGET,
- (APTR)&canupImage,
- (APTR)&candnImage,
- NULL,NULL,NULL, PGHI+1, NULL };
-
- static struct Gadget ok_gad = {
- &cn_gad, 10,PLY-19, 58,12,
- GADGIMAGE | GADGHIMAGE,
- RELVERIFY, /* OKAY */
- BOOLGADGET,
- (APTR)&okupImage,
- (APTR)&okdnImage,
- NULL,NULL,NULL, PGHI, NULL };
-
- static struct Image m3C[CTSIZ] = {
- {0,0,18,18,1, NULL ,0,0,NULL }, /* Color images */
- {0,0,18,18,1, NULL ,0,1,NULL },
- {0,0,18,18,1, NULL ,0,2,NULL },
- {0,0,18,18,1, NULL ,0,3,NULL },
- {0,0,18,18,1, NULL ,0,4,NULL },
- {0,0,18,18,1, NULL ,0,5,NULL },
- {0,0,18,18,1, NULL ,0,6,NULL },
- {0,0,18,18,1, NULL ,0,7,NULL }
-
- };
-
- /* gadgets for Color images */
-
- static struct Gadget palg[CTSIZ] = {
- { &palg[1], 8,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[0], NULL,NULL,NULL,NULL, PGAD+0, NULL},
- { &palg[2], 27,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[1], NULL,NULL,NULL,NULL, PGAD+1, NULL},
- { &palg[3], 46,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[2], NULL,NULL,NULL,NULL, PGAD+2, NULL},
- { &palg[4], 65,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[3], NULL,NULL,NULL,NULL, PGAD+3, NULL},
- { &palg[5], 84,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[4], NULL,NULL,NULL,NULL, PGAD+4, NULL},
- { &palg[6], 103,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[5], NULL,NULL,NULL,NULL, PGAD+5, NULL},
- { &palg[7], 122,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[6], NULL,NULL,NULL,NULL, PGAD+6, NULL},
- { &ok_gad, 141,15, 18,18, GADGHNONE | GADGIMAGE, RELVERIFY,
- BOOLGADGET, (APTR)&m3C[7], NULL,NULL,NULL,NULL, PGAD+7, NULL}
- };
-
-
- /* Used to open a Window */
- static struct NewWindow NewCmod = {
- 200,30,
- PLX,PLY,
- 7,4, NULL, /* IDCMP set up AFTER CALL */
- ACTIVATE | SMART_REFRESH | WINDOWDRAG,
- &palg[0],NULL,
- (UBYTE *)"Sample Color Palette", /*Title */
- NULL,NULL, /* MUST SET SCREEN AFTER CALL!!! */
- PLX,PLY,PLX,PLY,
- CUSTOMSCREEN
- };
-
-
- /**** Variables Initialized (needed) by init_palette() **********/
-
- extern struct ViewPort *ViewPortAddress();
- static struct Window *pW = NULL; /* Palette Window handle */
- static struct RastPort *p_Rp;
- static struct ViewPort *p_Vp;
- static USHORT p_ct[CTSIZ]; /* color table */
- static USHORT new_colors[CTSIZ];
- static USHORT def_colors[CTSIZ] =
- {
- 0x000,
- 0x999,
- 0x555,
- 0x900, /* put your own colors in here */
- 0xD60,
- 0xF90,
- 0x071,
- 0x009
- };
-
- /********************************************************************
- * these buffers are sent to and maintained by get_fname()
- * (see: getfile.c)
- *******************************************************************/
-
- UBYTE def_dir[80]="SYS:\0";
- UBYTE cfile[32]="Color.file\0";
-
- /*********************** init_palette() *****************************/
-
- void init_palette(win,screen)
- struct Window *win;
- {
- FAST int i;
-
- p_Vp = (struct ViewPort *)ViewPortAddress(win);
- for ( i =0; i<CTSIZ; i++)
- *(p_ct+i) = new_colors[i] = def_colors[i];
-
- set_cmap(); /* initialize begining colors as def_colors[] */
- }
-
- /********************************************************************
- * palette(window,screen)
- * This is the meat. This routine opens the palette window and
- * retains control until the user selects the OK or CANCEL gadget.
- * The calling arguements are a window & a screen pointer . The window
- * is expected to have opened an IDCMP port, which palette uses.
- ********************************************************************/
-
- void palette(calling_window,screen)
- struct Window *calling_window;
- struct Screen *screen;
- {
- FAST struct IntuiMessage *imsg;
- FAST struct Gadget *egad;
- FAST ULONG i,class,cursel; /* cursel = current selected color */
- BOOL keepon,munge;
-
- USHORT backup[CTSIZ]; /* This table restores calling colors... */
-
- /* if ( ! (fW = calling_window) ) return;*/
-
- for ( cursel = 0L; cursel<CTSIZ; cursel++)
- backup[cursel] = *(p_ct+cursel); /* backup the current colors */
-
- cursel--; /* Get Back into Palette range */
-
- r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
- r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
-
- /* Set Color Palette's Screen */
-
- NewCmod.Screen = screen;
-
- if ( ! (pW = (struct Window *)OpenWindow(&NewCmod)) ) /* Open Window */
- {
- return; /* Oops... */
- }
-
- p_Rp = pW->RPort; /* get a handle for RastPort */
-
- pW->UserPort = calling_window->UserPort; /* use calling window's UserPort */
-
- ModifyIDCMP(pW,(ULONG)(GADGETUP | GADGETDOWN | MENUVERIFY )); /* Flags */
-
- /* Fill in the window, then Refresh the Gadgets */
-
- SetAPen(p_Rp,2L);
- RectFill(p_Rp,4L,11L,(ULONG)(PLX-5),(ULONG)(PLY-3));
- DrawBorder(p_Rp,&cbdr,6L,13L);
- DrawBorder(p_Rp,&sbdr,165L,13L);
-
- RefreshGadgets(&palg[0],pW,NULL);
-
- for ( munge = keepon = TRUE; keepon; ) /* The BIG LOOP */
- {
- if ( munge )
- {
- SetAPen(p_Rp,cursel);
- RectFill(p_Rp,169L,15L,(ULONG)(PLX-17),32L); /* selected color */
-
- /* RJ will proably cringe if he see's this, but it is proably */
- /* safe to modify the HorizPot values this way, UNLESS the */
- /* gadgets were being fiddled with when you do it. Here that */
- /* is quite unlikely, since another gadget was just activated */
- /* to cause the munge flag to be set... */
-
- /* Make the prop gadgets represent the current selected color */
-
- r_prop.HorizPot = (p_ct[cursel] & 0xf00) << 4;
- g_prop.HorizPot = (p_ct[cursel] & 0x0f0) << 8;
- b_prop.HorizPot = p_ct[cursel] << 12;
-
- RefreshGadgets(&red_gad,pW,NULL);
-
- munge = FALSE;
- hxtxt.FrontPen = cursel ^ 15;
- hxtxt.BackPen = cursel;
- }
-
- while ( ! (imsg=(struct IntuiMessage *)GetMsg(pW->UserPort)) )
- {
- class = p_ct[cursel] = ((r_prop.HorizPot >> 4) & 0xf00) +
- ((g_prop.HorizPot >> 8) & 0xf0) + (b_prop.HorizPot >>12);
-
- set_cmap();
-
- /* get HEX values for selected color */
-
- frog[0] = hxtab[class >> 8];
- frog[1] = hxtab[ (class >> 4) & 0x0f];
- frog[2] = hxtab[class & 0x0f];
- PrintIText(p_Rp,&hxtxt,0L,0L);
-
- /* Proably, should do a WaitPort(pW->UserPort); */
- /* I didn't, so the color updates faster. */
- /* In a multitasking environment, the system is */
- /* being gronked... */
-
- }
- egad =(struct Gadget *) imsg->IAddress;
- if ( (class = imsg->Class) == MENUVERIFY )
- {
- imsg->Code = MENUCANCEL; /* Don't Let Em Out! */
- DisplayBeep(NULL);
- }
- ReplyMsg(imsg);
-
- switch ( class )
- {
- case GADGETUP:
- case GADGETDOWN:
- if ((egad->GadgetID >= PGAD)&&(egad->GadgetID < PGAD+CTSIZ))
- { /* must be one of the color gadgets... */
- cursel = egad->GadgetID - PGAD;
- munge = TRUE;
- break;
- }
- else switch ( egad->GadgetID )
- { /* otherwise.... */
- case PGHI+1: /* CANCEL */
- for ( i =0L ; i<CTSIZ; i++)
- *(p_ct+i) = backup[i];
- set_cmap();
-
- case PGHI: /* OK */
- for (i =0L ; i<CTSIZ; i++)
- new_colors[i] = p_ct[i];
- keepon = FALSE;
- break;
-
- case PGHI+2: /* RESET */
- for (i =0L ; i<CTSIZ ; i++)
- *(p_ct+i) = def_colors[i];
- set_cmap();
- munge = TRUE;
- break;
-
- case PGHI+6: /* SAVE */
- if(get_fname(calling_window,screen,
- "Save Color Palette filename",cfile,def_dir))
- {
- Save_colors(prep_file(cfile,def_dir));
- }
- break;
-
- case PGHI+7: /* LOAD */
- if(get_fname(calling_window,screen,
- "Load Color Palette filename",cfile,def_dir))
- {
- Load_colors(prep_file(cfile,def_dir));
- for (i =0 ; i<CTSIZ ; i++)
- *(p_ct+i) = new_colors[i];
- set_cmap();
- munge = TRUE;
- }
- break;
- }
- }
- }
-
- pW->UserPort = NULL;
- CloseWindow(pW);
- return;
- }
-
- /***************************************************************
- * prep_file()
- *
- * little routine to add file name and dir together
- ***************************************************************/
-
- UBYTE *prep_file(name,dir)
- UBYTE *name,*dir;
- {
- FAST UBYTE *retval;
-
- if(index(name,':') == 0)
- strcpy(retval,dir);
- strcat(retval,name);
- return (retval);
- }
-
- /****************************************************************
- * set_cmap()
- * This routine loads an updated p_ct color table into the
- * viewport. You must have initialized the variables p_ct and
- * p_Vp to point at color table and ViewPort
- *****************************************************************/
-
- set_cmap()
- {
- LoadRGB4(p_Vp,p_ct,CTSIZ);
- }
-
- /**************************** Save_colors() ********************************/
-
- Save_colors(name)
- UBYTE *name;
- {
- FILE *out;
- FAST int i,error;
-
- if((out = fopen(name,"w")) == NULL)
- {
- /* print_failure_string_to_screen() */
- return ;
- }
-
- fprintf(out,"%s\n", " ________________________________________________");
- fprintf(out,"%s\n", "| ");
- fprintf(out,"%s%s\n","| File: ",name );
- fprintf(out,"%s\n", "| Palette Colors generated by: My Program ");
- fprintf(out,"%s\n", "|________________________________________________");
-
- /*
- * I use the '%' char. to tell the "Load_Colors()" routine that there are
- * no more comments, so the next lines should be the colors it's looking
- * for.
- */
-
- fprintf(out,"%c\n",'%');
-
- /*
- * This may not be the best way to do this but... I print the values out
- * as a HEX string (and load them in the same way). The following code
- * just makes sure that the HEX value takes up all three spaces (after
- * the leading "0x") for readability.
- * i.e. the colors are printed as: 0xf00
- * 0x0f0
- * 0x00f
- * 0x909
- * etc...
- */
-
- for(i = 0; i < CTSIZ; i++)
- {
- fprintf(out,"%s","0x");
-
- if (*(p_ct+i) < 0x010)
- fprintf(out,"%d%d",0,0);
-
- else if (*(p_ct+i) < 0x100)
- fprintf(out,"%d",0);
-
- fprintf(out,"%x\n",*(p_ct+i));
- }
- error = ferror(out);
- fclose( out );
- if( error )
- {
- /* print_error_message_to_screen() */
- return ;
- }
- }
-
- /******************* Load_Colors() ***********************/
-
- Load_colors(name)
- UBYTE *name;
- {
- FAST int i;
- int n;
- char c;
- FILE *lib;
- USHORT t[CTSIZ];
-
-
- if((lib = fopen(name,"r")) == NULL)
- {
- /* print_failure_string_to_screen() */
- return ;
- }
-
- /*
- * Again, probably not the best way to do this...
- * (but hey, it works for me :-)
- */
- n=0;
- while(!n)
- n = (getc(lib) == '%'); /* stip off chars. until '%' char */
-
- c = getc(lib); /* strip off '\n' char. */
-
- fscanf(lib,"%x%x%x%x%x%x%x%x",&t[0],&t[1],&t[2],&t[3],&t[4],&t[5],&t[6],
- &t[7]);
- fclose(lib);
-
- /*
- * should do some error checking in here before copying in the new colors,
- * but I got lazy :-)
- *
- */
-
- for (i=0; i<CTSIZ; i++)
- new_colors[i] = t[i]; /* copy the new colors into new_colors[] */
- }
-