NDEVR
API Documentation
RGBColor.h
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/BufferBase.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 class StringView;
46 static const fltp04 inverse_255 = 1.0f/255.0f;
56 class RGBColor : public Vector<4, uint01>, public Color
57 {
58 public:
59 static const uint01 r_pos = 0;
60 static const uint01 g_pos = 1;
61 static const uint01 b_pos = 2;
62 static const uint01 a_pos = 3;
63
64 public:
68 constexpr RGBColor()
69 : Vector<4, uint01>(0)
70 {}
71
75 explicit RGBColor(uint04 value)
76 : Vector<4, uint01>(*reinterpret_cast<Vector<4, uint01>*>(&value))
77 {}
78
85 constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a = 255)
86 : Vector<4, uint01>(
87 cast<uint01>(clip(r, 0, 255))
88 , cast<uint01>(clip(g, 0, 255))
89 , cast<uint01>(clip(b, 0, 255))
90 , cast<uint01>(clip(a, 0, 255)))
91 {}
92
99 constexpr RGBColor(const uint01 r, const uint01 g, const uint01 b, const uint01 a = 255)
100 : Vector<4, uint01>(r, g, b, a)
101 {}
102
109 NDEVR_BASE_API RGBColor(const fltp04 r, const fltp04 g, const fltp04 b, const fltp04 a = 1.0f);
117 NDEVR_BASE_API RGBColor(const fltp08 r, const fltp08 g, const fltp08 b, const fltp08 a = 1.0f);
122 constexpr RGBColor(const RGBColor& color)
123 : Vector<4, uint01>(color)
124 {}
125
130 template<class t_type>
131 RGBColor(const Vector<1, t_type>& color)
132 : Vector<4, uint01>(color[0], color[0], color[0], 255)
133 {}
134
138 template<class t_type>
139 RGBColor(const Vector<2, t_type>& color)
140 : Vector<4, uint01>(cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[0]), cast<uint01>(color[1]))
141 {}
142
146 template<class t_type>
147 RGBColor(const Vector<3, t_type>& color)
148 : Vector<4, uint01>(color.template as<3, uint01>(), 255)
149 {}
150
154 template<class t_type>
155 RGBColor(const Vector<4, t_type>& color)
156 : Vector<4, uint01>(color.template as<4, uint01>())
157 {}
158
163 NDEVR_BASE_API explicit RGBColor(const Vector<4, fltp04>& color);
168 NDEVR_BASE_API explicit RGBColor(const Vector<4, fltp08>& color);
173 NDEVR_BASE_API explicit RGBColor(const Vector<3, fltp04>& color);
178 NDEVR_BASE_API explicit RGBColor(const Vector<3, fltp08>& color);
183 NDEVR_BASE_API explicit RGBColor(const Vector<2, fltp04>& color);
188 NDEVR_BASE_API explicit RGBColor(const Vector<2, fltp08>& color);
193 NDEVR_BASE_API explicit RGBColor(const Vector<1, fltp04>& color);
198 NDEVR_BASE_API explicit RGBColor(const Vector<1, fltp08>& color);
199
204 NDEVR_BASE_API explicit RGBColor(const ACIColor&);
209 NDEVR_BASE_API explicit RGBColor(const HSBColor&);
214 NDEVR_BASE_API explicit RGBColor(const HSLColor&);
219 NDEVR_BASE_API explicit RGBColor(const XYZColor&);
224 NDEVR_BASE_API explicit RGBColor(const LABColor&);
225
231 {
233 }
234
240 inline fltp04 getF(uint01 channel) const
241 {
242 return m_values[channel] * inverse_255;
243 }
244
249 inline void setF(uint01 channel, fltp04 value)
250 {
251 m_values[channel] = cast<uint01>(value * 255);
252 }
253
254
259 NDEVR_BASE_API Vector<3, fltp04> get3F() const;
264 NDEVR_BASE_API Vector<4, fltp04> get4F() const;
265
270 NDEVR_BASE_API uint04 convertToRGBA32BitColor() const;
275 NDEVR_BASE_API uint04 convertToABGR32BitColor() const;
281 NDEVR_BASE_API static RGBColor CreateRGBA32BitColor(uint04 color_32);
287 NDEVR_BASE_API static RGBColor CreateARGB32BitColor(uint04 color_32);
288
293 NDEVR_BASE_API uint04 convertTo24BitColor() const;
299 NDEVR_BASE_API static RGBColor create24BitColor(uint04 color_24);
300
305 NDEVR_BASE_API bool isLight() const;
310 NDEVR_BASE_API RGBColor contrastingColor() const;
311
316 NDEVR_BASE_API fltp04 luminance() const;
317
322 size_t operator()() const
323 {
325 }
326
331 constexpr RGBColor& operator=(const RGBColor& color)
332 {
333 for(uint04 dim = 0; dim < 4; ++dim)
334 m_values[dim] = color.m_values[dim];
335 return *this;
336 }
337
342 NDEVR_BASE_API TranslatedString getName() const;
347 NDEVR_BASE_API String getHexRGBA() const;
352 NDEVR_BASE_API String getHexARGB() const;
357 NDEVR_BASE_API String getHexRGB() const;
363 NDEVR_BASE_API static RGBColor FromHexARGB(StringView hex);
371 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
379 NDEVR_BASE_API static RGBColor Average(const RGBColor& a, const RGBColor& b, const RGBColor& c);
387 NDEVR_BASE_API static RGBColor AverageLAB(const RGBColor& a, const RGBColor& b, fltp04 percent = 0.5f);
388 private:
396 static fltp04 hue(fltp04 h, fltp04 m1, fltp04 m2);
397
398 };
399
403 template<>
404 struct ObjectInfo<RGBColor, true, false>
405 {
406 static const bool Vector = true;
407 static const uint01 Dimensions = 4;
409 static const bool Primitive = true;
411 static const bool Pointer = false;
412 static const bool Unsigned = false;
413 static const bool Float = false;
414 static const bool Integer = false;
415 static const bool Number = false;
416 static const bool Enum = false;
417 static const bool String = false;
418 static const bool Color = true;
419 static const bool Buffer = false;
420 static const bool Boolean = false;
426 };
427 static_assert(sizeof(RGBColor) == 4, "Bad Color size");
428
429 template<> constexpr const RGBColor Constant<RGBColor>::Invalid = RGBColor(255, 255, 254, 0);
430 template<> constexpr const RGBColor Constant<RGBColor>::Max = RGBColor(255, 255, 255, 255);
431 template<> constexpr const RGBColor Constant<RGBColor>::Min = RGBColor(0, 0, 0, 0);
432
439 template<class t_type>
440 constexpr RGBColor& operator*=(RGBColor& color, const t_type& value)
441 {
442 if (IsInvalid(color))
443 return color;
444 for (uint01 dim = 0; dim < 3; ++dim)
445 color[dim] = cast<uint01>(value * cast<t_type>(color[dim]));
446 return color;
447 }
448
454 constexpr RGBColor operator*(const RGBColor& color, const fltp04& value)
455 {
456 if (IsInvalid(color))
457 return color;
458 RGBColor new_color;
459 for (uint01 dim = 0; dim < 3; ++dim)
460 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
461 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
462 return new_color;
463 }
464
470 constexpr RGBColor operator*(const fltp04& value, const RGBColor& color)
471 {
472 if (IsInvalid(color))
473 return color;
474 RGBColor new_color;
475 for (uint01 dim = 0; dim < 3; ++dim)
476 new_color[dim] = cast<uint01>(value * cast<fltp04>(color[dim]));
477 new_color[RGBColor::a_pos] = color[RGBColor::a_pos];
478 return new_color;
479 }
480 static_assert(sizeof(RGBColor) == 4, "Bad color size");
481};
A color identified by an AutoCAD Color Index (ACI) number from 1 through 255.
Definition ACIColor.h:59
The core Color class in the NDEVR API.
Definition Color.h:42
A color defined by hue, saturation, and brightness components.
Definition HSBColor.h:62
A color defined by hue, saturation, and luminosity components.
Definition HSLColor.h:51
Forward declaration of the NDEVR translated string class.
Definition LABColor.h:65
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
fltp04 getF(uint01 channel) const
Gets a channel value as a normalized float in [0, 1].
Definition RGBColor.h:240
String getHexRGB() const
Gets the hex string representation of this color in RGB format (e.g., "#FF0000").
RGBColor(const Vector< 1, fltp04 > &color)
Constructs an RGBColor from a 1-dimensional single-precision float vector (grayscale).
String getHexARGB() const
Gets the hex string representation of this color in ARGB format (e.g., "#FFFF0000").
static const uint01 a_pos
The index of the alpha channel in the RGBA color struct.
Definition RGBColor.h:62
uint04 convertTo24BitColor() const
Packs this color into a 24-bit unsigned integer (RGB only, no alpha).
static const uint01 r_pos
The index of the red channel in the RGBA color struct.
Definition RGBColor.h:59
static RGBColor create24BitColor(uint04 color_24)
Creates an RGBColor from a packed 24-bit RGB color value.
Vector< 3, fltp04 > get3F() const
Gets the RGB channels as a 3-dimensional float vector normalized to [0, 1].
static const uint01 g_pos
The index of the green channel in the RGBA color struct.
Definition RGBColor.h:60
RGBColor(const fltp08 r, const fltp08 g, const fltp08 b, const fltp08 a=1.0f)
Constructs an RGBColor from double-precision floating-point channel values.
void setF(uint01 channel, fltp04 value)
Sets a channel value from a normalized float in [0, 1].
Definition RGBColor.h:249
TranslatedString getName() const
Gets the closest known name for this color as a translated string.
RGBColor(const Vector< 2, fltp04 > &color)
Constructs an RGBColor from a 2-dimensional single-precision float vector (grayscale + alpha).
static RGBColor Average(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
Computes a weighted average of two colors in RGB space.
RGBColor(const Vector< 3, t_type > &color)
Constructs an RGBColor from a 3-dimensional vector (RGB).
Definition RGBColor.h:147
RGBColor(const LABColor &)
Constructs an RGBColor by converting from a LABColor.
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.
RGBColor(const XYZColor &)
Constructs an RGBColor by converting from an XYZColor.
RGBColor(uint04 value)
Constructs an RGBColor from a packed 32-bit unsigned integer.
Definition RGBColor.h:75
uint04 convertToABGR32BitColor() const
Packs this color into a 32-bit unsigned integer in ABGR byte order.
static RGBColor CreateRGBA32BitColor(uint04 color_32)
Creates an RGBColor from a packed 32-bit RGBA color value.
RGBColor(const Vector< 2, t_type > &color)
Constructs an RGBColor from a 2-dimensional vector (grayscale + alpha).
Definition RGBColor.h:139
RGBColor(const Vector< 3, fltp08 > &color)
Constructs an RGBColor from a 3-dimensional double-precision float vector (RGB in [0,...
constexpr RGBColor()
Default constructor.
Definition RGBColor.h:68
constexpr RGBColor & operator=(const RGBColor &color)
Copy assignment operator.
Definition RGBColor.h:331
RGBColor(const Vector< 4, fltp08 > &color)
Constructs an RGBColor from a 4-dimensional double-precision float vector (RGBA in [0,...
constexpr RGBColor(const sint04 r, const sint04 g, const sint04 b, const sint04 a=255)
Constructs an RGBColor from signed integer channel values, clamping each to [0, 255].
Definition RGBColor.h:85
RGBColor(const Vector< 4, fltp04 > &color)
Constructs an RGBColor from a 4-dimensional single-precision float vector (RGBA in [0,...
RGBColor(const Vector< 3, fltp04 > &color)
Constructs an RGBColor from a 3-dimensional single-precision float vector (RGB in [0,...
RGBColor(const ACIColor &)
Constructs an RGBColor by converting from an ACIColor.
fltp04 luminance() const
Returns the luminance of the color.
Vector< 4, fltp04 > get4F() const
Gets all RGBA channels as a 4-dimensional float vector normalized to [0, 1].
static RGBColor AverageLAB(const RGBColor &a, const RGBColor &b, fltp04 percent=0.5f)
Computes a weighted average of two colors in perceptually uniform LAB space.
RGBColor(const Vector< 2, fltp08 > &color)
Constructs an RGBColor from a 2-dimensional double-precision float vector (grayscale + alpha).
size_t operator()() const
Functor operator that returns the packed 32-bit RGBA representation, useful as a hash.
Definition RGBColor.h:322
bool isLight() const
Determines whether this color is perceptually light.
static RGBColor CreateARGB32BitColor(uint04 color_32)
Creates an RGBColor from a packed 32-bit ARGB color value.
RGBColor(const Vector< 1, t_type > &color)
Constructs an RGBColor from a 1-dimensional vector, using the value as a grayscale intensity.
Definition RGBColor.h:131
static RGBColor FromHexARGB(StringView hex)
Creates an RGBColor from a hex string in ARGB format.
RGBColor(const HSBColor &)
Constructs an RGBColor by converting from an HSBColor.
RGBColor(const Vector< 1, fltp08 > &color)
Constructs an RGBColor from a 1-dimensional double-precision float vector (grayscale).
RGBColor contrastingColor() const
Returns a color that contrasts well with this color for readability.
RGBColor(const Vector< 4, t_type > &color)
Constructs an RGBColor from a 4-dimensional vector (RGBA).
Definition RGBColor.h:155
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:99
RGBColor(const HSLColor &)
Constructs an RGBColor by converting from an HSLColor.
constexpr RGBColor(const RGBColor &color)
Copy constructor.
Definition RGBColor.h:122
String getHexRGBA() const
Gets the hex string representation of this color in RGBA format (e.g., "#FF0000FF").
static RGBColor Average(const RGBColor &a, const RGBColor &b, const RGBColor &c)
Computes the average of three colors in RGB space.
RGBColor opaque() const
Returns a copy of this color with alpha set to 255 (fully opaque).
Definition RGBColor.h:230
uint04 convertToRGBA32BitColor() const
Packs this color into a 32-bit unsigned integer in RGBA byte order.
static const uint01 b_pos
The index of the blue channel in the RGBA color struct.
Definition RGBColor.h:61
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
uint01 m_values[t_dims]
Definition Vector.hpp:630
constexpr decltype(auto) as() const
Definition Vector.hpp:301
Forward declaration of HSLColor.
Definition XYZColor.h:55
The primary namespace for the NDEVR SDK.
static const fltp04 inverse_255
Precomputed reciprocal of 255 for fast normalization to [0,1].
Definition RGBColor.h:46
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
static constexpr Angle< t_type > operator*(const Angle< t_type > &angle_a, const Angle< t_type > &angle_b)
Multiplication operator.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
static constexpr Angle< t_angle_type > & operator*=(Angle< t_angle_type > &angle, const t_type &mult)
Multiplication assignment operator for an Angle and a scalar.
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.
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
static const bool Unsigned
Not treated as a generic unsigned type.
Definition RGBColor.h:412
static const bool Boolean
Not a boolean type.
Definition RGBColor.h:420
static constexpr ObjectInfo< uint01, false > VectorSub()
Returns the ObjectInfo for the underlying vector element type (uint01).
Definition RGBColor.h:425
static const bool Integer
Not a generic integer type.
Definition RGBColor.h:414
static const bool Primitive
Useful for buffers.
Definition RGBColor.h:409
static const bool Enum
Not an enum type.
Definition RGBColor.h:416
static const bool Pointer
True if the object is a pointer.
Definition RGBColor.h:411
static const bool Buffer
Not a buffer type.
Definition RGBColor.h:419
static const bool Color
This type represents a color.
Definition RGBColor.h:418
static const bool String
Not a string type.
Definition RGBColor.h:417
static const bool Vector
True because RGBColor is a vector type.
Definition RGBColor.h:406
static const bool Number
Not a generic number type.
Definition RGBColor.h:415
static const bool Float
Not a floating-point type.
Definition RGBColor.h:413
static const uint01 Dimensions
Number of channels (R, G, B, A).
Definition RGBColor.h:407
Information about the object.
Definition ObjectInfo.h:55