NAVTOR SDK  v.3.0
Software Development Kit for fast and efficient development of ECDIS, ECS and generic marine applications
color_base_types_helpers.h
1 #ifndef SDK_COLOR_BASE_TYPES_HELPERS_H_
11 #define SDK_COLOR_BASE_TYPES_HELPERS_H_
12 
13 #include <float.h>
14 #include <math.h>
15 #include "color_base_types.h"
16 
17 namespace sdk {
18 
21 class ColorF : public SDKColorF {
22  // Constructors
23 public:
24  // Uninitialized color
25  ColorF() noexcept { r = g = b = a = 0.0f; }
26  // Copy constructor
27  ColorF(const SDKColorF& src_clr) noexcept {
28  r = src_clr.r, g = src_clr.g, b = src_clr.b, a = src_clr.a;
29  }
30  // Copy constructor
31  ColorF(const ColorF& src_clr) noexcept {
32  r = src_clr.r, g = src_clr.g, b = src_clr.b, a = src_clr.a;
33  }
34  // From r, g, b, a
35  explicit ColorF(float init_r, float init_g, float init_b,
36  float init_a) noexcept {
37  r = init_r, g = init_g, b = init_b, a = init_a;
38  }
39  // From r, g, b, a
40  explicit ColorF(double init_r, double init_g, double init_b,
41  double init_a) noexcept {
42  r = static_cast<float>(init_r), g = static_cast<float>(init_g);
43  b = static_cast<float>(init_b), a = static_cast<float>(init_a);
44  }
45  // From 32-bit ARGB
46  explicit ColorF(SDKUInt32 argb) noexcept {
47  a = static_cast<float>((argb & 0xFF000000)>> 24) / 255.f;
48  r = static_cast<float>((argb & 0x00FF0000)>> 16) / 255.f;
49  g = static_cast<float>((argb & 0x0000FF00)>> 8) / 255.f;
50  b = static_cast<float>(argb & 0x000000FF) / 255.f;
51  }
52 
53  inline bool is_valid() const
54  {
55  return a >= 0.0f && a <= 1.0f && r >= 0.0f && r <= 1.0f
56  && g >= 0.0f && g <= 1.0f && b >= 0.0f && b <= 1.0f;
57  }
58 
59  // Operations
60  const ColorF& operator=(const ColorF& src_clr) noexcept {
61  r = src_clr.r, g = src_clr.g, b = src_clr.b, a = src_clr.a;
62  return *this;
63  }
64  const ColorF& operator=(const SDKColorF& src_clr) noexcept {
65  r = src_clr.r, g = src_clr.g, b = src_clr.b, a = src_clr.a;
66  return *this;
67  }
68 
69 // operator const ColorF&() const noexcept { return *this; }
70 // operator const SDKColorF&() const noexcept { return get(); }
71  const SDKColorF& get() const noexcept {
72  return *static_cast<const SDKColorF*>(this);
73  }
74  SDKUInt32 get_argb() const noexcept {
75  SDKUInt8 r8 = static_cast<SDKUInt8>(r * 255.0);
76  SDKUInt8 g8 = static_cast<SDKUInt8>(g * 255.0);
77  SDKUInt8 b8 = static_cast<SDKUInt8>(b * 255.0);
78  SDKUInt8 a8 = static_cast<SDKUInt8>(a * 255.0);
79 
80  return ColorF::ARGB(r8, g8, b8, a8);
81  }
82 
83  void get_argb(SDKUInt8* r8, SDKUInt8* g8, SDKUInt8* b8,
84  SDKUInt8* a8) const noexcept {
85  if (r8)
86  *r8 = static_cast<SDKUInt8>(r * 255.0);
87  if (g8)
88  *g8 = static_cast<SDKUInt8>(g * 255.0);
89  if (b8)
90  *b8 = static_cast<SDKUInt8>(b * 255.0);
91  if (a8)
92  *a8 = static_cast<SDKUInt8>(a * 255.0);
93  }
94 
95  bool operator!=(const ColorF& clr) const noexcept {
96  return !operator==(clr);
97  }
98  bool operator!=(const SDKColorF& clr) const noexcept {
99  return !operator==(clr);
100  }
101  bool operator==(const ColorF& clr) const noexcept {
102  return operator==(clr.get());
103  }
104  bool operator==(const SDKColorF& clr) const noexcept {
105  if (fabs(r - clr.r) <= FLT_EPSILON && fabs(g - clr.g) <= FLT_EPSILON &&
106  fabs(b - clr.b) <= FLT_EPSILON && fabs(a - clr.a) <= FLT_EPSILON)
107  return true;
108  return false;
109  }
111  static SDKUInt32 ARGB(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a) {
112  return ((_a << 24) | (_r << 16) | (_g << 8) | _b);
113  }
115  static SDKUInt32 RGBA(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a) {
116  return ((_r << 24) | (_g << 16) | (_b << 8) | _a);
117  }
119  static SDKUInt32 ABGR(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a) {
120  return ((_a << 24) | (_b << 16) | (_g << 8) | _r);
121  }
123  static void FromABGR(SDKUInt32 argb, SDKUInt8* _r, SDKUInt8* _g, SDKUInt8* _b,
124  SDKUInt8* _a) {
125  if (_r)
126  *_r = static_cast<SDKUInt8>(argb & 0x000000FF);
127  if (_g)
128  *_g = static_cast<SDKUInt8>((argb & 0x0000FF00) >> 8);
129  if (_b)
130  *_b = static_cast<SDKUInt8>((argb & 0x00FF0000) >> 16);
131  if (_a)
132  *_a = static_cast<SDKUInt8>((argb & 0x00FF0000) >> 24);
133  }
135  static ColorF GetInvalid() {
136  return ColorF(-1.0, -1.0, -1.0, -1.0);
137  }
138 };
139 
140 } // namespace sdk
141 
142 #endif // SDK_COLOR_BASE_TYPES_HELPERS_H_
SDKColorF
A RGBA color.
Definition: color_base_types.h:15
sdk::ColorF::ABGR
static SDKUInt32 ABGR(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a)
Returns ABGR for a r g b.
Definition: color_base_types_helpers.h:119
SDKColorF::r
float r
Red channel (0.0 - 1.0)
Definition: color_base_types.h:16
sdk::ColorF
ColorF.
Definition: color_base_types_helpers.h:21
sdk::ColorF::FromABGR
static void FromABGR(SDKUInt32 argb, SDKUInt8 *_r, SDKUInt8 *_g, SDKUInt8 *_b, SDKUInt8 *_a)
Returns ABGR for a r g b.
Definition: color_base_types_helpers.h:123
SDKColorF::g
float g
Green channel (0.0 - 1.0)
Definition: color_base_types.h:17
sdk::ColorF::ARGB
static SDKUInt32 ARGB(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a)
Returns ARGB for a r g b.
Definition: color_base_types_helpers.h:111
sdk::ColorF::RGBA
static SDKUInt32 RGBA(SDKUInt8 _r, SDKUInt8 _g, SDKUInt8 _b, SDKUInt8 _a)
Returns RGBA for a r g b.
Definition: color_base_types_helpers.h:115
SDKColorF::b
float b
Blue channel (0.0 - 1.0)
Definition: color_base_types.h:18
color_base_types.h
Color base definitions.
SDKColorF::a
float a
Alpha channel (0.0 - 1.0)
Definition: color_base_types.h:19
sdk::ColorF::GetInvalid
static ColorF GetInvalid()
Returns invalid color instance.
Definition: color_base_types_helpers.h:135