f0bd76d6b0ee87e78802b7823c521a96d81f4146
[supertux.git] / src / video / color.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef __COLOR_HPP__
21 #define __COLOR_HPP__
22
23 #include <vector>
24 #include <assert.h>
25 #include "log.hpp"
26
27 class Color
28 {
29 public:
30   Color()
31     : red(0), green(0), blue(0), alpha(1.0)
32   { }
33   Color(float red, float green, float blue, float alpha = 1.0)
34     : red(red), green(green), blue(blue), alpha(alpha)
35   {
36 #ifdef DEBUG
37     check_color_ranges();
38 #endif
39   }
40   Color(const std::vector<float>& vals)
41   {
42     assert(vals.size() >= 3);
43     red = vals[0];
44     green = vals[1];
45     blue = vals[2];
46     if(vals.size() > 3)
47       alpha = vals[3];
48     else
49       alpha = 1.0;
50 #ifdef DEBUG
51     check_color_ranges();
52 #endif
53   }
54
55   bool operator==(const Color& other) const
56   {
57     return red == other.red && green == other.green && blue == other.blue
58            && alpha == other.alpha;
59   }
60
61   void check_color_ranges()
62   {
63     if(red < 0 || red > 1.0 || green < 0 || green > 1.0
64             || blue < 0 || blue > 1.0
65             || alpha < 0 || alpha > 1.0)
66       log_warning << "color value out of range: " << red << ", " << green << ", " << blue << ", " << alpha << std::endl;
67   }
68
69   float greyscale() const
70   {
71     return red * 0.30 + green * 0.59 + blue * 0.11;
72   }
73
74   bool operator < (const Color& other) const
75   {
76     return greyscale() < other.greyscale();
77   }
78
79   float red, green, blue, alpha;
80
81   static const Color BLACK;
82   static const Color RED;
83   static const Color GREEN;
84   static const Color BLUE;
85   static const Color CYAN;
86   static const Color MAGENTA;
87   static const Color YELLOW;
88   static const Color WHITE;
89 };
90
91 #endif