11 struct std::hash<qx::color>
13 constexpr
size_t operator()(
const qx::color& color)
const noexcept
16 qx::hash_combine(nHash, color.
r());
17 qx::hash_combine(nHash, color.
g());
18 qx::hash_combine(nHash, color.
b());
19 qx::hash_combine(nHash, color.
a());
40 template<
class char_t>
45 #define _QX_ADD_COLOR(snakeCaseName, pascalCaseName, lowerCaseName, r, g, b) \
46 add(QX_STR_PREFIX(char_t, #snakeCaseName), color_name_type::css_snake, color(r, g, b)); \
47 add(QX_STR_PREFIX(char_t, #pascalCaseName), color_name_type::css_pascal, color(r, g, b)); \
48 add(QX_STR_PREFIX(char_t, #lowerCaseName), color_name_type::css_lower, color(r, g, b));
77 std::optional<basic_string_view<char_t>>
get(
const color&
color, color_name_type eColorNameType)
const noexcept;
80 std::unordered_map<basic_string<char_t>,
color> m_StringToColor;
81 std::unordered_map<color, basic_string<char_t>> m_ColorToCssSnake;
82 std::unordered_map<color, basic_string<char_t>> m_ColorToCssPascal;
83 std::unordered_map<color, basic_string<char_t>> m_ColorToCssLower;
86 template<
class char_t>
92 template<
class char_t>
95 color_name_type eColorNameType,
98 if (eColorNameType == color_name_type::css_snake)
99 m_ColorToCssSnake[
color] = sName;
100 else if (eColorNameType == color_name_type::css_pascal)
101 m_ColorToCssPascal[
color] = sName;
102 else if (eColorNameType == color_name_type::css_lower)
103 m_ColorToCssLower[
color] = sName;
105 m_StringToColor[sName] = std::move(
color);
108 template<
class char_t>
111 if (
const auto it = m_StringToColor.find(sName); it != m_StringToColor.end())
117 template<
class char_t>
120 color_name_type eColorNameType)
const noexcept
122 if (eColorNameType == color_name_type::css_snake)
124 if (
const auto it = m_ColorToCssSnake.find(
color); it != m_ColorToCssSnake.end())
127 else if (eColorNameType == color_name_type::css_pascal)
129 if (
const auto it = m_ColorToCssPascal.find(
color); it != m_ColorToCssPascal.end())
132 else if (eColorNameType == color_name_type::css_lower)
134 if (
const auto it = m_ColorToCssLower.find(
color); it != m_ColorToCssLower.end())
143 constexpr color::color(
float fRed,
float fGreen,
float fBlue,
float fAlpha) noexcept
145 assign_checked({ fRed, fGreen, fBlue, fAlpha });
148 constexpr color::color(
int nRed,
int nGreen,
int nBlue,
int nAlpha) noexcept
149 :
color(dec_to_float(nRed), dec_to_float(nGreen), dec_to_float(nBlue), dec_to_float(nAlpha))
153 constexpr color::color(
u64 nHexValue) noexcept
155 dec_to_float(nHexValue >> 24 & 0xFF),
156 dec_to_float(nHexValue >> 16 & 0xFF),
157 dec_to_float(nHexValue >> 8 & 0xFF),
158 dec_to_float(nHexValue >> 0 & 0xFF))
162 constexpr color::color(
const glm::ivec3& vec3) noexcept
163 :
color(dec_to_float(vec3.x), dec_to_float(vec3.y), dec_to_float(vec3.z), 1.f)
167 constexpr color::color(
const glm::ivec4& vec4) noexcept
168 :
color(dec_to_float(vec4.x), dec_to_float(vec4.y), dec_to_float(vec4.z), dec_to_float(vec4.w))
194 return m_Color[
static_cast<glm::length_t
>(i)];
199 return m_Color[
static_cast<glm::length_t
>(i)];
204 return float_to_dec(m_Color.x);
209 return float_to_dec(m_Color.y);
214 return float_to_dec(m_Color.z);
219 return float_to_dec(m_Color.w);
229 const unsigned int r =
static_cast<unsigned int>(float_to_dec(m_Color.x));
230 const unsigned int g =
static_cast<unsigned int>(float_to_dec(m_Color.y));
231 const unsigned int b =
static_cast<unsigned int>(float_to_dec(m_Color.z));
233 return ((
r & 0xff) << 16) + ((
g & 0xff) << 8) + ((
b & 0xff) << 0);
238 const unsigned int r =
static_cast<unsigned int>(float_to_dec(m_Color.x));
239 const unsigned int g =
static_cast<unsigned int>(float_to_dec(m_Color.y));
240 const unsigned int b =
static_cast<unsigned int>(float_to_dec(m_Color.z));
241 const unsigned int a =
static_cast<unsigned int>(float_to_dec(m_Color.w));
243 return ((
r & 0xff) << 24) + ((
g & 0xff) << 16) + ((
b & 0xff) << 8) + (
a & 0xff);
248 const unsigned int r =
static_cast<unsigned int>(float_to_dec(m_Color.x));
249 const unsigned int g =
static_cast<unsigned int>(float_to_dec(m_Color.y));
250 const unsigned int b =
static_cast<unsigned int>(float_to_dec(m_Color.z));
251 const unsigned int a =
static_cast<unsigned int>(float_to_dec(m_Color.w));
253 return ((
a & 0xff) << 24) + ((
r & 0xff) << 16) + ((
g & 0xff) << 8) + (
b & 0xff);
256 constexpr
bool color::operator==(
const color& other)
const noexcept
258 return m_Color == other.m_Color;
261 constexpr color::operator glm::vec3() const noexcept
266 constexpr color::operator glm::vec4() const noexcept
273 assign_component_checked(m_Color.x, m_Color.x + fDeltaValue);
278 assign_component_checked(m_Color.y, m_Color.y + fDeltaValue);
283 assign_component_checked(m_Color.z, m_Color.z + fDeltaValue);
288 assign_component_checked(m_Color.w, m_Color.w + fDeltaValue);
293 assign_component_checked(m_Color.x, m_Color.x + dec_to_float(nDeltaValue));
298 assign_component_checked(m_Color.y, m_Color.y + dec_to_float(nDeltaValue));
303 assign_component_checked(m_Color.z, m_Color.z + dec_to_float(nDeltaValue));
308 assign_component_checked(m_Color.w, m_Color.w + dec_to_float(nDeltaValue));
313 assign_component_checked(m_Color.x, fValue);
318 assign_component_checked(m_Color.y, fValue);
323 assign_component_checked(m_Color.z, fValue);
328 assign_component_checked(m_Color.w, fValue);
333 assign_component_checked(m_Color.x, dec_to_float(nValue));
338 assign_component_checked(m_Color.y, dec_to_float(nValue));
343 assign_component_checked(m_Color.z, dec_to_float(nValue));
348 assign_component_checked(m_Color.w, dec_to_float(nValue));
358 m_Color.x *= (100.f + fPercent) / 100.f;
359 m_Color.y *= (100.f + fPercent) / 100.f;
360 m_Color.z *= (100.f + fPercent) / 100.f;
382 std::optional<color> optColor;
384 const bool bStartsWith0x =
385 svColorName.starts_with(QXT(
"0x")) && (svColorName.size() == 8 || svColorName.size() == 10);
387 if (bStartsWith0x || svColorName.starts_with(QXT(
"#")) && svColorName.size() == 7)
391 const size_t nOffset = bStartsWith0x ? 2 : 1;
392 string sColorName = string_view(svColorName.data() + nOffset, svColorName.size() - nOffset);
394 auto ReadHex = [](
string& s) -> std::optional<color>
397 if (
const auto optInt = s.template to<u64>(QXT(
"%llx")))
398 return color(*optInt);
403 if (sColorName.
length() == 6)
405 sColorName += QXT(
"FF");
406 optColor = ReadHex(sColorName);
408 else if (sColorName.
length() == 8)
410 optColor = ReadHex(sColorName);
420 static_cast<u8>(nHash >> 0 & 0xFF),
421 static_cast<u8>(nHash >> 8 & 0xFF),
422 static_cast<u8>(nHash >> 16 & 0xFF));
427 return color(0, 0, 0, 0);
432 return glm::vec4::length();
435 constexpr
float color::clamp_value(
float fValue) noexcept
437 return std::clamp(fValue, 0.f, 1.f);
440 constexpr
float color::dec_to_float(
int nValue) noexcept
442 return static_cast<float>(nValue) / 255.f;
445 constexpr
int color::float_to_dec(
float fValue) noexcept
454 return static_cast<int>(fValue * 255.f);
457 constexpr
void color::assign_checked(
const glm::vec4& other) noexcept
459 assign_component_checked(m_Color.x, other.x);
460 assign_component_checked(m_Color.y, other.y);
461 assign_component_checked(m_Color.z, other.z);
462 assign_component_checked(m_Color.w, other.w);
465 constexpr
void color::assign_component_checked(
float& pComponent,
float fValue) noexcept
467 pComponent = clamp_value(fValue);
472 template<
class char_t>
473 struct QX_FMT_NS::formatter<qx::color, char_t>
475 template<
class format_parse_context_t>
476 constexpr
auto parse(format_parse_context_t& ctx)
478 auto it = ctx.begin();
484 optColorNameType = qx::color_name_type::css_snake;
489 optColorNameType = qx::color_name_type::css_pascal;
494 optColorNameType = qx::color_name_type::css_lower;
499 optColorNameType = qx::color_name_type::hex_lower;
504 optColorNameType = qx::color_name_type::hex_upper;
509 optColorNameType = qx::color_name_type::rgb;
523 throw QX_FMT_NS::format_error(
"unknown spec");
528 template<
class format_context_t>
529 auto format(
const qx::color& color, format_context_t& ctx)
const -> decltype(ctx.out())
531 const qx::color_name_type eColorNameType =
532 optColorNameType ? *optColorNameType : qx::color_name_type::hex_upper;
534 std::optional<qx::basic_string_view<char_t>> optResult =
539 return QX_FMT_NS::format_to(ctx.out(),
QX_STR_PREFIX(char_t,
"{}"), *optResult);
543 if (eColorNameType == qx::color_name_type::rgb)
545 auto it = QX_FMT_NS::format_to(
557 else if (eColorNameType == qx::color_name_type::hex_lower)
559 auto it = QX_FMT_NS::format_to(
574 auto it = QX_FMT_NS::format_to(
581 if (bAddAlpha || !optColorNameType)
589 std::optional<qx::color_name_type> optColorNameType;
590 bool bAddAlpha =
false;
size_type length() const noexcept
Get string length.
constexpr float g() const noexcept
Get green component.
constexpr void update_r_dec(int nDeltaValue) noexcept
Add value to red component.
constexpr void update_a(float fDeltaValue) noexcept
Add value to alpha component.
constexpr int r_dec() const noexcept
Get red component as decimal.
static std::optional< color > from_string(string_view svColorName) noexcept
Try to create color from string.
constexpr void update_g(float fDeltaValue) noexcept
Add value to green component.
constexpr void update_b(float fDeltaValue) noexcept
Add value to blue component.
constexpr void darken(float fPercent) noexcept
Make color darker.
constexpr int g_dec() const noexcept
Get green component as decimal.
constexpr float a() const noexcept
Get alpha component.
constexpr const float * data() const noexcept
Get pointer to the first component.
constexpr float b() const noexcept
Get blue component.
constexpr float r() const noexcept
Get red component.
constexpr void set_g(float fValue) noexcept
Set new value of green component.
constexpr float & operator[](size_t i) noexcept
Get color component.
constexpr void set_b(float fValue) noexcept
Set new value of blue component.
constexpr int b_dec() const noexcept
Get blue component as decimal.
static constexpr color from_hash(size_t nHash) noexcept
Generate a color unique for a hash value.
static constexpr color empty() noexcept
Get empty color (0, 0, 0, 0)
constexpr void update_g_dec(int nDeltaValue) noexcept
Add value to green component.
constexpr void set_r(float fValue) noexcept
Set new value of red component.
constexpr unsigned int hex_rgba() const noexcept
Get color as hex.
constexpr unsigned int hex_rgb() const noexcept
Get color as hex.
constexpr void update_r(float fDeltaValue) noexcept
Add value to red component.
constexpr int a_dec() const noexcept
Get alpha component as decimal.
constexpr void set_g_dec(int nValue) noexcept
Set new value of green component.
static constexpr size_t size() noexcept
Get number of float components.
constexpr void brighten(float fPercent) noexcept
Make color brighter.
constexpr unsigned int hex_argb() const noexcept
Get color as hex.
constexpr void update_b_dec(int nDeltaValue) noexcept
Add value to blue component.
constexpr void set_r_dec(int nValue) noexcept
Set new value of red component.
constexpr void set_b_dec(int nValue) noexcept
Set new value of blue component.
constexpr void update_a_dec(int nDeltaValue) noexcept
Add value to alpha component.
constexpr void set_a(float fValue) noexcept
Set new value of alpha component.
constexpr void set_a_dec(int nValue) noexcept
Set new value of alpha component.
Helper class for string -> color conversion.
std::optional< color > get(const basic_string< char_t > &sName) const noexcept
Try to get ac olor from a color name.
void add(basic_string< char_t > sName, color_name_type eColorNameType, color color) noexcept
Add new color to the mapping.
Inherit the necessary singleton class from this.
#define _QX_COLORS(macro)
#define QX_CHAR_PREFIX(value_t, ch)
Chose witch of prefixes add to char : L or none.
#define QX_STR_PREFIX(value_t, str)
Chose witch of prefixes add to string : L or none.
std::uint8_t u8
0 .. 65 535