Tileson  1.3.0
A helpful json parser for Tiled maps
Color.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 09.08.2019.
3 //
4 
5 #ifndef TILESON_COLOR_HPP
6 #define TILESON_COLOR_HPP
7 
8 #include <type_traits>
9 #include <cstdint>
10 #include <string>
11 
12 namespace tson
13 {
14 
15  template<typename T>
16  class Color
17  {
18 
19  public:
25  inline explicit Color(const std::string &color)
26  {
27  parseHexString(color);
28  }
29  inline Color(T red, T green, T blue, T alpha);
30  inline Color() { r = g = b = 0; a = 255; }
31 
32  inline bool operator==(const Color &rhs) const;
33  inline bool operator==(const std::string &rhs) const;
34  inline bool operator!=(const Color &rhs) const;
35 
38 
40  T r;
42  T g;
44  T b;
46  T a;
47 
48  private:
49  void parseHexString(const std::string &color)
50  {
51  if constexpr (std::is_same<T, float>::value)
52  {
53  if (color.size() == 9)
54  {
55  a = (float) std::stoi(color.substr(1, 2), nullptr, 16) / 255;
56  r = (float) std::stoi(color.substr(3, 2), nullptr, 16) / 255;
57  g = (float) std::stoi(color.substr(5, 2), nullptr, 16) / 255;
58  b = (float) std::stoi(color.substr(7, 2), nullptr, 16) / 255;
59  }
60  else if (color.size() == 7)
61  {
62  r = (float) std::stoi(color.substr(1, 2), nullptr, 16) / 255;
63  g = (float) std::stoi(color.substr(3, 2), nullptr, 16) / 255;
64  b = (float) std::stoi(color.substr(5, 2), nullptr, 16) / 255;
65  a = 1.f;
66  }
67  }
68  else
69  {
70  if (color.size() == 9)
71  {
72  a = std::stoi(color.substr(1, 2), nullptr, 16);
73  r = std::stoi(color.substr(3, 2), nullptr, 16);
74  g = std::stoi(color.substr(5, 2), nullptr, 16);
75  b = std::stoi(color.substr(7, 2), nullptr, 16);
76  }
77  else if (color.size() == 7)
78  {
79  r = std::stoi(color.substr(1, 2), nullptr, 16);
80  g = std::stoi(color.substr(3, 2), nullptr, 16);
81  b = std::stoi(color.substr(5, 2), nullptr, 16);
82  a = 255;
83  }
84  }
85  }
86 
87  };
88 
91 
98  template<typename T>
100  {
101  if constexpr (std::is_same<T, float>::value)
102  *this;
103  else
104  return tson::Colorf((float) r / 255, (float) g / 255, (float) b / 255, (float) a / 255);
105  }
106 
113  template<typename T>
115  {
116  if constexpr (std::is_same<T, float>::value)
117  return tson::Colori((float) r * 255, (float) g * 255, (float) b * 255, (float) a * 255);
118  else
119  *this;
120  }
121 
130  template<typename T>
131  Color<T>::Color(T red, T green, T blue, T alpha)
132  {
133  r = red;
134  g = green;
135  b = blue;
136  a = alpha;
137  }
138 
139  template<typename T>
140  bool Color<T>::operator==(const std::string &rhs) const {
141  Color other {rhs};
142  return *this == other;
143  }
144 
145  template<typename T>
146  bool Color<T>::operator==(const Color &rhs) const
147  {
148  return r == rhs.r &&
149  g == rhs.g &&
150  b == rhs.b &&
151  a == rhs.a;
152  }
153 
154  template<typename T>
155  bool Color<T>::operator!=(const Color &rhs) const
156  {
157  return !(rhs == *this);
158  }
159 
160 
161 
162 
163 }
164 
165 #endif //TILESON_COLOR_HPP
Definition: Color.hpp:17
Color< uint8_t > asInt()
Definition: Color.hpp:114
Color(const std::string &color)
Definition: Color.hpp:25
Color()
Definition: Color.hpp:30
Color< float > asFloat()
Definition: Color.hpp:99
Color(T red, T green, T blue, T alpha)
Definition: Color.hpp:131
T a
Definition: Color.hpp:46
bool operator!=(const Color &rhs) const
Definition: Color.hpp:155
bool operator==(const Color &rhs) const
Definition: Color.hpp:146
T b
Definition: Color.hpp:44
T g
Definition: Color.hpp:42
T r
Definition: Color.hpp:40
bool operator==(const std::string &rhs) const
Definition: Color.hpp:140
Definition: Base64.hpp:12
Color< float > Colorf
Definition: Color.hpp:90
Color< uint8_t > Colori
Definition: Color.hpp:89