Mountain  1.0.0
Simple C++ 2D Game Framework
color.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <format>
4 #include <sstream>
5 
6 #include <Maths/easing.hpp>
7 #include <Maths/vector3.hpp>
8 #include <Maths/vector4.hpp>
9 
10 #include "Mountain/core.hpp"
11 
12 // Undef windows min and max macros in case they were defined
13 #undef min
14 #undef max
15 
18 
19 namespace Mountain
20 {
21  struct ColorHsva;
22 
26  struct MOUNTAIN_API Color
27  {
28  // Constants taken from https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Color.cs
29 
32  static constexpr Color Transparent();
33 
36  static constexpr Color AliceBlue();
37 
40  static constexpr Color AntiqueWhite();
41 
44  static constexpr Color Aqua();
45 
48  static constexpr Color Aquamarine();
49 
52  static constexpr Color Azure();
53 
56  static constexpr Color Beige();
57 
60  static constexpr Color Bisque();
61 
64  static constexpr Color Black();
65 
68  static constexpr Color BlanchedAlmond();
69 
72  static constexpr Color Blue();
73 
76  static constexpr Color BlueViolet();
77 
80  static constexpr Color Brown();
81 
84  static constexpr Color BurlyWood();
85 
88  static constexpr Color CadetBlue();
89 
92  static constexpr Color Chartreuse();
93 
96  static constexpr Color Chocolate();
97 
100  static constexpr Color Coral();
101 
104  static constexpr Color CornflowerBlue();
105 
108  static constexpr Color Cornsilk();
109 
112  static constexpr Color Crimson();
113 
116  static constexpr Color Cyan();
117 
120  static constexpr Color DarkBlue();
121 
124  static constexpr Color DarkCyan();
125 
128  static constexpr Color DarkGoldenrod();
129 
132  static constexpr Color DarkGray();
133 
136  static constexpr Color DarkGreen();
137 
140  static constexpr Color DarkKhaki();
141 
144  static constexpr Color DarkMagenta();
145 
148  static constexpr Color DarkOliveGreen();
149 
152  static constexpr Color DarkOrange();
153 
156  static constexpr Color DarkOrchid();
157 
160  static constexpr Color DarkRed();
161 
164  static constexpr Color DarkSalmon();
165 
168  static constexpr Color DarkSeaGreen();
169 
172  static constexpr Color DarkSlateBlue();
173 
176  static constexpr Color DarkSlateGray();
177 
180  static constexpr Color DarkTurquoise();
181 
184  static constexpr Color DarkViolet();
185 
188  static constexpr Color DeepPink();
189 
192  static constexpr Color DeepSkyBlue();
193 
196  static constexpr Color DimGray();
197 
200  static constexpr Color DodgerBlue();
201 
204  static constexpr Color Firebrick();
205 
208  static constexpr Color FloralWhite();
209 
212  static constexpr Color ForestGreen();
213 
216  static constexpr Color Fuchsia();
217 
220  static constexpr Color Gainsboro();
221 
224  static constexpr Color GhostWhite();
225 
228  static constexpr Color Gold();
229 
232  static constexpr Color Goldenrod();
233 
236  static constexpr Color Gray();
237 
240  static constexpr Color Green();
241 
244  static constexpr Color GreenYellow();
245 
248  static constexpr Color Honeydew();
249 
252  static constexpr Color HotPink();
253 
256  static constexpr Color IndianRed();
257 
260  static constexpr Color Indigo();
261 
264  static constexpr Color Ivory();
265 
268  static constexpr Color Khaki();
269 
272  static constexpr Color Lavender();
273 
276  static constexpr Color LavenderBlush();
277 
280  static constexpr Color LawnGreen();
281 
284  static constexpr Color LemonChiffon();
285 
288  static constexpr Color LightBlue();
289 
292  static constexpr Color LightCoral();
293 
296  static constexpr Color LightCyan();
297 
300  static constexpr Color LightGoldenrodYellow();
301 
304  static constexpr Color LightGray();
305 
308  static constexpr Color LightGreen();
309 
312  static constexpr Color LightPink();
313 
316  static constexpr Color LightSalmon();
317 
320  static constexpr Color LightSeaGreen();
321 
324  static constexpr Color LightSkyBlue();
325 
328  static constexpr Color LightSlateGray();
329 
332  static constexpr Color LightSteelBlue();
333 
336  static constexpr Color LightYellow();
337 
340  static constexpr Color Lime();
341 
344  static constexpr Color LimeGreen();
345 
348  static constexpr Color Linen();
349 
352  static constexpr Color Magenta();
353 
356  static constexpr Color Maroon();
357 
360  static constexpr Color MediumAquamarine();
361 
364  static constexpr Color MediumBlue();
365 
368  static constexpr Color MediumOrchid();
369 
372  static constexpr Color MediumPurple();
373 
376  static constexpr Color MediumSeaGreen();
377 
380  static constexpr Color MediumSlateBlue();
381 
384  static constexpr Color MediumSpringGreen();
385 
388  static constexpr Color MediumTurquoise();
389 
392  static constexpr Color MediumVioletRed();
393 
396  static constexpr Color MidnightBlue();
397 
400  static constexpr Color MintCream();
401 
404  static constexpr Color MistyRose();
405 
408  static constexpr Color Moccasin();
409 
412  static constexpr Color MonoGameOrange();
413 
416  static constexpr Color NavajoWhite();
417 
420  static constexpr Color Navy();
421 
424  static constexpr Color OldLace();
425 
428  static constexpr Color Olive();
429 
432  static constexpr Color OliveDrab();
433 
436  static constexpr Color Orange();
437 
440  static constexpr Color OrangeRed();
441 
444  static constexpr Color Orchid();
445 
448  static constexpr Color PaleGoldenrod();
449 
452  static constexpr Color PaleGreen();
453 
456  static constexpr Color PaleTurquoise();
457 
460  static constexpr Color PaleVioletRed();
461 
464  static constexpr Color PapayaWhip();
465 
468  static constexpr Color PeachPuff();
469 
472  static constexpr Color Peru();
473 
476  static constexpr Color Pink();
477 
480  static constexpr Color Plum();
481 
484  static constexpr Color PowderBlue();
485 
488  static constexpr Color Purple();
489 
492  static constexpr Color Red();
493 
496  static constexpr Color RosyBrown();
497 
500  static constexpr Color RoyalBlue();
501 
504  static constexpr Color SaddleBrown();
505 
508  static constexpr Color Salmon();
509 
512  static constexpr Color SandyBrown();
513 
516  static constexpr Color SeaGreen();
517 
520  static constexpr Color SeaShell();
521 
524  static constexpr Color Sienna();
525 
528  static constexpr Color Silver();
529 
532  static constexpr Color SkyBlue();
533 
536  static constexpr Color SlateBlue();
537 
540  static constexpr Color SlateGray();
541 
544  static constexpr Color Snow();
545 
548  static constexpr Color SpringGreen();
549 
552  static constexpr Color SteelBlue();
553 
556  static constexpr Color Tan();
557 
560  static constexpr Color Teal();
561 
564  static constexpr Color Thistle();
565 
568  static constexpr Color Tomato();
569 
572  static constexpr Color Turquoise();
573 
576  static constexpr Color Violet();
577 
580  static constexpr Color Wheat();
581 
584  static constexpr Color White();
585 
588  static constexpr Color WhiteSmoke();
589 
592  static constexpr Color Yellow();
593 
596  static constexpr Color YellowGreen();
597 
599  float_t r = 0.f;
601  float_t g = 0.f;
603  float_t b = 0.f;
605  float_t a = 1.f;
606 
607  [[nodiscard]]
608  Color() = default;
609 
614  [[nodiscard]]
615  explicit constexpr Color(const float_t rgb, const float_t a = 1.f);
616 
623  [[nodiscard]]
624  constexpr Color(const float_t r, const float_t g, const float_t b, const float_t a = 1.f);
625 
630  [[nodiscard]]
631  explicit constexpr Color(const Vector3& rgb, const float_t a = 1.f);
632 
636  [[nodiscard]]
637  explicit constexpr Color(const Vector4& rgba);
638 
642  [[nodiscard]]
643  explicit constexpr Color(const float* data);
644 
645  [[nodiscard]]
646  uint32_t GetPackedValue() const;
647 
648  [[nodiscard]]
649  constexpr float_t* Data();
650 
651  [[nodiscard]]
652  constexpr const float_t* Data() const;
653 
655  [[nodiscard]]
656  constexpr explicit operator ColorHsva() const;
657 
659  [[nodiscard]]
660  constexpr explicit operator Vector3() const;
661 
663  [[nodiscard]]
664  constexpr explicit operator Vector4() const;
665  };
666 
670  struct MOUNTAIN_API ColorHsva
671  {
674  [[nodiscard]]
675  static constexpr ColorHsva White();
676 
679  [[nodiscard]]
680  static constexpr ColorHsva Gray();
681 
684  [[nodiscard]]
685  static constexpr ColorHsva Black();
686 
689  [[nodiscard]]
690  static constexpr ColorHsva Red();
691 
694  [[nodiscard]]
695  static constexpr ColorHsva Green();
696 
699  [[nodiscard]]
700  static constexpr ColorHsva Blue();
701 
704  [[nodiscard]]
705  static constexpr ColorHsva Yellow();
706 
709  [[nodiscard]]
710  static constexpr ColorHsva LightBlue();
711 
714  [[nodiscard]]
715  static constexpr ColorHsva Magenta();
716 
718  float_t h = 0;
720  float_t s = 0;
722  float_t v = 0;
724  float_t a = 1.f;
725 
726  ColorHsva() = default;
727 
734  [[nodiscard]]
735  constexpr ColorHsva(const float_t h, const float_t s, const float_t v, const float_t a = 1.f);
736 
738  [[nodiscard]]
739  constexpr explicit operator Color() const;
740 
742  [[nodiscard]]
743  constexpr explicit operator Vector3() const;
744 
746  [[nodiscard]]
747  constexpr explicit operator Vector4() const;
748  };
749 
754  [[nodiscard]]
755  constexpr Color operator+(const Color& c1, const Color& c2);
756 
761  [[nodiscard]]
762  constexpr Color operator*(const Color& c1, const Color& c2);
763 
768  [[nodiscard]]
769  constexpr Color operator*(Color color, float_t alphaFactor);
770 
775  [[nodiscard]]
776  constexpr bool_t operator==(const Color& c1, const Color& c2);
777 
782  [[nodiscard]]
783  constexpr bool_t operator!=(const Color& c1, const Color& c2);
784 
789  [[nodiscard]]
790  constexpr ColorHsva operator*(const ColorHsva& color, float_t alphaFactor);
791 
796  [[nodiscard]]
797  constexpr bool_t operator==(const ColorHsva& c1, const ColorHsva& c2);
798 
803  [[nodiscard]]
804  constexpr bool_t operator!=(const ColorHsva& c1, const ColorHsva& c2);
805 }
806 
808 template <>
809 struct std::formatter<Mountain::Color>
810 {
811  template <class ParseContext>
812  constexpr typename ParseContext::iterator parse(ParseContext& ctx)
813  {
814  auto it = ctx.begin();
815  if (it == ctx.end())
816  return it;
817 
818  if (*it != '}')
819  throw std::format_error("Invalid format args for Color");
820 
821  return it;
822  }
823 
824  template <class FmtContext>
825  typename FmtContext::iterator format(Mountain::Color c, FmtContext& ctx) const
826  {
827  std::ostringstream out;
828 
829  out << std::vformat("{:" + m_Format + "} ; {:" + m_Format + "} ; {:" + m_Format + "} ; {:" + m_Format + '}', std::make_format_args(c.r, c.g, c.b, c.a));
830 
831  return std::ranges::copy(std::move(out).str(), ctx.out()).out;
832  }
833 
834 private:
835  std::string m_Format;
836 };
837 
839 template <>
840 struct std::formatter<Mountain::ColorHsva>
841 {
842  template <class ParseContext>
843  constexpr typename ParseContext::iterator parse(ParseContext& ctx)
844  {
845  auto it = ctx.begin();
846  if (it == ctx.end())
847  return it;
848 
849  if (*it != '}')
850  throw std::format_error("Invalid format args for ColorHsva");
851 
852  return it;
853  }
854 
855  template <class FmtContext>
856  typename FmtContext::iterator format(Mountain::ColorHsva c, FmtContext& ctx) const
857  {
858  std::ostringstream out;
859 
860  out << std::vformat("{:" + m_Format + "} ; {:" + m_Format + "} ; {:" + m_Format + "} ; {:" + m_Format + '}', std::make_format_args(c.h, c.s, c.v, c.a));
861 
862  return std::ranges::copy(std::move(out).str(), ctx.out()).out;
863  }
864 
865 private:
866  std::string m_Format;
867 };
868 
869 namespace Calc
870 {
871  [[nodiscard]]
872  Mountain::Color Lerp(const Mountain::Color& value, const Mountain::Color& target, float_t time);
873 
874  [[nodiscard]]
875  Mountain::Color Lerp(const Mountain::Color& value, const Mountain::Color& target, float_t time, Easing::Easer easer);
876 }
877 
878 #include "Mountain/utils/color.inl"
constexpr bool_t operator==(const Color &c1, const Color &c2)
Compares 2 Color component-wise.
constexpr bool_t operator!=(const Color &c1, const Color &c2)
Compares 2 Color component-wise.
float_t h
Hue component.
Definition: color.hpp:718
constexpr Color operator*(const Color &c1, const Color &c2)
Multiplies 2 Color.
float_t a
Alpha component.
Definition: color.hpp:605
float_t g
Green component.
Definition: color.hpp:601
The Color struct represents a color in RGBA color space.
Definition: color.hpp:26
constexpr Color operator+(const Color &c1, const Color &c2)
Adds 2 Color, caps at 1.f.
float_t v
Value component.
Definition: color.hpp:722
float_t s
Saturation component.
Definition: color.hpp:720
float_t r
Red component.
Definition: color.hpp:599
The ColorHsva struct represents a color in HSVA color space.
Definition: color.hpp:670
float_t a
Alpha component.
Definition: color.hpp:724
float_t b
Blue component.
Definition: color.hpp:603
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22