Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Color.H

Go to the documentation of this file.
00001 
00010 #ifndef COLOR_H
00011 #define COLOR_H
00012 
00013 // Gluebase includes
00014 #define OUTSIDE_GLUE_CORE
00015 #include <std/strings.H>
00016 #include <math.h>
00017 
00019 #define MIN_L 0.0
00020 #define MAX_L 100.0
00021 #define MIN_A -86.18717193603515625
00022 #define MAX_A 98.2308502197265625
00023 #define MIN_B -107.85298919677734375
00024 #define MAX_B 94.485137939453125
00025 #define MIN_SATURATION 0.0
00026 #define MAX_SATURATION 15.22387409210205078125 // recomputed using indexed values for chroma
00027 #define MIN_CHROMA 0.0
00028 #define MAX_CHROMA 133.8037872314453125
00029 
00031 #define INITIAL_INTERVAL 4.0
00032 
00033 #define INTERVAL_FACTOR 8.0
00034 #define MIN_INTERVAL 0.0078125
00035 
00036 #define MIN_INTERVAL_FAILURE 0.25
00037 
00038 class Color {
00039 public:
00040   
00041   Color() {
00042     for (int i=0;i<4;i++)
00043       _col[i] = 1.0;
00044   };
00045 
00046   Color(const Color& c){
00047     _col[0]=c[0];
00048     _col[1]=c[1];
00049     _col[2]=c[2];
00050     _col[3]=c[3];
00051   };
00052 
00053   Color(const float r,const float g,const float b,const float a=1.0) {
00054     _col[0] = r;
00055     _col[1] = g;
00056     _col[2] = b;
00057     _col[3] = a;
00058   };
00059   
00060   Color(str_ptr colstr) {
00061     for (int i=0;i<4;i++)
00062       _col[i] = 1.0;
00063     set(colstr);
00064   };
00065   
00066   virtual ~Color() {};
00067 
00068   void set(const float r,const float g,const float b,const float a=1.0) {
00069     _col[0] = r;
00070     _col[1] = g;
00071     _col[2] = b;
00072     _col[3] = a;
00073   };
00074 
00076   void set(str_ptr colstr);
00077 
00078   float r() const { return _col[0]; };
00079   float g() const { return _col[1]; };
00080   float b() const { return _col[2]; };
00081   float a() const { return _col[3]; };
00082 
00084   void setHLS(const float h, const float l, const float s, const float a=1.0);
00085   
00087   void getHLS(float &h, float &l, float &s);
00088   
00089   /*
00090     CIELAB used with D65 whitepoint
00091     L measures lightness that ranges from 0 (black) to 100 (white).
00092     a defines redness (positive a) or greenness (negative a) -- ranges from MIN_A to MAX_A
00093     b defines yellowness (positive b) or blueness (negative b) -- ranges from MIN_B to MAX_B
00094     a and b approach zero for neutral colors
00095     NOTE: not all valid LAB coordinates translate to a valid RGB value--all functions assume valid LAB values
00096   */
00097   
00099   bool setLAB(const float L,const float a,const float b, const float alpha=1.0);
00100   
00102   void getLAB(float &L, float &a, float &b);
00103   
00105   static float getLABChroma(const float L, const float a, const float b);
00106 
00108   static float getLABSaturation(const float L, const float a, const float b);
00109   
00111   static float getLABHue(const float L, const float a, const float b);
00112   
00114   static void setLABLightnessChromaHue(const float L, float &a, float &b, const float chroma, const float hue);
00115   static void setLABChromaSaturationHue(float &L, float &a, float &b, const float chroma, const float saturation, const float hue);
00116   static void setLABLightnessSaturationHue(const float L, float &a, float &b, const float saturation, const float hue);
00117   static void setLABSaturationAB(float &L, const float a, const float b, const float saturation);
00118   
00120   static float getLABColorDifference(const float L1, const float a1, const float b1, const float L2, const float a2, const float b2);
00121   
00123   void getXYZ(float &X, float &Y, float &Z);
00124   
00126   void LABtoXYZ(const float L, const float A, const float B, float &X, float &Y, float &Z);
00127   
00129   static bool findMaxL_LAB(float &L, const float a, const float b, const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00130   static bool findMinL_LAB(float &L, const float a, const float b, const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00131   static bool findMaxA_LAB(const float L, float &a, const float b, const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00132   static bool findMinA_LAB(const float L, float &a, const float b, const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00133   static bool findMaxB_LAB(const float L, const float a, float &b, const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00134   static bool findMinB_LAB(const float L, const float a, float &b, const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00135   
00136   /*
00137     the following functions find max/min values with two variable values and one constant value
00138     for instance, findMaxL_LA_LAB() finds the maximum value of 'L' with a constant 'b' value
00139     maximum 'L' and corresponding 'a' returned
00140   */
00141   static bool findMaxL_LA_LAB(float &L, float &a, const float b,const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00142   static bool findMinL_LA_LAB(float &L, float &a, const float b,const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00143   static bool findMaxA_LA_LAB(float &L, float &a, const float b,const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00144   static bool findMinA_LA_LAB(float &L, float &a, const float b,const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00145   static bool findMaxL_LB_LAB(float &L, const float a, float &b,const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00146   static bool findMinL_LB_LAB(float &L, const float a, float &b,const float minL = MIN_L, const float maxL = MAX_L, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00147   static bool findMaxB_LB_LAB(float &L, const float a, float &b,const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00148   static bool findMinB_LB_LAB(float &L, const float a, float &b,const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00149   static bool findMaxA_AB_LAB(const float L, float &a, float &b,const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00150   static bool findMinA_AB_LAB(const float L, float &a, float &b,const float minA = MIN_A, const float maxA = MAX_A, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00151   static bool findMaxB_AB_LAB(const float L, float &a, float &b,const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00152   static bool findMinB_AB_LAB(const float L, float &a, float &b,const float minB = MIN_B, const float maxB = MAX_B, const float interval = INITIAL_INTERVAL, const bool recursiveSuccess = false);
00153   
00154   float* array() { return _col; };
00155   
00156   // color constants
00157   static const Color     black;
00158   static const Color     white;
00159   static const Color      grey;
00160   static const Color       red;
00161   static const Color     green;
00162   static const Color    yellow;
00163   static const Color      blue;
00164   static const Color   magenta;
00165   static const Color      cyan;
00166   static const Color      pink;
00167   static const Color     brown;
00168   static const Color       tan;
00169   static const Color    orange;
00170   static const Color firebrick;
00171 
00172   // matrices for converting to and from XYZ
00173   static const double RGBtoXYZarray[3][3];
00174   static const double XYZtoRGBarray[3][3];
00175 
00176 
00177   bool operator ==(const Color &c) const { 
00178     return (_col[0]==c._col[0] && _col[1]==c._col[1] && 
00179         _col[2]==c._col[2] && _col[3]==c._col[3]);
00180   }
00181   float  operator [](const int index)  const { return _col[index]; }
00182   float& operator [](const int index)        { return _col[index]; }
00183 
00184  protected:
00185   
00186   float _col[4];
00187 
00188   
00190   static double value(double n1, double n2, double hue);
00191 
00193   static inline double Labf(const double ratio);
00194   static inline double Labfinv(const double ratio);
00195 
00196 
00197 };
00198 
00199 inline ostream &
00200 operator <<(ostream &os, const Color &c)
00201 { 
00202   return os << c[0] << " " << c[1] << " " << c[2] << " " << c[3];
00203 }
00204 
00205 inline istream &
00206 operator >>(istream &is, Color &c)
00207 {
00208   return is >> c[0] >> c[1] >> c[2] >> c[3];
00209 }
00210 
00211 #endif
00212 
00213 /*
00214 values attained by cycling through rgb values via floats, for more precision consider using doubles
00215 The RGB interval was: 0.001953125
00216 Min L: 0 Max L: 100
00217   Minimum: 
00218       R: 0 G: 0 B: 0
00219       L: 0 a: 0 b: 0
00220   Maximum 
00221       R: 1 G: 1 B: 1
00222       L: 100 a: -0.007732189260423183441162109375 b: 0.002583012916147708892822265625
00223 Min A: -86.18717193603515625 Max A: 98.2308502197265625
00224   Minimum: 
00225       R: 0 G: 1 B: 0
00226       L: 87.7355194091796875 a: -86.18717193603515625 b: 83.1878509521484375
00227   Maximum 
00228       R: 1 G: 0 B: 1
00229       L: 60.32273101806640625 a: 98.2308502197265625 b: -60.82642364501953125
00230 Min B: -107.85298919677734375 Max B: 94.485137939453125
00231   Minimum: 
00232       R: 0 G: 0 B: 1
00233       L: 32.300872802734375 a: 79.190826416015625 b: -107.85298919677734375
00234   Maximum 
00235       R: 1 G: 1 B: 0
00236       L: 97.13855743408203125 a: -21.5671787261962890625 b: 94.485137939453125
00237 Min Chroma: 0 Max Chroma: 133.8037872314453125
00238   Minimum: 
00239       R: 0 G: 0 B: 0
00240       L: 0 a: 0 b: 0
00241   Maximum 
00242       R: 0 G: 0 B: 1
00243       L: 32.300872802734375 a: 79.190826416015625 b: -107.85298919677734375
00244 Min Saturation: 0 Max Saturation: 20.370121002197265625
00245   Minimum: 
00246       R: 0 G: 0 B: 0
00247       L: 0 a: 0 b: 0
00248   Maximum 
00249       R: 0 G: 0 B: 0.005859375
00250       L: 0.382094204425811767578125 a: 2.6848618984222412109375 b: -7.305570125579833984375
00251 */

Generated on Mon Sep 15 16:27:55 2003 for inspace by doxygen1.2.18