Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
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
12namespace 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 if constexpr (std::is_same<T, uint8_t>::value)
69 {
70 if (color.size() == 9)
71 {
72 a = static_cast<uint8_t>(std::stoi(color.substr(1, 2), nullptr, 16));
73 r = static_cast<uint8_t>(std::stoi(color.substr(3, 2), nullptr, 16));
74 g = static_cast<uint8_t>(std::stoi(color.substr(5, 2), nullptr, 16));
75 b = static_cast<uint8_t>(std::stoi(color.substr(7, 2), nullptr, 16));
76 }
77 else if (color.size() == 7)
78 {
79 r = static_cast<uint8_t>(std::stoi(color.substr(1, 2), nullptr, 16));
80 g = static_cast<uint8_t>(std::stoi(color.substr(3, 2), nullptr, 16));
81 b = static_cast<uint8_t>(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(static_cast<std::uint8_t>((float) r * 255),
118 static_cast<std::uint8_t>((float) g * 255),
119 static_cast<std::uint8_t>((float) b * 255),
120 static_cast<std::uint8_t>((float) a * 255));
121 else
122 *this;
123 }
124
133 template<typename T>
134 Color<T>::Color(T red, T green, T blue, T alpha)
135 {
136 r = red;
137 g = green;
138 b = blue;
139 a = alpha;
140 }
141
142 template<typename T>
143 bool Color<T>::operator==(const std::string &rhs) const {
144 Color other {rhs};
145 return *this == other;
146 }
147
148 template<typename T>
149 bool Color<T>::operator==(const Color &rhs) const
150 {
151 return r == rhs.r &&
152 g == rhs.g &&
153 b == rhs.b &&
154 a == rhs.a;
155 }
156
157 template<typename T>
158 bool Color<T>::operator!=(const Color &rhs) const
159 {
160 return !(rhs == *this);
161 }
162
163
164
165
166}
167
168#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:134
T a
Definition Color.hpp:46
bool operator!=(const Color &rhs) const
Definition Color.hpp:158
bool operator==(const Color &rhs) const
Definition Color.hpp:149
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:143
Definition Base64.hpp:12
Color< float > Colorf
Definition Color.hpp:90
Color< uint8_t > Colori
Definition Color.hpp:89