HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Color.h
Go to the documentation of this file.
1 #ifndef _UT_Color_
2 #define _UT_Color_
3 
4 #include "UT_API.h"
5 #include <SYS/SYS_Types.h>
6 #include <iosfwd>
7 
8 class UT_IStream;
9 class UT_JSONWriter;
10 class UT_JSONParser;
11 class UT_JSONValue;
12 class UT_Ramp;
13 
14 //
15 // Each color space has its own range for each of the three parameters.
16 //
17 // RGB: 0..1, 0..1, 0..1
18 // HSL: 0..360, 0..1, 0..1
19 // HSV: 0..360, 0..1, 0..1
20 // XYZ: 0..1, 0..1, 0..1 work well
21 // LAB: 0..100, a/b are unbounded but TIFF uses [-128, 127]
22 // TMI: -1..1, -1..1, -1..1
23 
26 };
27 
28 // Conversion of UT_ColorType values to/from string for saving/loading.
29 UT_API extern const char * UTnameFromColorType(UT_ColorType ctype);
30 UT_API extern UT_ColorType UTcolorTypeFromName(const char *cname);
31 
33  UT_COLORRAMP_FALSE, // False colour
34  UT_COLORRAMP_PINK, // White-red
35  UT_COLORRAMP_MONO, // Black-white
36  UT_COLORRAMP_BLACKBODY, // Black-red-yellow-white
37  UT_COLORRAMP_BIPARTITE, // Cyan-blue then Red-yellow
38  UT_COLORRAMP_NUMMODES // Illegal value.
39 };
40 
43  UT_MONORAMP_DECREASING, // Backslash
44  UT_MONORAMP_HILL, // slash backslash
45  UT_MONORAMP_VALLEY, // Backslash slash
46  UT_MONORAMP_STEP, // Underscore Overbar
47  UT_MONORAMP_SQUARE, // Pipe Overbar Pipe
48  UT_MONORAMP_NUMMODES // Illegal value.
49 };
50 
51 class UT_Color;
52 #include "UT_VectorTypes.h"
53 
54 //
55 // Filter functions useable with UI_Vector class:
56 //
57 UT_API extern void UT_RGBtoHSV(fpreal *values, int nvalues);
58 UT_API extern void UT_HSVtoRGB(fpreal *values, int nvalues);
59 
60 UT_API extern void UT_RGBtoTMI(fpreal *values, int nvalues);
61 UT_API extern void UT_TMItoRGB(fpreal *values, int nvalues);
62 
63 #define UT_BLACK (UT_Color::getConstantColor(0))
64 #define UT_BLUE (UT_Color::getConstantColor(1))
65 #define UT_GREEN (UT_Color::getConstantColor(2))
66 #define UT_RED (UT_Color::getConstantColor(3))
67 #define UT_CYAN (UT_Color::getConstantColor(4))
68 #define UT_MAGENTA (UT_Color::getConstantColor(5))
69 #define UT_YELLOW (UT_Color::getConstantColor(6))
70 #define UT_WHITE (UT_Color::getConstantColor(7))
71 #define UT_GREY1 (UT_Color::getConstantColor(8))
72 #define UT_GREY2 (UT_Color::getConstantColor(9))
73 #define UT_GREY3 (UT_Color::getConstantColor(10))
74 #define UT_GREY4 (UT_Color::getConstantColor(11))
75 #define UT_GREY5 (UT_Color::getConstantColor(12))
76 #define UT_GREY6 (UT_Color::getConstantColor(13))
77 #define UT_GREY7 (UT_Color::getConstantColor(14))
78 #define UT_GREY8 (UT_Color::getConstantColor(15))
79 #define UT_GREY9 (UT_Color::getConstantColor(16))
80 
81 /*
82  * UT_Color is the base class for colors in one of the above color spaces
83  */
84 
86 public:
87  UT_Color();
90  UT_Color(const UT_Color &c);
91  explicit UT_Color(const char *colorname);
92 
93  ~UT_Color();
94 
95  UT_ColorType getType() const { return type; }
96 
97  void getValue(float &x, float &y, float &z) const
98  { x = u; y = v; z = w;}
99  void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
100  { x = fpreal64(u); y = fpreal64(v); z = fpreal64(w);}
101  void getRGB(float *r, float *g, float *b) const
102  {
103  // Small optimization since we work in RGB most times.
104  if(type == UT_RGB) { *r = u; *g=v; *b = w; }
105  else getRGBConvert(r,g,b);
106  }
107 
108  void getHSV(float *h, float *s, float *v) const;
109  void getHSL(float *h, float *s, float *l) const;
110  void getXYZ(float *x, float *y, float *z) const;
111  void getLAB(float *l, float *a, float *b) const;
112  void getTMI(float *t, float *m, float *i) const;
113  void getColor(UT_Color &c) const { c = *this; }
114 
115  UT_Vector3F rgb() const;
116  void setRGB(UT_Vector3F c);
117  void setRGB(UT_Vector3D c);
118 
119  /// luminance() actually returns "luma", an approximation of luminance
120  /// derived from the gamma corrected rgb values. So if you have linear
121  /// rgb values and call this function, you will get the wrong result.
122  fpreal luminance() const;
123 
124  /// relativeLuminance() returns relative luminance [0,1] assuming the color
125  /// is in linear RGB.
126  fpreal relativeLuminance() const;
127 
128  void setType(UT_ColorType t);
129  void setValue(fpreal x, fpreal y, fpreal z) { u = x; v = y; w = z; }
130  void setRGB(fpreal r, fpreal g, fpreal b);
131  void setHSV(fpreal h, fpreal s, fpreal v);
132  void setHSL(fpreal h, fpreal s, fpreal l);
133  void setXYZ(fpreal x, fpreal y, fpreal z);
134  void setLAB(fpreal l, fpreal a, fpreal b);
135  void setLABluminance(fpreal l);
136  void setTMI(fpreal t, fpreal m, fpreal i);
137  void setUnique(int64 idx);
138 
139  void setColorByName(const char *colorname);
140 
141  void clampToLegal();
142 
143  void ghost(fpreal gfactor = 0.75f);
144  UT_Color copyGhosted( fpreal gfactor = 0.75f ) const;
145 
146  void operator=(const UT_Color &c);
147 
148  int operator==(const UT_Color &color) const;
149  int operator!=(const UT_Color &c) const { return !(*this == c); }
150  bool operator<(const UT_Color &b) const;
151 
152  uint32 hash() const;
153 
154  void brighten(fpreal amount); // *this *= clr
155  void tintRGB(const UT_Color &clr); // *this *= clr;
156  void addHSV(const UT_Color &clr); // *this += clr
157  void addRGB(const UT_Color &clr); // *this += clr
158  void subRGB(const UT_Color &clr); // *this -= clr
159 
160  UT_Color copyWithGamma( fpreal gamma ) const;
161  void gamma( fpreal gamma );
162 
163  // *this = lerp(*this, clr, bias) = (1-bias)*this + bias*clr
164  void blendRGB(const UT_Color &clr, fpreal bias=0.5);
165 
166  // Class I/O methods. Return 0 if successful, -1 if not
167  int save(std::ostream &os, int binary) const;
168  void saveMinimalAsText(std::ostream &os) const;
169  bool load(UT_IStream &is);
170 
171  /// @{
172  /// Methods to serialize to a JSON stream. The color is stored as a map:
173  /// @code
174  /// "type" : "color space",
175  /// "data" : [ x, y, z, ... ]
176  /// @endcode
177  bool save(UT_JSONWriter &w) const;
178  bool save(UT_JSONValue &v) const;
179  bool load(UT_JSONParser &p);
180  /// @}
181 
182  const char *className() const { return "UT_Color"; }
183 
184  /// Computes a ramp colour from the predefined ramp enums.
185  static void getRampColor(UT_ColorRamp ramp, fpreal val,
186  float *r, float *g, float *b);
187 
188  /// Return the name of the ramp colour.
189  static const char *getRampColorName(UT_ColorRamp ramp);
190 
191  /// Builds a UT_Ramp to correspond to the given internal ramp.
192  static UT_Ramp *buildRamp(UT_ColorRamp ramp);
193 
194  static UT_Ramp *buildMonoRamp(UT_MonoRamp ramp);
195 
196  /// A mapping from integers to colours that tries to ensure
197  /// that the chosen colours are well separated and aesthetically
198  /// pleasing.
199  static void getUniqueColor(int64 idx,
200  float *r, float *g, float *b);
201 
202  // Returns the standard color at index i.
203  static const UT_Color &getConstantColor(int i);
204 
205  static UT_Color getColorTemperature(fpreal temp);
206 
207  /// Obtain a random color that is contrasting to the specified other color.
208  /// If null is passed for other color, a random color is returned.
209  static void getRandomContrastingColor( const UT_Vector3 * contrast_to,
210  unsigned color_index, UT_Vector3 &color );
211 
212 protected:
213 
214  // modifies the color with a random luminance
215  // (rand_num is a uniformly distributed random number between 0 and 1)
216  // NB: - we can only handle colors with at least 1 zero component
217  // - black colors are un-modified
218  static void assignRandomLuminance( UT_Vector3 &color );
219 
220 
221 protected:
223  float u;
224  float v;
225  float w;
226  void convertToType(UT_ColorType type);
227  void convertToHSL();
228  void convertToHSV();
229  void convertToRGB();
230  void convertToXYZ();
231  void convertToLAB();
232  void convertToTMI();
233 
234  void getRGBConvert(float *r, float *g, float *b) const;
235 
236 private:
237  //To allow proper and safe saving for a UT_Color in UT_OptionFile, we
238  //override these operators. They simply call save/load respectively.
239  friend UT_API std::ostream &operator<<(std::ostream &os, const UT_Color &color);
240 };
241 
242 // Overload for custom formatting of UT_Color with UTformat.
243 UT_API extern size_t format(char *buffer, size_t buffer_size, const UT_Color &v);
244 
245 // Modifies the given ramp to contain the blackbody spectrum ranging from 0 to
246 // maxtemp. The ramp will have N equidistant control points (with linear
247 // interpolation). Adaptation and burn arguments correspond to the parameters
248 // of the same name on the Pyro Blackbody VOP, and are only used when
249 // tonemapping is enabled.
250 UT_API extern void UTinitPhysicalBBRamp(UT_Ramp& ramp, float adaptation = 0.15,
251  float burn = 0, float maxtemp = 40000,
252  int N = 64, bool tonemap = true);
253 
254 #endif
void getValue(float &x, float &y, float &z) const
Definition: UT_Color.h:97
void getRGB(float *r, float *g, float *b) const
Definition: UT_Color.h:101
const GLdouble * v
Definition: glcorearb.h:837
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean g
Definition: glcorearb.h:1222
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define UT_API
Definition: UT_API.h:14
void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
Definition: UT_Color.h:99
UT_API void UT_TMItoRGB(fpreal *values, int nvalues)
GLint y
Definition: glcorearb.h:103
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
UT_API size_t format(char *buffer, size_t buffer_size, const UT_Color &v)
double fpreal64
Definition: SYS_Types.h:201
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GLfloat f
Definition: glcorearb.h:1926
Definition: core.h:760
const char * className() const
Definition: UT_Color.h:182
UT_ColorRamp
Definition: UT_Color.h:32
UT_API void UT_HSVtoRGB(fpreal *values, int nvalues)
void setValue(fpreal x, fpreal y, fpreal z)
Definition: UT_Color.h:129
bool operator<(const GU_TetrahedronFacet &a, const GU_TetrahedronFacet &b)
long long int64
Definition: SYS_Types.h:116
UT_API void UT_RGBtoTMI(fpreal *values, int nvalues)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
GLdouble t
Definition: glad.h:2397
UT_MonoRamp
Definition: UT_Color.h:41
float w
Definition: UT_Color.h:225
UT_API void UTinitPhysicalBBRamp(UT_Ramp &ramp, float adaptation=0.15, float burn=0, float maxtemp=40000, int N=64, bool tonemap=true)
float u
Definition: UT_Color.h:223
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
UT_ColorType
Definition: UT_Color.h:24
int operator!=(const UT_Color &c) const
Definition: UT_Color.h:149
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:277
LeafData & operator=(const LeafData &)=delete
Utility class for containing a color ramp.
Definition: UT_Ramp.h:92
GLuint GLfloat * val
Definition: glcorearb.h:1608
GA_API const UT_StringHolder N
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
unsigned int uint32
Definition: SYS_Types.h:40
UT_API UT_ColorType UTcolorTypeFromName(const char *cname)
UT_API const char * UTnameFromColorType(UT_ColorType ctype)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
void getColor(UT_Color &c) const
Definition: UT_Color.h:113
GLboolean r
Definition: glcorearb.h:1222
UT_API void UT_RGBtoHSV(fpreal *values, int nvalues)
type
Definition: core.h:1059
float v
Definition: UT_Color.h:224
UT_ColorType type
Definition: UT_Color.h:222
UT_ColorType getType() const
Definition: UT_Color.h:95