3#include "pixelbullet/serialization/node.h"
5#define GLM_ENABLE_EXPERIMENTAL
6#include <glm/geometric.hpp>
33 constexpr Color() noexcept
41 constexpr Color(
float r,
float g,
float b,
float a = 1.0f) noexcept
50 constexpr Color(uint32_t i, Packing packing = Packing::Rgb)
55 value.x =
static_cast<float>((i >> 24) & 0xFF) / 255.0f;
56 value.y =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
57 value.z =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
58 value.w =
static_cast<float>(i & 0xFF) / 255.0f;
61 value.w =
static_cast<float>((i >> 24) & 0xFF) / 255.0f;
62 value.x =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
63 value.y =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
64 value.z =
static_cast<float>(i & 0xFF) / 255.0f;
67 value.x =
static_cast<float>((i >> 16) & 0xFF) / 255.0f;
68 value.y =
static_cast<float>((i >> 8) & 0xFF) / 255.0f;
69 value.z =
static_cast<float>(i & 0xFF) / 255.0f;
73 throw std::runtime_error(
"Unknown Color packing");
80 explicit Color(std::string hex,
float a = 1.0f)
82 if (!hex.empty() && hex[0] ==
'#')
88 throw std::runtime_error(
"Invalid hex color string");
90 uint32_t hexValue = 0;
91 for (
char digit : hex)
93 const int value = hex_digit_value(digit);
96 throw std::runtime_error(
"Invalid hex color string");
98 hexValue = (hexValue << 4u) | static_cast<uint32_t>(value);
101 value.x =
static_cast<float>((hexValue >> 16) & 0xFF) / 255.0f;
102 value.y =
static_cast<float>((hexValue >> 8) & 0xFF) / 255.0f;
103 value.z =
static_cast<float>(hexValue & 0xFF) / 255.0f;
110 constexpr Color
lerp(
const Color& other,
float t)
const noexcept
112 return Color(value.x + (other.value.x - value.x) * t, value.y + (other.value.y - value.y) * t,
113 value.z + (other.value.z - value.z) * t, value.w + (other.value.w - value.w) * t);
124 throw std::runtime_error(
"Cannot normalize zero-length color");
126 return Color(value.x / len, value.y / len, value.z / len, value.w / len);
134 return glm::dot(value, value);
148 constexpr uint32_t
packed(Packing packing = Packing::Rgba)
const
153 return (
static_cast<uint32_t
>(to_byte(value.x)) << 24) | (
static_cast<uint32_t
>(to_byte(value.y)) << 16) |
154 (
static_cast<uint32_t
>(to_byte(value.z)) << 8) |
static_cast<uint32_t
>(to_byte(value.w));
156 return (
static_cast<uint32_t
>(to_byte(value.w)) << 24) | (
static_cast<uint32_t
>(to_byte(value.x)) << 16) |
157 (
static_cast<uint32_t
>(to_byte(value.y)) << 8) |
static_cast<uint32_t
>(to_byte(value.z));
159 return (
static_cast<uint32_t
>(to_byte(value.x)) << 16) | (
static_cast<uint32_t
>(to_byte(value.y)) << 8) |
160 static_cast<uint32_t
>(to_byte(value.z));
162 throw std::runtime_error(
"Unknown Color packing");
171 std::stringstream stream;
172 stream <<
"#" << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(to_byte(value.x))
173 << std::setw(2) <<
static_cast<int>(to_byte(value.y)) << std::setw(2) <<
static_cast<int>(to_byte(value.z));
180 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
184 return (i == 0) ? value.x : (i == 1) ? value.y : (i == 2) ? value.z : value.w;
187 constexpr bool operator==(
const Color& rhs)
const noexcept
189 return value == rhs.value;
191 constexpr bool operator!=(
const Color& rhs)
const noexcept
193 return !(*
this == rhs);
197 friend constexpr Color operator+(
const Color& lhs,
const Color& rhs)
noexcept
199 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);
201 friend constexpr Color operator-(
const Color& lhs,
const Color& rhs)
noexcept
203 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);
205 friend constexpr Color operator*(
const Color& lhs,
const Color& rhs)
noexcept
207 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);
209 friend constexpr Color operator/(
const Color& lhs,
const Color& rhs)
noexcept
211 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);
213 friend constexpr Color operator*(
float lhs,
const Color& rhs)
noexcept
215 return Color(lhs * rhs.value.x, lhs * rhs.value.y, lhs * rhs.value.z, lhs * rhs.value.w);
217 friend constexpr Color operator*(
const Color& lhs,
float rhs)
noexcept
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 / rhs);
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 + rhs);
229 friend constexpr Color operator-(
const Color& lhs,
float rhs)
noexcept
231 return Color(lhs.value.x - rhs, lhs.value.y - rhs, lhs.value.z - rhs, lhs.value.w - rhs);
234 constexpr Color& operator+=(
const Color& rhs)
noexcept
239 constexpr Color& operator-=(
const Color& rhs)
noexcept
244 constexpr Color& operator*=(
const Color& rhs)
noexcept
249 constexpr Color& operator/=(
const Color& rhs)
noexcept
254 constexpr Color& operator+=(
float rhs)
noexcept
259 constexpr Color& operator-=(
float rhs)
noexcept
264 constexpr Color& operator*=(
float rhs)
noexcept
269 constexpr Color& operator/=(
float rhs)
noexcept
275 friend const Node& operator>>(
const Node& node, Color& color);
276 friend Node& operator<<(Node& node,
const Color& color);
278 static const Color clear;
279 static const Color black;
280 static const Color grey;
281 static const Color silver;
282 static const Color white;
283 static const Color maroon;
284 static const Color red;
285 static const Color olive;
286 static const Color yellow;
287 static const Color green;
288 static const Color lime;
289 static const Color teal;
290 static const Color aqua;
291 static const Color navy;
292 static const Color blue;
293 static const Color purple;
294 static const Color fuchsia;
297 static constexpr uint8_t to_byte(
float component)
noexcept
299 if (!(component > 0.0f))
303 if (component >= 1.0f)
307 return static_cast<uint8_t
>(component * 255.0f + 0.5f);
310 static constexpr int hex_digit_value(
char digit)
noexcept
312 if (digit >=
'0' && digit <=
'9')
316 if (digit >=
'A' && digit <=
'F')
318 return digit -
'A' + 10;
320 if (digit >=
'a' && digit <=
'f')
322 return digit -
'a' + 10;
333 if (!node.properties().empty())
335 node[
"r"] >> color.value.x;
336 node[
"g"] >> color.value.y;
337 node[
"b"] >> color.value.z;
338 node[
"a"] >> color.value.w;
351 node[
"r"] << color.value.x;
352 node[
"g"] << color.value.y;
353 node[
"b"] << color.value.z;
354 node[
"a"] << color.value.w;
A polished Color class storing RGBA values in a glm::vec4.
Definition color.h:24
Color normalized() const
Normalizes this color (treating it as a 4D vector).
Definition color.h:119
constexpr uint32_t packed(Packing packing=Packing::Rgba) const
Returns the packed 32-bit integer representation.
Definition color.h:148
Color(std::string hex, float a=1.0f)
Constructs a Color from a hexadecimal string (e.g. "#FF00FF").
Definition color.h:80
std::string to_hex() const
Returns the hexadecimal string representation (e.g. "#FF00FF").
Definition color.h:169
constexpr Color(uint32_t i, Packing packing=Packing::Rgb)
Constructs a Color from a packed 32-bit integer. The 'packing' parameter defines the component order.
Definition color.h:50
constexpr float length_squared() const noexcept
Returns the squared length of the color vector.
Definition color.h:132
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:110
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:41
constexpr float operator[](uint32_t i) const noexcept
Component access (0: red, 1: green, 2: blue, 3: alpha).
Definition color.h:178
float length() const noexcept
Returns the length of the color vector.
Definition color.h:140
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:49