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
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
00093
00094
00095
00096
00097
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
00140
00141
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
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
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 }
00214
00215
00216 #endif
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
00252
00253
00254
00255
00256
00257
00258