API Documentation
Loading...
Searching...
No Matches
RGBColor.h
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: RGBColor
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Vector.h>
34#include <NDEVR/ObjectInfo.h>
35namespace NDEVR
36{
37 class HSBColor;
38 class HSLColor;
39 class ACIColor;
40 class XYZColor;
41 class LABColor;
42 class String;
43 class TranslatedString;
44 static const fltp04 inverse_255 = 1.0f/255.0f;
45 /**
46 * @class RGBColor
47 * @brief Represents a color in the RGB space with optional alpha transparency.
48 *
49 * Extends the Vector class to include specific methods for RGB manipulation,
50 * providing flexibility for use in graphics and image processing applications.
51 */
52 class RGBColor : public Vector<4, uint01>
53 {
54 public:
55 static const uint01 r_pos = 0;
56 static const uint01 g_pos = 1;
57 static const uint01 b_pos = 2;
58 static const uint01 a_pos = 3;
59
60 public:
61 constexpr RGBColor()
62 : Vector<4, uint01>(0)
63 {}
64 explicit RGBColor(uint04 value)
65 : Vector<4, uint01>(*reinterpret_cast<Vector<4, uint01>*>(&value))
66 {}
67 constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a = 255)
68 : Vector<4, uint01>(
69 cast<uint01>(clip(r, 0, 255))
70 , cast<uint01>(clip(g, 0, 255))
71 , cast<uint01>(clip(b, 0, 255))
72 , cast<uint01>(clip(a, 0, 255)))
73 {}
74 /**
75 * @brief Constructor to initialize color using integer values for each channel.
76 * @param r Red channel value (0-255).
77 * @param g Green channel value (0-255).
78 * @param b Blue channel value (0-255).
79 * @param a Alpha channel value (0-255, default is 255).
80 */
81 constexpr RGBColor(const uint01 r, const uint01 g, const uint01 b, const uint01 a = 255)
82 : Vector<4, uint01>(r, g, b, a)
83 {}
84 /**
85 * @brief Constructor to initialize color using floating-point values for each channel.
86 * @param r Red channel value (0.0f-1.0f).
87 * @param g Green channel value (0.0f-1.0f).
88 * @param b Blue channel value (0.0f-1.0f).
89 * @param a Alpha channel value (0.0f-1.0f, default is 1.0f).
90 */
91 NDEVR_BASE_API RGBColor(const fltp04 r, const fltp04 g, const fltp04 b, const fltp04 a = 1.0f);
92 NDEVR_BASE_API RGBColor(const fltp08 r, const fltp08 g, const fltp08 b, const fltp08 a = 1.0f);
93 constexpr RGBColor(const RGBColor& color)
94 : Vector<4, uint01>(color)
95 {}
96
97 template<class t_type>
99 : Vector<4, uint01>(color[0], color[0], color[0], 255)
100 {}
101 template<class t_type>
103 : Vector<4, uint01>(cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[1]))
104 {}
105 template<class t_type>
107 : Vector<4, uint01>(color.template as<3, uint01>(), 255)
108 {}
109 template<class t_type>
111 : Vector<4, uint01>(color.template as<4, uint01>())
112 {}
113
114 NDEVR_BASE_API explicit RGBColor(const Vector<4, fltp04>& color);
115 NDEVR_BASE_API explicit RGBColor(const Vector<4, fltp08>& color);
116 NDEVR_BASE_API explicit RGBColor(const Vector<3, fltp04>& color);
117 NDEVR_BASE_API explicit RGBColor(const Vector<3, fltp08>& color);
118 NDEVR_BASE_API explicit RGBColor(const Vector<2, fltp04>& color);
119 NDEVR_BASE_API explicit RGBColor(const Vector<2, fltp08>& color);
120 NDEVR_BASE_API explicit RGBColor(const Vector<1, fltp04>& color);
121 NDEVR_BASE_API explicit RGBColor(const Vector<1, fltp08>& color);
122
123 NDEVR_BASE_API explicit RGBColor(const ACIColor&);
124 NDEVR_BASE_API explicit RGBColor(const HSBColor&);
125 NDEVR_BASE_API explicit RGBColor(const HSLColor&);
126 NDEVR_BASE_API explicit RGBColor(const XYZColor&);
127 NDEVR_BASE_API explicit RGBColor(const LABColor&);
128
130 {
132 }
133
134 inline fltp04 getF(uint01 channel) const
135 {
136 return m_values[channel] * inverse_255;
137 }
138 inline void setF(uint01 channel, fltp04 value)
139 {
140 m_values[channel] = cast<uint01>(value * 255);
141 }
142
143
146
151
154
155 NDEVR_BASE_API bool isLight() const;
157
158 /**
159 * @brief Returns the luminance of the color.
160 * @return Luminance as a floating-point value.
161 */
163
164 size_t operator()() const
165 {
167 }
168 constexpr RGBColor& operator=(const RGBColor& color)
169 {
170 for(uint04 dim = 0; dim < 4; ++dim)
171 m_values[dim] = color.m_values[dim];
172 return *this;
173 }
174
179 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
180 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, const RGBColor& c);
181 NDEVR_BASE_API static RGBColor AverageLAB(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
182 private:
183 /**
184 * @brief Utility method to calculate hue adjustment.
185 * @param h Hue component.
186 * @param m1 Intermediate value 1.
187 * @param m2 Intermediate value 2.
188 * @return Adjusted hue value.
189 */
190 static fltp04 hue(fltp04 h, fltp04 m1, fltp04 m2);
191
192 };
193
194 template<>
195 struct ObjectInfo<RGBColor, true, false>
196 {
197 static const bool Vector = true;
198 static const uint01 Dimensions = 4;
199 /** Useful for buffers. Determines whether the object needs to call an allocater. */
200 static const bool Primitive = true;
201 /** True if the object is a pointer. */
202 static const bool Pointer = false;
203 static const bool Unsigned = false;
204 static const bool Float = false;
205 static const bool Integer = false;
206 static const bool Number = false;
207 static const bool Enum = false;
208 static const bool String = false;
209 static const bool Color = true;
210 static const bool Buffer = false;
211 static const bool Boolean = false;
213 };
214 static_assert(sizeof(RGBColor) == 4, "Bad Color size");
215
216 template<> constexpr const RGBColor Constant<RGBColor>::NaN = RGBColor(255, 255, 254, 0);
217 template<> constexpr const RGBColor Constant<RGBColor>::Max = RGBColor(255, 255, 255, 255);
218 template<> constexpr const RGBColor Constant<RGBColor>::Min = RGBColor(0, 0, 0, 0);
219
220 template<class t_type>
221 constexpr RGBColor& operator*=(RGBColor& color, const t_type& value)
222 {
223 if (isNaN(color))
224 return color;
225 for (uint01 dim = 0; dim < 3; ++dim)
226 color[dim] = cast<uint01>(value * cast<t_type>(color[dim]));
227 return color;
228 }
229 constexpr RGBColor operator*(const RGBColor& color, const fltp04& value)
230 {
231 if (isNaN(color))
232 return color;
233 RGBColor new_color;
234 for (uint01 dim = 0; dim < 3; ++dim)
235 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
236 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
237 return new_color;
238 }
239 constexpr RGBColor operator*(const fltp04& value, const RGBColor& color)
240 {
241 if (isNaN(color))
242 return color;
243 RGBColor new_color;
244 for (uint01 dim = 0; dim < 3; ++dim)
245 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
246 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
247 return new_color;
248 }
249};
#define NDEVR_BASE_API
Definition DLLInfo.h:78
ACI colors are the standard colors used in AutoCAD-based products. Each color is identified by an ACI...
Definition ACIColor.h:58
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
Definition Color.h:36
Definition HSBColor.h:56
Definition HSLColor.h:54
Definition LABColor.h:60
Definition Pointer.hpp:62
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:53
RGBColor(const Vector< 4, t_type > &color)
Definition RGBColor.h:110
static NDEVR_BASE_API RGBColor create24BitColor(uint04 color_24)
Definition RGBColor.cpp:98
NDEVR_BASE_API String getHexRGBA() const
Definition RGBColor.cpp:291
NDEVR_BASE_API fltp04 luminance() const
Returns the luminance of the color.
Definition RGBColor.cpp:124
fltp04 getF(uint01 channel) const
Definition RGBColor.h:134
NDEVR_BASE_API String getHexRGB() const
Definition RGBColor.cpp:305
NDEVR_BASE_API uint04 convertToRGBA32BitColor() const
Definition RGBColor.cpp:57
RGBColor(const Vector< 3, t_type > &color)
Definition RGBColor.h:106
NDEVR_BASE_API TranslatedString getName() const
Definition RGBColor.cpp:282
static NDEVR_BASE_API RGBColor Average(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
Definition RGBColor.cpp:159
NDEVR_BASE_API uint04 convertToABGR32BitColor() const
Definition RGBColor.cpp:66
constexpr RGBColor(const RGBColor &color)
Definition RGBColor.h:93
RGBColor(uint04 value)
Definition RGBColor.h:64
size_t operator()() const
Definition RGBColor.h:164
constexpr RGBColor()
Definition RGBColor.h:61
static NDEVR_BASE_API RGBColor AverageLAB(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
Definition RGBColor.cpp:173
NDEVR_BASE_API Vector< 3, fltp04 > get3F() const
Definition RGBColor.cpp:151
static const uint01 a_pos
Definition RGBColor.h:58
NDEVR_BASE_API Vector< 4, fltp04 > get4F() const
Definition RGBColor.cpp:155
void setF(uint01 channel, fltp04 value)
Definition RGBColor.h:138
static NDEVR_BASE_API RGBColor CreateRGBA32BitColor(uint04 color_32)
Definition RGBColor.cpp:75
static const uint01 r_pos
Definition RGBColor.h:55
constexpr RGBColor & operator=(const RGBColor &color)
Definition RGBColor.h:168
static const uint01 g_pos
Definition RGBColor.h:56
NDEVR_BASE_API String getHexARGB() const
Definition RGBColor.cpp:298
RGBColor(const Vector< 1, t_type > &color)
Definition RGBColor.h:98
constexpr RGBColor(const uint01 r, const uint01 g, const uint01 b, const uint01 a=255)
Constructor to initialize color using integer values for each channel.
Definition RGBColor.h:81
constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a=255)
Definition RGBColor.h:67
NDEVR_BASE_API uint04 convertTo24BitColor() const
Definition RGBColor.cpp:93
RGBColor(const Vector< 2, t_type > &color)
Definition RGBColor.h:102
static const uint01 b_pos
Definition RGBColor.h:57
NDEVR_BASE_API bool isLight() const
Definition RGBColor.cpp:130
NDEVR_BASE_API RGBColor contrastingColor() const
Definition RGBColor.cpp:134
RGBColor opaque() const
Definition RGBColor.h:129
static NDEVR_BASE_API RGBColor CreateARGB32BitColor(uint04 color_32)
Definition RGBColor.cpp:84
Definition String.h:40
Definition TranslatedString.h:9
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
uint01 m_values[t_dims]
Definition Vector.hpp:675
constexpr Vector< t_dims, t_new_type > as() const
Definition Vector.hpp:324
Definition XYZColor.h:50
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
constexpr std::enable_if< IsVecType< t_vector_type, Angle< fltp08 > >::value, t_vector_type >::type operator*(const t_vector_type &angle, const t_type &mult)
Definition AngleFunctions.h:403
constexpr t_type clip(const t_type &value, const t_type &lower_bound, const t_type &upper_bound)
Clips the value given so that that the returned value falls between upper and lower bound.
Definition BaseFunctions.hpp:240
float fltp04
Defines an alias representing a 4 byte floating-point number.
Definition BaseValues.hpp:157
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
constexpr RGBColor & operator*=(RGBColor &color, const t_type &value)
Definition RGBColor.h:221
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Definition BaseValues.hpp:272
static constexpr ObjectInfo< uint01, false > VectorSub()
Definition RGBColor.h:212
Information about the object.
Definition ObjectInfo.h:56