3#include "PixelBullet/Serialization/Node.hpp"
5#define GLM_ENABLE_EXPERIMENTAL
6#include <glm/geometric.hpp>
32 constexpr Color() noexcept
40 constexpr Color(
float r,
float g,
float b,
float a = 1.0f) noexcept
49 constexpr Color(uint32_t i, Type type = Type::RGB)
noexcept
54 value.x =
static_cast<float>((i >> 24) & 0xFF) / 255.0f;
55 value.y =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
56 value.z =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
57 value.w =
static_cast<float>(i & 0xFF) / 255.0f;
60 value.w =
static_cast<float>((i >> 24) & 0xFF) / 255.0f;
61 value.x =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
62 value.y =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
63 value.z =
static_cast<float>(i & 0xFF) / 255.0f;
66 value.x =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
67 value.y =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
68 value.z =
static_cast<float>(i & 0xFF) / 255.0f;
72 throw std::runtime_error(
"Unknown Color type");
79 explicit Color(std::string hex,
float a = 1.0f)
81 if (!hex.empty() && hex[0] ==
'#')
87 throw std::runtime_error(
"Invalid hex color string");
89 uint32_t hexValue = std::stoul(hex,
nullptr, 16);
90 value.x =
static_cast<float>((hexValue >> 16) & 0xFF) / 255.0f;
91 value.y =
static_cast<float>((hexValue >> 8) & 0xFF) / 255.0f;
92 value.z =
static_cast<float>(hexValue & 0xFF) / 255.0f;
101 return Color(value.x + (other.value.x - value.x) * t, value.y + (other.value.y - value.y) * t,
102 value.z + (other.value.z - value.z) * t, value.w + (other.value.w - value.w) * t);
113 throw std::runtime_error(
"Cannot normalize zero-length color");
115 return Color(value.x / len, value.y / len, value.z / len, value.w / len);
123 return glm::dot(value, value);
137 constexpr uint32_t
GetInt(Type type = Type::RGBA)
const noexcept
142 return (
static_cast<uint32_t
>(value.x * 255.0f) << 24) |
143 (
static_cast<uint32_t
>(value.y * 255.0f) << 16) |
144 (
static_cast<uint32_t
>(value.z * 255.0f) << 8) | (
static_cast<uint32_t
>(value.w * 255.0f));
146 return (
static_cast<uint32_t
>(value.w * 255.0f) << 24) |
147 (
static_cast<uint32_t
>(value.x * 255.0f) << 16) |
148 (
static_cast<uint32_t
>(value.y * 255.0f) << 8) | (
static_cast<uint32_t
>(value.z * 255.0f));
150 return (
static_cast<uint32_t
>(value.x * 255.0f) << 16) |
151 (
static_cast<uint32_t
>(value.y * 255.0f) << 8) | (
static_cast<uint32_t
>(value.z * 255.0f));
153 throw std::runtime_error(
"Unknown Color type");
162 std::stringstream stream;
163 stream <<
"#" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(value.x * 255.0f)
164 << std::setw(2) <<
static_cast<int>(value.y * 255.0f) << std::setw(2)
165 <<
static_cast<int>(value.z * 255.0f);
172 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
176 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
179 constexpr bool operator==(
const Color& rhs)
const noexcept
181 return value == rhs.value;
183 constexpr bool operator!=(
const Color& rhs)
const noexcept
185 return !(*
this == rhs);
189 friend constexpr Color operator+(
const Color& lhs,
const Color& rhs)
noexcept
191 return Color(lhs.value.x + rhs.value.x, lhs.value.y + rhs.value.y, lhs.value.z + rhs.value.z,
192 lhs.value.w + rhs.value.w);
194 friend constexpr Color operator-(
const Color& lhs,
const Color& rhs)
noexcept
196 return Color(lhs.value.x - rhs.value.x, lhs.value.y - rhs.value.y, lhs.value.z - rhs.value.z,
197 lhs.value.w - rhs.value.w);
199 friend constexpr Color operator*(
const Color& lhs,
const Color& rhs)
noexcept
201 return Color(lhs.value.x * rhs.value.x, lhs.value.y * rhs.value.y, lhs.value.z * rhs.value.z,
202 lhs.value.w * rhs.value.w);
204 friend constexpr Color operator/(
const Color& lhs,
const Color& rhs)
noexcept
206 return Color(lhs.value.x / rhs.value.x, lhs.value.y / rhs.value.y, lhs.value.z / rhs.value.z,
207 lhs.value.w / rhs.value.w);
209 friend constexpr Color operator*(
float lhs,
const Color& rhs)
noexcept
211 return Color(lhs * rhs.value.x, lhs * rhs.value.y, lhs * rhs.value.z, lhs * rhs.value.w);
213 friend constexpr Color operator*(
const Color& lhs,
float rhs)
noexcept
217 friend constexpr Color operator/(
const Color& lhs,
float rhs)
noexcept
219 return Color(lhs.value.x / rhs, lhs.value.y / rhs, lhs.value.z / rhs, lhs.value.w / rhs);
221 friend constexpr Color operator+(
const Color& lhs,
float rhs)
noexcept
223 return Color(lhs.value.x + rhs, lhs.value.y + rhs, lhs.value.z + rhs, lhs.value.w);
225 friend constexpr Color operator-(
const Color& lhs,
float rhs)
noexcept
227 return Color(lhs.value.x - rhs, lhs.value.y - rhs, lhs.value.z - rhs, lhs.value.w);
230 constexpr Color& operator+=(
const Color& rhs)
noexcept
235 constexpr Color& operator-=(
const Color& rhs)
noexcept
240 constexpr Color& operator*=(
const Color& rhs)
noexcept
245 constexpr Color& operator/=(
const Color& rhs)
noexcept
250 constexpr Color& operator+=(
float rhs)
noexcept
255 constexpr Color& operator-=(
float rhs)
noexcept
260 constexpr Color& operator*=(
float rhs)
noexcept
265 constexpr Color& operator/=(
float rhs)
noexcept
271 friend const Node& operator>>(
const Node& node, Color& color);
272 friend Node& operator<<(Node& node,
const Color& color);
274 static const Color Clear;
275 static const Color Black;
276 static const Color Grey;
277 static const Color Silver;
278 static const Color White;
279 static const Color Maroon;
280 static const Color Red;
281 static const Color Olive;
282 static const Color Yellow;
283 static const Color Green;
284 static const Color Lime;
285 static const Color Teal;
286 static const Color Aqua;
287 static const Color Navy;
288 static const Color Blue;
289 static const Color Purple;
290 static const Color Fuchsia;
297 inline const Node& operator>>(
const Node& node, Color& color)
299 if (!node.GetProperties().empty())
301 color.value.x = String::From<float>(node[
"r"].GetValue());
302 color.value.y = String::From<float>(node[
"g"].GetValue());
303 color.value.z = String::From<float>(node[
"b"].GetValue());
304 color.value.w = String::From<float>(node[
"a"].GetValue());
315 inline Node& operator<<(Node& node,
const Color& color)
317 node[
"r"].SetValue(String::To(color.value.x));
318 node[
"r"].SetType(NodeType::Decimal);
319 node[
"g"].SetValue(String::To(color.value.y));
320 node[
"g"].SetType(NodeType::Decimal);
321 node[
"b"].SetValue(String::To(color.value.z));
322 node[
"b"].SetType(NodeType::Decimal);
323 node[
"a"].SetValue(String::To(color.value.w));
324 node[
"a"].SetType(NodeType::Decimal);
A polished Color class storing RGBA values in a glm::vec4.
Definition Color.hpp:23
float Length() const noexcept
Returns the length of the color vector.
Definition Color.hpp:129
Color Normalize() const
Normalizes this color (treating it as a 4D vector).
Definition Color.hpp:108
std::string GetHex() const
Returns the hexadecimal string representation (e.g. "#FF00FF").
Definition Color.hpp:160
constexpr Color Lerp(const Color &other, float t) const noexcept
Returns a color that is the linear interpolation between this and another color.
Definition Color.hpp:99
Color(std::string hex, float a=1.0f)
Constructs a Color from a hexadecimal string (e.g. "#FF00FF").
Definition Color.hpp:79
constexpr float operator[](uint32_t i) const noexcept
Component access (0: red, 1: green, 2: blue, 3: alpha)
Definition Color.hpp:170
constexpr Color(uint32_t i, Type type=Type::RGB) noexcept
Constructs a Color from a packed 32-bit integer. The 'type' parameter defines the component order.
Definition Color.hpp:49
constexpr uint32_t GetInt(Type type=Type::RGBA) const noexcept
Returns the packed 32-bit integer representation.
Definition Color.hpp:137
constexpr Color(float r, float g, float b, float a=1.0f) noexcept
Constructs a Color from individual float components (0.0f to 1.0f).
Definition Color.hpp:40
constexpr float Length2() const noexcept
Returns the squared length of the color vector.
Definition Color.hpp:121