[comp.sys.atari.st] Saving the color pallette

woodside@ttidca.TTI.COM (George Woodside) (03/15/88)

In article <422@mks.UUCP> wheels@mks.UUCP (Gerry Wheeler) writes:
>
>/*
  [...deleted...]
> * By the way, if anyone knows, can they tell me how to read the current
> * contents of the ST's colour palette? If I could do that, I could restore
> * it at the end of the program.
  [...deleted...]
> */
>

Be glad to. I wish everyone would save and restore the pallette. Here's
the necessary code in C (this is in MWC, but it is quite standard), and 
then in GFA BASIC.

These routines use the Setcolor(color,-1) mode to read the pallette without
changing it. You can then change it to your heart's content, and restore
it at exit. If your program sets the pallette only once, you can change it
and save the old colors at the same time by replacing the -1 in the Setcolor
call with the new color you wish to use. Setcolor always returns the old
color RGB value, whether you change it or not. Any negative value for the
RGB component will read the old color, but not change it.

The standard syntax for Setcolor is 

old_col = Setcolor(color,rgb);

where
int  old_col is the old RGB color values
int  color   is the color number
int  rgb     is the new color value

Thanks, Gerry, for a nice job on the program. 

/****************************************************************************/
/* Sample code to save and restore desktop colors                           */
/****************************************************************************/

#include <stdio.h>
#include <osbind.h>

#define  LOWREZ  0                      /* sixteen colors                   */
#define  MEDREZ  1                      /* four colors                      */

int   rez;                              /* system resolution                */
int   d_col[16];                        /* save pallette here               */

main()
{
  save_col();                           /* save desktop colors              */

  Setcolor(0,4);                        /* background blue                  */
  if(rez == LOWREZ)                     /* if low resolution                */
    Setcolor(15,0x777);                 /* text white                       */
  else if(rez == MEDREZ)                /* if medium resolution             */
    Setcolor(3,0x777);                  /* text white                       */

  printf("\033E");                      /* clear screen                     */
  printf("Hello, world!\n");            /* <==Your program here             */
  printf("Press RETURN.\n");            /* issue prompt                     */
  getchar();                            /* get a keypress                   */
  rest_col();                           /* restore desktop colors           */
}                                       /* end main                         */

/****************************************************************************/
/* Save desktop colors                                                      */
/****************************************************************************/
save_col()
{
  register  int  i;                     /* local counter                    */

  rez = Getrez();                       /* get system resolution            */

  if(rez == LOWREZ)                     /* if in low resolution             */
    {
      for(i = 0; i < 16; i++)           /* there are 16 colors              */
        d_col[i] = Setcolor(i,-1);      /* save them                        */
    }                                   /* end low resolution               */
  else if(rez == MEDREZ)                /* if in medium resolution          */
    {
      for(i = 0; i < 4; i++)            /* there are 4 colors               */
        d_col[i] = Setcolor(i,-1);      /* save them                        */
    }                                   /* end medium resolution            */
}                                       /* not needed for monochrome        */

/****************************************************************************/
/* Restore desktop colors                                                   */
/****************************************************************************/
rest_col()
{
  register  int  i;                     /* local counter                    */

  if(rez == LOWREZ)                     /* if in low resolution             */
    {
      for(i = 0; i < 16; i++)           /* there are 16 colors              */
        Setcolor(i,d_col[i]);           /* restore them                     */
    }                                   /* end low resolution               */
  else if(rez == MEDREZ)                /* if in medium resolution          */
    {
      for(i = 0; i < 4; i++)            /* there are 4 colors               */
        Setcolor(i,d_col[i]);           /* restore them                     */
    }                                   /* end medium resolution            */
}                                       /* not needed for monochrome        */

And, in GFA BASIC (since no one seems to restore colors in GFA):

Rem :
Rem : Sample code to save and restore desktop colors
Rem :
Gosub Save_col
Setcolor 0,0,0,4
If Rez%=1
  Setcolor 3,7,7,7
Endif
If Rez%=0
  Setcolor 15,7,7,7
Endif
Hidem
Print "Hello, World!"
Input "Press RETURN. ",A$
Gosub Rest_col
End
Procedure Save_col
  Rez%=Xbios(4)
  Rem Low resolution
  If Rez%=0
    Dim D_col(15)
    For I%=0 To 15
      D_col(I%)=Xbios(7,I%,-1)
    Next I%
  Endif
  Rem Medium resolution
  If Rez%=1
    Dim D_col(3)
    For I%=0 To 3
      D_col(I%)=Xbios(7,I%,-1)
    Next I%
  Endif
Return
Procedure Rest_col
  If Rez%=0
    Rem Low resolution
    For I%=0 To 15
      Setcolor I%,D_col(I%)
    Next I%
  Endif
  If Rez%=1
    Rem Medium resolution
    For I%=0 To 3
      Setcolor I%,D_col(I%)
    Next I%
  Endif
Return


-- 
*George R. Woodside - Citicorp/TTI - Santa Monica, CA 
*Path: ..!{trwrb|philabs|csun|psivax}!ttidca!woodside