lxgui
gui_color.hpp
1 #ifndef LXGUI_GUI_COLOR_HPP
2 #define LXGUI_GUI_COLOR_HPP
3 
4 #include "lxgui/lxgui.hpp"
5 
6 #include <iosfwd>
7 #include <string>
8 
9 namespace lxgui::gui {
10 
12 class color {
13 public:
14  using channel = float;
15 
16  struct hls {
17  channel h, l, s, a;
18  };
19 
20  struct hsv {
21  channel h, s, v, a;
22  };
23 
24  constexpr color() = default;
25  constexpr color(channel nr, channel ng, channel nb, channel na = 1.0f) noexcept :
26  r(nr), g(ng), b(nb), a(na) {}
27  explicit color(const std::string& s);
28 
29  hls to_hls() const noexcept;
30  hsv to_hsv() const noexcept;
31 
32  bool operator==(const color& c) const noexcept;
33  bool operator!=(const color& c) const noexcept;
34 
35  void operator+=(const color& c) noexcept;
36  void operator-=(const color& c) noexcept;
37  void operator*=(const color& c) noexcept;
38  void operator*=(float f) noexcept;
39 
40  channel r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;
41 
42  static const color empty;
43  static const color white;
44  static const color black;
45  static const color red;
46  static const color green;
47  static const color blue;
48  static const color grey;
49 
50  static constexpr color
51  from_bytes(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a = 255u) noexcept {
52  return color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
53  }
54 
55  static color from_hls(const hls& hls) noexcept;
56  static color from_hsv(const hsv& hsv) noexcept;
57 };
58 
59 color operator+(const color& c1, const color& c2) noexcept;
60 color operator-(const color& c1, const color& c2) noexcept;
61 color operator*(const color& c1, const color& c2) noexcept;
62 color operator*(const color& c1, float f) noexcept;
63 color operator*(float f, const color& c2) noexcept;
64 
65 std::ostream& operator<<(std::ostream& stream, const color& c);
66 std::istream& operator>>(std::istream& stream, color& c);
67 
75 struct color32 {
76  using chanel = unsigned char;
77 
78  chanel r, g, b, a;
79 };
80 
81 } // namespace lxgui::gui
82 
83 #endif
Holds a single color (float RGBA, 128 bits)
Definition: gui_color.hpp:12
static const color green
Definition: gui_color.hpp:46
static const color red
Definition: gui_color.hpp:45
static const color blue
Definition: gui_color.hpp:47
hsv to_hsv() const noexcept
Definition: gui_color.cpp:62
static color from_hls(const hls &hls) noexcept
Definition: gui_color.cpp:108
static color from_hsv(const hsv &hsv) noexcept
Definition: gui_color.cpp:133
constexpr color()=default
static const color white
Definition: gui_color.hpp:43
constexpr color(channel nr, channel ng, channel nb, channel na=1.0f) noexcept
Definition: gui_color.hpp:25
static constexpr color from_bytes(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a=255u) noexcept
Definition: gui_color.hpp:51
static const color grey
Definition: gui_color.hpp:48
static const color black
Definition: gui_color.hpp:44
hls to_hls() const noexcept
Definition: gui_color.cpp:26
static const color empty
Definition: gui_color.hpp:42
std::ostream & operator<<(std::ostream &stream, const color &c)
Definition: gui_color.cpp:212
std::istream & operator>>(std::istream &stream, color &c)
Definition: gui_color.cpp:219
color operator+(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:192
color operator*(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:200
color operator-(const color &c1, const color &c2) noexcept
Definition: gui_color.cpp:196
Holds a single color (byte RGBA, 32 bits)
Definition: gui_color.hpp:75
unsigned char chanel
Definition: gui_color.hpp:76