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

Generated on Thu Jul 8 15:19:28 2004 for inspace by doxygen 1.3.4