HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
color.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_COLOR_H
8 #define PXR_BASE_GF_COLOR_H
9 
10 /// \file gf/color.h
11 /// \ingroup group_gf_Color
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/colorSpace.h"
15 #include "pxr/base/gf/vec2f.h"
16 #include "pxr/base/gf/vec3f.h"
17 #include "pxr/base/gf/matrix3f.h"
18 #include "pxr/base/gf/api.h"
19 
20 #include <iosfwd>
21 
23 
24 /// \class GfColor
25 /// \brief Represents a color in a specific color space.
26 /// \ingroup group_gf_Color
27 ///
28 /// Basic type: Color
29 ///
30 /// The GfColor class represents a color in a specific color space. It provides
31 /// various methods for constructing, manipulating, and retrieving color values.
32 ///
33 /// The color values are stored as an RGB tuple and are associated with a color
34 /// space. The color space determines the interpretation of the RGB values. The
35 /// values are colorimetric, but not photometric as there is no normalizing
36 /// constant (such as a luminance factor).
37 ///
38 /// This class provides methods for setting and getting color values, converting
39 /// between color spaces, normalizing luminance, and comparing colors.
40 
41 class GfColor {
42 public:
43  /// The default constructor creates black, in the "lin_rec709" color space.
44  GF_API GfColor();
45 
46  /// Construct a black color in the given color space.
47  /// \param colorSpace The color space.
48  GF_API
49  explicit GfColor(const GfColorSpace& colorSpace);
50 
51  /// Construct a color from an RGB tuple and color space.
52  /// \param rgb The RGB tuple (red, green, blue), in the color space
53  /// provided.
54  /// \param colorSpace The color space.
55  GF_API
56  GfColor(const GfVec3f &rgb, const GfColorSpace& colorSpace);
57 
58  /// Construct a color by converting the source color into the specified color space.
59  /// \param color The color to convert, in its color space.
60  /// \param colorSpace The desired color space.
61  GF_API
62  GfColor(const GfColor &color, const GfColorSpace& colorSpace);
63 
64  /// Set the color from the Planckian locus (blackbody radiation) temperature
65  /// in Kelvin, in the existing color space.
66  /// Values are computed for temperatures between 1000K and 15000K.
67  /// Note that temperatures below 1900K are out of gamut for Rec709.
68  /// \param kelvin The temperature in Kelvin.
69  /// \param luminance The desired luminance.
70  GF_API
71  void SetFromPlanckianLocus(float kelvin, float luminance);
72 
73  /// Get the RGB tuple.
74  /// \return The RGB tuple.
75  GfVec3f GetRGB() const { return _rgb; }
76 
77  /// Get the color space.
78  /// \return The color space.
80 
81  /// Equality operator.
82  /// \param rh The right-hand side color.
83  /// \return True if the colors are equal, false otherwise.
84  bool operator ==(const GfColor &rh) const {
85  return _rgb == rh._rgb && _colorSpace == rh._colorSpace;
86  }
87 
88  /// Inequality operator.
89  /// \param r The right-hand side color.
90  /// \return True if the colors are not equal, false otherwise.
91  bool operator !=(const GfColor &rh) const { return !(*this == rh); }
92 
93 protected:
94  GfColorSpace _colorSpace; ///< The color space.
95  GfVec3f _rgb; ///< The RGB tuple.
96 
97  // Get the CIEXY coordinate of the color in the chromaticity chart,
98  // For use in testing.
99  GF_API
100  GfVec2f _GetChromaticity() const;
101 
102  // Set the color from a CIEXY coordinate in the chromaticity chart.
103  // For use in testing.
104  GF_API
105  void _SetFromChromaticity(const GfVec2f& xy);
106 };
107 
108 
109 /// Tests for equality of the RGB tuple in a color with a given tolerance,
110 /// returning \c true if the length of the difference vector is less than or
111 /// equal to \p tolerance. This comparison does not adapt the colors to the
112 /// same color space before comparing, and is not a perceptual comparison.
113 inline bool
114 GfIsClose(GfColor const &c1, GfColor const &c2, double tolerance)
115 {
116  return GfIsClose(c1.GetRGB(), c2.GetRGB(), tolerance);
117 }
118 
119 /// Output a GfColor.
120 /// \ingroup group_gf_DebuggingOutput
121 /// @brief Stream insertion operator for outputting GfColor objects to an output stream.
122 ///
123 /// This operator allows GfColor objects to be written to an output stream.
124 ///
125 /// @param os The output stream to write to.
126 /// @param color The GfColor object to be outputted.
127 /// @return The output stream after writing the GfColor object.
128 std::ostream& operator<<(std::ostream &, GfColor const &);
129 
131 
132 #endif // PXR_BASE_GF_COLOR_H
bool operator!=(const GfColor &rh) const
Definition: color.h:91
GF_API void _SetFromChromaticity(const GfVec2f &xy)
Definition: vec3f.h:45
GF_API void SetFromPlanckianLocus(float kelvin, float luminance)
Represents a color in a specific color space.Basic type: Color.
Definition: color.h:41
bool operator==(const GfColor &rh) const
Definition: color.h:84
std::ostream & operator<<(std::ostream &, GfColor const &)
Stream insertion operator for outputting GfColor objects to an output stream.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GLuint color
Definition: glcorearb.h:1261
GfColorSpace GetColorSpace() const
Definition: color.h:79
GF_API GfVec2f _GetChromaticity() const
Definition: vec2f.h:45
bool GfIsClose(GfColor const &c1, GfColor const &c2, double tolerance)
Definition: color.h:114
GfVec3f _rgb
The RGB tuple.
Definition: color.h:95
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfVec3f GetRGB() const
Definition: color.h:75
GfColorSpace _colorSpace
The color space.
Definition: color.h:94
GF_API GfColor()
The default constructor creates black, in the "lin_rec709" color space.
#define GF_API
Definition: api.h:23