3#include "pixelbullet/serialization/node.h"
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)
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
142 return (
static_cast<uint32_t
>(value.x * 255.0f) << 24) | (
static_cast<uint32_t
>(value.y * 255.0f) << 16) |
143 (
static_cast<uint32_t
>(value.z * 255.0f) << 8) | (
static_cast<uint32_t
>(value.w * 255.0f));
145 return (
static_cast<uint32_t
>(value.w * 255.0f) << 24) | (
static_cast<uint32_t
>(value.x * 255.0f) << 16) |
146 (
static_cast<uint32_t
>(value.y * 255.0f) << 8) | (
static_cast<uint32_t
>(value.z * 255.0f));
148 return (
static_cast<uint32_t
>(value.x * 255.0f) << 16) | (
static_cast<uint32_t
>(value.y * 255.0f) << 8) |
149 (
static_cast<uint32_t
>(value.z * 255.0f));
151 throw std::runtime_error(
"Unknown Color type");
160 std::stringstream stream;
161 stream <<
"#" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(value.x * 255.0f) << std::setw(2)
162 <<
static_cast<int>(value.y * 255.0f) << std::setw(2) <<
static_cast<int>(value.z * 255.0f);
169 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
173 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
176 constexpr bool operator==(
const Color& rhs)
const noexcept
178 return value == rhs.value;
180 constexpr bool operator!=(
const Color& rhs)
const noexcept
182 return !(*
this == rhs);
186 friend constexpr Color operator+(
const Color& lhs,
const Color& rhs)
noexcept
188 return Color(lhs.value.x + rhs.value.x, lhs.value.y + rhs.value.y, lhs.value.z + rhs.value.z, lhs.value.w + rhs.value.w);
190 friend constexpr Color operator-(
const Color& lhs,
const Color& rhs)
noexcept
192 return Color(lhs.value.x - rhs.value.x, lhs.value.y - rhs.value.y, lhs.value.z - rhs.value.z, 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, lhs.value.w * rhs.value.w);
198 friend constexpr Color operator/(
const Color& lhs,
const Color& rhs)
noexcept
200 return Color(lhs.value.x / rhs.value.x, lhs.value.y / rhs.value.y, lhs.value.z / rhs.value.z, lhs.value.w / rhs.value.w);
202 friend constexpr Color operator*(
float lhs,
const Color& rhs)
noexcept
204 return Color(lhs * rhs.value.x, lhs * rhs.value.y, lhs * rhs.value.z, lhs * rhs.value.w);
206 friend constexpr Color operator*(
const Color& lhs,
float rhs)
noexcept
210 friend constexpr Color operator/(
const Color& lhs,
float rhs)
noexcept
212 return Color(lhs.value.x / rhs, lhs.value.y / rhs, lhs.value.z / rhs, lhs.value.w / rhs);
214 friend constexpr Color operator+(
const Color& lhs,
float rhs)
noexcept
216 return Color(lhs.value.x + rhs, lhs.value.y + rhs, lhs.value.z + rhs, lhs.value.w);
218 friend constexpr Color operator-(
const Color& lhs,
float rhs)
noexcept
220 return Color(lhs.value.x - rhs, lhs.value.y - rhs, lhs.value.z - rhs, lhs.value.w);
223 constexpr Color& operator+=(
const Color& rhs)
noexcept
228 constexpr Color& operator-=(
const Color& rhs)
noexcept
233 constexpr Color& operator*=(
const Color& rhs)
noexcept
238 constexpr Color& operator/=(
const Color& rhs)
noexcept
243 constexpr Color& operator+=(
float rhs)
noexcept
248 constexpr Color& operator-=(
float rhs)
noexcept
253 constexpr Color& operator*=(
float rhs)
noexcept
258 constexpr Color& operator/=(
float rhs)
noexcept
264 friend const Node& operator>>(
const Node& node, Color& color);
265 friend Node& operator<<(Node& node,
const Color& color);
267 static const Color clear;
268 static const Color black;
269 static const Color grey;
270 static const Color silver;
271 static const Color white;
272 static const Color maroon;
273 static const Color red;
274 static const Color olive;
275 static const Color yellow;
276 static const Color green;
277 static const Color lime;
278 static const Color teal;
279 static const Color aqua;
280 static const Color navy;
281 static const Color blue;
282 static const Color purple;
283 static const Color fuchsia;
290inline const Node& operator>>(
const Node& node, Color& color)
292 if (!node.GetProperties().empty())
294 node[
"r"] >> color.value.x;
295 node[
"g"] >> color.value.y;
296 node[
"b"] >> color.value.z;
297 node[
"a"] >> color.value.w;
308inline Node& operator<<(Node& node,
const Color& color)
310 node[
"r"] << color.value.x;
311 node[
"g"] << color.value.y;
312 node[
"b"] << color.value.z;
313 node[
"a"] << color.value.w;
A polished Color class storing RGBA values in a glm::vec4.
Definition color.h:23
Color Normalize() const
Normalizes this color (treating it as a 4D vector).
Definition color.h:108
Color(std::string hex, float a=1.0f)
Constructs a Color from a hexadecimal string (e.g. "#FF00FF").
Definition color.h:79
constexpr Color(uint32_t i, Type type=Type::RGB)
Constructs a Color from a packed 32-bit integer. The 'type' parameter defines the component order.
Definition color.h:49
float Length() const noexcept
Returns the length of the color vector.
Definition color.h:129
constexpr float Length2() const noexcept
Returns the squared length of the color vector.
Definition color.h:121
std::string GetHex() const
Returns the hexadecimal string representation (e.g. "#FF00FF").
Definition color.h:158
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.h:99
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.h:40
constexpr float operator[](uint32_t i) const noexcept
Component access (0: red, 1: green, 2: blue, 3: alpha)
Definition color.h:167
constexpr uint32_t GetInt(Type type=Type::RGBA) const
Returns the packed 32-bit integer representation.
Definition color.h:137