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 <SYS/SYS_Math.h>
7 #include <iosfwd>
8 
9 class UT_IStream;
10 class UT_JSONWriter;
11 class UT_JSONParser;
12 class UT_JSONValue;
13 class UT_Ramp;
14 
15 //
16 // Each color space has its own range for each of the three parameters.
17 //
18 // RGB: 0..1, 0..1, 0..1
19 // HSL: 0..360, 0..1, 0..1
20 // HSV: 0..360, 0..1, 0..1
21 // XYZ: 0..1, 0..1, 0..1 work well
22 // LAB: 0..100, a/b are unbounded but TIFF uses [-128, 127]
23 // TMI: -1..1, -1..1, -1..1
24 
27 };
28 
29 // Conversion of UT_ColorType values to/from string for saving/loading.
30 UT_API extern const char * UTnameFromColorType(UT_ColorType ctype);
31 UT_API extern UT_ColorType UTcolorTypeFromName(const char *cname);
32 
34  UT_COLORRAMP_FALSE, // False colour
35  UT_COLORRAMP_PINK, // White-red
36  UT_COLORRAMP_MONO, // Black-white
37  UT_COLORRAMP_BLACKBODY, // Black-red-yellow-white
38  UT_COLORRAMP_BIPARTITE, // Cyan-blue then Red-yellow
39  UT_COLORRAMP_NUMMODES // Illegal value.
40 };
41 
44  UT_MONORAMP_DECREASING, // Backslash
45  UT_MONORAMP_HILL, // slash backslash
46  UT_MONORAMP_VALLEY, // Backslash slash
47  UT_MONORAMP_STEP, // Underscore Overbar
48  UT_MONORAMP_SQUARE, // Pipe Overbar Pipe
49  UT_MONORAMP_NUMMODES // Illegal value.
50 };
51 
52 class UT_Color;
53 #include "UT_VectorTypes.h"
54 
55 namespace UT
56 {
57  // Conversion between different color spaces
58  template <typename T> static constexpr inline T
59  linearToSRGB(T x)
60  {
61  constexpr T zero = 0.0031308;
62  constexpr T scale = 12.92;
63  constexpr T a = 0.055;
64  constexpr T gamma = 2.4;
65  return x <= zero ? x*scale : (1+a)*SYSpow(x, 1/gamma) - a;
66  }
67  template <typename T> static constexpr inline T
68  linearToRec709(T x)
69  {
70  constexpr T zero = 0.018;
71  constexpr T scale = 4.5;
72  constexpr T a = 0.099;
73  constexpr T gamma = 0.45;
74  return x <= zero ? x*scale : (1+a)*SYSpow(x, gamma) - a;
75  }
76  template <typename T> static constexpr inline T
77  linearToGamma(T x, T gamma)
78  {
79  return SYSsafepow(x, 1/gamma);
80  }
81  template <typename T> static constexpr inline T
82  linearToGamma22(T x)
83  {
84  constexpr T gamma = 2.2;
85  return linearToGamma(x, gamma);
86  }
87 
88  template <typename T> static constexpr inline T
89  srgbToLinear(T x)
90  {
91  constexpr T zero = 0.04045;
92  constexpr T scale = 12.92;
93  constexpr T a = 0.055;
94  constexpr T gamma = 2.4;
95  return x <= zero ? x*(1/scale) : SYSpow((x+a)*(1/(1+a)), gamma);
96  }
97  template <typename T> static constexpr inline T
98  rec709ToLinear(T x)
99  {
100  constexpr T zero = 0.081;
101  constexpr T scale = 4.5;
102  constexpr T a = 0.099;
103  constexpr T gamma = 0.45;
104  return x <= zero ? x*(1/scale) : SYSpow((x+a)*(1/(1+a)), (1/gamma));
105  }
106  template <typename T> static constexpr inline T
107  gammaToLinear(T x, T gamma)
108  {
109  return SYSsafepow(x, gamma);
110  }
111  template <typename T> static constexpr inline T
112  gamma22ToLinear(T x)
113  {
114  constexpr T gamma = 2.2;
115  return gammaToLinear(x, gamma);
116  }
117 }
118 
119 //
120 // Filter functions useable with UI_Vector class:
121 //
122 UT_API extern void UT_RGBtoHSV(fpreal *values, int nvalues);
123 UT_API extern void UT_HSVtoRGB(fpreal *values, int nvalues);
124 
125 UT_API extern void UT_RGBtoTMI(fpreal *values, int nvalues);
126 UT_API extern void UT_TMItoRGB(fpreal *values, int nvalues);
127 
128 #define UT_BLACK (UT_Color::getConstantColor(0))
129 #define UT_BLUE (UT_Color::getConstantColor(1))
130 #define UT_GREEN (UT_Color::getConstantColor(2))
131 #define UT_RED (UT_Color::getConstantColor(3))
132 #define UT_CYAN (UT_Color::getConstantColor(4))
133 #define UT_MAGENTA (UT_Color::getConstantColor(5))
134 #define UT_YELLOW (UT_Color::getConstantColor(6))
135 #define UT_WHITE (UT_Color::getConstantColor(7))
136 #define UT_GREY1 (UT_Color::getConstantColor(8))
137 #define UT_GREY2 (UT_Color::getConstantColor(9))
138 #define UT_GREY3 (UT_Color::getConstantColor(10))
139 #define UT_GREY4 (UT_Color::getConstantColor(11))
140 #define UT_GREY5 (UT_Color::getConstantColor(12))
141 #define UT_GREY6 (UT_Color::getConstantColor(13))
142 #define UT_GREY7 (UT_Color::getConstantColor(14))
143 #define UT_GREY8 (UT_Color::getConstantColor(15))
144 #define UT_GREY9 (UT_Color::getConstantColor(16))
145 
146 /*
147  * UT_Color is the base class for colors in one of the above color spaces
148  */
149 
151 {
152 public:
153  UT_Color();
156  UT_Color(const UT_Color &c);
157  explicit UT_Color(const char *colorname);
158 
159  ~UT_Color();
160 
161  UT_ColorType getType() const { return type; }
162 
163  void getValue(float &x, float &y, float &z) const
164  { x = u; y = v; z = w;}
165  void getValue(fpreal64 &x, fpreal64 &y, fpreal64 &z) const
166  { x = fpreal64(u); y = fpreal64(v); z = fpreal64(w);}
167  void getRGB(float *r, float *g, float *b) const
168  {
169  // Small optimization since we work in RGB most times.
170  if(type == UT_RGB) { *r = u; *g=v; *b = w; }
171  else getRGBConvert(r,g,b);
172  }
173 
174  void getHSV(float *h, float *s, float *v) const;
175  void getHSL(float *h, float *s, float *l) const;
176  void getXYZ(float *x, float *y, float *z) const;
177  void getLAB(float *l, float *a, float *b) const;
178  void getTMI(float *t, float *m, float *i) const;
179  void getColor(UT_Color &c) const { c = *this; }
180 
181  UT_Vector3F rgb() const;
182  void setRGB(UT_Vector3F c);
183  void setRGB(UT_Vector3D c);
184 
185  /// luminance() actually returns "luma", an approximation of luminance
186  /// derived from the gamma corrected rgb values. So if you have linear
187  /// rgb values and call this function, you will get the wrong result.
188  fpreal luminance() const;
189 
190  /// relativeLuminance() returns relative luminance [0,1] assuming the color
191  /// is in linear RGB.
192  fpreal relativeLuminance() const;
193 
194  void setType(UT_ColorType t);
195  void setValue(fpreal x, fpreal y, fpreal z) { u = x; v = y; w = z; }
196  void setRGB(fpreal r, fpreal g, fpreal b);
197  void setHSV(fpreal h, fpreal s, fpreal v);
198  void setHSL(fpreal h, fpreal s, fpreal l);
199  void setXYZ(fpreal x, fpreal y, fpreal z);
200  void setLAB(fpreal l, fpreal a, fpreal b);
201  void setLABluminance(fpreal l);
202  void setTMI(fpreal t, fpreal m, fpreal i);
203  void setUnique(int64 idx);
204 
205  void setColorByName(const char *colorname);
206 
207  void clampToLegal();
208 
209  void ghost(fpreal gfactor = 0.75f);
210  UT_Color copyGhosted( fpreal gfactor = 0.75f ) const;
211 
212  void operator=(const UT_Color &c);
213 
214  int operator==(const UT_Color &color) const;
215  int operator!=(const UT_Color &c) const { return !(*this == c); }
216  bool operator<(const UT_Color &b) const;
217 
218  uint32 hash() const;
219 
220  void brighten(fpreal amount); // *this *= clr
221  void tintRGB(const UT_Color &clr); // *this *= clr;
222  void addHSV(const UT_Color &clr); // *this += clr
223  void addRGB(const UT_Color &clr); // *this += clr
224  void subRGB(const UT_Color &clr); // *this -= clr
225 
226  UT_Color copyWithGamma( fpreal gamma ) const;
227  void gamma( fpreal gamma );
228 
229  // *this = lerp(*this, clr, bias) = (1-bias)*this + bias*clr
230  void blendRGB(const UT_Color &clr, fpreal bias=0.5);
231 
232  // Class I/O methods. Return 0 if successful, -1 if not
233  int save(std::ostream &os, int binary) const;
234  void saveMinimalAsText(std::ostream &os) const;
235  bool load(UT_IStream &is);
236 
237  /// @{
238  /// Methods to serialize to a JSON stream. The color is stored as a map:
239  /// @code
240  /// "type" : "color space",
241  /// "data" : [ x, y, z, ... ]
242  /// @endcode
243  bool save(UT_JSONWriter &w) const;
244  bool save(UT_JSONValue &v) const;
245  bool load(UT_JSONParser &p);
246  /// @}
247 
248  const char *className() const { return "UT_Color"; }
249 
250  /// Computes a ramp colour from the predefined ramp enums.
251  static void getRampColor(UT_ColorRamp ramp, fpreal val,
252  float *r, float *g, float *b);
253 
254  /// Return the name of the ramp colour.
255  static const char *getRampColorName(UT_ColorRamp ramp);
256 
257  /// Builds a UT_Ramp to correspond to the given internal ramp.
258  static UT_Ramp *buildRamp(UT_ColorRamp ramp);
259 
260  static UT_Ramp *buildMonoRamp(UT_MonoRamp ramp);
261 
262  /// A mapping from integers to colours that tries to ensure
263  /// that the chosen colours are well separated and aesthetically
264  /// pleasing.
265  static void getUniqueColor(int64 idx,
266  float *r, float *g, float *b);
267 
268  // Returns the standard color at index i.
269  static const UT_Color &getConstantColor(int i);
270 
271  static UT_Color getColorTemperature(fpreal temp);
272 
273  /// Obtain a random color that is contrasting to the specified other color.
274  /// If null is passed for other color, a random color is returned.
275  static void getRandomContrastingColor( const UT_Vector3 * contrast_to,
276  unsigned color_index, UT_Vector3 &color );
277 
278 protected:
279 
280  // modifies the color with a random luminance
281  // (rand_num is a uniformly distributed random number between 0 and 1)
282  // NB: - we can only handle colors with at least 1 zero component
283  // - black colors are un-modified
284  static void assignRandomLuminance( UT_Vector3 &color );
285 
286 
287 protected:
289  float u;
290  float v;
291  float w;
292  void convertToType(UT_ColorType type);
293  void convertToHSL();
294  void convertToHSV();
295  void convertToRGB();
296  void convertToXYZ();
297  void convertToLAB();
298  void convertToTMI();
299 
300  void getRGBConvert(float *r, float *g, float *b) const;
301 
302 private:
303  //To allow proper and safe saving for a UT_Color in UT_OptionFile, we
304  //override these operators. They simply call save/load respectively.
305  friend UT_API std::ostream &operator<<(std::ostream &os, const UT_Color &color);
306 };
307 
308 // Overload for custom formatting of UT_Color with UTformat.
309 UT_API extern size_t
310 UTformatBuffer(char *buffer, size_t buffer_size, const UT_Color &v);
311 
312 // Modifies the given ramp to contain the blackbody spectrum ranging from 0 to
313 // maxtemp. The ramp will have N equidistant control points (with linear
314 // interpolation). Adaptation and burn arguments correspond to the parameters
315 // of the same name on the Pyro Blackbody VOP, and are only used when
316 // tonemapping is enabled.
317 UT_API extern void UTinitPhysicalBBRamp(UT_Ramp& ramp, float adaptation = 0.15,
318  float burn = 0, float maxtemp = 40000,
319  int N = 64, bool tonemap = true);
320 
321 #endif
type
Definition: core.h:556
void getValue(float &x, float &y, float &z) const
Definition: UT_Color.h:163
void getRGB(float *r, float *g, float *b) const
Definition: UT_Color.h:167
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:165
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:39
GLuint buffer
Definition: glcorearb.h:660
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
GA_API const UT_StringHolder scale
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:248
UT_ColorRamp
Definition: UT_Color.h:33
UT_API void UT_HSVtoRGB(fpreal *values, int nvalues)
void setValue(fpreal x, fpreal y, fpreal z)
Definition: UT_Color.h:195
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:42
float w
Definition: UT_Color.h:291
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:289
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
UT_API size_t UTformatBuffer(char *buffer, size_t buffer_size, const UT_Color &v)
UT_ColorType
Definition: UT_Color.h:25
int operator!=(const UT_Color &c) const
Definition: UT_Color.h:215
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:283
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:179
GLboolean r
Definition: glcorearb.h:1222
UT_API void UT_RGBtoHSV(fpreal *values, int nvalues)
ImageBuf OIIO_API zero(ROI roi, int nthreads=0)
float v
Definition: UT_Color.h:290
UT_ColorType type
Definition: UT_Color.h:288
UT_ColorType getType() const
Definition: UT_Color.h:161