/***********************************************************************/
/*                                                                     */
/* PALETTE.C  C language source code for a utility which creates       */
/*            four small example RIPscrip files numbered from          */
/*                            01.RIP to 04.RIP                         */
/*            Each file demonstrates the use of the RIP_SET_PALETTE    */
/*            RIPscrip command.  This command can be used to partially */
/*            or completely change the 16-color RIP palette (v1.54).   */
/*            The RIP palette contains 16 entries from a total of      */
/*            64 possible colors.  Each entry is user-settable by      */
/*            means of the RIP_SET_PALETTE command.                    */
/*                                                                     */
/*        Example:  !|Q000102030405060708090A0B0C0D0E0F                */
/*                      | | | | | | | | | | | | | | | |                */ 
/*        color 00 -----' | | | | | | | | | | | | | | |                */ 
/*        color 01 -------' | | | | | | | | | | | | | |                */ 
/*        color 02 ---------' | | | | | | | | | | | | |                */
/*        color 03 -----------' | | | | | | | | | | | |                */
/*        color 04 -------------' | | | | | | | | | | |                */ 
/*        color 05 ---------------' | | | | | | | | | |                */ 
/*        color 06 -----------------' | | | | | | | | |                */ 
/*        color 07 -------------------' | | | | | | | |                */ 
/*        color 08 ---------------------' | | | | | | |                */ 
/*        color 09 -----------------------' | | | | | |                */ 
/*        color 10 -------------------------' | | | | |                */
/*        color 11 ---------------------------' | | | |                */ 
/*        color 12 -----------------------------' | | |                */ 
/*        color 13 -------------------------------' | |                */
/*        color 14 ---------------------------------' |                */
/*        color 15 -----------------------------------'                */
/*                                                                     */
/*                                                                     */
/*  16-Color RIP Palette     Master 64-Color EGA                       */
/*      Color Code           Palette Color Code       Color            */
/*  ---------------------------------------------------------------    */
/*         00                     0  (00)             Black            */
/*         01                     1  (01)             Blue             */
/*         02                     2  (02)             Green            */
/*         03                     3  (03)             Cyan             */
/*         04                     4  (04)             Red              */
/*         05                     5  (05)             Magenta          */
/*         06                     7  (06)             Brown            */
/*         07                     20 (0K)             Light Gray       */
/*         08                     56 (1K)             Dark Gray        */
/*         09                     57 (1L)             Light Blue       */
/*         0A                     58 (1M)             Light Green      */
/*         0B                     59 (1N)             Light Cyan       */
/*         0C                     60 (1O)             Light Red        */
/*         0D                     61 (1P)             Light Magenta    */
/*         0E                     62 (1Q)             Yellow           */
/*         0F                     63 (1R)             White            */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


#include "stdio.h"



FILE *fp;

char number[10];
char ripfile[15];

void RIP           (void);
void make_file     (int color);
void mega          (int n);
int  calc          (int x);




main()
{
	printf("\n         ** PALETTE.EXE is test utility by Ken McKay **\n");
	printf("\n          This utility will create four RIP screens");
	printf("\n          to demonstrate the RIP_SET_PALETTE command.\n\n");

	printf("\n Press any key and 4 files from 01.RIP to 04.RIP will be created.");
	printf("\n Press ESC key to abort.");

        if (getch() == 27)
                exit(0);

        RIP();
}







void RIP (void)
{
int count = 0;
int file_No = 1;


        /* counts from 0 to 15 */
        while (count < 64)
                {
                /* make filenames 01.RIP to 15.RIP */
                sprintf(ripfile, "%02d.RIP", file_No);

                if ((fp = fopen(ripfile, "wt")) == NULL)
                        printf("\nCannot create RIPfile %s\n", ripfile);

                else
                        make_file(count);

                count = count + 16;
		++file_No;
                }
}






void make_file (int color)
{
int down = 0;
int across = 0;
int x0, y0, x1, y1, x2, y2;
char s1[5],  s2[5],  s3[5],  s4[5];
char s5[5],  s6[5],  s7[5],  s8[5];
char s9[5],  s10[5], s11[5], s12[5];
char s13[5], s14[5], s15[5], s16[5];

	fprintf(fp, "\n!|1K|*|W00|Y01000500");

	mega(color);	strcpy(s1, number);
	mega(color+1);	strcpy(s2, number);
	mega(color+2);	strcpy(s3, number);
	mega(color+3);	strcpy(s4, number);
	mega(color+4);	strcpy(s5, number);
	mega(color+5);	strcpy(s6, number);
	mega(color+6);	strcpy(s7, number);
	mega(color+7);	strcpy(s8, number);
	mega(color+8);	strcpy(s9, number);
	mega(color+9);	strcpy(s10, number);
	mega(color+10);	strcpy(s11, number);
	mega(color+11);	strcpy(s12, number);
	mega(color+12);	strcpy(s13, number);
	mega(color+13);	strcpy(s14, number);
	mega(color+14);	strcpy(s15, number);
	mega(color+15);	strcpy(s16, number);

        /* write the RIP_SET_PALETTE command */
	fprintf(fp, "|Q%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
                    s1, s2,  s3,  s4,  s5,  s6,  s7,  s8, s9, s10, s11, s12, s13, s14, s15, s16);

        /* create 16 color boxes in four rows of four across */

        /* create color boxes down the screen about 8.7 pixels high */
        while(down < 4)
                {
                /* create color boxes across the screen 160 pixels wide */
                while (across < 4)
                        {
                        x0 = across*160;        /* color start screen coords */
                        y0 = down*87.5;
                        x1 = (across*160)+160;  /* color end   screen coords */
                        y1 = (down*87.5)+87.5;
                        x2 = 50 + x0;           /* text  start screen coords */
                        y2 = 16 + y0;

                	mega(color); strcpy(s1, number);
                	mega(x0);    strcpy(s2, number);
                	mega(y0);    strcpy(s3, number);
                	mega(x1);    strcpy(s4, number);
                	mega(y1);    strcpy(s5, number);
                	mega(x2);    strcpy(s6, number);
                	mega(y2);    strcpy(s7, number);
                	mega(x2+2);  strcpy(s8, number); /* drop shadow offset*/
                	mega(y2+1);  strcpy(s9, number);

                        fprintf(fp, "\n!|S01%s|B%s%s%s%s|c00|@%s%s%02d|c0F|@%s%s%02d",
                                        s1, s2, s3, s4, s5,
                                        s6, s7, color,
                                        s8, s9, color);
                        ++across;
			++color;
                        }
                ++down;
		across = 0;
                }

	fprintf(fp, "\n!|#|#|#\n");

        fclose(fp);
}







void mega (int n)
{
int a, b;

        a = n / 36;
	number[0] = calc(a);
	b = n - ((n/36)*36);
	number[1] = calc(b);
	number[2] = '\0';
}






int calc (int x)
{
	if (x >= 0 && x <= 9)
		return (x+48);
	else
		return (x+55);
}





