home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DTOSHI2.ZIP / mpgfx / source / Gfxpal.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  5.8 KB  |  200 lines

  1.  
  2. // gfxpal.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "gfxpal.h"
  8. #include <math.h>
  9.  
  10. //***************************************************
  11. //
  12. // RGB Palette Class
  13. //
  14. //***************************************************
  15.  
  16. RGBPALETTE::RGBPALETTE () : MYOBJECT ()
  17.   {
  18.   } // End of Constructor for RGBPALETTE
  19.  
  20. RGBPALETTE::~RGBPALETTE ()
  21.   {
  22.   } // End of Destructor for RGBPALETTE
  23.  
  24. double RGBPALETTE::GetColorDistance ( RGBCOLOR Col1, LONG Index )
  25.   {
  26.     double D1,D2,D3;
  27.     double Distance;
  28.  
  29.     D1 = (double)(Col1.Red-Entry[Index].Red);  
  30.     D2 = (double)(Col1.Green-Entry[Index].Green);
  31.     D3 = (double)(Col1.Blue-Entry[Index].Blue);
  32.  
  33.     Distance = D1*D1 + D2*D2 + D3*D3;
  34.     Distance = sqrt ( Distance );
  35.     return Distance;
  36.   } // End of GetColorDistance for RGBPALETTE
  37.  
  38. LONG RGBPALETTE::GetClosestColor ( RGBCOLOR Color )
  39.   {
  40.     INT i;
  41.     LONG Closest;
  42.     double Min;
  43.     double Distance;
  44.  
  45.     Closest = 0;
  46.     Min = Distance = GetColorDistance ( Color, 0 );
  47.     for (i=1;i<256;i++)
  48.       {
  49.         Distance = GetColorDistance ( Color, i );
  50.         if (Distance<Min)
  51.           {
  52.             Min = Distance;
  53.             Closest = i;  
  54.           } // End if
  55.       } // End for
  56.     return Closest;  
  57.   } // End of GetClosestColor for RGBPALETTE
  58.  
  59. VOID RGBPALETTE::SetColorScale ( RGBCOLOR Color, INT Start, INT End,
  60.                                  float StartInt, float EndInt )
  61.   {
  62.     Start = MaxValue ( Start, 0 );    
  63.     End = MinValue ( End, 255 );
  64.     
  65.     if (End<Start)
  66.       return;
  67.  
  68.     if (End==Start)
  69.       {
  70.         Entry[Start] = Color;
  71.       } // End if  
  72.         
  73.     StartInt /= 100;  
  74.     EndInt /= 100;
  75.  
  76.     INT NumLevels = End - Start;
  77.     float Step = (EndInt - StartInt) / (NumLevels);
  78.  
  79.     INT R,G,B;
  80.     INT BaseR,BaseG,BaseB;
  81.     float Intensity;
  82.     INT i;
  83.  
  84.     Intensity = StartInt;
  85.     i = Start;
  86.     R = G = B = 0;
  87.     BaseR = Color.Red;
  88.     BaseG = Color.Green;
  89.     BaseB = Color.Blue;
  90.     while ((Intensity<1.00)&&(i<=End))
  91.       {
  92.         R = (INT)(Intensity*BaseR);
  93.         G = (INT)(Intensity*BaseG);
  94.         B = (INT)(Intensity*BaseB);
  95.         R = MaxValue ( 0, MinValue ( R, 255 ) );
  96.         G = MaxValue ( 0, MinValue ( G, 255 ) );
  97.         B = MaxValue ( 0, MinValue ( B, 255 ) );
  98.           
  99.         Entry[i].Red = (BYTE)R;  
  100.         Entry[i].Green = (BYTE)G;  
  101.         Entry[i].Blue = (BYTE)B;
  102.         Intensity += Step;
  103.         i++;  
  104.       } // End while
  105.  
  106.     while (i<=End)
  107.       {
  108.         if (BaseR==0)
  109.           R = (INT)((Intensity-1.00)*255);
  110.         else  
  111.           R = (INT)(Intensity*BaseR);
  112.           
  113.         if (BaseG==0)
  114.           G = (INT)((Intensity-1.00)*255);
  115.         else  
  116.           G = (INT)(Intensity*BaseG);
  117.           
  118.         if (BaseB==0)
  119.           B = (INT)((Intensity-1.00)*255);
  120.         else  
  121.           B = (INT)(Intensity*BaseB);
  122.         
  123.         R = MaxValue ( 0, MinValue ( R, 255 ) );
  124.         G = MaxValue ( 0, MinValue ( G, 255 ) );
  125.         B = MaxValue ( 0, MinValue ( B, 255 ) );
  126.           
  127.         Entry[i].Red = (BYTE)R;  
  128.         Entry[i].Green = (BYTE)G;  
  129.         Entry[i].Blue = (BYTE)B;
  130.         Intensity += Step;
  131.         i++;  
  132.       } // End while
  133.   } // End of SetColorScale for RGBPALETTE
  134.  
  135. COLOR RGBPALETTE::GetRGB ( BYTE R, BYTE G, BYTE B )
  136.   {
  137.     #if defined (__FORWINDOWS__)
  138.       return RGB ( R, G, B );
  139.     #else
  140.       RGBCOLOR RGBCol;
  141.       RGBCol.Red = R; 
  142.       RGBCol.Red = G; 
  143.       RGBCol.Red = B; 
  144.       return GetClosestColor ( RGBCol );  
  145.     #endif    
  146.   } // End of GetRGB for RGBPALETTE
  147.  
  148. COLOR RGBPALETTE::GetIndex ( LONG Index )
  149.   {
  150.     #if defined (__FORWINDOWS__)
  151.       return PALETTEINDEX ( Index );
  152.     #else
  153.       return Index;  
  154.     #endif    
  155.   } // End of GetIndex for RGBPALETTE
  156.  
  157. VOID RGBPALETTE::SetWindowsPalette ()
  158.   {
  159.     Entry[0].Red = 0; Entry[0].Green = 0; Entry[0].Blue = 0;  
  160.     Entry[1].Red = 128; Entry[1].Green = 0; Entry[1].Blue = 0;  
  161.     Entry[2].Red = 0; Entry[2].Green = 128; Entry[2].Blue = 0;  
  162.     Entry[3].Red = 128; Entry[3].Green = 128; Entry[3].Blue = 0;  
  163.     Entry[4].Red = 0; Entry[4].Green = 0; Entry[4].Blue = 128;  
  164.     Entry[5].Red = 128; Entry[5].Green = 0; Entry[5].Blue = 128;  
  165.     Entry[6].Red = 0; Entry[6].Green = 128; Entry[6].Blue = 128;  
  166.     Entry[7].Red = 192; Entry[7].Green = 192; Entry[7].Blue = 192;  
  167.     Entry[8].Red = 192; Entry[8].Green = 220; Entry[8].Blue = 192;  
  168.     Entry[9].Red = 166; Entry[9].Green = 202; Entry[9].Blue = 240;
  169.     
  170.     Entry[246].Red = 255; Entry[246].Green = 251; Entry[246].Blue = 240;  
  171.     Entry[247].Red = 160; Entry[247].Green = 160; Entry[247].Blue = 164;  
  172.     Entry[248].Red = 128; Entry[248].Green = 128; Entry[248].Blue = 128;  
  173.     Entry[249].Red = 255; Entry[249].Green = 0; Entry[249].Blue = 0;
  174.     Entry[250].Red = 0; Entry[250].Green = 255; Entry[250].Blue = 0;  
  175.     Entry[251].Red = 255; Entry[251].Green = 255; Entry[251].Blue = 0;  
  176.     Entry[252].Red = 0; Entry[252].Green = 0; Entry[252].Blue = 255;  
  177.     Entry[253].Red = 255; Entry[253].Green = 0; Entry[253].Blue = 255;  
  178.     Entry[254].Red = 0; Entry[254].Green = 255; Entry[254].Blue = 255;  
  179.     Entry[255].Red = 255; Entry[255].Green = 255; Entry[255].Blue = 255;    
  180.   } // End of SetWindowPalette for RGBPALETTE
  181.  
  182. BOOLEAN RGBPALETTE::IsIdentical ( RGBPALETTE *Pal )
  183.   {
  184.     RGBCOLOR *Entry1;
  185.     Entry1 = Pal->GetEntry ();
  186.  
  187.     INT i;
  188.     for (i=0;i<256;i++)
  189.       {
  190.         if (Entry[i].Red!=Entry1[i].Red)
  191.           return FALSE;  
  192.         if (Entry[i].Green!=Entry1[i].Green)
  193.           return FALSE;  
  194.         if (Entry[i].Blue!=Entry1[i].Blue)
  195.           return FALSE;  
  196.       } // End for
  197.     return TRUE;  
  198.   } // End of IsIdentical for RGBPALETTE
  199.  
  200.