Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / video / 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 <assert.h>
21 #include <vector>
22
23 #include "util/log.hpp"
24
25 class Color
26 {
27 public:
28   Color() :
29     red(0), 
30     green(0), 
31     blue(0), 
32     alpha(1.0f)
33   {}
34
35   Color(float red, float green, float blue, float alpha = 1.0) :
36     red(red),
37     green(green), 
38     blue(blue), 
39     alpha(alpha)
40   {
41 #ifdef DEBUG
42     check_color_ranges();
43 #endif
44   }
45
46   Color(const std::vector<float>& vals) :
47     red(),
48     green(),
49     blue(),
50     alpha()
51   {
52     assert(vals.size() >= 3);
53     red   = vals[0];
54     green = vals[1];
55     blue  = vals[2];
56     if(vals.size() > 3)
57       alpha = vals[3];
58     else
59       alpha = 1.0;
60 #ifdef DEBUG
61     check_color_ranges();
62 #endif
63   }
64
65   bool operator==(const Color& other) const
66   {
67     return red == other.red && green == other.green && blue == other.blue
68       && alpha == other.alpha;
69   }
70
71   void check_color_ranges()
72   {
73     if(red < 0 || red > 1.0 || green < 0 || green > 1.0
74        || blue < 0 || blue > 1.0
75        || alpha < 0 || alpha > 1.0)
76       log_warning << "color value out of range: " << red << ", " << green << ", " << blue << ", " << alpha << std::endl;
77   }
78
79   float greyscale() const
80   {
81     return red * 0.30 + green * 0.59 + blue * 0.11;
82   }
83
84   bool operator < (const Color& other) const
85   {
86     return greyscale() < other.greyscale();
87   }
88
89   float red, green, blue, alpha;
90
91   static const Color BLACK;
92   static const Color RED;
93   static const Color GREEN;
94   static const Color BLUE;
95   static const Color CYAN;
96   static const Color MAGENTA;
97   static const Color YELLOW;
98   static const Color WHITE;
99 };
100
101 #endif
102
103 /* EOF */