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
26class Color final
27{
28public:
29 static const Color BLACK;
30 static const Color RED;
31 static const Color GREEN;
32 static const Color BLUE;
33 static const Color CYAN;
34 static const Color MAGENTA;
35 static const Color YELLOW;
36 static const Color WHITE;
37
38public:
39 static Color from_rgb888(uint8_t r, uint8_t g, uint8_t b)
40 {
41 return Color(static_cast<float>(r) / 255.0f,
42 static_cast<float>(g) / 255.0f,
43 static_cast<float>(b) / 255.0f);
44 }
45
46 static Color from_rgba8888(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
47 {
48 return Color(static_cast<float>(r) / 255.0f,
49 static_cast<float>(g) / 255.0f,
50 static_cast<float>(b) / 255.0f,
51 static_cast<float>(a) / 255.0f);
52 }
53
54 static Color from_linear(float r, float g, float b, float a = 1.0f)
55 {
56 return Color(add_gamma(r), add_gamma(g), add_gamma(b), a);
57 }
58
59 // Helper functions to approximately transform to/from sRGB colours
60 static float add_gamma(float x) { return powf(x, 1.0f / 2.2f); }
61 static float remove_gamma(float x) { return powf(x, 2.2f); }
62
63public:
64 Color();
65
66 Color(float red_, float green_, float blue_, float alpha_ = 1.0);
67
68 Color(const std::vector<float>& vals);
69
70 bool operator==(const Color& other) const;
71 bool operator!=(const Color& other) const;
72
73 float greyscale() const;
74
75 bool operator < (const Color& other) const;
76
77 std::vector<float> toVector();
78
79 inline uint8_t r8() const { return static_cast<uint8_t>(255.0f * red); }
80 inline uint8_t g8() const { return static_cast<uint8_t>(255.0f * green); }
81 inline uint8_t b8() const { return static_cast<uint8_t>(255.0f * blue); }
82 inline uint8_t a8() const { return static_cast<uint8_t>(255.0f * alpha); }
83
84 inline uint32_t rgba() const
85 {
86 return ((static_cast<uint32_t>(a8()) << 24u) |
87 (static_cast<uint32_t>(b8()) << 16u) |
88 (static_cast<uint32_t>(g8()) << 8u) |
89 (static_cast<uint32_t>(r8()) << 0u));
90 }
91
92 /** Return a human-readable string representation for this color */
93 std::string to_string() const
94 {
95 return std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
96 }
97
98 SDL_Color to_sdl_color() const
99 {
100 return { r8(), g8(), b8(), a8() };
101 }
102
103public:
104 float red, green, blue, alpha;
105};
106
107#endif
108
109/* EOF */
110