[net.micro.pc] Pallette.c - Changing the EGA pallette registers

jwg@duke.UUCP (Jeffrey William Gillette) (06/22/86)

[]

One of the nicest features of the Enhanced Graphics Adapter is 
the ability to change the colors displayed.  The 16 pallette 
registers can be loaded with arbitrary values from a selection of 
64 colors.

I have not seen a program to redefine the pallette registers, so 
I am posting the one I wrote.  It is written for the Microsoft C 
Compiler (v. 3.0), but should work with minor modifications on 
others.  'Pallette' displays the default EGA pallette register 
settings.  One moves between registers with the left and right 
arrow keys.  The pallette values are stepped up or down with the 
up and down arrow keys.  Escape terminates the program.  Note 
that the next program which does a mode reset will reset the 
pallette registers also.

		Jeffrey William Gillette
		The Divinity School
		Duke University

		uucp:  duke!phys!lisa
		bitnet:  DYBBUK at TUCCVM



--------------------------  PALLETTE.C  -------------------------
/*
                            PALLETTE.C


                     Jeffrey William Gillette
                         Duke University

    Pallette displays the default settings for the pallette 
    registers used by the Enhanced Graphics Adapter.  It then 
    allows one to change these settings by using the left and 
    right arrow keys to move between registers, and the up and 
    down arrow keys to step up or down the pallette attribute.  
    Esc quits Pallette.  This program is in the public domain, 
    and may be copied and used freely. 
*/
 
#include <stdio.h>
#include <dos.h>

/*  Placement of pallette ruler on screen   */
#define ROW         11
#define COL         1
#define VIDSEG      0xb8000000

/*  ANSI escape codes                       */
#define SETCURSOR   "\033[%d;%dH"
#define CLEARSCREEN "\033[2J"
#define CURSOR      '\030'
                        
/*  Default pallette register values (from EGA BIOS)    */
int value[16] = {  0,    1,   2,    3,    4,    5, 0x14,    7, 
                0x38, 0x39, 0x3a, 0x3b, 0x3c,0x3d, 0x3e, 0x3f 
} ;

/*  Pallette ruler line (chars & attributes */
char ruler[] = {
      ' ', 0,' ',  0,'0',  0,' ',  0,    
      ' ', 1,' ',  1,'1',  1,' ',  1,
      ' ', 2,' ',  2,'2',  2,' ',  2,    
      ' ', 3,' ',  3,'3',  3,' ',  3,
      ' ', 4,' ',  4,'4',  4,' ',  4,    
      ' ', 5,' ',  5,'5',  5,' ',  5,
      ' ', 6,' ',  6,'6',  6,' ',  6,    
      ' ', 7,' ',  7,'7',  7,' ',  7,
      ' ', 8,' ',  8,'8',  8,' ',  8,    
      ' ', 9,' ',  9,'9',  9,' ',  9,
      ' ', 10,'1',10,'0', 10,' ', 10,    
      ' ', 11,'1',11,'1', 11,' ', 11,
      ' ', 12,'1',12,'2', 12,' ', 12,
      ' ', 13,'1',13,'3', 13,' ', 13,
      ' ', 14,'1',14,'4', 14,' ', 14,    
      ' ', 15,'1',15,'5', 15,' ', 15
} ;

main()
{
    int i;
    int regster = 0;
    unsigned char c;

    printf(CLEARSCREEN); 
    putruler();
    for (i = 0; i < 16; i++)        /*  Initialize registers    */
        putval(i);

for (;;) {
    putval(regster);
    put_cursor(regster,CURSOR);     /*  Turn cursor on  */
    if ((c = getch()) == 27)
        break;
    put_cursor(regster,' ');        /*  Turn cursor off */

    if (!c)
        switch (c = getch() ) {
        case 0x4b:  /*  Left arrow - regster   */
            regster = (regster +15) % 16;
            break;
        case 0x4d:  /*  Right arrow - regster  */
            regster = (regster +1) % 16;
            break;
        case 0x48:  /*  Up arrow - value        */
            value[regster] = (value[regster] + 1) % 64;
            break;
        case 0x50:  /*  Down arrow - value      */
            value[regster] = (value[regster] + 63) % 64;
            break;
        }
}
    printf("\n\n");
    exit(0);
}

/*  Modify the value of a pallette register     */
putval(reg)
    int reg;        /*  # of pallette register to modify    */
{
    union REGS ireg;

    /*  Set pallette register - via EGA BIOS call   */
    ireg.x.ax = 0x1000;                     /*  Function 10h    */
    ireg.h.bl = reg;                        /*  register #      */
    ireg.h.bh = value[reg];                 /*  irgb value      */
    int86(0x10,&ireg,&ireg);

    /*  Update screen with new value    */
    printf(SETCURSOR,
        ROW + 3, COL + (reg * 4) + 2);
    printf("%2d",value[reg]);
}

put_cursor(reg,code)
    int reg,code;
{
    printf(SETCURSOR,
        ROW + 2, COL + (reg * 4) + 3);
    putchar(code);
}

/*  Write ruler line to screen - do it directly     */
putruler()
{
    char far *scrnbuf;
    char *p;
    int i;

    scrnbuf = (char far *)VIDSEG + (ROW * 160) + (COL * 2);

    /*  Move pallette ruler to screen   */
    for (p = ruler,i = 0; i < 16 * 8; i++)
        *scrnbuf++ = *p++;

}

-- 
Jeffrey William Gillette	uucp:  mcnc!duke!phys!lisa
The Divinity School		bitnet: DYBBUK @ TUCCVM
Duke University