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
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 enum UT_ColorType {
00020 UT_HSL, UT_HSV, UT_RGB, UT_XYZ, UT_LAB
00021 };
00022
00023 enum UT_ColorRamp {
00024 UT_COLORRAMP_FALSE,
00025 UT_COLORRAMP_PINK,
00026 UT_COLORRAMP_MONO,
00027 UT_COLORRAMP_BLACKBODY,
00028 UT_COLORRAMP_NUMMODES
00029 };
00030
00031 class UT_Color;
00032 class UT_Vector3;
00033
00034
00035
00036
00037 UT_API extern void UT_RGBtoHSV(float *values, int nvalues);
00038 UT_API extern void UT_HSVtoRGB(float *values, int nvalues);
00039
00040 #define UT_BLACK (UT_Color::getConstantColor(0))
00041 #define UT_BLUE (UT_Color::getConstantColor(1))
00042 #define UT_GREEN (UT_Color::getConstantColor(2))
00043 #define UT_RED (UT_Color::getConstantColor(3))
00044 #define UT_CYAN (UT_Color::getConstantColor(4))
00045 #define UT_MAGENTA (UT_Color::getConstantColor(5))
00046 #define UT_YELLOW (UT_Color::getConstantColor(6))
00047 #define UT_WHITE (UT_Color::getConstantColor(7))
00048 #define UT_GREY1 (UT_Color::getConstantColor(8))
00049 #define UT_GREY2 (UT_Color::getConstantColor(9))
00050 #define UT_GREY3 (UT_Color::getConstantColor(10))
00051 #define UT_GREY4 (UT_Color::getConstantColor(11))
00052 #define UT_GREY5 (UT_Color::getConstantColor(12))
00053 #define UT_GREY6 (UT_Color::getConstantColor(13))
00054 #define UT_GREY7 (UT_Color::getConstantColor(14))
00055 #define UT_GREY8 (UT_Color::getConstantColor(15))
00056 #define UT_GREY9 (UT_Color::getConstantColor(16))
00057
00058
00059
00060
00061
00062 class UT_API UT_Color {
00063 public:
00064 UT_Color();
00065 UT_Color(UT_ColorType t);
00066 UT_Color(UT_ColorType t, fpreal x, fpreal y, fpreal z);
00067 UT_Color(const UT_Color &c);
00068 UT_Color(const char *colorname);
00069
00070 virtual ~UT_Color();
00071
00072 UT_ColorType getType() const { return type; }
00073
00074 void getValue(float &x, float &y, float &z) const
00075 { x = u; y = v; z = w;}
00076 void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
00077 { x = fpreal64(u); y = fpreal64(v); z = fpreal64(w);}
00078 virtual void getRGB(float *r, float *g, float *b) const
00079 {
00080
00081 if(type == UT_RGB) { *r = u; *g=v; *b = w; }
00082 else getRGBConvert(r,g,b);
00083 }
00084
00085 virtual void getHSV(float *h, float *s, float *v) const;
00086 virtual void getHSL(float *h, float *s, float *l) const;
00087 virtual void getXYZ(float *x, float *y, float *z) const;
00088 virtual void getLAB(float *l, float *a, float *b) const;
00089 void getColor(UT_Color &c) const { c = *this; }
00090
00091
00092
00093
00094 fpreal luminance() const;
00095
00096
00097
00098 fpreal relativeLuminance() const;
00099
00100 void setType(UT_ColorType t);
00101 void setValue(fpreal x, fpreal y, fpreal z) { u = x; v = y; w = z; }
00102 virtual void setRGB(fpreal r, fpreal g, fpreal b);
00103 virtual void setHSV(fpreal h, fpreal s, fpreal v);
00104 virtual void setHSL(fpreal h, fpreal s, fpreal l);
00105 virtual void setXYZ(fpreal x, fpreal y, fpreal z);
00106 virtual void setLAB(fpreal l, fpreal a, fpreal b);
00107 virtual void setLABluminance(fpreal l);
00108
00109 virtual void setColorByName(const char *colorname);
00110
00111 void clampToLegal();
00112
00113 void ghost( fpreal gfactor = 0.75f );
00114
00115 void operator=(const UT_Color &c);
00116
00117 int operator==(const UT_Color &color) const;
00118 uint32 hash() const;
00119
00120 void brighten(fpreal amount);
00121 void tintRGB(const UT_Color &clr);
00122 void addHSV(const UT_Color &clr);
00123 void addRGB(const UT_Color &clr);
00124 void subRGB(const UT_Color &clr);
00125
00126
00127 void blendRGB(const UT_Color &clr, fpreal bias=0.5);
00128
00129
00130 int save(ostream &os, int binary) const;
00131 bool load(UT_IStream &is);
00132
00133 const char *className() const;
00134
00135
00136 static void getRampColor(UT_ColorRamp ramp, fpreal val,
00137 float *r, float *g, float *b);
00138
00139
00140
00141
00142 static void getUniqueColor(int64 idx,
00143 float *r, float *g, float *b);
00144
00145
00146 static const UT_Color &getConstantColor(int i);
00147
00148
00149
00150 static void getRandomContrastingColor( const UT_Vector3 * contrast_to,
00151 unsigned color_index, UT_Vector3 &color );
00152
00153 protected:
00154
00155
00156
00157
00158
00159 static void assignRandomLuminance( UT_Vector3 &color );
00160
00161
00162 protected:
00163 UT_ColorType type;
00164 float u;
00165 float v;
00166 float w;
00167 void convertToType(UT_ColorType type);
00168 void convertToHSL();
00169 void convertToHSV();
00170 void convertToRGB();
00171 void convertToXYZ();
00172 void convertToLAB();
00173
00174 void getRGBConvert(float *r, float *g, float *b) const;
00175
00176 private:
00177
00178
00179 friend UT_API ostream &operator<<(ostream &os, const UT_Color &color);
00180 };
00181
00182 #endif