Main Page | Class Hierarchy | Class List | Directories | File List

ColorTypes.h

00001 #ifndef __COLOR_TYPES__
00002 #define __COLOR_TYPES__
00003 
00004 //#include <OpenGL.h>
00005 #include <GL/gl.h>
00006 #include <QColor>
00007 
00008 namespace apig {
00009 
00010     template<class Color>
00011     class GenericColor {
00012         public:
00013             static Color create(float r, float g, float b, float a) { Color res; res.reset(r,g,b,a); return res; }
00014             static Color create(float c) { return create(c,c,c,c); }
00015             static Color BLACK() { return create(0); }
00016             static Color WHITE() { return create(1); }
00017             
00018             GenericColor(Color &color) : color(color) {}
00019             
00020             float norm2() const {
00021                 float e = 0.0f;
00022                 for (int c=0; c<Color::SIZE; c++) e += color[c] * color[c];
00023                 return e;
00024                 }
00025     
00026             float norm() const { return sqrt(norm2()); }
00027             float normRMS() const { return sqrt(norm2() / Color::SIZE); }
00028             
00029         private:
00030             Color &color;
00031         };
00032     
00033     class Float1 {
00034         public:
00035             Float1(float x = 0);
00036             Float1(const float *c);   // tableau de 1 valeur
00037             Float1(QRgb c);
00038         
00039             // opérateurs de conversion :
00040             inline operator const float* () const {return c;}     // cast du Float1 en const float*
00041             inline operator       float* ()       {return c;}     // cast du Float1 en float*
00042             inline operator const float  () const {return r;}     // cast du Float1 en const float
00043             inline operator       float  ()       {return r;}     // cast du Float1 en float
00044             QRgb toQRgb() const;
00045             QString toQString() const { return QString("(%1)").arg(r); }
00046     
00047             //opérateurs arithmétiques unaires :
00048             friend Float1 operator-(const Float1 &a);
00049         
00050             // opérateurs arithmétiques binaires (membres à membres) :
00051             friend Float1 operator+(const Float1 &a, const Float1 &b);
00052             friend Float1 operator-(const Float1 &a, const Float1 &b);
00053             friend Float1 operator*(const Float1 &a, const Float1 &b);
00054             friend Float1 operator/(const Float1 &a, const Float1 &b);
00055                 
00056             // opérations avec un scalaire :
00057             friend Float1 operator*(float s, const Float1 &a);
00058             friend Float1 operator*(const Float1 &a, float s);
00059             friend Float1 operator/(float s, const Float1 &a);
00060             friend Float1 operator/(const Float1 &a, float s);
00061             
00062             // opérateurs de modification :
00063             Float1 operator+=(const Float1 &a);
00064             Float1 operator-=(const Float1 &a);
00065             Float1 operator*=(const Float1 &a);
00066             Float1 operator/=(const Float1 &a);
00067             Float1 operator*=(float s);
00068             Float1 operator/=(float s);
00069     
00070             void reset(float r=0, float g=0, float b=0, float a=0);
00071             void setGL() const;
00072             
00073         public:
00074             union {
00075                 struct { float r; };
00076                 struct { float x; };
00077                 float c[1];
00078                 };
00079             static const int SIZE = 1;
00080             static const GLint  TEX_FORMAT = 1;  // = GL_LUMINANCE
00081             static const GLenum DATA_FORMAT = GL_LUMINANCE;
00082             static const GLenum DATA_TYPE = GL_FLOAT;
00083         };
00084     
00085     class Float2 {
00086         public:
00087             Float2(float x = 0, float y = 0);
00088             Float2(const float *c);   // tableau de 2 valeurs
00089             Float2(QRgb c);
00090         
00091             // opérateurs de conversion :
00092             inline operator const float* () const {return c;}     // cast du Float2 en const float*
00093             inline operator       float* ()       {return c;}     // cast du Float2 en float*
00094             QRgb toQRgb() const;
00095             QString toQString() const { return QString("(%1, %2)").arg(r).arg(g); }
00096     
00097             //opérateurs arithmétiques unaires :
00098             friend Float2 operator-(const Float2 &a);
00099         
00100             // opérateurs arithmétiques binaires (membres à membres) :
00101             friend Float2 operator+(const Float2 &a, const Float2 &b);
00102             friend Float2 operator-(const Float2 &a, const Float2 &b);
00103             friend Float2 operator*(const Float2 &a, const Float2 &b);
00104             friend Float2 operator/(const Float2 &a, const Float2 &b);
00105                 
00106             // opérations avec un scalaire :
00107             friend Float2 operator*(float s, const Float2 &a);
00108             friend Float2 operator*(const Float2 &a, float s);
00109             friend Float2 operator/(float s, const Float2 &a);
00110             friend Float2 operator/(const Float2 &a, float s);
00111             
00112             // opérateurs de modification :
00113             Float2 operator+=(const Float2 &a);
00114             Float2 operator-=(const Float2 &a);
00115             Float2 operator*=(const Float2 &a);
00116             Float2 operator/=(const Float2 &a);
00117             Float2 operator*=(float s);
00118             Float2 operator/=(float s);
00119     
00120             void reset(float r=0, float g=0, float b=0, float a=0);
00121             void setGL() const;
00122             
00123         public:
00124             union {
00125                 struct { float r, g; };
00126                 struct { float x, y; };
00127                 float c[2];
00128                 };
00129             static const int SIZE = 2;
00130             static const GLint  TEX_FORMAT = 2;  // = GL_LUMINANCE_ALPHA
00131             static const GLenum DATA_FORMAT = GL_LUMINANCE_ALPHA;
00132             static const GLenum DATA_TYPE = GL_FLOAT;
00133         };
00134     
00135     class Float3 {
00136         public:
00137             Float3(float x = 0, float y = 0, float z = 0);
00138             Float3(const float *c);   // tableau de 3 valeurs
00139             Float3(QRgb c);
00140         
00141             // opérateurs de conversion :
00142             inline operator const float* () const {return c;}     // cast du Float3 en const float*
00143             inline operator       float* ()       {return c;}     // cast du Float3 en float*
00144             QRgb toQRgb() const;
00145             QString toQString() const { return QString("(%1, %2, %3)").arg(r).arg(g).arg(b); }
00146     
00147             //opérateurs arithmétiques unaires :
00148             friend Float3 operator-(const Float3 &a);
00149         
00150             // opérateurs arithmétiques binaires (membres à membres) :
00151             friend Float3 operator+(const Float3 &a, const Float3 &b);
00152             friend Float3 operator-(const Float3 &a, const Float3 &b);
00153             friend Float3 operator*(const Float3 &a, const Float3 &b);
00154             friend Float3 operator/(const Float3 &a, const Float3 &b);
00155                 
00156             // opérations avec un scalaire :
00157             friend Float3 operator*(float s, const Float3 &a);
00158             friend Float3 operator*(const Float3 &a, float s);
00159             friend Float3 operator/(float s, const Float3 &a);
00160             friend Float3 operator/(const Float3 &a, float s);
00161             
00162             // opérateurs de modification :
00163             Float3 operator+=(const Float3 &a);
00164             Float3 operator-=(const Float3 &a);
00165             Float3 operator*=(const Float3 &a);
00166             Float3 operator/=(const Float3 &a);
00167             Float3 operator*=(float s);
00168             Float3 operator/=(float s);
00169     
00170             void reset(float r=0, float g=0, float b=0, float a=0);
00171             void setGL() const;
00172             
00173         public:
00174             union {
00175                 struct { float r, g, b; };
00176                 struct { float x, y, z; };
00177                 float c[3];
00178                 };
00179             static const int SIZE = 3;
00180             static const GLint  TEX_FORMAT = 3;  // = GL_RGB
00181             static const GLenum DATA_FORMAT = GL_RGB;
00182             static const GLenum DATA_TYPE = GL_FLOAT;
00183         };
00184     
00185     class Float4 {
00186         public:
00187             Float4(float x = 0, float y = 0, float z = 0, float w = 1);
00188             Float4(const float *c);   // tableau de 4 valeurs
00189             Float4(const float *xy, const float *zw);   // 2 tableaux de 2 valeurs
00190             Float4(QRgb c);
00191             
00192             // opérateurs de conversion :
00193             inline operator const float* () const {return c;}     // cast du Float4 en const float*
00194             inline operator       float* ()       {return c;}     // cast du Float4 en float*
00195             QRgb toQRgb() const;
00196             QString toQString() const { return QString("(%1, %2, %3, %4)").arg(r).arg(g).arg(b).arg(a); }
00197     
00198             //opérateurs arithmétiques unaires :
00199             friend Float4 operator-(const Float4 &a);
00200         
00201             // opérateurs arithmétiques binaires (membres à membres) :
00202             friend Float4 operator+(const Float4 &a, const Float4 &b);
00203             friend Float4 operator-(const Float4 &a, const Float4 &b);
00204             friend Float4 operator*(const Float4 &a, const Float4 &b);
00205             friend Float4 operator/(const Float4 &a, const Float4 &b);
00206                 
00207             // opérations avec un scalaire :
00208             friend Float4 operator*(float s, const Float4 &a);
00209             friend Float4 operator*(const Float4 &a, float s);
00210             friend Float4 operator/(float s, const Float4 &a);
00211             friend Float4 operator/(const Float4 &a, float s);
00212             
00213             // opérateurs de modification :
00214             Float4 operator+=(const Float4 &a);
00215             Float4 operator-=(const Float4 &a);
00216             Float4 operator*=(const Float4 &a);
00217             Float4 operator/=(const Float4 &a);
00218             Float4 operator*=(float s);
00219             Float4 operator/=(float s);
00220     
00221             void reset(float r=0, float g=0, float b=0, float a=0);
00222             void setAlpha(float a);
00223             void setGL() const;
00224             
00225         public:
00226             union {
00227                 struct { float r, g, b, a; };
00228                 struct { float x, y, z, w; };
00229                 float c[4];
00230                 };
00231             static const int SIZE = 4;
00232             static const GLint  TEX_FORMAT = 4;  // = GL_RGBA
00233             static const GLenum DATA_FORMAT = GL_RGBA;
00234             static const GLenum DATA_TYPE = GL_FLOAT;
00235         };
00236     
00237     class UByte3;
00238     
00239     class UInt3 {
00240         public:
00241             UInt3() : x(0), y(0), z(0) {}
00242             UInt3(GLuint x, GLuint y, GLuint z);
00243             UInt3(const GLuint *c);   // tableau de 3 valeurs
00244             UInt3(QRgb c);
00245         
00246             // opérateurs de conversion :
00247             inline operator const GLuint* () const {return c;}     // cast du UInt3 en const GLuint*
00248             inline operator       GLuint* ()       {return c;}     // cast du UInt3 en GLuint*
00249             QRgb toQRgb() const;
00250             QString toQString() const { return QString("(%1, %2, %3)").arg(r).arg(g).arg(b); }
00251     
00252             // opérateurs arithmétiques binaires (membres à membres) :
00253             friend UInt3 operator+(const UInt3 &a, const UInt3 &b);
00254             friend UInt3 operator-(const UInt3 &a, const UInt3 &b);
00255             friend UInt3 operator*(const UInt3 &a, const UInt3 &b);
00256             friend UInt3 operator/(const UInt3 &a, const UInt3 &b);
00257                 
00258             // opérations avec un scalaire :
00259             friend UInt3 operator*(float s, const UInt3 &a);
00260             friend UInt3 operator*(const UInt3 &a, float s);
00261             friend UInt3 operator/(float s, const UInt3 &a);
00262             friend UInt3 operator/(const UInt3 &a, float s);
00263             
00264             // opérateurs de modification :
00265             UInt3 operator+=(const UInt3 &a);
00266             UInt3 operator-=(const UInt3 &a);
00267             UInt3 operator*=(const UInt3 &a);
00268             UInt3 operator/=(const UInt3 &a);
00269             UInt3 operator*=(float s);
00270             UInt3 operator/=(float s);
00271     
00272             void reset(float r=0, float g=0, float b=0, float a=0);
00273             void setGL() const;
00274             
00275             UByte3 toUByte3() const;
00276             
00277         public:
00278             union {
00279                 struct { GLuint r, g, b; };
00280                 struct { GLuint x, y, z; };
00281                 GLuint c[3];
00282                 };
00283             static const int SIZE = 3;
00284             static const GLint  TEX_FORMAT = 3;  // = GL_RGB
00285             static const GLenum DATA_FORMAT = GL_RGB;
00286             static const GLenum DATA_TYPE = GL_UNSIGNED_INT;
00287         };
00288     
00289     class UByte4;
00290     
00291     class UInt4 {
00292         public:
00293             UInt4() : x(0), y(0), z(0), w(255) {}
00294             UInt4(GLuint x, GLuint y, GLuint z, GLuint w);
00295             UInt4(const GLuint *c);   // tableau de 4 valeurs
00296             UInt4(QRgb c);
00297         
00298             // opérateurs de conversion :
00299             inline operator const GLuint* () const {return c;}     // cast du UInt4 en const GLuint*
00300             inline operator       GLuint* ()       {return c;}     // cast du UInt4 en GLuint*
00301             QRgb toQRgb() const;
00302             QString toQString() const { return QString("(%1, %2, %3, %4)").arg(r).arg(g).arg(b).arg(a); }
00303     
00304             // opérateurs arithmétiques binaires (membres à membres) :
00305             friend UInt4 operator+(const UInt4 &a, const UInt4 &b);
00306             friend UInt4 operator-(const UInt4 &a, const UInt4 &b);
00307             friend UInt4 operator*(const UInt4 &a, const UInt4 &b);
00308             friend UInt4 operator/(const UInt4 &a, const UInt4 &b);
00309                 
00310             // opérations avec un scalaire :
00311             friend UInt4 operator*(float s, const UInt4 &a);
00312             friend UInt4 operator*(const UInt4 &a, float s);
00313             friend UInt4 operator/(float s, const UInt4 &a);
00314             friend UInt4 operator/(const UInt4 &a, float s);
00315             
00316             // opérateurs de modification :
00317             UInt4 operator+=(const UInt4 &a);
00318             UInt4 operator-=(const UInt4 &a);
00319             UInt4 operator*=(const UInt4 &a);
00320             UInt4 operator/=(const UInt4 &a);
00321             UInt4 operator*=(float s);
00322             UInt4 operator/=(float s);
00323     
00324             void reset(float r=0, float g=0, float b=0, float a=0);
00325             void setAlpha(float a);
00326             void setGL() const;
00327             
00328             UByte4 toUByte4() const;
00329             
00330         public:
00331             union {
00332                 struct { GLuint r, g, b, a; };
00333                 struct { GLuint x, y, z, w; };
00334                 GLuint c[4];
00335                 };
00336             static const int SIZE = 4;
00337             static const GLint  TEX_FORMAT = 4;  // = GL_RGBA
00338             static const GLenum DATA_FORMAT = GL_RGBA;
00339             static const GLenum DATA_TYPE = GL_UNSIGNED_INT;
00340         };
00341     
00342     class UByte3 {
00343         public:
00344             UByte3(GLubyte x = 0, GLubyte y = 0, GLubyte z = 0);
00345             UByte3(const GLubyte *c);   // tableau de 3 valeurs
00346             UByte3(QRgb c);
00347         
00348             // opérateurs de conversion :
00349             inline operator const GLubyte* () const {return c;}     // cast du UByte3 en const GLubyte*
00350             inline operator       GLubyte* ()       {return c;}     // cast du UByte3 en GLubyte*
00351             QRgb toQRgb() const;
00352             QString toQString() const { return QString("(%1, %2, %3)").arg(r).arg(g).arg(b); }
00353     
00354             // opérateurs arithmétiques binaires (membres à membres) :
00355             friend UByte3 operator+(const UByte3 &a, const UByte3 &b);
00356             friend UByte3 operator-(const UByte3 &a, const UByte3 &b);
00357             friend UByte3 operator*(const UByte3 &a, const UByte3 &b);
00358             friend UByte3 operator/(const UByte3 &a, const UByte3 &b);
00359                 
00360             // opérations avec un scalaire :
00361             friend UByte3 operator*(float s, const UByte3 &a);
00362             friend UByte3 operator*(const UByte3 &a, float s);
00363             friend UByte3 operator/(float s, const UByte3 &a);
00364             friend UByte3 operator/(const UByte3 &a, float s);
00365             
00366             // opérateurs de modification :
00367             UByte3 operator+=(const UByte3 &a);
00368             UByte3 operator-=(const UByte3 &a);
00369             UByte3 operator*=(const UByte3 &a);
00370             UByte3 operator/=(const UByte3 &a);
00371             UByte3 operator*=(float s);
00372             UByte3 operator/=(float s);
00373     
00374             void reset(float r=0, float g=0, float b=0, float a=0);
00375             void setGL() const;
00376             
00377             UInt3 toUInt3() const;
00378             
00379         public:
00380             union {
00381                 struct { GLubyte r, g, b; };
00382                 struct { GLubyte x, y, z; };
00383                 GLubyte c[3];
00384                 };
00385             static const int SIZE = 3;
00386             static const GLint  TEX_FORMAT = 3;  // = GL_RGB
00387             static const GLenum DATA_FORMAT = GL_RGB;
00388             static const GLenum DATA_TYPE = GL_UNSIGNED_BYTE;
00389         };
00390     
00391     class UByte4 {
00392         public:
00393             UByte4(GLubyte x = 0, GLubyte y = 0, GLubyte z = 0, GLubyte w = 255);
00394             UByte4(const GLubyte *c);   // tableau de 4 valeurs
00395             UByte4(QRgb c);
00396         
00397             // opérateurs de conversion :
00398             inline operator const GLubyte* () const {return c;}     // cast du UByte4 en const GLubyte*
00399             inline operator       GLubyte* ()       {return c;}     // cast du UByte4 en GLubyte*
00400             QRgb toQRgb() const;
00401             QString toQString() const { return QString("(%1, %2, %3, %4)").arg(r).arg(g).arg(b).arg(a); }
00402     
00403             // opérateurs arithmétiques binaires (membres à membres) :
00404             friend UByte4 operator+(const UByte4 &a, const UByte4 &b);
00405             friend UByte4 operator-(const UByte4 &a, const UByte4 &b);
00406             friend UByte4 operator*(const UByte4 &a, const UByte4 &b);
00407             friend UByte4 operator/(const UByte4 &a, const UByte4 &b);
00408                 
00409             // opérations avec un scalaire :
00410             friend UByte4 operator*(float s, const UByte4 &a);
00411             friend UByte4 operator*(const UByte4 &a, float s);
00412             friend UByte4 operator/(float s, const UByte4 &a);
00413             friend UByte4 operator/(const UByte4 &a, float s);
00414             
00415             // opérateurs de modification :
00416             UByte4 operator+=(const UByte4 &a);
00417             UByte4 operator-=(const UByte4 &a);
00418             UByte4 operator*=(const UByte4 &a);
00419             UByte4 operator/=(const UByte4 &a);
00420             UByte4 operator*=(float s);
00421             UByte4 operator/=(float s);
00422     
00423             void reset(float r=0, float g=0, float b=0, float a=0);
00424             void setAlpha(float a);
00425             void setGL() const;
00426             
00427             UInt4 toUInt4() const;
00428             
00429         public:
00430             union {
00431                 struct { GLubyte r, g, b, a; };
00432                 struct { GLubyte x, y, z, w; };
00433                 GLubyte c[4];
00434                 };
00435             static const int SIZE = 4;
00436             static const GLint  TEX_FORMAT = 4;  // = GL_RGBA
00437             static const GLenum DATA_FORMAT = GL_RGBA;
00438             static const GLenum DATA_TYPE = GL_UNSIGNED_BYTE;
00439         };
00440     }
00441 
00442 #endif
00443 

Generated on Fri Nov 14 20:49:47 2008 for Api Graphics by  doxygen 1.4.4