GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
Color.hpp
Go to the documentation of this file.
1 #ifndef GL_COLOR_HPP
2 #define GL_COLOR_HPP
3 
4 #include <cmath>
5 #include <unordered_map>
6 #include <string>
7 #include <functional>
8 #include <sstream>
9 #include <iomanip>
10 
11 #define GL_FORCE_INTO_RANGE(VAR, LOWER, UPPER) \
12  VAR < LOWER ? LOWER : (VAR > UPPER ? UPPER : VAR)
13 
14 namespace gl
15 {
16 
21 class Color
22 {
23 public:
24  using color_val_t = unsigned char;
34  Color() : r_(0), g_(0), b_(0), a_(100) {}
42  Color(const int r, const int g,
43  const int b, const int a = 100) : r_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(r, 0, 255))),
44  g_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(g, 0, 255))),
45  b_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(b, 0, 255))),
46  a_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(a, 0, 100))) {}
54  Color(const double r, const double g,
55  const double b, const double a = 1.0) : r_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(r, 0., 1.) * 255)),
56  g_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(g, 0., 1.) * 255)),
57  b_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(b, 0., 1.) * 255)),
58  a_(static_cast<color_val_t>(GL_FORCE_INTO_RANGE(a, 0., 1.) * 100)) {}
64  explicit Color(const unsigned int hex, const unsigned int a=0x64) :
65  a_(a),
66  r_((hex >> 16) & 0xFF),
67  g_((hex >> 8) & 0xFF),
68  b_((hex) & 0xFF)
69  {}
76  Color(const char *name, color_val_t a = 100) : a_(a)
77  {
78  const std::unordered_map<std::string, std::function<void()>> map{
79  {"black", [&]() { hex(0x000000); }},
80  {"gray", [&]() { hex(0x808080); }},
81  {"silver", [&]() { hex(0xC0C0C0); }},
82  {"white", [&]() { hex(0xFFFFFF); }},
83  {"orange", [&]() { hex(0xED9121); }},
84  {"maroon", [&]() { hex(0x800000); }},
85  {"red", [&]() { hex(0xFF0000); }},
86  {"olive", [&]() { hex(0x808000); }},
87  {"yellow", [&]() { hex(0xFFFF00); }},
88  {"green", [&]() { hex(0x008000); }},
89  {"lime", [&]() { hex(0x00FF00); }},
90  {"teal", [&]() { hex(0x008080); }},
91  {"aqua", [&]() { hex(0x00FFFF); }},
92  {"navy", [&]() { hex(0x000080); }},
93  {"blue", [&]() { hex(0x0000FF); }},
94  {"purple", [&]() { hex(0x800080); }},
95  {"fuchsia", [&]() { hex(0xFF00FF); }},
96  };
97  auto it = map.find(name);
98  if (it != map.end())
99  {
100  it->second();
101  }
102  else
103  {
104  hex(0xFFFFFF);
105  }
106  }
108  Color(const Color &) = default;
109  Color(Color &&) noexcept = default;
110  Color &operator=(const Color &) = default;
111  Color &operator=(Color &&) noexcept = default;
112  ~Color() = default;
113 
118  bool operator== (const Color& rhs) const
119  {
120  return r_ == rhs.r_
121  && g_ == rhs.g_
122  && b_ == rhs.b_
123  && a_ == rhs.a_;
124  }
129  bool operator!= (const Color& rhs) const
130  {
131  return !operator==(rhs);
132  }
141  inline std::string RGBA() const
142  {
143  std::stringstream ss;
144  ss << std::setfill('0') << std::setw(8) << std::hex << (hex() | 0);
145  return ss.str();
146  }
150  inline std::string RGB() const
151  {
152  std::stringstream ss;
153  int val = (hex() >> 8) & 0xffffff;
154  ss << std::setfill('0') << std::setw(6) << std::hex << (val | 0);
155  return ss.str();
156  }
160  inline int hex() const
161  {
162  return (r_ << 24) | (g_ << 16) | (b_ << 8) | a_;
163  }
167  inline color_val_t r() const { return r_; }
171  inline color_val_t g() const { return g_; }
175  inline color_val_t b() const { return b_; }
179  inline color_val_t a() const { return a_; }
181 
192  inline void hex(const int hex)
193  {
194  r_ = (hex >> 16) & 0xFF;
195  g_ = (hex >> 8) & 0xFF;
196  b_ = (hex) & 0xFF;
197  }
203  inline void hex(const int hex, const unsigned int a)
204  {
205  r_ = (hex >> 16) & 0xFF;
206  g_ = (hex >> 8) & 0xFF;
207  b_ = (hex) & 0xFF;
208  a_ = GL_FORCE_INTO_RANGE(a, 0, 100);
209  }
213  inline void r(color_val_t r) { r_ = r; }
217  inline void g(color_val_t g) { g_ = g; }
221  inline void b(color_val_t b) { b_ = b; }
225  inline void a(color_val_t a) { a_ = a; }
227 
228 private:
230  g_,
231  b_,
232  a_;
233 };
234 
235 } // namespace gl
236 
237 #undef GL_FORCE_INTO_RANGE
238 
239 #endif // GL_COLOR_HPP
color_val_t b_
Blue value. .
Definition: Color.hpp:229
Color(const int r, const int g, const int b, const int a=100)
Constructor for integer R,G,B,A values.
Definition: Color.hpp:42
Stores an RGBA Color.
Definition: Color.hpp:21
color_val_t a_
Alpha/opacity value. .
Definition: Color.hpp:229
void b(color_val_t b)
Sets the blue value of the RGBA color.
Definition: Color.hpp:221
#define GL_FORCE_INTO_RANGE(VAR, LOWER, UPPER)
Definition: Color.hpp:11
Color(const char *name, color_val_t a=100)
Constructor for combined hexadecimal R,G,B,A values.
Definition: Color.hpp:76
bool operator==(const Color &rhs) const
Check whether two colors are equal.
Definition: Color.hpp:118
bool operator!=(const Color &rhs) const
Check whether two colors are not equal.
Definition: Color.hpp:129
int hex() const
Gets the hexadecimal value of the RGBA color.
Definition: Color.hpp:160
void g(color_val_t g)
Sets the green value of the RGBA color.
Definition: Color.hpp:217
Color(const unsigned int hex, const unsigned int a=0x64)
Constructor for combined hexadecimal R,G,B,A values.
Definition: Color.hpp:64
Color()
Default constructor.
Definition: Color.hpp:34
void hex(const int hex)
Sets all values of the RGBA color.
Definition: Color.hpp:192
void a(color_val_t a)
Sets the alpha/opacity value of the RGBA color.
Definition: Color.hpp:225
color_val_t b() const
Gets the blue value of the RGBA color.
Definition: Color.hpp:175
void r(color_val_t r)
Sets the red value of the RGBA color.
Definition: Color.hpp:213
color_val_t r_
Red value. .
Definition: Color.hpp:229
color_val_t g() const
Gets the green value of the RGBA color.
Definition: Color.hpp:171
unsigned char color_val_t
Definition: Color.hpp:24
void hex(const int hex, const unsigned int a)
Sets all values of the RGBA color.
Definition: Color.hpp:203
std::string RGB() const
Gets the hexadecimal value of the RGB color as a string.
Definition: Color.hpp:150
color_val_t a() const
Gets the alpha/opacity value of the RGBA color.
Definition: Color.hpp:179
color_val_t g_
Green value. .
Definition: Color.hpp:229
std::string RGBA() const
Gets the hexadecimal value of the RGBA color as a string.
Definition: Color.hpp:141
Color(const double r, const double g, const double b, const double a=1.0)
Constructor for separate double R,G,B,A values.
Definition: Color.hpp:54
color_val_t r() const
Gets the red value of the RGBA color.
Definition: Color.hpp:167