The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
color_range.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /** @file */
16 
17 #ifndef COLOR_RANGE_H_INCLUDED
18 #define COLOR_RANGE_H_INCLUDED
19 
20 //These macros interfere with MS VC++
21 #ifdef _MSC_VER
22  #undef max
23  #undef min
24 #endif
25 
26 #include "global.hpp"
27 #include <map>
28 #include <string>
29 #include <vector>
30 
31 #include <SDL_types.h>
32 
33 /* Convert comma separated string into rgb values.
34  * Return false and empty result on error.
35  */
36 bool string2rgb(const std::string& s, std::vector<Uint32>& result);
37 
38 /**
39  * A color range definition is made of four reference RGB colors, used
40  * for calculating conversions from a source/key palette.
41  *
42  * 1) The average shade of a unit's team-color portions
43  * (default: gray #808080)
44  * 2) The maximum highlight shade of a unit's team-color portions
45  * (default: white)
46  * 3) The minimum shadow shade of a unit's team-color portions
47  * (default: black)
48  * 4) A plain high-contrast color, used for the markers on the mini-map
49  * (default: same as the provided average shade, or gray #808080)
50  *
51  * The first three reference colors are used for converting a source palette
52  * with the external recolor_range() method.
53  */
55 {
56 public:
57  /**
58  * Constructor, which expects four reference RGB colors.
59  * @param mid Average color shade.
60  * @param max Maximum (highlight) color shade
61  * @param min Minimum color shade
62  * @param rep High-contrast reference color
63  */
64  color_range(Uint32 mid , Uint32 max = 0x00FFFFFF , Uint32 min = 0x00000000 , Uint32 rep = 0x00808080):mid_(mid),max_(max),min_(min),rep_(rep){}
65 
66  /**
67  * Constructor, which expects four reference RGB colors.
68  * @param v STL vector with the four reference colors in order.
69  */
70  color_range(const std::vector<Uint32>& v)
71  : mid_(v.size() ? v[0] : 0x00808080),
72  max_(v.size() > 1 ? v[1] : 0x00FFFFFF),
73  min_(v.size() > 2 ? v[2] : 0x00000000),
74  rep_(v.size() > 3 ? v[3] : mid_)
75  {
76  }
77 
78  /** Default constructor. */
79  color_range() : mid_(0x00808080), max_(0x00FFFFFF), min_(0x00000000), rep_(0x00808080) {}
80 
81  /** Average color shade. */
82  Uint32 mid() const{return(mid_);}
83  /** Maximum color shade. */
84  Uint32 max() const{return(max_);}
85  /** Minimum color shade. */
86  Uint32 min() const{return(min_);}
87  /** High-contrast shade, intended for the minimap markers. */
88  Uint32 rep() const{return(rep_);}
89 
90  bool operator<(const color_range& b) const
91  {
92  if(mid_ != b.mid()) return(mid_ < b.mid());
93  if(max_ != b.max()) return(max_ < b.max());
94  if(min_ != b.min()) return(min_ < b.min());
95  return(rep_ < b.rep());
96  }
97 
98  bool operator==(const color_range& b) const
99  {
100  return(mid_ == b.mid() && max_ == b.max() && min_ == b.min() && rep_ == b.rep());
101  }
102 
103  int index() const; // the default team index for this color, or 0 for none
104 
105  /** Return a string describing the color range for debug output. */
106  std::string debug() const;
107 
108 private:
109  Uint32 mid_ , max_ , min_ , rep_;
110 };
111 
112 /**
113  * Creates a reference color palette from a color range.
114  */
115 std::vector<Uint32> palette(color_range cr);
116 
117 /**
118  * Converts a source palette using the specified color_range object.
119  * This holds the main interface for range-based team coloring. The output is
120  * used with the recolor_image() method to do the actual recoloring.
121  * @param new_rgb Specifies parameters for the conversion.
122  * @param old_rgb Source palette.
123  * @return A STL map of colors, with the keys being source palette elements, and the values
124  * are the result of applying the color range conversion on it.
125  */
126 std::map<Uint32, Uint32> recolor_range(const color_range& new_rgb, const std::vector<Uint32>& old_rgb);
127 
128 /**
129  * Converts a color value to WML text markup syntax for highlighting.
130  * For example, 0x00CC00CC becomes "<204,0,204>".
131  */
132 std::string rgb2highlight(Uint32 rgb);
133 
134 /**
135  * Converts a color value to WML text markup syntax for highlighting.
136  * For example, 0x00CC00CC becomes "#CC00CC".
137  */
138 std::string rgb2highlight_pango(Uint32 rgb);
139 #endif
std::string debug() const
Return a string describing the color range for debug output.
color_range(const std::vector< Uint32 > &v)
Constructor, which expects four reference RGB colors.
Definition: color_range.hpp:70
Uint32 mid() const
Average color shade.
Definition: color_range.hpp:82
Uint32 rep() const
High-contrast shade, intended for the minimap markers.
Definition: color_range.hpp:88
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLuint64EXT * result
Definition: glew.h:10727
std::vector< Uint32 > palette(color_range cr)
Creates a reference color palette from a color range.
const GLdouble * v
Definition: glew.h:1359
bool operator==(const color_range &b) const
Definition: color_range.hpp:98
color_range(Uint32 mid, Uint32 max=0x00FFFFFF, Uint32 min=0x00000000, Uint32 rep=0x00808080)
Constructor, which expects four reference RGB colors.
Definition: color_range.hpp:64
color_range()
Default constructor.
Definition: color_range.hpp:79
std::string rgb2highlight(Uint32 rgb)
Converts a color value to WML text markup syntax for highlighting.
std::string rgb2highlight_pango(Uint32 rgb)
Converts a color value to WML text markup syntax for highlighting.
bool operator<(const color_range &b) const
Definition: color_range.hpp:90
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:54
bool string2rgb(const std::string &s, std::vector< Uint32 > &result)
Definition: color_range.cpp:91
std::map< Uint32, Uint32 > recolor_range(const color_range &new_rgb, const std::vector< Uint32 > &old_rgb)
Converts a source palette using the specified color_range object.
Definition: color_range.cpp:30
int index() const
GLsizeiptr size
Definition: glew.h:1649
Uint32 min() const
Minimum color shade.
Definition: color_range.hpp:86
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Uint32 max() const
Maximum color shade.
Definition: color_range.hpp:84