qxLib
color.inl
Go to the documentation of this file.
1 /**
2 
3  @file color.inl
4  @author Khrapov
5  @date 10.04.2021
6  @copyright (c) Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 
10 template<>
11 struct std::hash<qx::color>
12 {
13  constexpr size_t operator()(const qx::color& color) const noexcept
14  {
15  size_t nHash = 0;
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());
20  return nHash;
21  }
22 };
23 
24 namespace qx
25 {
26 
27 namespace details
28 {
29 
30 /**
31 
32  @class string_to_color_converter
33  @brief Helper class for string -> color conversion
34  @details The only purpose of this class is to hide the map from a user
35  @tparam char_t - string char type
36  @author Khrapov
37  @date 5.04.2023
38 
39 **/
40 template<class char_t>
41 class string_to_color_converter final : public singleton<string_to_color_converter<char_t>>
42 {
43  friend color;
44 
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));
49 
50 public:
51  string_to_color_converter() noexcept;
52 
53  /**
54  @brief Add new color to the mapping
55  @param sName - name registered for forward lookup
56  @param eColorNameType - css_snake, css_pascal or css_lower selects the reverse lookup table;
57  other formats only update the forward lookup
58  @param color - RGBA value associated with sName; an existing entry with
59  the same key is replaced
60  **/
61  void add(basic_string<char_t> sName, color_name_type eColorNameType, color color) noexcept;
62 
63  /**
64  @brief Try to get ac olor from a color name
65  @param sName - color name
66  @return - color or nullopt
67  **/
68  std::optional<color> get(const basic_string<char_t>& sName) const noexcept;
69 
70  /**
71  @brief Try to a color name get from a color
72  @param color - RGBA lookup key
73  @param eColorNameType - css_snake, css_pascal or css_lower selects the reverse lookup table
74  @return - registered name, or nullopt if the key is absent or
75  the requested name format is unsupported
76  **/
77  std::optional<basic_string_view<char_t>> get(const color& color, color_name_type eColorNameType) const noexcept;
78 
79 private:
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;
84 };
85 
86 template<class char_t>
88 {
89  _QX_COLORS(_QX_ADD_COLOR)
90 }
91 
92 template<class char_t>
95  color_name_type eColorNameType,
96  color color) noexcept
97 {
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;
104 
105  m_StringToColor[sName] = std::move(color);
106 }
107 
108 template<class char_t>
109 inline std::optional<color> string_to_color_converter<char_t>::get(const basic_string<char_t>& sName) const noexcept
110 {
111  if (const auto it = m_StringToColor.find(sName); it != m_StringToColor.end())
112  return it->second;
113 
114  return std::nullopt;
115 }
116 
117 template<class char_t>
118 inline std::optional<basic_string_view<char_t>> string_to_color_converter<char_t>::get(
119  const color& color,
120  color_name_type eColorNameType) const noexcept
121 {
122  if (eColorNameType == color_name_type::css_snake)
123  {
124  if (const auto it = m_ColorToCssSnake.find(color); it != m_ColorToCssSnake.end())
125  return it->second;
126  }
127  else if (eColorNameType == color_name_type::css_pascal)
128  {
129  if (const auto it = m_ColorToCssPascal.find(color); it != m_ColorToCssPascal.end())
130  return it->second;
131  }
132  else if (eColorNameType == color_name_type::css_lower)
133  {
134  if (const auto it = m_ColorToCssLower.find(color); it != m_ColorToCssLower.end())
135  return it->second;
136  }
137 
138  return std::nullopt;
139 }
140 
141 } // namespace details
142 
143 constexpr color::color(float fRed, float fGreen, float fBlue, float fAlpha) noexcept
144 {
145  assign_checked({ fRed, fGreen, fBlue, fAlpha });
146 }
147 
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))
150 {
151 }
152 
153 constexpr color::color(u64 nHexValue) noexcept
154  : color(
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))
159 {
160 }
161 
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)
164 {
165 }
166 
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))
169 {
170 }
171 
172 constexpr float color::r() const noexcept
173 {
174  return m_Color.x;
175 }
176 
177 constexpr float color::g() const noexcept
178 {
179  return m_Color.y;
180 }
181 
182 constexpr float color::b() const noexcept
183 {
184  return m_Color.z;
185 }
186 
187 constexpr float color::a() const noexcept
188 {
189  return m_Color.w;
190 }
191 
192 constexpr float& color::operator[](size_t i) noexcept
193 {
194  return m_Color[static_cast<glm::length_t>(i)];
195 }
196 
197 constexpr const float& color::operator[](size_t i) const noexcept
198 {
199  return m_Color[static_cast<glm::length_t>(i)];
200 }
201 
202 constexpr int color::r_dec() const noexcept
203 {
204  return float_to_dec(m_Color.x);
205 }
206 
207 constexpr int color::g_dec() const noexcept
208 {
209  return float_to_dec(m_Color.y);
210 }
211 
212 constexpr int color::b_dec() const noexcept
213 {
214  return float_to_dec(m_Color.z);
215 }
216 
217 constexpr int color::a_dec() const noexcept
218 {
219  return float_to_dec(m_Color.w);
220 }
221 
222 constexpr const float* color::data() const noexcept
223 {
224  return &(m_Color.x);
225 }
226 
227 constexpr unsigned int color::hex_rgb() const noexcept
228 {
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));
232 
233  return ((r & 0xff) << 16) + ((g & 0xff) << 8) + ((b & 0xff) << 0);
234 }
235 
236 constexpr unsigned int color::hex_rgba() const noexcept
237 {
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));
242 
243  return ((r & 0xff) << 24) + ((g & 0xff) << 16) + ((b & 0xff) << 8) + (a & 0xff);
244 }
245 
246 constexpr unsigned int color::hex_argb() const noexcept
247 {
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));
252 
253  return ((a & 0xff) << 24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
254 }
255 
256 constexpr bool color::operator==(const color& other) const noexcept
257 {
258  return m_Color == other.m_Color;
259 }
260 
261 constexpr color::operator glm::vec3() const noexcept
262 {
263  return m_Color;
264 }
265 
266 constexpr color::operator glm::vec4() const noexcept
267 {
268  return m_Color;
269 }
270 
271 constexpr void color::update_r(float fDeltaValue) noexcept
272 {
273  assign_component_checked(m_Color.x, m_Color.x + fDeltaValue);
274 }
275 
276 constexpr void color::update_g(float fDeltaValue) noexcept
277 {
278  assign_component_checked(m_Color.y, m_Color.y + fDeltaValue);
279 }
280 
281 constexpr void color::update_b(float fDeltaValue) noexcept
282 {
283  assign_component_checked(m_Color.z, m_Color.z + fDeltaValue);
284 }
285 
286 constexpr void color::update_a(float fDeltaValue) noexcept
287 {
288  assign_component_checked(m_Color.w, m_Color.w + fDeltaValue);
289 }
290 
291 constexpr void color::update_r_dec(int nDeltaValue) noexcept
292 {
293  assign_component_checked(m_Color.x, m_Color.x + dec_to_float(nDeltaValue));
294 }
295 
296 constexpr void color::update_g_dec(int nDeltaValue) noexcept
297 {
298  assign_component_checked(m_Color.y, m_Color.y + dec_to_float(nDeltaValue));
299 }
300 
301 constexpr void color::update_b_dec(int nDeltaValue) noexcept
302 {
303  assign_component_checked(m_Color.z, m_Color.z + dec_to_float(nDeltaValue));
304 }
305 
306 constexpr void color::update_a_dec(int nDeltaValue) noexcept
307 {
308  assign_component_checked(m_Color.w, m_Color.w + dec_to_float(nDeltaValue));
309 }
310 
311 constexpr void color::set_r(float fValue) noexcept
312 {
313  assign_component_checked(m_Color.x, fValue);
314 }
315 
316 constexpr void color::set_g(float fValue) noexcept
317 {
318  assign_component_checked(m_Color.y, fValue);
319 }
320 
321 constexpr void color::set_b(float fValue) noexcept
322 {
323  assign_component_checked(m_Color.z, fValue);
324 }
325 
326 constexpr void color::set_a(float fValue) noexcept
327 {
328  assign_component_checked(m_Color.w, fValue);
329 }
330 
331 constexpr void color::set_r_dec(int nValue) noexcept
332 {
333  assign_component_checked(m_Color.x, dec_to_float(nValue));
334 }
335 
336 constexpr void color::set_g_dec(int nValue) noexcept
337 {
338  assign_component_checked(m_Color.y, dec_to_float(nValue));
339 }
340 
341 constexpr void color::set_b_dec(int nValue) noexcept
342 {
343  assign_component_checked(m_Color.z, dec_to_float(nValue));
344 }
345 
346 constexpr void color::set_a_dec(int nValue) noexcept
347 {
348  assign_component_checked(m_Color.w, dec_to_float(nValue));
349 }
350 
351 constexpr void color::darken(float fPercent) noexcept
352 {
353  brighten(-fPercent);
354 }
355 
356 constexpr void color::brighten(float fPercent) noexcept
357 {
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;
361 }
362 
363 constexpr color color::darken(const color& other, float fPercent) noexcept
364 {
365  color ret = other;
366  ret.darken(fPercent);
367  return ret;
368 }
369 
370 constexpr color color::brighten(const color& other, float fPercent) noexcept
371 {
372  color ret = other;
373  ret.brighten(fPercent);
374  return ret;
375 }
376 
377 inline std::optional<color> color::from_string(string_view svColorName) noexcept
378 {
379  if (const auto optColor = details::string_to_color_converter<char_type>::get_instance().get(svColorName))
380  return *optColor;
381 
382  std::optional<color> optColor;
383 
384  const bool bStartsWith0x =
385  svColorName.starts_with(QXT("0x")) && (svColorName.size() == 8 || svColorName.size() == 10);
386 
387  if (bStartsWith0x || svColorName.starts_with(QXT("#")) && svColorName.size() == 7)
388  {
389  // TODO no conversion method for string view so far
390  // but with SSO this should be fine
391  const size_t nOffset = bStartsWith0x ? 2 : 1;
392  string sColorName = string_view(svColorName.data() + nOffset, svColorName.size() - nOffset);
393 
394  auto ReadHex = [](string& s) -> std::optional<color>
395  {
396  s.to_lower();
397  if (const auto optInt = s.template to<u64>(QXT("%llx")))
398  return color(*optInt);
399 
400  return std::nullopt;
401  };
402 
403  if (sColorName.length() == 6)
404  {
405  sColorName += QXT("FF");
406  optColor = ReadHex(sColorName);
407  }
408  else if (sColorName.length() == 8)
409  {
410  optColor = ReadHex(sColorName);
411  }
412  }
413 
414  return optColor;
415 }
416 
417 constexpr color color::from_hash(size_t nHash) noexcept
418 {
419  return color(
420  static_cast<u8>(nHash >> 0 & 0xFF),
421  static_cast<u8>(nHash >> 8 & 0xFF),
422  static_cast<u8>(nHash >> 16 & 0xFF));
423 }
424 
425 constexpr color color::empty() noexcept
426 {
427  return color(0, 0, 0, 0);
428 }
429 
430 constexpr size_t color::size() noexcept
431 {
432  return glm::vec4::length();
433 }
434 
435 constexpr float color::clamp_value(float fValue) noexcept
436 {
437  return std::clamp(fValue, 0.f, 1.f);
438 }
439 
440 constexpr float color::dec_to_float(int nValue) noexcept
441 {
442  return static_cast<float>(nValue) / 255.f;
443 }
444 
445 constexpr int color::float_to_dec(float fValue) noexcept
446 {
447  // Saturate to the channel range and map NaN to zero before converting.
448  if (!(fValue > 0.f))
449  return 0;
450 
451  if (fValue >= 1.f)
452  return 255;
453 
454  return static_cast<int>(fValue * 255.f); //-V2003
455 }
456 
457 constexpr void color::assign_checked(const glm::vec4& other) noexcept
458 {
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);
463 }
464 
465 constexpr void color::assign_component_checked(float& pComponent, float fValue) noexcept
466 {
467  pComponent = clamp_value(fValue);
468 }
469 
470 } // namespace qx
471 
472 template<class char_t>
473 struct QX_FMT_NS::formatter<qx::color, char_t>
474 {
475  template<class format_parse_context_t>
476  constexpr auto parse(format_parse_context_t& ctx)
477  {
478  auto it = ctx.begin();
479  if (it != ctx.end())
480  {
481  if (*it == QX_CHAR_PREFIX(char_t, 's'))
482  {
483  ++it;
484  optColorNameType = qx::color_name_type::css_snake;
485  }
486  else if (*it == QX_CHAR_PREFIX(char_t, 'p'))
487  {
488  ++it;
489  optColorNameType = qx::color_name_type::css_pascal;
490  }
491  else if (*it == QX_CHAR_PREFIX(char_t, 'l'))
492  {
493  ++it;
494  optColorNameType = qx::color_name_type::css_lower;
495  }
496  else if (*it == QX_CHAR_PREFIX(char_t, 'x'))
497  {
498  ++it;
499  optColorNameType = qx::color_name_type::hex_lower;
500  }
501  else if (*it == QX_CHAR_PREFIX(char_t, 'X'))
502  {
503  ++it;
504  optColorNameType = qx::color_name_type::hex_upper;
505  }
506  else if (*it == QX_CHAR_PREFIX(char_t, 'r'))
507  {
508  ++it;
509  optColorNameType = qx::color_name_type::rgb;
510  }
511 
512  if (it != ctx.end())
513  {
514  if (*it == QX_CHAR_PREFIX(char_t, 'a'))
515  {
516  ++it;
517  bAddAlpha = true;
518  }
519  }
520  }
521 
522  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
523  throw QX_FMT_NS::format_error("unknown spec");
524 
525  return it;
526  }
527 
528  template<class format_context_t>
529  auto format(const qx::color& color, format_context_t& ctx) const -> decltype(ctx.out())
530  {
531  const qx::color_name_type eColorNameType =
532  optColorNameType ? *optColorNameType : qx::color_name_type::hex_upper;
533 
534  std::optional<qx::basic_string_view<char_t>> optResult =
536 
537  if (optResult)
538  {
539  return QX_FMT_NS::format_to(ctx.out(), QX_STR_PREFIX(char_t, "{}"), *optResult);
540  }
541  else
542  {
543  if (eColorNameType == qx::color_name_type::rgb)
544  {
545  auto it = QX_FMT_NS::format_to(
546  ctx.out(),
547  QX_STR_PREFIX(char_t, "{},{},{}"),
548  color.r_dec(),
549  color.g_dec(),
550  color.b_dec());
551 
552  if (bAddAlpha)
553  it = QX_FMT_NS::format_to(ctx.out(), QX_STR_PREFIX(char_t, ",{}"), color.a_dec());
554 
555  return it;
556  }
557  else if (eColorNameType == qx::color_name_type::hex_lower)
558  {
559  auto it = QX_FMT_NS::format_to(
560  ctx.out(),
561  QX_STR_PREFIX(char_t, "{:x}{:x}{:x}"),
562  color.r_dec(),
563  color.g_dec(),
564  color.b_dec());
565 
566  if (bAddAlpha)
567  it = QX_FMT_NS::format_to(ctx.out(), QX_STR_PREFIX(char_t, "{:x}"), color.a_dec());
568 
569  return it;
570  }
571  else
572  {
573  // hex_upper as a fallback
574  auto it = QX_FMT_NS::format_to(
575  ctx.out(),
576  QX_STR_PREFIX(char_t, "{:X}{:X}{:X}"),
577  color.r_dec(),
578  color.g_dec(),
579  color.b_dec());
580 
581  if (bAddAlpha || !optColorNameType)
582  it = QX_FMT_NS::format_to(ctx.out(), QX_STR_PREFIX(char_t, "{:X}"), color.a_dec());
583 
584  return it;
585  }
586  }
587  }
588 
589  std::optional<qx::color_name_type> optColorNameType;
590  bool bAddAlpha = false;
591 };
String class.
Definition: string.h:83
size_type length() const noexcept
Get string length.
Definition: string.inl:267
RGBA color.
Definition: color.h:196
constexpr float g() const noexcept
Get green component.
Definition: color.inl:177
constexpr void update_r_dec(int nDeltaValue) noexcept
Add value to red component.
Definition: color.inl:291
constexpr void update_a(float fDeltaValue) noexcept
Add value to alpha component.
Definition: color.inl:286
constexpr int r_dec() const noexcept
Get red component as decimal.
Definition: color.inl:202
static std::optional< color > from_string(string_view svColorName) noexcept
Try to create color from string.
Definition: color.inl:377
constexpr void update_g(float fDeltaValue) noexcept
Add value to green component.
Definition: color.inl:276
constexpr void update_b(float fDeltaValue) noexcept
Add value to blue component.
Definition: color.inl:281
constexpr void darken(float fPercent) noexcept
Make color darker.
Definition: color.inl:351
constexpr int g_dec() const noexcept
Get green component as decimal.
Definition: color.inl:207
constexpr float a() const noexcept
Get alpha component.
Definition: color.inl:187
constexpr const float * data() const noexcept
Get pointer to the first component.
Definition: color.inl:222
constexpr float b() const noexcept
Get blue component.
Definition: color.inl:182
constexpr float r() const noexcept
Get red component.
Definition: color.inl:172
constexpr void set_g(float fValue) noexcept
Set new value of green component.
Definition: color.inl:316
constexpr float & operator[](size_t i) noexcept
Get color component.
Definition: color.inl:192
constexpr void set_b(float fValue) noexcept
Set new value of blue component.
Definition: color.inl:321
constexpr int b_dec() const noexcept
Get blue component as decimal.
Definition: color.inl:212
static constexpr color from_hash(size_t nHash) noexcept
Generate a color unique for a hash value.
Definition: color.inl:417
static constexpr color empty() noexcept
Get empty color (0, 0, 0, 0)
Definition: color.inl:425
constexpr void update_g_dec(int nDeltaValue) noexcept
Add value to green component.
Definition: color.inl:296
constexpr void set_r(float fValue) noexcept
Set new value of red component.
Definition: color.inl:311
constexpr unsigned int hex_rgba() const noexcept
Get color as hex.
Definition: color.inl:236
constexpr unsigned int hex_rgb() const noexcept
Get color as hex.
Definition: color.inl:227
constexpr void update_r(float fDeltaValue) noexcept
Add value to red component.
Definition: color.inl:271
constexpr int a_dec() const noexcept
Get alpha component as decimal.
Definition: color.inl:217
constexpr void set_g_dec(int nValue) noexcept
Set new value of green component.
Definition: color.inl:336
static constexpr size_t size() noexcept
Get number of float components.
Definition: color.inl:430
constexpr void brighten(float fPercent) noexcept
Make color brighter.
Definition: color.inl:356
constexpr unsigned int hex_argb() const noexcept
Get color as hex.
Definition: color.inl:246
constexpr void update_b_dec(int nDeltaValue) noexcept
Add value to blue component.
Definition: color.inl:301
constexpr void set_r_dec(int nValue) noexcept
Set new value of red component.
Definition: color.inl:331
constexpr void set_b_dec(int nValue) noexcept
Set new value of blue component.
Definition: color.inl:341
constexpr void update_a_dec(int nDeltaValue) noexcept
Add value to alpha component.
Definition: color.inl:306
constexpr void set_a(float fValue) noexcept
Set new value of alpha component.
Definition: color.inl:326
constexpr void set_a_dec(int nValue) noexcept
Set new value of alpha component.
Definition: color.inl:346
Helper class for string -> color conversion.
Definition: color.inl:42
std::optional< color > get(const basic_string< char_t > &sName) const noexcept
Try to get ac olor from a color name.
Definition: color.inl:109
void add(basic_string< char_t > sName, color_name_type eColorNameType, color color) noexcept
Add new color to the mapping.
Definition: color.inl:93
Inherit the necessary singleton class from this.
#define _QX_COLORS(macro)
Definition: color.h:28
#define QX_CHAR_PREFIX(value_t, ch)
Chose witch of prefixes add to char : L or none.
Definition: string_utils.h:128
#define QX_STR_PREFIX(value_t, str)
Chose witch of prefixes add to string : L or none.
Definition: string_utils.h:120
std::uint8_t u8
0 .. 65 535
Definition: typedefs.h:19
std::uint64_t u64
Definition: typedefs.h:26