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/Color.h>
34#include <NDEVR/Vector.h>
35#include <NDEVR/ObjectInfo.h>
36namespace NDEVR
37{
38 class HSBColor;
39 class HSLColor;
40 class ACIColor;
41 class XYZColor;
42 class LABColor;
43 class String;
44 class TranslatedString;
45 static const fltp04 inverse_255 = 1.0f/255.0f;
46 /**
47 * @class RGBColor
48 * @brief Represents a color in the RGB space with optional alpha transparency.
49 *
50 * Extends the Vector class to include specific methods for RGB manipulation,
51 * providing flexibility for use in graphics and image processing applications.
52 */
53 class RGBColor : public Vector<4, uint01>, public Color
54 {
55 public:
56 static const uint01 r_pos = 0;//The index of red in the RGBA color struct
57 static const uint01 g_pos = 1;//The index of green in the RGBA color struct
58 static const uint01 b_pos = 2;//The index of blue in the RGBA color struct
59 static const uint01 a_pos = 3;//The index of alpha in the RGBA color struct
60
61 public:
62 constexpr RGBColor()
63 : Vector<4, uint01>(0)
64 {}
65 explicit RGBColor(uint04 value)
66 : Vector<4, uint01>(*reinterpret_cast<Vector<4, uint01>*>(&value))
67 {}
68 constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a = 255)
69 : Vector<4, uint01>(
70 cast<uint01>(clip(r, 0, 255))
71 , cast<uint01>(clip(g, 0, 255))
72 , cast<uint01>(clip(b, 0, 255))
73 , cast<uint01>(clip(a, 0, 255)))
74 {}
75 /**
76 * @brief Constructor to initialize color using integer values for each channel.
77 * @param r Red channel value (0-255).
78 * @param g Green channel value (0-255).
79 * @param b Blue channel value (0-255).
80 * @param a Alpha channel value (0-255, default is 255).
81 */
82 constexpr RGBColor(const uint01 r, const uint01 g, const uint01 b, const uint01 a = 255)
83 : Vector<4, uint01>(r, g, b, a)
84 {}
85 /**
86 * @brief Constructor to initialize color using floating-point values for each channel.
87 * @param r Red channel value (0.0f-1.0f).
88 * @param g Green channel value (0.0f-1.0f).
89 * @param b Blue channel value (0.0f-1.0f).
90 * @param a Alpha channel value (0.0f-1.0f, default is 1.0f).
91 */
92 NDEVR_BASE_API RGBColor(const fltp04 r, const fltp04 g, const fltp04 b, const fltp04 a = 1.0f);
93 NDEVR_BASE_API RGBColor(const fltp08 r, const fltp08 g, const fltp08 b, const fltp08 a = 1.0f);
94 constexpr RGBColor(const RGBColor& color)
95 : Vector<4, uint01>(color)
96 {}
97
98 template<class t_type>
100 : Vector<4, uint01>(color[0], color[0], color[0], 255)
101 {}
102 template<class t_type>
104 : Vector<4, uint01>(cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[1]))
105 {}
106 template<class t_type>
108 : Vector<4, uint01>(color.template as<3, uint01>(), 255)
109 {}
110 template<class t_type>
112 : Vector<4, uint01>(color.template as<4, uint01>())
113 {}
114
123
129
131 {
133 }
134
135 inline fltp04 getF(uint01 channel) const
136 {
137 return m_values[channel] * inverse_255;
138 }
139 inline void setF(uint01 channel, fltp04 value)
140 {
141 m_values[channel] = cast<uint01>(value * 255);
142 }
143
144
147
152
155
158
159 /**
160 * @brief Returns the luminance of the color.
161 * @return Luminance as a floating-point value.
162 */
164
165 size_t operator()() const
166 {
168 }
169 constexpr RGBColor& operator=(const RGBColor& color)
170 {
171 for(uint04 dim = 0; dim < 4; ++dim)
172 m_values[dim] = color.m_values[dim];
173 return *this;
174 }
175
180 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
181 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, const RGBColor& c);
182 NDEVR_BASE_API static RGBColor AverageLAB(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
183 private:
184 /**
185 * @brief Utility method to calculate hue adjustment.
186 * @param h Hue component.
187 * @param m1 Intermediate value 1.
188 * @param m2 Intermediate value 2.
189 * @return Adjusted hue value.
190 */
191 static fltp04 hue(fltp04 h, fltp04 m1, fltp04 m2);
192
193 };
194
195 template<>
196 struct ObjectInfo<RGBColor, true, false>
197 {
198 static const bool Vector = true;
199 static const uint01 Dimensions = 4;
200 /** Useful for buffers. Determines whether the object needs to call an allocater. */
201 static const bool Primitive = true;
202 /** True if the object is a pointer. */
203 static const bool Pointer = false;
204 static const bool Unsigned = false;
205 static const bool Float = false;
206 static const bool Integer = false;
207 static const bool Number = false;
208 static const bool Enum = false;
209 static const bool String = false;
210 static const bool Color = true;
211 static const bool Buffer = false;
212 static const bool Boolean = false;
213 static constexpr ObjectInfo<uint01, false> VectorSub() { return ObjectInfo<uint01, false>(); }
214 };
215 static_assert(sizeof(RGBColor) == 4, "Bad Color size");
216
217 template<> constexpr const RGBColor Constant<RGBColor>::Invalid = RGBColor(255, 255, 254, 0);
218 template<> constexpr const RGBColor Constant<RGBColor>::Max = RGBColor(255, 255, 255, 255);
219 template<> constexpr const RGBColor Constant<RGBColor>::Min = RGBColor(0, 0, 0, 0);
220
221 template<class t_type>
222 constexpr RGBColor& operator*=(RGBColor& color, const t_type& value)
223 {
224 if (IsInvalid(color))
225 return color;
226 for (uint01 dim = 0; dim < 3; ++dim)
227 color[dim] = cast<uint01>(value * cast<t_type>(color[dim]));
228 return color;
229 }
230 constexpr RGBColor operator*(const RGBColor& color, const fltp04& value)
231 {
232 if (IsInvalid(color))
233 return color;
234 RGBColor new_color;
235 for (uint01 dim = 0; dim < 3; ++dim)
236 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
237 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
238 return new_color;
239 }
240 constexpr RGBColor operator*(const fltp04& value, const RGBColor& color)
241 {
242 if (IsInvalid(color))
243 return color;
244 RGBColor new_color;
245 for (uint01 dim = 0; dim < 3; ++dim)
246 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
247 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
248 return new_color;
249 }
250 static_assert(sizeof(RGBColor) == 4, "Bad color size");
251};
#define NDEVR_BASE_API
Definition DLLInfo.h:57
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:56
The core Color class in the NDEVR API. Colors can be defined in several ways. The ACIColor is compact...
Definition Color.h:41
The HSB (Hue, Saturation, Brightness) color model defines a color space in terms of three constituent...
Definition HSBColor.h:57
The HSL (Hue, Saturation, Luminosity) color model defines a color space in.
Definition HSLColor.h:47
A Lab color space is a color-opponent space with dimension L for luminance and a and b for the color-...
Definition LABColor.h:61
Provides shared ownership of a dynamically allocated object.
Definition Pointer.hpp:71
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:54
NDEVR_BASE_API RGBColor(const Vector< 2, fltp04 > &color)
RGBColor(const Vector< 4, t_type > &color)
Definition RGBColor.h:111
NDEVR_BASE_API RGBColor(const Vector< 3, fltp08 > &color)
NDEVR_BASE_API String getHexRGBA() const
fltp04 getF(uint01 channel) const
Definition RGBColor.h:135
RGBColor(const Vector< 3, t_type > &color)
Definition RGBColor.h:107
NDEVR_BASE_API RGBColor(const fltp08 r, const fltp08 g, const fltp08 b, const fltp08 a=1.0f)
NDEVR_BASE_API TranslatedString getName() const
constexpr RGBColor(const RGBColor &color)
Definition RGBColor.h:94
NDEVR_BASE_API RGBColor(const Vector< 4, fltp08 > &color)
RGBColor(uint04 value)
Definition RGBColor.h:65
size_t operator()() const
Definition RGBColor.h:165
NDEVR_BASE_API bool isLight() const
constexpr RGBColor()
Definition RGBColor.h:62
NDEVR_BASE_API RGBColor(const Vector< 2, fltp08 > &color)
NDEVR_BASE_API String getHexARGB() const
static const uint01 a_pos
Definition RGBColor.h:59
static NDEVR_BASE_API RGBColor CreateRGBA32BitColor(uint04 color_32)
NDEVR_BASE_API RGBColor(const Vector< 1, fltp08 > &color)
NDEVR_BASE_API RGBColor(const Vector< 1, fltp04 > &color)
static NDEVR_BASE_API RGBColor create24BitColor(uint04 color_24)
NDEVR_BASE_API RGBColor(const HSBColor &)
static NDEVR_BASE_API RGBColor Average(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
void setF(uint01 channel, fltp04 value)
Definition RGBColor.h:139
NDEVR_BASE_API RGBColor(const Vector< 3, fltp04 > &color)
static const uint01 r_pos
Definition RGBColor.h:56
NDEVR_BASE_API Vector< 3, fltp04 > get3F() const
NDEVR_BASE_API uint04 convertToRGBA32BitColor() const
NDEVR_BASE_API RGBColor(const fltp04 r, const fltp04 g, const fltp04 b, const fltp04 a=1.0f)
Constructor to initialize color using floating-point values for each channel.
NDEVR_BASE_API uint04 convertTo24BitColor() const
constexpr RGBColor & operator=(const RGBColor &color)
Definition RGBColor.h:169
NDEVR_BASE_API Vector< 4, fltp04 > get4F() const
NDEVR_BASE_API RGBColor contrastingColor() const
static const uint01 g_pos
Definition RGBColor.h:57
NDEVR_BASE_API String getHexRGB() const
RGBColor(const Vector< 1, t_type > &color)
Definition RGBColor.h:99
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:82
constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a=255)
Definition RGBColor.h:68
NDEVR_BASE_API RGBColor(const XYZColor &)
NDEVR_BASE_API RGBColor(const ACIColor &)
RGBColor(const Vector< 2, t_type > &color)
Definition RGBColor.h:103
NDEVR_BASE_API RGBColor(const HSLColor &)
static const uint01 b_pos
Definition RGBColor.h:58
NDEVR_BASE_API RGBColor(const Vector< 4, fltp04 > &color)
NDEVR_BASE_API uint04 convertToABGR32BitColor() const
static NDEVR_BASE_API RGBColor Average(const RGBColor &a, const RGBColor &b, const RGBColor &c)
RGBColor opaque() const
Definition RGBColor.h:130
NDEVR_BASE_API RGBColor(const LABColor &)
NDEVR_BASE_API fltp04 luminance() const
Returns the luminance of the color.
static NDEVR_BASE_API RGBColor AverageLAB(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
static NDEVR_BASE_API RGBColor CreateARGB32BitColor(uint04 color_32)
The core String class for the NDEVR API.
Definition String.h:69
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
uint01 m_values[t_dims]
Definition Vector.hpp:623
constexpr Vector< t_dims, t_new_type > as() const
Definition Vector.hpp:300
In opposition to the previous models, the CIE XYZ model defines an absolute color space....
Definition XYZColor.h:51
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:64
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
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)
Multiplication operator for a Vector of Angles.
Definition AngleFunctions.h:326
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:207
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
constexpr RGBColor & operator*=(RGBColor &color, const t_type &value)
Definition RGBColor.h:222
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Information about the object.
Definition ObjectInfo.h:54