Debian Codegraph @

avogadro/core/color3f.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_COLOR3F_H
7 #define AVOGADRO_CORE_COLOR3F_H
8
9 #include <array>
10
11 namespace Avogadro::Core {
12 namespace detail {
13 /** Used to represent Maximum RGB value as a float. */
14 constexpr float k_MaxRGBColorValue = 255.0f;
15 } // namespace detail
16
17 /**
18 * @class Color3f color3f.h <avogadro/core/color3f.h>
19 * @brief Representation of an RGB color using three floats.
20 * @author Marcus D. Hanwell
21 *
22 * This class represents a color as three floats ranging from 0.0 to 1.0
23 * specifying the intensity of the red, green and blue components of the
24 * color. It is stored in memory as float[3], and so vectors containing this
25 * type may be passed directly to OpenGL and other functions as C arrays.
26 *
27 * Several convenience functions are provided, the class is written with an
28 * emphasis on efficiency and memory layout.
29 */
30
31 class Color3f
32 {
33 public:
34 /**
35 * Constructor, results in a black Color3f object unless the RGB values are
36 * set.
37 * @param red Intensity (from 0.0 to 1.0) of the red component of the color.
38 * @param green Intensity (from 0.0 to 1.0) of the green component of the
39 * color.
40 * @param blue Intensity (from 0.0 to 1.0) of the blue component of the color.
41 */
42 Color3f(float red = 0.0, float green = 0.0, float blue = 0.0);
43
44 /**
45 * Constructor where the color is constructed from integer values.
46 * @param red Intensity (from 0 to 255) of the red component of the color.
47 * @param green Intensity (from 0 to 255) of the green component of the color.
48 * @param blue Intensity (from 0 to 255) of the blue component of the color.
49 */
50 Color3f(int red, int green, int blue);
51
52 /**
53 * Sets the color objects components.
54 * @param red Intensity (from 0.0 to 1.0) of the red component of the color.
55 * @param green Intensity (from 0.0 to 1.0) of the green component of the
56 * color.
57 * @param blue Intensity (from 0.0 to 1.0) of the blue component of the color.
58 */
59 void set(float red, float green, float blue);
60
61 /**
62 * @return The intensity of the red component of the color (0.0 to 1.0).
63 */
64 float red() const { return m_data[0]; }
65
66 /**
67 * @return The intensity of the green component of the color (0.0 to 1.0).
68 */
69 float green() const { return m_data[1]; }
70
71 /**
72 * @return The intensity of the blue component of the color (0.0 to 1.0).
73 */
74 float blue() const { return m_data[2]; }
75
76 /**
77 * @return Direct access to the underlying float array of size 3.
78 */
79 float* data();
80
81 /**
82 * This function is useful when calling OpenGL functions which expect a
83 * float * array of size 3.
84 * @return Direct access to the underlying float array of size 3.
85 */
86 const float* data() const;
87
88 protected:
89 std::array<float, 3> m_data;
90 };
91
92 inline Color3f::Color3f(float r, float g, float b) : m_data({ r, g, b }) {}
93
94 // clang-format off
95 inline Color3f::Color3f(int r, int g, int b)
96 : m_data({static_cast<float>(r) / detail::k_MaxRGBColorValue,
97 static_cast<float>(g) / detail::k_MaxRGBColorValue,
98 static_cast<float>(b) / detail::k_MaxRGBColorValue})
99 {
100 }
101 // clang-format on
102
103 inline void Color3f::set(float r, float g, float b)
104 {
105 m_data[0] = r;
106 m_data[1] = g;
107 m_data[2] = b;
108 }
109
110 inline float* Color3f::data()
111 {
112 return m_data.data();
113 }
114
115 inline const float* Color3f::data() const
116 {
117 return m_data.data();
118 }
119
120 } // End namespace Avogadro::Core
121
122 #endif // AVOGADRO_CORE_COLOR3F_H