/* PALETTE.C - Routines which control the palette */
#include "lsdino.h"

void rotate_dac(void)
{
        unsigned char temp[3];

        temp[0]=dacbox[1][0]; temp[1]=dacbox[1][1]; temp[2]=dacbox[1][2];
        memmove(dacbox+1,dacbox+2,254*3);
        dacbox[255][0]=temp[0];
        dacbox[255][1]=temp[1];
        dacbox[255][2]=temp[2];
}

void rotate_dacbak(void)
{
        unsigned char temp[3];

        temp[0]=dacbox[255][0]; temp[1]=dacbox[255][1]; temp[2]=dacbox[255][2];
        memmove(dacbox+2,dacbox+1,254*3);
        dacbox[1][0]=temp[0];
        dacbox[1][1]=temp[1];
        dacbox[1][2]=temp[2];

}

/* This will set our DAC up in memory to be the blending of three colors.
   This can be done in 2 ways, either 3 colors blending together in 3 different
   bands of color, or we can do a total of 15 bands of color, alternating
   between the colors.  This can be forced to one or the other by the command
   line.
*/
void set_dac(char start[3], char middle[3], char finish[3])
{
   int i, j, c;
  
   dacbox[0][0]=dacbox[0][1]=dacbox[0][2]=0;
   switch(paltype){
        case PALFULL:
           for(i=1;i<=85;i++)
              for (j = 0; j < 3; j++) {
                 dacbox[i][j]     = (i*middle[j] + (86-i)*start[j] )/85;
                 dacbox[i+85][j]  = (i*finish[j] + (86-i)*middle[j])/85;
                 dacbox[i+170][j] = (i*start[j]  + (86-i)*finish[j])/85;
              }
           break;
        case PALDIV:
           for(j=0;j<5;j++){
                   for(i=1;i<=17;i++){
                        for(c=0;c<3;c++){
                                 dacbox[i+51*j][c]     = (i*middle[c] + (17-i)*start[c] )/17;
                                 dacbox[i+51*j+17][c]  = (i*finish[c] + (17-i)*middle[c])/17;
                                 dacbox[i+51*j+34][c] = (i*start[c]  + (17-i)*finish[c])/17;
                        }
                   }
           }
           break;
   }
}

/* Set fade does the same thing as set_dac, but it alters the fadeto variable,
so a call to fadeto_dac function will cause our screen to fade to this slowly.
*/
void set_fade(char start[3], char middle[3], char finish[3])
{
   int i, j,c;
   
   fadeto[0][0]=fadeto[0][1]=fadeto[0][2]=0;
   switch(paltype){
        case PALFULL:
           for(i=1;i<=85;i++)
              for (j = 0; j < 3; j++) {
                 fadeto[i][j]     = (i*middle[j] + (86-i)*start[j] )/85;
                 fadeto[i+85][j]  = (i*finish[j] + (86-i)*middle[j])/85;
                 fadeto[i+170][j] = (i*start[j]  + (86-i)*finish[j])/85;
         
              }
           break;
        case PALDIV:
           for(j=0;j<5;j++){
                   for(i=1;i<=17;i++){
                        for(c=0;c<3;c++){
                                 fadeto[i+51*j][c]     = (i*middle[c] + (17-i)*start[c] )/17;
                                 fadeto[i+51*j+17][c]  = (i*finish[c] + (17-i)*middle[c])/17;
                                 fadeto[i+51*j+34][c] = (i*start[c]  + (17-i)*finish[c])/17;
                        }
                   }
           }
           break;
   }
}

/* Fade's to the dac in the fadeto variable */
void fadeto_dac(void)
{
        int i,color,i1,j;
        int change=1;

        for(i=0;i<64 && !kbhit() && change;i++){
               waitretrace();
               asm  cli
               change=0;
               delay(delaytime);                
               for(color=1;color<256;color++){
                        for(i1=0;i1<3;i1++){
                                if(dacbox[color][i1]>fadeto[color][i1]){
                                        change=1;
                                        dacbox[color][i1]--;                                
                                }else if(dacbox[color][i1]<fadeto[color][i1]){
                                        change=1;
                                        dacbox[color][i1]++;
                                }
                        }                        
                        rgb(color,dacbox[color][0],dacbox[color][1],dacbox[color][2]);
               }
               asm sti
        }
}

