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 #include <math.h>
23 
24 #include <SDL_image.h>
25 
26 class Color final
27 {
28 public:
29  // CalculateColor is the same as Color but without validation
30  class CalculateColor final
31  {
32  public:
33  CalculateColor(float r_, float g_, float b_, float a_ = 1.f) :
34  r(r_),
35  g(g_),
36  b(b_),
37  a(a_)
38  {
39  }
40 
41  Color validate() const { return Color(r, g, b, a); }
42 
43  CalculateColor operator+(const CalculateColor& o) const { return CalculateColor(r + o.r, g + o.g, b + o.b, a + o.a); }
44  CalculateColor operator-(const CalculateColor& o) const { return CalculateColor(r - o.r, g - o.g, b - o.b, a - o.a); }
45  CalculateColor operator*(const CalculateColor& o) const { return CalculateColor(r * o.r, g * o.g, b * o.b, a * o.a); }
46  CalculateColor operator/(const CalculateColor& o) const { return CalculateColor(r / o.r, g / o.g, b / o.b, a / o.a); }
47  CalculateColor operator+(const Color& o) const { return CalculateColor(r + o.red, g + o.green, b + o.blue, a + o.alpha); }
48  CalculateColor operator-(const Color& o) const { return CalculateColor(r - o.red, g - o.green, b - o.blue, a - o.alpha); }
49  CalculateColor operator*(const Color& o) const { return CalculateColor(r * o.red, g * o.green, b * o.blue, a * o.alpha); }
50  CalculateColor operator/(const Color& o) const { return CalculateColor(r / o.red, g / o.green, b / o.blue, a / o.alpha); }
51  CalculateColor operator*(float m) const { return CalculateColor(r * m, g * m, b * m, a * m); }
52  CalculateColor operator/(float d) const { return CalculateColor(r / d, g / d, b / d, a / d); }
53 
54  public:
55  float r, g, b, a;
56  };
57 
58 public:
59  static const Color BLACK;
60  static const Color RED;
61  static const Color GREEN;
62  static const Color BLUE;
63  static const Color CYAN;
64  static const Color MAGENTA;
65  static const Color YELLOW;
66  static const Color WHITE;
67 
68 public:
69  static Color from_rgb888(uint8_t r, uint8_t g, uint8_t b)
70  {
71  return Color(static_cast<float>(r) / 255.0f,
72  static_cast<float>(g) / 255.0f,
73  static_cast<float>(b) / 255.0f);
74  }
75 
76  static Color from_rgba8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
77  {
78  return Color(static_cast<float>(r) / 255.0f,
79  static_cast<float>(g) / 255.0f,
80  static_cast<float>(b) / 255.0f,
81  static_cast<float>(a) / 255.0f);
82  }
83 
84  static Color from_linear(float r, float g, float b, float a = 1.0f)
85  {
86  return Color(add_gamma(r), add_gamma(g), add_gamma(b), a);
87  }
88 
89  // Helper functions to approximately transform to/from sRGB colours
90  static float add_gamma(float x) { return powf(x, 1.0f / 2.2f); }
91  static float remove_gamma(float x) { return powf(x, 2.2f); }
92 
93 public:
94  Color();
95 
96  Color(float red_, float green_, float blue_, float alpha_ = 1.0);
97 
98  Color(const std::vector<float>& vals);
99 
100  bool operator==(const Color& other) const;
101  bool operator!=(const Color& other) const;
102 
103  float greyscale() const;
104 
105  // Multiplies the sRGB color values by v gamma-correctly
106  Color multiply_linearly(float v) const;
107 
108  bool operator < (const Color& other) const;
109 
110  std::vector<float> toVector();
111 
112  inline uint8_t r8() const { return static_cast<uint8_t>(255.0f * red); }
113  inline uint8_t g8() const { return static_cast<uint8_t>(255.0f * green); }
114  inline uint8_t b8() const { return static_cast<uint8_t>(255.0f * blue); }
115  inline uint8_t a8() const { return static_cast<uint8_t>(255.0f * alpha); }
116 
117  inline uint32_t rgba() const
118  {
119  return ((static_cast<uint32_t>(a8()) << 24u) |
120  (static_cast<uint32_t>(b8()) << 16u) |
121  (static_cast<uint32_t>(g8()) << 8u) |
122  (static_cast<uint32_t>(r8()) << 0u));
123  }
124 
126  std::string to_string() const
127  {
128  return std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
129  }
130 
131  SDL_Color to_sdl_color() const
132  {
133  return { r8(), g8(), b8(), a8() };
134  }
135 
136  CalculateColor operator+(const Color& o) const { return CalculateColor(red + o.red, green + o.green, blue + o.blue, alpha + o.alpha); }
137  CalculateColor operator-(const Color& o) const { return CalculateColor(red - o.red, green - o.green, blue - o.blue, alpha - o.alpha); }
138  CalculateColor operator*(const Color& o) const { return CalculateColor(red * o.red, green * o.green, blue * o.blue, alpha * o.alpha); }
139  CalculateColor operator/(const Color& o) const { return CalculateColor(red / o.red, green / o.green, blue / o.blue, alpha / o.alpha); }
140  CalculateColor operator+(const CalculateColor& o) const { return CalculateColor(red + o.r, green + o.g, blue + o.b, alpha + o.a); }
141  CalculateColor operator-(const CalculateColor& o) const { return CalculateColor(red - o.r, green - o.g, blue - o.b, alpha - o.a); }
142  CalculateColor operator*(const CalculateColor& o) const { return CalculateColor(red * o.r, green * o.g, blue * o.b, alpha * o.a); }
143  CalculateColor operator/(const CalculateColor& o) const { return CalculateColor(red / o.r, green / o.g, blue / o.b, alpha / o.a); }
144  CalculateColor operator*(float m) const { return CalculateColor(red * m, green * m, blue * m, alpha * m); }
145  CalculateColor operator/(float d) const { return CalculateColor(red / d, green / d, blue / d, alpha / d); }
146 
147 public:
148  float red, green, blue, alpha;
149 };
150 
151 #endif
152 
153 /* EOF */
Definition: color.hpp:30
std::string to_string() const
Return a human-readable string representation for this color.
Definition: color.hpp:126
Definition: color.hpp:26