supertux
color.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_VIDEO_COLOR_HPP
18 #define HEADER_SUPERTUX_VIDEO_COLOR_HPP
19 
20 #include <string>
21 #include <vector>
22 
23 #include <SDL_image.h>
24 
25 class Color final
26 {
27 public:
28  static const Color BLACK;
29  static const Color RED;
30  static const Color GREEN;
31  static const Color BLUE;
32  static const Color CYAN;
33  static const Color MAGENTA;
34  static const Color YELLOW;
35  static const Color WHITE;
36 
37 public:
38  static Color from_rgb888(uint8_t r, uint8_t g, uint8_t b)
39  {
40  return Color(static_cast<float>(r) / 255.0f,
41  static_cast<float>(g) / 255.0f,
42  static_cast<float>(b) / 255.0f);
43  }
44 
45  static Color from_rgba8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
46  {
47  return Color(static_cast<float>(r) / 255.0f,
48  static_cast<float>(g) / 255.0f,
49  static_cast<float>(b) / 255.0f,
50  static_cast<float>(a) / 255.0f);
51  }
52 
53 public:
54  Color();
55 
56  Color(float red_, float green_, float blue_, float alpha_ = 1.0);
57 
58  Color(const std::vector<float>& vals);
59 
60  bool operator==(const Color& other) const;
61  bool operator!=(const Color& other) const;
62 
63  float greyscale() const;
64 
65  bool operator < (const Color& other) const;
66 
67  std::vector<float> toVector();
68 
69  inline uint8_t r8() const { return static_cast<uint8_t>(255.0f * red); }
70  inline uint8_t g8() const { return static_cast<uint8_t>(255.0f * green); }
71  inline uint8_t b8() const { return static_cast<uint8_t>(255.0f * blue); }
72  inline uint8_t a8() const { return static_cast<uint8_t>(255.0f * alpha); }
73 
74  inline uint32_t rgba() const
75  {
76  return ((static_cast<uint32_t>(a8()) << 24u) |
77  (static_cast<uint32_t>(b8()) << 16u) |
78  (static_cast<uint32_t>(g8()) << 8u) |
79  (static_cast<uint32_t>(r8()) << 0u));
80  }
81 
83  std::string to_string() const
84  {
85  return std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
86  }
87 
88  SDL_Color to_sdl_color() const
89  {
90  return { r8(), g8(), b8(), a8() };
91  }
92 
93 public:
94  float red, green, blue, alpha;
95 };
96 
97 #endif
98 
99 /* EOF */
std::string to_string() const
Return a human-readable string representation for this color.
Definition: color.hpp:83
Definition: color.hpp:25