20130427
[gdash.git] / src / cave / colors.hpp
blob3baf2163d22f7cee35a9dfdbea7ec67c921a699d
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef COLORS_HPP_INCLUDED
24 #define COLORS_HPP_INCLUDED
26 #include "config.h"
28 #include <iostream>
30 #include "misc/printf.hpp"
32 /// @brief A class which stores a color in a cave.
33 ///
34 /// Can store different kind of colors:
35 /// - C64 colors,
36 /// - C64DTV colors,
37 /// - Atari colors,
38 /// - RGB colors.
39 ///
40 /// Can convert any of them to RGB color during playing the game.
41 /// The conversion from paletted to RGB colors uses a palette,
42 /// determined by user preferences.
43 class GdColor {
44 public:
45 /// This enum selects the type of the color used.
46 enum Type {
47 TypeRGB,
48 TypeC64,
49 TypeC64DTV,
50 TypeAtari,
51 TypeHSV,
52 TypeInvalid,
54 /// Constructor, which created a color object, initialized to invalid.
55 /// For convenience only; any color which is to be used must be initialized.
56 GdColor() : type(TypeInvalid) { }
57 static GdColor from_rgb(unsigned r, unsigned g, unsigned b);
58 static GdColor from_hsv(unsigned short h, unsigned char s, unsigned char v);
59 static GdColor from_c64(unsigned index);
60 static GdColor from_atari(unsigned index);
61 static GdColor from_atari_huesat(unsigned hue, unsigned sat);
62 static GdColor from_c64dtv(unsigned index);
63 static GdColor from_c64dtv_huesat(unsigned hue, unsigned sat);
64 static GdColor from_gdash_index(unsigned c); /* similar to c64 colors */
66 bool operator==(const GdColor &rhs) const;
67 /// Compare two color objects for inequality.
68 /// @return True, if they are not equal.
69 bool operator!=(const GdColor &rhs) const {
70 return !(*this == rhs);
73 void get_rgb(unsigned char & r, unsigned char & g, unsigned char & b) const;
74 void get_hsv(unsigned short & h, unsigned char & s, unsigned char & v) const;
75 unsigned int get_uint_0rgb() const;
76 GdColor to_rgb() const;
77 GdColor to_hsv() const;
79 bool is_c64() const;
81 int get_c64_index() const;
83 static const char **get_c64_palette_names();
84 static const char **get_atari_palette_names();
85 static const char **get_c64dtv_palette_names();
86 static const char **get_palette_types_names();
88 private:
89 Type type;
90 union {
91 unsigned char index;
92 struct {
93 unsigned char r;
94 unsigned char g;
95 unsigned char b;
96 } rgb;
97 struct {
98 unsigned short h; /* [0;360) */
99 unsigned char s; /* [0;100] */
100 unsigned char v; /* [0;100] */
101 } hsv;
104 GdColor(unsigned char r, unsigned char g, unsigned char b) :
105 type(TypeRGB) {
106 rgb.r = r;
107 rgb.g = g;
108 rgb.b = b;
110 GdColor(Type _type, int i) :
111 type(_type) {
112 index = i;
114 GdColor(unsigned int rgb);
116 friend std::string visible_name(const GdColor &c);
117 friend std::ostream &operator<<(std::ostream &os, const GdColor &c);
121 /// Get red, green, blue (0..255) component of color.
122 /// Uses the current user palette, if needed.
123 /// Inlined, because it is used millions of times when creating a colorized game theme.
124 inline void GdColor::get_rgb(unsigned char & r, unsigned char & g, unsigned char & b) const {
125 if (type != TypeRGB)
126 to_rgb().get_rgb(r, g, b);
127 else {
128 r = rgb.r;
129 g = rgb.g;
130 b = rgb.b;
135 /// Get h, s, v (0..359, 0..99, 0..99) component of color.
136 /// Inlined, because it is used millions of times when creating a colorized game theme.
137 inline void GdColor::get_hsv(unsigned short & h, unsigned char & s, unsigned char & v) const {
138 if (type != TypeHSV)
139 to_hsv().get_hsv(h, s, v);
140 else {
141 h = hsv.h;
142 s = hsv.s;
143 v = hsv.v;
148 /// Create a color from a given r, g, b value.
149 /// Inlined, because it is used millions of times when creating a colorized game theme.
150 /// @param r Red value 0..255.
151 /// @param g Green value 0..255.
152 /// @param b Blue value 0..255.
153 inline GdColor GdColor::from_rgb(unsigned r, unsigned g, unsigned b) {
154 return GdColor(r, g, b);
158 /// Make up GdColor from h,s,v values.
159 /// Inlined, because it is used millions of times when creating a colorized game theme.
160 /// @param h Hue, 0..360
161 /// @param s Saturation, 0..100
162 /// @param v Value, 0..100
163 inline GdColor GdColor::from_hsv(unsigned short h, unsigned char s, unsigned char v) {
164 GdColor n;
165 n.type = TypeHSV;
166 n.hsv.h = h;
167 n.hsv.s = s;
168 n.hsv.v = v;
169 return n;
173 /* i/o operators and functions like for all other cave types */
174 std::ostream &operator<<(std::ostream &os, const GdColor &c);
175 std::string visible_name(const GdColor &c);
176 bool read_from_string(std::string const &s, GdColor &c);
179 /// Traditional C64 color indexes, plus one GDash special color.
180 enum GdColorIndex {
181 GD_COLOR_INDEX_BLACK = 0,
182 GD_COLOR_INDEX_WHITE = 1,
183 GD_COLOR_INDEX_RED = 2,
184 GD_COLOR_INDEX_PURPLE = 4,
185 GD_COLOR_INDEX_CYAN = 3,
186 GD_COLOR_INDEX_GREEN = 5,
187 GD_COLOR_INDEX_BLUE = 6,
188 GD_COLOR_INDEX_YELLOW = 7,
189 GD_COLOR_INDEX_ORANGE = 8,
190 GD_COLOR_INDEX_BROWN = 9,
191 GD_COLOR_INDEX_LIGHTRED = 10,
192 GD_COLOR_INDEX_GRAY1 = 11,
193 GD_COLOR_INDEX_GRAY2 = 12,
194 GD_COLOR_INDEX_LIGHTGREEN = 13,
195 GD_COLOR_INDEX_LIGHTBLUE = 14,
196 GD_COLOR_INDEX_GRAY3 = 15,
197 GD_COLOR_INDEX_MIDDLEBLUE = 16,
199 enum GdColorIndexHelper {
200 GD_COLOR_SETCOLOR = 31, /* for blittext */
203 // specialized for color codes for font manager
204 template <>
205 inline Printf const &Printf::operator%(GdColorIndex const &colorindex) const {
206 /* +64 is needed so it is a normal ascii char in the encoding, not a string limiter \0
207 * or a \n or whatever */
208 char s[3] = { GD_COLOR_SETCOLOR, char(colorindex + 64), 0 };
209 return (*this) % s;
213 #define GD_GDASH_BLACK (GdColor::from_gdash_index(GD_COLOR_INDEX_BLACK))
214 #define GD_GDASH_WHITE (GdColor::from_gdash_index(GD_COLOR_INDEX_WHITE))
215 #define GD_GDASH_RED (GdColor::from_gdash_index(GD_COLOR_INDEX_RED))
216 #define GD_GDASH_PURPLE (GdColor::from_gdash_index(GD_COLOR_INDEX_PURPLE))
217 #define GD_GDASH_CYAN (GdColor::from_gdash_index(GD_COLOR_INDEX_CYAN))
218 #define GD_GDASH_GREEN (GdColor::from_gdash_index(GD_COLOR_INDEX_GREEN))
219 #define GD_GDASH_BLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_BLUE))
220 #define GD_GDASH_YELLOW (GdColor::from_gdash_index(GD_COLOR_INDEX_YELLOW))
221 #define GD_GDASH_ORANGE (GdColor::from_gdash_index(GD_COLOR_INDEX_ORANGE))
222 #define GD_GDASH_BROWN (GdColor::from_gdash_index(GD_COLOR_INDEX_BROWN))
223 #define GD_GDASH_LIGHTRED (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTRED))
224 #define GD_GDASH_GRAY1 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY1))
225 #define GD_GDASH_GRAY2 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY2))
226 #define GD_GDASH_LIGHTGREEN (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTGREEN))
227 #define GD_GDASH_LIGHTBLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTBLUE))
228 #define GD_GDASH_GRAY3 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY3))
229 #define GD_GDASH_MIDDLEBLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_MIDDLEBLUE))
231 extern const GdColor gd_flash_color;
232 extern const GdColor gd_select_color;
234 #endif