1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom 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 OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OPENXCOM_PALETTE_H
20 #define OPENXCOM_PALETTE_H
21 
22 #include <string>
23 #include <SDL.h>
24 
25 namespace OpenXcom
26 {
27 
28 /**
29  * Container for palettes (sets of 8bpp colors).
30  * Works as an encapsulation for SDL's SDL_Color struct and
31  * provides shortcuts for common tasks to make code more readable.
32  */
33 class Palette
34 {
35 private:
36 	SDL_Color *_colors;
37 	int _count;
38 public:
39 	/// Creates a blank palette.
40 	Palette();
41 	/// Cleans up the palette.
42 	~Palette();
43 	/// Loads the colors from an X-Com palette.
44 	void loadDat(const std::string &filename, int ncolors, int offset = 0);
45 	// Gets a certain color from the palette.
46 	SDL_Color *getColors(int offset = 0) const;
47 
48 	void savePal(const std::string &file) const;
49 
50 	/// Converts a given color into a RGBA color value.
51 	static Uint32 getRGBA(SDL_Color* pal, Uint8 color);
52 	/// Gets the position of a given palette.
53 	/**
54 	 * Returns the position of a palette inside an X-Com palette file (each is a 768-byte chunks).
55 	 * Handy for loading the palettes from the game files.
56 	 * @param palette Requested palette.
57 	 * @return Palette position in bytes.
58 	 */
palOffset(int palette)59 	static inline int palOffset(int palette) { return palette*(768+6); }
60 	/// Gets the position of a certain color block in a palette.
61 	/**
62 	 * Returns the position of a certain color block in an X-Com palette (they're usually split in 16-color gradients).
63 	 * Makes setting element colors a lot easier than determining the exact color position.
64 	 * @param block Requested block.
65 	 * @return Color position.
66 	 */
blockOffset(Uint8 block)67 	static inline Uint8 blockOffset(Uint8 block) { return block*16; }
68 	/// Position of the background colors block in an X-Com palette (used for background images in screens).
69 	static const int backPos = 224;
70 };
71 
72 }
73 
74 #endif
75