20130313
[gdash.git] / src / cave / helper / colors.hpp
blob0a26aaed9dcf5033cd18b841cf7309f42938582d
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #ifndef _GD_COLORS
17 #define _GD_COLORS
19 #include "config.h"
21 #include <iostream>
23 #include "misc/printf.hpp"
25 /// @brief A class which stores a color in a cave.
26 ///
27 /// Can store different kind of colors:
28 /// - C64 colors,
29 /// - C64DTV colors,
30 /// - Atari colors,
31 /// - RGB colors.
32 ///
33 /// Can convert any of them to RGB color during playing the game.
34 /// The conversion from paletted to RGB colors uses a palette,
35 /// determined by user preferences.
36 class GdColor {
37 public:
38 /// This enum selects the type of the color used.
39 enum Type {
40 TypeRGB,
41 TypeC64,
42 TypeC64DTV,
43 TypeAtari,
44 TypeHSV,
46 enum TypeInvalid {
47 Invalid=int(TypeAtari)+1
49 /// Constructor, which created a color object, initialized to black.
50 /// For convenience only; any color which is to be used must be initialized.
51 GdColor() : type(TypeRGB) { rgb.r = 0; rgb.b = 0; rgb.b = 0; }
52 static GdColor from_rgb(unsigned r, unsigned g, unsigned b);
53 static GdColor from_hsv(unsigned short h, unsigned char s, unsigned char v);
54 static GdColor from_c64(unsigned index);
55 static GdColor from_atari(unsigned index);
56 static GdColor from_atari_huesat(unsigned hue, unsigned sat);
57 static GdColor from_c64dtv(unsigned index);
58 static GdColor from_c64dtv_huesat(unsigned hue, unsigned sat);
59 static GdColor from_gdash_index(unsigned c); /* similar to c64 colors */
61 bool operator==(const GdColor &rhs) const;
62 /// Compare two color objects for inequality.
63 /// @return True, if they are not equal.
64 bool operator!=(const GdColor &rhs) const { return !(*this==rhs); }
66 unsigned int get_r() const;
67 unsigned int get_g() const;
68 unsigned int get_b() const;
69 unsigned int get_h() const;
70 unsigned int get_s() const;
71 unsigned int get_v() const;
72 unsigned int get_uint() const;
73 GdColor to_rgb() const;
74 GdColor to_hsv() const;
76 bool is_c64() const;
78 int get_c64_index() const;
80 static const char ** get_c64_palette_names();
81 static const char ** get_atari_palette_names();
82 static const char ** get_c64dtv_palette_names();
83 static const char ** get_palette_types_names();
85 private:
86 Type type;
87 union {
88 unsigned char index;
89 struct {
90 unsigned char r;
91 unsigned char g;
92 unsigned char b;
93 } rgb;
94 struct {
95 unsigned short h; /* [0;360) */
96 unsigned char s; /* [0;100] */
97 unsigned char v; /* [0;100] */
98 } hsv;
101 GdColor(unsigned char r, unsigned char g, unsigned char b) :
102 type(TypeRGB) { rgb.r = r; rgb.g = g; rgb.b = b; }
103 GdColor(Type _type, int i) :
104 type(_type) { index = i; }
105 GdColor(unsigned int rgb);
107 friend std::string visible_name(const GdColor& c);
108 friend std::ostream& operator<<(std::ostream& os, const GdColor& c);
111 /* i/o operators and functions like for all other cave types */
112 std::ostream& operator<<(std::ostream& os, const GdColor& c);
113 std::string visible_name(const GdColor& c);
114 bool read_from_string(std::string const& s, GdColor &c);
117 /// Traditional C64 color indexes, plus one GDash special color.
118 enum GdColorIndex {
119 GD_COLOR_INDEX_BLACK=0,
120 GD_COLOR_INDEX_WHITE=1,
121 GD_COLOR_INDEX_RED=2,
122 GD_COLOR_INDEX_PURPLE=4,
123 GD_COLOR_INDEX_CYAN=3,
124 GD_COLOR_INDEX_GREEN=5,
125 GD_COLOR_INDEX_BLUE=6,
126 GD_COLOR_INDEX_YELLOW=7,
127 GD_COLOR_INDEX_ORANGE=8,
128 GD_COLOR_INDEX_BROWN=9,
129 GD_COLOR_INDEX_LIGHTRED=10,
130 GD_COLOR_INDEX_GRAY1=11,
131 GD_COLOR_INDEX_GRAY2=12,
132 GD_COLOR_INDEX_LIGHTGREEN=13,
133 GD_COLOR_INDEX_LIGHTBLUE=14,
134 GD_COLOR_INDEX_GRAY3=15,
135 GD_COLOR_INDEX_MIDDLEBLUE=16,
137 enum GdColorIndexHelper {
138 GD_COLOR_SETCOLOR=31, /* for blittext */
141 // specialized for color codes for font manager
142 template <>
143 inline Printf const& Printf::operator%(GdColorIndex const &colorindex) const {
144 /* +64 is needed so it is a normal ascii char in the encoding, not a string limiter \0
145 * or a \n or whatever */
146 char s[3] = { GD_COLOR_SETCOLOR, char(colorindex+64), 0 };
147 return (*this) % s;
151 #define GD_GDASH_BLACK (GdColor::from_gdash_index(GD_COLOR_INDEX_BLACK))
152 #define GD_GDASH_WHITE (GdColor::from_gdash_index(GD_COLOR_INDEX_WHITE))
153 #define GD_GDASH_RED (GdColor::from_gdash_index(GD_COLOR_INDEX_RED))
154 #define GD_GDASH_PURPLE (GdColor::from_gdash_index(GD_COLOR_INDEX_PURPLE))
155 #define GD_GDASH_CYAN (GdColor::from_gdash_index(GD_COLOR_INDEX_CYAN))
156 #define GD_GDASH_GREEN (GdColor::from_gdash_index(GD_COLOR_INDEX_GREEN))
157 #define GD_GDASH_BLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_BLUE))
158 #define GD_GDASH_YELLOW (GdColor::from_gdash_index(GD_COLOR_INDEX_YELLOW))
159 #define GD_GDASH_ORANGE (GdColor::from_gdash_index(GD_COLOR_INDEX_ORANGE))
160 #define GD_GDASH_BROWN (GdColor::from_gdash_index(GD_COLOR_INDEX_BROWN))
161 #define GD_GDASH_LIGHTRED (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTRED))
162 #define GD_GDASH_GRAY1 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY1))
163 #define GD_GDASH_GRAY2 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY2))
164 #define GD_GDASH_LIGHTGREEN (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTGREEN))
165 #define GD_GDASH_LIGHTBLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_LIGHTBLUE))
166 #define GD_GDASH_GRAY3 (GdColor::from_gdash_index(GD_COLOR_INDEX_GRAY3))
167 #define GD_GDASH_MIDDLEBLUE (GdColor::from_gdash_index(GD_COLOR_INDEX_MIDDLEBLUE))
169 extern const GdColor gd_flash_color;
170 extern const GdColor gd_select_color;
172 #endif