color_range.hpp

Go to the documentation of this file.
00001 /* $Id: color_range.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /** @file */
00017 
00018 #ifndef COLOR_RANGE_H_INCLUDED
00019 #define COLOR_RANGE_H_INCLUDED
00020 
00021 //These macros interfere with MS VC++
00022 #ifdef _MSC_VER
00023     #undef max
00024     #undef min
00025 #endif
00026 
00027 #include "global.hpp"
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031 
00032 #include "SDL_types.h"
00033 
00034 /* Convert comma separated string into rgb values.
00035  * Return false and empty result on error.
00036  */
00037 bool string2rgb(const std::string& s, std::vector<Uint32>& result);
00038 
00039 /**
00040  * A color range definition is made of four reference RGB colors, used
00041  * for calculating conversions from a source/key palette.
00042  *
00043  *   1) The average shade of a unit's team-color portions
00044  *      (default: gray #808080)
00045  *   2) The maximum highlight shade of a unit's team-color portions
00046  *      (default: white)
00047  *   3) The minimum shadow shade of a unit's team-color portions
00048  *      (default: black)
00049  *   4) A plain high-contrast color, used for the markers on the mini-ma
00050  *      (default: same as the provided average shade, or gray #808080)
00051  *
00052  * The first three reference colors are used for converting a source palette
00053  * with the external recolor_range() method.
00054  */
00055 class color_range
00056 {
00057 public:
00058  /**
00059   * Constructor, which expects four reference RGB colors.
00060   * @param mid Average color shade.
00061   * @param max Maximum (highlight) color shade
00062   * @param min Minimum color shade
00063   * @param rep High-contrast reference color
00064   */
00065   color_range(Uint32 mid , Uint32 max = 0x00FFFFFF , Uint32 min = 0x00000000 , Uint32 rep = 0x00808080):mid_(mid),max_(max),min_(min),rep_(rep){};
00066 
00067   /**
00068    * Constructor, which expects four reference RGB colors.
00069    * @param v STL vector with the four reference colors in order.
00070    */
00071   color_range(const std::vector<Uint32>& v)
00072     : mid_(v.size()     ? v[0] : 0x00808080),
00073       max_(v.size() > 1 ? v[1] : 0x00FFFFFF),
00074       min_(v.size() > 2 ? v[2] : 0x00000000),
00075       rep_(v.size() > 3 ? v[3] : mid_)
00076   {
00077   };
00078 
00079   /** Default constructor. */
00080   color_range() : mid_(0x00808080), max_(0x00FFFFFF), min_(0x00000000), rep_(0x00808080) {};
00081 
00082   /** Average color shade. */
00083   Uint32 mid() const{return(mid_);};
00084   /** Maximum color shade. */
00085   Uint32 max() const{return(max_);};
00086   /** Minimum color shade. */
00087   Uint32 min() const{return(min_);};
00088   /** High-contrast shade, intended for the minimap markers. */
00089   Uint32 rep() const{return(rep_);};
00090 
00091   bool operator<(const color_range& b) const
00092   {
00093     if(mid_ != b.mid()) return(mid_ < b.mid());
00094     if(max_ != b.max()) return(max_ < b.max());
00095     if(min_ != b.min()) return(min_ < b.min());
00096     return(rep_ < b.rep());
00097   }
00098 
00099   bool operator==(const color_range& b) const
00100   {
00101     return(mid_ == b.mid() && max_ == b.max() && min_ == b.min() && rep_ == b.rep());
00102   }
00103 
00104   int index() const; // the default team index for this color, or 0 for none
00105 
00106 private:
00107   Uint32 mid_ , max_ , min_ , rep_;
00108 };
00109 
00110 /**
00111  * Creates a reference color palette from a color range.
00112  */
00113 std::vector<Uint32> palette(color_range cr);
00114 
00115 /**
00116  * Converts a source palette using the specified color_range object.
00117  * This holds the main interface for range-based team coloring. The output is
00118  * used with the recolor_image() method to do the actual recoloring.
00119  * @param new_rgb Specifies parameters for the conversion.
00120  * @param old_rgb Source palette.
00121  * @return A STL map of colors, with the keys being source palette elements, and the values
00122  *         are the result of applying the color range conversion on it.
00123  */
00124 std::map<Uint32, Uint32> recolor_range(const color_range& new_rgb, const std::vector<Uint32>& old_rgb);
00125 
00126 /**
00127  * Converts a color value to WML text markup syntax for highlighting.
00128  * For example, 0x00CC00CC becomes "<204,0,204>".
00129  */
00130 std::string rgb2highlight(Uint32 rgb);
00131 
00132 /**
00133  * Converts a color value to WML text markup syntax for highlighting.
00134  * For example, 0x00CC00CC becomes "#CC00CC".
00135  */
00136 std::string rgb2highlight_pango(Uint32 rgb);
00137 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:32 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs