home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / Hls.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-01  |  4.1 KB  |  119 lines

  1. // HLS.cpp: implementation of the CHLS class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "stdafx.h"
  6. #include "HLS.h"
  7.  
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11.  
  12. CHLS::~CHLS()
  13. {
  14.  
  15. }
  16.  
  17. // default constructor (sets color to black)
  18. CHLS::CHLS():H(0),L(0),S(0){}
  19. CHLS::CHLS(unsigned short int h,unsigned short int l,unsigned short int s):H(h),L(l),S(s){}    //constructor #2
  20.  
  21. CHLS::CHLS(unsigned long int RGBColor)
  22. // This constructor is adapted from code from Microsoft Knowledge Base Article ID: Q29240
  23. {// begin RGBtoHLS constructor
  24.    unsigned short int R,G,B;          // input RGB values
  25.    unsigned short int cMax,cMin;      // max and min RGB values
  26.    unsigned short int Rdelta,Gdelta,Bdelta; // intermediate value: % of spread from max
  27.  
  28.    // get R, G, and B out of DWORD
  29.    R = GetRValue(RGBColor);
  30.    G = GetGValue(RGBColor);
  31.    B = GetBValue(RGBColor);
  32.  
  33.    // calculate lightness
  34.    cMax = max( max(R,G), B);
  35.    cMin = min( min(R,G), B);
  36.    L = (unsigned short int)(( ((cMax+cMin)*HLSMAX) + RGBMAX )/(2*RGBMAX));
  37.  
  38.    if (cMax == cMin)
  39.    {           // r=g=b --> achromatic case
  40.       S = 0;                     // saturation
  41.       H = UNDEFINED;             // hue
  42.    }
  43.    else
  44.    {                        // chromatic case
  45.       // saturation
  46.       if (L <= (HLSMAX/2))
  47.          S = (unsigned short int)(( ((cMax-cMin)*HLSMAX) + ((cMax+cMin)/2) ) / (cMax+cMin));
  48.       else
  49.          S = (unsigned short int)(( ((cMax-cMin)*HLSMAX) + ((2*RGBMAX-cMax-cMin)/2) )/ (2*RGBMAX-cMax-cMin));
  50.  
  51.       // hue
  52.       Rdelta = (unsigned short int)(( ((cMax-R)*(HLSMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  53.       Gdelta = (unsigned short int)(( ((cMax-G)*(HLSMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  54.       Bdelta = (unsigned short int)(( ((cMax-B)*(HLSMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  55.  
  56.       if (R == cMax)
  57.          H = (unsigned short int)(Bdelta - Gdelta);
  58.       else if (G == cMax)
  59.          H = (unsigned short int)((HLSMAX/3) + Rdelta - Bdelta);
  60.       else // B == cMax
  61.          H = (unsigned short int)(((2*HLSMAX)/3) + Gdelta - Rdelta);
  62.  
  63.       if (H < 0)
  64.          H += HLSMAX;
  65.       if (H > HLSMAX)
  66.          H -= HLSMAX;
  67.    }
  68. }// end RGBtoHLS constructor
  69.  
  70. unsigned short int CHLS::HueToRGB(unsigned short int n1,unsigned short int n2,unsigned short int hue)
  71. // HueToRGB() is adapted from code from Microsoft Knowledge Base Article ID: Q29240
  72. {// begin HueToRGB
  73.    // range check: note values passed add/subtract thirds of range
  74.    if (hue < 0)
  75.       hue += HLSMAX;
  76.    if (hue > HLSMAX)
  77.       hue -= HLSMAX;
  78.  
  79.    // return r,g, or b value from this tridrant
  80.    if (hue < (HLSMAX/6))
  81.       return (unsigned short int)(( n1 + (((n2-n1)*hue+(HLSMAX/12))/(HLSMAX/6)) ));
  82.    if (hue < (HLSMAX/2))
  83.       return (unsigned short int)( n2 );
  84.    if (hue < ((HLSMAX*2)/3))
  85.       return (unsigned short int)( n1 + (((n2-n1)*(((HLSMAX*2)/3)-hue)+(HLSMAX/12))/(HLSMAX/6))); 
  86.    else
  87.       return (unsigned short int)( n1 );
  88. }// end HueToRGB
  89.  
  90. unsigned long int CHLS::ToRGB()
  91. // ToRGB() is adapted from code from Microsoft Knowledge Base Article ID: Q29240
  92. {// begin ToRGB
  93.    unsigned short int R,G,B;                // RGB component values 
  94.    unsigned short int Magic1,Magic2;       // calculated magic numbers (really!) 
  95.  
  96.    if (S == 0)
  97.    {            // achromatic case 
  98.       R=G=B=(unsigned short int)((L*RGBMAX)/HLSMAX);
  99.       if (H != UNDEFINED)
  100.       {
  101.          // ERROR 
  102.       }
  103.    }
  104.    else
  105.    {                    // chromatic case 
  106.       // set up magic numbers 
  107.       if (L <= (HLSMAX/2))
  108.          Magic2 = (unsigned short int)((L*(HLSMAX + S) + (HLSMAX/2))/HLSMAX);
  109.       else
  110.          Magic2 = (unsigned short int)(L + S - ((L*S) + (HLSMAX/2))/HLSMAX);
  111.       Magic1 = (unsigned short int)(2*L-Magic2);
  112.  
  113.       // get RGB, change units from HLSMAX to RGBMAX 
  114.       R = (unsigned short int)((HueToRGB(Magic1,Magic2,H+(HLSMAX/3))*RGBMAX + (HLSMAX/2))/HLSMAX); 
  115.       G = (unsigned short int)((HueToRGB(Magic1,Magic2,H)*RGBMAX + (HLSMAX/2)) / HLSMAX);
  116.       B = (unsigned short int)((HueToRGB(Magic1,Magic2,H-(HLSMAX/3))*RGBMAX + (HLSMAX/2))/HLSMAX); 
  117.    }
  118.    return(RGB(R,G,B));
  119. }// end ToRGB