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