00001
00010 #ifndef COLOR_H
00011 #define COLOR_H
00012
00013
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
00091
00092
00093
00094
00095
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
00138
00139
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
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
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
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251