00001 #ifndef _UT_Color_
00002 #define _UT_Color_
00003
00004 #include "UT_API.h"
00005 #include <iostream.h>
00006 #include <SYS/SYS_Types.h>
00007
00008 class UT_IStream;
00009 class UT_JSONWriter;
00010 class UT_JSONParser;
00011 class UT_JSONValue;
00012 class UT_Ramp;
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 enum UT_ColorType {
00024 UT_HSL, UT_HSV, UT_RGB, UT_XYZ, UT_LAB
00025 };
00026
00027
00028 UT_API extern const char * UTnameFromColorType(UT_ColorType ctype);
00029 UT_API extern UT_ColorType UTcolorTypeFromName(const char *cname);
00030
00031
00032 enum UT_ColorRamp {
00033 UT_COLORRAMP_FALSE,
00034 UT_COLORRAMP_PINK,
00035 UT_COLORRAMP_MONO,
00036 UT_COLORRAMP_BLACKBODY,
00037 UT_COLORRAMP_BIPARTITE,
00038 UT_COLORRAMP_NUMMODES
00039 };
00040
00041 enum UT_MonoRamp {
00042 UT_MONORAMP_INCREASING,
00043 UT_MONORAMP_DECREASING,
00044 UT_MONORAMP_HILL,
00045 UT_MONORAMP_VALLEY,
00046 UT_MONORAMP_STEP,
00047 UT_MONORAMP_SQUARE,
00048 UT_MONORAMP_NUMMODES
00049 };
00050
00051 class UT_Color;
00052 #include "UT_VectorTypes.h"
00053
00054
00055
00056
00057 UT_API extern void UT_RGBtoHSV(fpreal *values, int nvalues);
00058 UT_API extern void UT_HSVtoRGB(fpreal *values, int nvalues);
00059
00060 #define UT_BLACK (UT_Color::getConstantColor(0))
00061 #define UT_BLUE (UT_Color::getConstantColor(1))
00062 #define UT_GREEN (UT_Color::getConstantColor(2))
00063 #define UT_RED (UT_Color::getConstantColor(3))
00064 #define UT_CYAN (UT_Color::getConstantColor(4))
00065 #define UT_MAGENTA (UT_Color::getConstantColor(5))
00066 #define UT_YELLOW (UT_Color::getConstantColor(6))
00067 #define UT_WHITE (UT_Color::getConstantColor(7))
00068 #define UT_GREY1 (UT_Color::getConstantColor(8))
00069 #define UT_GREY2 (UT_Color::getConstantColor(9))
00070 #define UT_GREY3 (UT_Color::getConstantColor(10))
00071 #define UT_GREY4 (UT_Color::getConstantColor(11))
00072 #define UT_GREY5 (UT_Color::getConstantColor(12))
00073 #define UT_GREY6 (UT_Color::getConstantColor(13))
00074 #define UT_GREY7 (UT_Color::getConstantColor(14))
00075 #define UT_GREY8 (UT_Color::getConstantColor(15))
00076 #define UT_GREY9 (UT_Color::getConstantColor(16))
00077
00078
00079
00080
00081
00082 class UT_API UT_Color {
00083 public:
00084 UT_Color();
00085 UT_Color(UT_ColorType t);
00086 UT_Color(UT_ColorType t, fpreal x, fpreal y, fpreal z);
00087 UT_Color(const UT_Color &c);
00088 UT_Color(const char *colorname);
00089
00090 ~UT_Color();
00091
00092 UT_ColorType getType() const { return type; }
00093
00094 void getValue(float &x, float &y, float &z) const
00095 { x = u; y = v; z = w;}
00096 void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
00097 { x = fpreal64(u); y = fpreal64(v); z = fpreal64(w);}
00098 void getRGB(float *r, float *g, float *b) const
00099 {
00100
00101 if(type == UT_RGB) { *r = u; *g=v; *b = w; }
00102 else getRGBConvert(r,g,b);
00103 }
00104
00105 void getHSV(float *h, float *s, float *v) const;
00106 void getHSL(float *h, float *s, float *l) const;
00107 void getXYZ(float *x, float *y, float *z) const;
00108 void getLAB(float *l, float *a, float *b) const;
00109 void getColor(UT_Color &c) const { c = *this; }
00110
00111
00112
00113
00114 fpreal luminance() const;
00115
00116
00117
00118 fpreal relativeLuminance() const;
00119
00120 void setType(UT_ColorType t);
00121 void setValue(fpreal x, fpreal y, fpreal z) { u = x; v = y; w = z; }
00122 void setRGB(fpreal r, fpreal g, fpreal b);
00123 void setHSV(fpreal h, fpreal s, fpreal v);
00124 void setHSL(fpreal h, fpreal s, fpreal l);
00125 void setXYZ(fpreal x, fpreal y, fpreal z);
00126 void setLAB(fpreal l, fpreal a, fpreal b);
00127 void setLABluminance(fpreal l);
00128
00129 void setColorByName(const char *colorname);
00130
00131 void clampToLegal();
00132
00133 void ghost( fpreal gfactor = 0.75f );
00134
00135 void operator=(const UT_Color &c);
00136
00137 int operator==(const UT_Color &color) const;
00138 int operator!=(const UT_Color &c) const { return !(*this == c); }
00139 bool operator<(const UT_Color &b) const;
00140
00141 uint32 hash() const;
00142
00143 void brighten(fpreal amount);
00144 void tintRGB(const UT_Color &clr);
00145 void addHSV(const UT_Color &clr);
00146 void addRGB(const UT_Color &clr);
00147 void subRGB(const UT_Color &clr);
00148
00149
00150 void blendRGB(const UT_Color &clr, fpreal bias=0.5);
00151
00152
00153 int save(ostream &os, int binary) const;
00154 bool load(UT_IStream &is);
00155
00156
00157
00158
00159
00160
00161
00162 bool save(UT_JSONWriter &w) const;
00163 bool save(UT_JSONValue &v) const;
00164 bool load(UT_JSONParser &p);
00165
00166
00167 const char *className() const;
00168
00169
00170 static void getRampColor(UT_ColorRamp ramp, fpreal val,
00171 float *r, float *g, float *b);
00172
00173
00174 static const char *getRampColorName(UT_ColorRamp ramp);
00175
00176
00177 static UT_Ramp *buildRamp(UT_ColorRamp ramp);
00178
00179 static UT_Ramp *buildMonoRamp(UT_MonoRamp ramp);
00180
00181
00182
00183
00184 static void getUniqueColor(int64 idx,
00185 float *r, float *g, float *b);
00186
00187
00188 static const UT_Color &getConstantColor(int i);
00189
00190
00191
00192 static void getRandomContrastingColor( const UT_Vector3 * contrast_to,
00193 unsigned color_index, UT_Vector3 &color );
00194
00195 protected:
00196
00197
00198
00199
00200
00201 static void assignRandomLuminance( UT_Vector3 &color );
00202
00203
00204 protected:
00205 UT_ColorType type;
00206 float u;
00207 float v;
00208 float w;
00209 void convertToType(UT_ColorType type);
00210 void convertToHSL();
00211 void convertToHSV();
00212 void convertToRGB();
00213 void convertToXYZ();
00214 void convertToLAB();
00215
00216 void getRGBConvert(float *r, float *g, float *b) const;
00217
00218 private:
00219
00220
00221 friend UT_API ostream &operator<<(ostream &os, const UT_Color &color);
00222 };
00223
00224 #endif