color.h

Go to the documentation of this file.
00001     /**********************************************************************
00002  *<
00003     FILE: color.h
00004 
00005     DESCRIPTION:
00006 
00007     CREATED BY: Dan Silva
00008 
00009     HISTORY:
00010 
00011  *> Copyright (c) 1994, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 
00016 #include "GeomExport.h"
00017 #include "maxheap.h"
00018 #include <WTypes.h>
00019 #include "point3.h"
00020 #include "maxtypes.h"
00021 
00022 #define FLto255(x) ((int)((x)*255.0f+.5))   
00023 
00024 class Color;
00025 
00048 struct RealPixel: public MaxHeapOperators {
00050     unsigned char r,g,b;   // mantissas
00052     unsigned char e;       // exponent
00054     GEOMEXPORT operator Color();
00055     };
00056 
00057 GEOMEXPORT RealPixel MakeRealPixel(float r, float g, float b);
00058 GEOMEXPORT void ExpandRealPixel(const RealPixel &rp, float &r, float &g, float &b);
00059 
00067 class Color : public MaxHeapOperators {
00068 public:
00070     float r;
00072     float g;
00074     float b;
00075 
00076     // Constructors
00079     Color()  {}
00089     Color(float R, float G, float B)  { r = R; g = G; b = B;  }
00099     Color(double R, double G, double B) { r = (float)R; g = (float)G; b = (float)B; }
00109     Color(int R, int G, int B) { r = (float)R; g = (float)G; b = (float)B; }
00114     Color(const Color& a) { r = a.r; g = a.g; b = a.b; } 
00119     GEOMEXPORT explicit Color(DWORD rgb);  // from Windows RGB value
00125     Color(Point3 p) { r = p.x; g = p.y; b = p.z; }
00130     Color(float af[3]) { r = af[0]; g = af[1]; b = af[2]; }
00136     Color(RealPixel rp) { ExpandRealPixel(rp,r,g,b); } 
00141     Color(const BMM_Color_24& c) { 
00142         r = float(c.r)/255.0f; g = float(c.g)/255.0f; b = float(c.b)/255.0f;  
00143         }
00148     Color(const BMM_Color_32& c) { 
00149         r = float(c.r)/255.0f; g = float(c.g)/255.0f; b = float(c.b)/255.0f;  
00150         }
00155     Color(const BMM_Color_48& c) { 
00156         r = float(c.r)/65535.0f; g = float(c.g)/65535.0f; b = float(c.b)/65535.0f;  
00157         }
00162     Color(const BMM_Color_64& c) { 
00163         r = float(c.r)/65535.0f; g = float(c.g)/65535.0f; b = float(c.b)/65535.0f;  
00164         }
00170     Color(const BMM_Color_fl& c) { 
00171         r = c.r; g = c.g; b = c.b;  
00172         }
00173     
00174         
00176     void Black() { r = g = b = 0.0f; }
00178     void White() { r = g = b = 1.0f; }
00179 
00181     GEOMEXPORT void ClampMax();  // makes components >= 0.0
00183     GEOMEXPORT void ClampMin();  // makes components <= 1.0
00185     GEOMEXPORT void ClampMinMax();  // makes components in [0,1]
00186 
00187     // Access operators
00193     float& operator[](int i) { return (&r)[i]; }     
00199     const float& operator[](int i) const { return (&r)[i]; }  
00200 
00201     // Conversion function
00204     operator float*() { return(&r); }
00205     operator const float*() const { return(&r); }
00206 
00207     // Convert to Windows RGB
00208 //  operator DWORD() { return RGB(FLto255(r),FLto255(g), FLto255(b)); }
00209     DWORD toRGB() { return RGB(FLto255(r),FLto255(g), FLto255(b)); };
00210 
00211     // Convert to Point3
00213     operator Point3() { return Point3(r,g,b); }
00214 
00215     // Convert to RealPixel
00217     GEOMEXPORT operator RealPixel();
00218 
00219     // Convert to Bitmap Manager types
00221     operator BMM_Color_24() { 
00222         BMM_Color_24 c; 
00223         c.r = (BYTE) int(r*255.0f); c.g = (BYTE)int(g*255.0f); c.b = (BYTE) int(b*255.0f);
00224         return c;
00225         }
00227     operator BMM_Color_32() { 
00228         BMM_Color_32 c; 
00229         c.r = (BYTE)int(r*255.0f); c.g = (BYTE)int(g*255.0f); c.b = (BYTE)int(b*255.0f);
00230         return c;
00231         }
00233     operator BMM_Color_48() { 
00234         BMM_Color_48 c; 
00235         c.r = (WORD)int(r*65535.0f); c.g = (WORD)int(g*65535.0f); c.b = (WORD)int(b*65535.0f);
00236         return c;
00237         }
00239     operator BMM_Color_64() { 
00240         BMM_Color_64 c; 
00241         c.r = (WORD)int(r*65535.0f); c.g = (WORD)int(g*65535.0f); c.b = (WORD)int(b*65535.0f);
00242         return c;
00243         }
00245     operator BMM_Color_fl() { 
00246         BMM_Color_fl c; 
00247         c.r = r; c.g = g; c.b = b;
00248         return c;
00249         }
00250 
00251     // Unary operators
00255     Color operator-() const { return(Color(-r,-g,-b)); } 
00258     Color operator+() const { return *this; } 
00259 
00260     // Assignment operators
00263     inline Color& operator-=(const Color&);
00266     inline Color& operator+=(const Color&);
00269     inline Color& operator*=(float); 
00272     inline Color& operator/=(float);
00275     inline Color& operator*=(const Color&); // element-by-element multiplg.
00276 
00277     // Test for equality
00280     int operator==(const Color& p) const { return ((p.r==r)&&(p.g==g)&&(p.b==b)); }
00283     int operator!=(const Color& p) const { return ((p.r!=r)||(p.g!=g)||(p.b!=b)); }
00284 
00285     // Binary operators
00288     inline Color operator-(const Color&) const;
00291     inline Color operator+(const Color&) const;
00294     inline Color operator/(const Color&) const;
00297     inline Color operator*(const Color&) const;   
00298     inline Color operator^(const Color&) const;   // CROSS PRODUCT
00299 };
00300 
00308 GEOMEXPORT int MaxComponent(const Color&);  // index of the component with the maximum abs value
00316 GEOMEXPORT int  MinComponent(const Color&);  // index of the component with the minimum abs value
00317 
00324 GEOMEXPORT float  MaxVal(const Color&);  // value of the component with the maximum abs value
00330 GEOMEXPORT float MinVal(const Color&);  // value of the component with the minimum abs value
00331 
00332 // Inlines:
00333 
00342 inline float Length(const Color& v) {   
00343     return (float)sqrt(v.r*v.r+v.g*v.g+v.b*v.b);
00344     }
00345 
00346 inline Color& Color::operator-=(const Color& a) {   
00347     r -= a.r;   g -= a.g;   b -= a.b;
00348     return *this;
00349     }
00350 
00351 inline Color& Color::operator+=(const Color& a) {
00352     r += a.r;   g += a.g;   b += a.b;
00353     return *this;
00354     }
00355 
00356 inline Color& Color::operator*=(float f) {
00357     r *= f;   g *= f;   b *= f;
00358     return *this;
00359     }
00360 
00361 inline Color& Color::operator/=(float f) { 
00362     r /= f; g /= f; b /= f; 
00363     return *this; 
00364     }
00365 
00366 inline Color& Color::operator*=(const Color& a) { 
00367     r *= a.r;   g *= a.g;   b *= a.b;   
00368     return *this; 
00369     }
00370 
00371 inline Color Color::operator-(const Color& c) const {
00372     return(Color(r-c.r,g-c.g,b-c.b));
00373     }
00374 
00375 inline Color Color::operator+(const Color& c) const {
00376     return(Color(r+c.r,g+c.g,b+c.b));
00377     }
00378 
00379 inline Color Color::operator/(const Color& c) const {
00380     return Color(r/c.r,g/c.g,b/c.b);
00381     }
00382 
00383 inline Color Color::operator*(const Color& c) const {  
00384     return Color(r*c.r, g*c.g, b*c.b);
00385     }
00386 
00387 inline Color Color::operator^(const Color& c) const {
00388     return Color(g * c.b - b * c.g, b * c.r - r * c.b, r * c.g - g * c.r);
00389     }
00390 
00391 inline Color operator*(float f, const Color& a) {
00392     return(Color(a.r*f, a.g*f, a.b*f));
00393     }
00394 
00395 inline Color operator*(const Color& a, float f) {
00396     return(Color(a.r*f, a.g*f, a.b*f));
00397     }
00398 
00399 inline Color operator/(const Color& a, float f) {
00400     return(Color(a.r/f, a.g/f, a.b/f));
00401     }
00402 
00403 inline Color operator+(const Color& a, float f) {
00404     return(Color(a.r+f, a.g+f, a.b+f));
00405     }
00406 
00407 inline Color operator+(float f, const Color& a) {
00408     return(Color(a.r+f, a.g+f, a.b+f));
00409     }
00410 
00411 inline Color operator-(const Color& a, float f) {
00412     return(Color(a.r-f, a.g-f, a.b-f));
00413     }
00414 
00415 inline Color operator-(float f, const Color& a) {
00416     return(Color(f-a.r, f-a.g, f-a.b));
00417     }
00418 
00419 
00440 struct LogLUV32Pixel: public MaxHeapOperators {
00442     DWORD32     value;
00443 
00446     operator Color() const { Color c; GetRGB(c); return c; }
00452     LogLUV32Pixel& operator=(const float c[3]) { SetRGB(c); return *this; }
00453 
00459     GEOMEXPORT void GetRGB(float rgb[3]) const;
00465     GEOMEXPORT void SetRGB(const float rgb[3]);
00466 
00472     GEOMEXPORT void GetXYZ(float xyz[3]) const;
00478     GEOMEXPORT void SetXYZ(const float xyz[3]);
00479 
00487     GEOMEXPORT static void XYZtoRGB(const float xyz[3], float rgb[3]);
00495     GEOMEXPORT static void RGBtoXYZ(const float rgb[3], float xyz[3]);
00496 };
00497 
00502 struct LogLUV24Pixel: public MaxHeapOperators {
00504     unsigned char   value[3];
00505 
00508     operator Color() const { Color c; GetRGB(c); return c; }
00514     LogLUV24Pixel& operator=(const float c[3]) { SetRGB(c); return *this; }
00515 
00521     GEOMEXPORT void GetRGB(float rgb[3]) const;
00527     GEOMEXPORT void SetRGB(const float rgb[3]);
00528 
00534     GEOMEXPORT void GetXYZ(float xyz[3]) const;
00540     GEOMEXPORT void SetXYZ(const float xyz[3]);
00541 
00549     GEOMEXPORT static void XYZtoRGB(const float xyz[3], float rgb[3]);
00557     GEOMEXPORT static void RGBtoXYZ(const float rgb[3], float xyz[3]);
00558 };
00559 
00560 
00561