home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / graphics / palette1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-25  |  3.7 KB  |  109 lines

  1. /* PALETTE.C illustrates functions for assigning color values to
  2.  * color indexes. Functions illustrated include:
  3.  *      _remappalette       _remapallpalette
  4.  */
  5.  
  6. #include <graph.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. /* Macro for mixing Red, Green, and Blue elements of color */
  12. #define RGB(r,g,b) (((long) ((b) << 8 | (g)) << 8) | (r))
  13.  
  14. main()
  15. {
  16.     short red, blue, green;
  17.     short inc, i, mode, cells, x, y, xinc, yinc;
  18.     long tmp, pal[256];
  19.     char buf[40];
  20.     struct videoconfig vc;
  21.  
  22.     /* Make sure all palette numbers are valid */
  23.     for( i = 0; i < 256; i++ )
  24.         pal[i] = _BLACK;
  25.  
  26.     /* Loop through each graphics mode that supports palettes. */
  27.     for( mode = _MRES4COLOR; mode <= _MRES256COLOR; mode++ )
  28.     {
  29.         if( mode == _ERESNOCOLOR )
  30.             mode++;
  31.         if( !_setvideomode( mode ) )
  32.             continue;
  33.  
  34.         /* Set variables for each mode. */
  35.         _getvideoconfig( &vc );
  36.         switch( vc.numcolors )
  37.         {
  38.             case 256:           /* Active bits in this order:           */
  39.                 cells = 13;
  40.                 inc = 12;       /* ???????? ??bbbbbb ??gggggg ??rrrrrr  */
  41.                 break;
  42.             case  16:
  43.                 cells = 4;
  44.                 if( (vc.mode == _ERESCOLOR) || (vc.mode == _VRES16COLOR) )
  45.                     inc = 16;   /* ???????? ??????bb ??????gg ??????rr  */
  46.                 else
  47.                     inc = 32;   /* ???????? ??????Bb ??????Gg ??????Rr  */
  48.                 break;
  49.             case   4:
  50.                 cells = 2;
  51.                 inc = 32;       /* ???????? ??????Bb ??????Gg ??????Rr  */
  52.                 break;
  53.             default:
  54.                 continue;
  55.         }
  56.         xinc = vc.numxpixels / cells;
  57.         yinc = vc.numypixels / cells;
  58.  
  59.         /* Fill palette arrays in BGR order */
  60.         for( i = 0, blue = 0; blue < 64; blue += inc )
  61.             for( green = 0; green < 64; green += inc )
  62.                 for( red = 0; red < 64; red += inc )
  63.                 {
  64.                     pal[i] = RGB( red, green, blue );
  65.                     /* Special case of using 6 bits to represent 16 colors.
  66.                      * If both bits are on for any color, intensity is set.
  67.                      * If one bit is set for a color, the color is on.
  68.                      */
  69.                     if( inc == 32 )
  70.                         pal[i + 8] = pal[i] | (pal[i] >> 1);
  71.                     i++;
  72.                 }
  73.  
  74.         /* If palettes available, remap all palettes at once. */
  75.         if( !_remapallpalette( pal ) )
  76.         {
  77.             _setvideomode( _DEFAULTMODE );
  78.             _outtext( "Palettes not available with this adapter" );
  79.             exit( 1 );
  80.         }
  81.  
  82.         /* Draw colored squares */
  83.         for( i = 0, x = 0; x < vc.numxpixels; x += xinc )
  84.             for( y = 0; y < vc.numypixels; y += yinc )
  85.             {
  86.                 _setcolor( i++ );
  87.                 _rectangle( _GFILLINTERIOR, x, y, x + xinc, y + yinc );
  88.             }
  89.         sprintf( buf, "Mode %d has %d colors", vc.mode, vc.numcolors );
  90.         _setcolor( vc.numcolors / 2 );
  91.         _outtext( buf );
  92.         getch();
  93.  
  94.         /* Change each palette entry separately in GRB order. */
  95.         for( i = 0, green = 0; green < 64; green += inc )
  96.             for( red = 0; red < 64; red += inc )
  97.                 for(blue = 0; blue < 64; blue += inc )
  98.                 {
  99.                     tmp = RGB( red, green, blue );
  100.                     _remappalette( i, tmp );
  101.                     if( inc == 32 )
  102.                         _remappalette( i + 8, tmp | (tmp >> 1) );
  103.                     i++;
  104.                 }
  105.         getch();
  106.     }
  107.     exit( !_setvideomode( _DEFAULTMODE ) );
  108. }
  109.