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 {
87 public:
88  UT_Color();
91  UT_Color(const UT_Color &c);
92  explicit UT_Color(const char *colorname);
93 
94  ~UT_Color();
95 
96  UT_ColorType getType() const { return type; }
97 
98  void getValue(float &x, float &y, float &z) const
99  { x = u; y = v; z = w;}
100  void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
101  { x = fpreal64(u); y = fpreal64(v); z = fpreal64(w);}
102  void getRGB(float *r, float *g, float *b) const
103  {
104  // Small optimization since we work in RGB most times.
105  if(type == UT_RGB) { *r = u; *g=v; *b = w; }
106  else getRGBConvert(r,g,b);
107  }
108 
109  void getHSV(float *h, float *s, float *v) const;
110  void getHSL(float *h, float *s, float *l) const;
111  void getXYZ(float *x, float *y, float *z) const;
112  void getLAB(float *l, float *a, float *b) const;
113  void getTMI(float *t, float *m, float *i) const;
114  void getColor(UT_Color &c) const { c = *this; }
115 
116  UT_Vector3F rgb() const;
117  void setRGB(UT_Vector3F c);
118  void setRGB(UT_Vector3D c);
119 
120  /// luminance() actually returns "luma", an approximation of luminance
121  /// derived from the gamma corrected rgb values. So if you have linear
122  /// rgb values and call this function, you will get the wrong result.
123  fpreal luminance() const;
124 
125  /// relativeLuminance() returns relative luminance [0,1] assuming the color
126  /// is in linear RGB.
127  fpreal relativeLuminance() const;
128 
129  void setType(UT_ColorType t);
130  void setValue(fpreal x, fpreal y, fpreal z) { u = x; v = y; w = z; }
131  void setRGB(fpreal r, fpreal g, fpreal b);
132  void setHSV(fpreal h, fpreal s, fpreal v);
133  void setHSL(fpreal h, fpreal s, fpreal l);
134  void setXYZ(fpreal x, fpreal y, fpreal z);
135  void setLAB(fpreal l, fpreal a, fpreal b);
136  void setLABluminance(fpreal l);
137  void setTMI(fpreal t, fpreal m, fpreal i);
138  void setUnique(int64 idx);
139 
140  void setColorByName(const char *colorname);
141 
142  void clampToLegal();
143 
144  void ghost(fpreal gfactor = 0.75f);
145  UT_Color copyGhosted( fpreal gfactor = 0.75f ) const;
146 
147  void operator=(const UT_Color &c);
148 
149  int operator==(const UT_Color &color) const;
150  int operator!=(const UT_Color &c) const { return !(*this == c); }
151  bool operator<(const UT_Color &b) const;
152 
153  uint32 hash() const;
154 
155  void brighten(fpreal amount); // *this *= clr
156  void tintRGB(const UT_Color &clr); // *this *= clr;
157  void addHSV(const UT_Color &clr); // *this += clr
158  void addRGB(const UT_Color &clr); // *this += clr
159  void subRGB(const UT_Color &clr); // *this -= clr
160 
161  UT_Color copyWithGamma( fpreal gamma ) const;
162  void gamma( fpreal gamma );
163 
164  // *this = lerp(*this, clr, bias) = (1-bias)*this + bias*clr
165  void blendRGB(const UT_Color &clr, fpreal bias=0.5);
166 
167  // Class I/O methods. Return 0 if successful, -1 if not
168  int save(std::ostream &os, int binary) const;
169  void saveMinimalAsText(std::ostream &os) const;
170  bool load(UT_IStream &is);
171 
172  /// @{
173  /// Methods to serialize to a JSON stream. The color is stored as a map:
174  /// @code
175  /// "type" : "color space",
176  /// "data" : [ x, y, z, ... ]
177  /// @endcode
178  bool save(UT_JSONWriter &w) const;
179  bool save(UT_JSONValue &v) const;
180  bool load(UT_JSONParser &p);
181  /// @}
182 
183  const char *className() const { return "UT_Color"; }
184 
185  /// Computes a ramp colour from the predefined ramp enums.
186  static void getRampColor(UT_ColorRamp ramp, fpreal val,
187  float *r, float *g, float *b);
188 
189  /// Return the name of the ramp colour.
190  static const char *getRampColorName(UT_ColorRamp ramp);
191 
192  /// Builds a UT_Ramp to correspond to the given internal ramp.
193  static UT_Ramp *buildRamp(UT_ColorRamp ramp);
194 
195  static UT_Ramp *buildMonoRamp(UT_MonoRamp ramp);
196 
197  /// A mapping from integers to colours that tries to ensure
198  /// that the chosen colours are well separated and aesthetically
199  /// pleasing.
200  static void getUniqueColor(int64 idx,
201  float *r, float *g, float *b);
202 
203  // Returns the standard color at index i.
204  static const UT_Color &getConstantColor(int i);
205 
206  static UT_Color getColorTemperature(fpreal temp);
207 
208  /// Obtain a random color that is contrasting to the specified other color.
209  /// If null is passed for other color, a random color is returned.
210  static void getRandomContrastingColor( const UT_Vector3 * contrast_to,
211  unsigned color_index, UT_Vector3 &color );
212 
213 protected:
214 
215  // modifies the color with a random luminance
216  // (rand_num is a uniformly distributed random number between 0 and 1)
217  // NB: - we can only handle colors with at least 1 zero component
218  // - black colors are un-modified
219  static void assignRandomLuminance( UT_Vector3 &color );
220 
221 
222 protected:
224  float u;
225  float v;
226  float w;
227  void convertToType(UT_ColorType type);
228  void convertToHSL();
229  void convertToHSV();
230  void convertToRGB();
231  void convertToXYZ();
232  void convertToLAB();
233  void convertToTMI();
234 
235  void getRGBConvert(float *r, float *g, float *b) const;
236 
237 private:
238  //To allow proper and safe saving for a UT_Color in UT_OptionFile, we
239  //override these operators. They simply call save/load respectively.
240  friend UT_API std::ostream &operator<<(std::ostream &os, const UT_Color &color);
241 };
242 
243 // Overload for custom formatting of UT_Color with UTformat.
244 UT_API extern size_t format(char *buffer, size_t buffer_size, const UT_Color &v);
245 
246 // Modifies the given ramp to contain the blackbody spectrum ranging from 0 to
247 // maxtemp. The ramp will have N equidistant control points (with linear
248 // interpolation). Adaptation and burn arguments correspond to the parameters
249 // of the same name on the Pyro Blackbody VOP, and are only used when
250 // tonemapping is enabled.
251 UT_API extern void UTinitPhysicalBBRamp(UT_Ramp& ramp, float adaptation = 0.15,
252  float burn = 0, float maxtemp = 40000,
253  int N = 64, bool tonemap = true);
254 
255 #endif
type
Definition: core.h:556
void getValue(float &x, float &y, float &z) const
Definition: UT_Color.h:98
void getRGB(float *r, float *g, float *b) const
Definition: UT_Color.h:102
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:100
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
GLuint buffer
Definition: glcorearb.h:660
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
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
const char * className() const
Definition: UT_Color.h:183
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:130
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:226
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:224
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:150
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:278
LeafData & operator=(const LeafData &)=delete
Utility class for containing a color ramp.
Definition: UT_Ramp.h:96
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:114
GLboolean r
Definition: glcorearb.h:1222
UT_API void UT_RGBtoHSV(fpreal *values, int nvalues)
float v
Definition: UT_Color.h:225
UT_ColorType type
Definition: UT_Color.h:223
UT_ColorType getType() const
Definition: UT_Color.h:96