acolor.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: acolor.h
00004 
00005     DESCRIPTION:  floating point color + alpha
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 "maxtypes.h"
00019 #include "point3.h"
00020 #include "point4.h"
00021 #include "color.h"
00022 
00029 class AColor : public MaxHeapOperators
00030 {
00031 public:
00033     float r,g,b,a;
00034 
00035     // Constructors
00038     AColor()  {}
00041     AColor(float R, float G, float B, float A=1.0f)  { r = R; g = G; b = B; a = A; }
00044     AColor(double R, double G, double B, double A=1.0) {
00045          r = (float)R; g = (float)G; b = (float)B; a = (float)A; }
00048     AColor(int R, int G, int B, int A=0) { 
00049         r = (float)R; g = (float)G; b = (float)B; a = (float)A; }
00051     AColor(const AColor& c) { r = c.r; g = c.g; b = c.b; a = c.a; } 
00054     AColor(const Color& c, float alph=1.0f) { r = c.r; g = c.g; b = c.b; a = alph; } 
00057     GEOMEXPORT explicit AColor(DWORD rgb, float alph=1.0f);  // from Windows RGB value
00058     AColor(Point4 p) { r = p.x; g = p.y; b = p.z; a = p.w; }
00063     AColor(float af[4]) { r = af[0]; g = af[1]; b = af[2];a = af[3]; }
00068     AColor(const BMM_Color_24& c) { 
00069         r = float(c.r)/255.0f; g = float(c.g)/255.0f; b = float(c.b)/255.0f; a = 1.0f; 
00070         }
00075     AColor(const BMM_Color_32& c) { 
00076         r = float(c.r)/255.0f; g = float(c.g)/255.0f; b = float(c.b)/255.0f; a = float(c.a)/255.0f; 
00077         }
00082     AColor(const BMM_Color_48& c) { 
00083         r = float(c.r)/65535.0f; g = float(c.g)/65535.0f; b = float(c.b)/65535.0f; a = 1.0f; 
00084         }
00089     AColor(const BMM_Color_64& c) { 
00090         r = float(c.r)/65535.0f; g = float(c.g)/65535.0f; b = float(c.b)/65535.0f; a = float(c.a)/65535.0f; 
00091         }
00097     AColor(const BMM_Color_fl& c) { 
00098         r = c.r; g = c.g; b = c.b; a = c.a; 
00099         }
00100     
00102     void Black() { r = g = b = 0.0f; a = 1.0f; }
00104     void White() { r = g = b = 1.0f; a = 1.0f; }
00105 
00107     GEOMEXPORT void ClampMax();  // makes components >= 0.0
00109     GEOMEXPORT void ClampMin();  // makes components <= 1.0
00112     GEOMEXPORT void ClampMinMax();  // makes components in [0,1]
00113 
00119     float& operator[](int i) { return (&r)[i]; }     
00120 
00126     const float& operator[](int i) const { return (&r)[i]; }  
00127 
00128     // Conversion functions
00131     operator float*() { return(&r); }
00132     operator Color() { return Color(r,g,b); }
00133 
00134     // Convert to Bitmap Manager types
00137     operator BMM_Color_24() { 
00138         BMM_Color_24 c; 
00139         c.r = (BYTE)int(r*255.0f); c.g = (BYTE)int(g*255.0f); c.b = (BYTE)int(b*255.0f);
00140         return c;
00141         }
00143     operator BMM_Color_32() { 
00144         BMM_Color_32 c; 
00145         c.r = (BYTE)int(r*255.0f); c.g = (BYTE)int(g*255.0f); c.b = (BYTE)int(b*255.0f); c.a = (BYTE)int(a*255.0f);
00146         return c;
00147         }
00149     operator BMM_Color_48() { 
00150         BMM_Color_48 c; 
00151         c.r = (WORD)int(r*65535.0f); c.g = (WORD)int(g*65535.0f); c.b = (WORD)int(b*65535.0f); 
00152         return c;
00153         }
00155     operator BMM_Color_64() { 
00156         BMM_Color_64 c; 
00157         c.r = (WORD)int(r*65535.0f); c.g = (WORD)int(g*65535.0f); c.b = (WORD)int(b*65535.0f); c.a = (WORD)int(a*65535.0f);
00158         return c;
00159         }
00161     operator BMM_Color_fl() { 
00162         BMM_Color_fl c; 
00163         c.r = r;    c.g = g;    c.b = b;    c.a = a;
00164         return c;
00165         }
00166 
00167     // Convert to Windows RGB
00168 //  operator DWORD() { return RGB(FLto255(r),FLto255(g), FLto255(b)); }
00172     DWORD toRGB() { return RGB(FLto255(r),FLto255(g), FLto255(b)); };
00173 
00174     // Convert to Point3, 4
00177     operator Point3() { return Point3(r,g,b); }
00178     operator Point4() { return Point4(r,g,b,a); }
00179 
00180     // Unary operators
00184     AColor operator-() const { return (AColor(-r,-g,-b, -a)); } 
00187     AColor operator+() const { return *this; } 
00188 
00189     // Assignment operators
00192     inline AColor& operator-=(const AColor&);
00195     inline AColor& operator+=(const AColor&);
00198     inline AColor& operator*=(float); 
00201     inline AColor& operator/=(float);
00204     inline AColor& operator*=(const AColor&);   // element-by-element multiplg.
00205 
00206     // Test for equality
00209     int operator==(const AColor& p) const { return ((p.r==r)&&(p.g==g)&&(p.b==b)&&(p.a==a)); }
00212     int operator!=(const AColor& p) const { return ((p.r!=r)||(p.g!=g)||(p.b!=b)||(p.a!=a)); }
00213 
00214     // Binary operators
00217     inline AColor operator-(const AColor&) const;
00220     inline AColor operator+(const AColor&) const;
00223     inline AColor operator/(const AColor&) const;
00226     inline AColor operator*(const AColor&) const;   
00230     inline AColor operator^(const AColor&) const;   // CROSS PRODUCT
00231     };
00232 
00240 GEOMEXPORT int MaxComponent(const AColor&);  // the component with the maximum abs value
00241 
00242 
00250 GEOMEXPORT int MinComponent(const AColor&);  // the component with the minimum abs value
00251 
00252 // Inlines:
00253 
00254 inline AColor& AColor::operator-=(const AColor& c) {    
00255     r -= c.r;   g -= c.g;   b -= c.b;  a -= c.a;
00256     return *this;
00257     }
00258 
00259 inline AColor& AColor::operator+=(const AColor& c) {
00260     r += c.r;   g += c.g;   b += c.b; a += c.a;
00261     return *this;
00262     }
00263 
00264 inline AColor& AColor::operator*=(float f) {
00265     r *= f;   g *= f;   b *= f;  a *= f;
00266     return *this;
00267     }
00268 
00269 inline AColor& AColor::operator/=(float f) { 
00270     r /= f; g /= f; b /= f; a /= f;
00271     return *this; 
00272     }
00273 
00274 inline AColor& AColor::operator*=(const AColor& c) { 
00275     r *= c.r;   g *= c.g;   b *= c.b;   a *= c.a;
00276     return *this; 
00277     }
00278 
00279 inline AColor AColor::operator-(const AColor& c) const {
00280     return(AColor(r-c.r,g-c.g,b-c.b,a-c.a));
00281     }
00282 
00283 inline AColor AColor::operator+(const AColor& c) const {
00284     return(AColor(r+c.r,g+c.g,b+c.b,a+c.a));
00285     }
00286 
00287 inline AColor AColor::operator/(const AColor& c) const {
00288     return AColor(r/c.r,g/c.g,b/c.b,a/c.a);
00289     }
00290 
00291 inline AColor AColor::operator*(const AColor& c) const {  
00292     return AColor(r*c.r, g*c.g, b*c.b, a*c.a);  
00293     }
00294 
00295 inline AColor AColor::operator^(const AColor& c) const {
00296     return AColor(g * c.b - b * c.g, b * c.r - r * c.b, r * c.g - g * c.r, c.a);
00297     }
00298 
00301 inline AColor operator*(float f, const AColor& a) {
00302     return(AColor(a.r*f, a.g*f, a.b*f, a.a*f));
00303     }
00304 
00307 inline AColor operator*(const AColor& a, float f) {
00308     return(AColor(a.r*f, a.g*f, a.b*f, a.a*f));
00309     }
00310 
00311 // Composite  fg over bg, assuming associated alpha,
00312 // i.e. pre-multiplied alpha for both fg and bg
00322 inline AColor CompOver(const AColor &fg, const AColor& bg) {
00323     return  fg + (1.0f-fg.a)*bg;
00324     }
00325 
00326 typedef AColor RGBA;
00327 
00328