The Battle for Wesnoth  1.15.1+dev
color_range.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
18 
19 #include "color.hpp"
20 
21 #include <cstdint>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 //These macros interfere with MS VC++
27 #ifdef _MSC_VER
28  #undef max
29  #undef min
30 #endif
31 
32 using color_range_map = std::unordered_map<color_t, color_t>;
33 
34 /**
35  * A color range definition is made of four reference RGB colors, used
36  * for calculating conversions from a source/key palette.
37  *
38  * 1) The average shade of a unit's team-color portions
39  * (default: gray #808080)
40  * 2) The maximum highlight shade of a unit's team-color portions
41  * (default: white)
42  * 3) The minimum shadow shade of a unit's team-color portions
43  * (default: black)
44  * 4) A plain high-contrast color, used for the markers on the mini-map
45  * (default: same as the provided average shade, or gray #808080)
46  *
47  * The first three reference colors are used for converting a source palette
48  * with the external recolor_range() method.
49  */
51 {
52 public:
53  /**
54  * Constructor, which expects four reference RGB colors.
55  * @param mid Average color shade.
56  * @param max Maximum (highlight) color shade
57  * @param min Minimum color shade
58  * @param rep High-contrast reference color
59  */
60  color_range(color_t mid, color_t max = {255, 255, 255}, color_t min = {0, 0, 0}, color_t rep = {128, 128, 128})
61  : mid_(mid)
62  , max_(max)
63  , min_(min)
64  , rep_(rep)
65  {}
66 
67  /**
68  * Constructor, which expects four reference RGB colors.
69  * @param v STL vector with the four reference colors in order.
70  */
71  color_range(const std::vector<color_t>& v)
72  : mid_(v.size() ? v[0] : color_t(128, 128, 128))
73  , max_(v.size() > 1 ? v[1] : color_t(255, 255, 255))
74  , min_(v.size() > 2 ? v[2] : color_t(0 , 0 , 0 ))
75  , rep_(v.size() > 3 ? v[3] : mid_)
76  {}
77 
78  /** Default constructor. */
80  : mid_(128, 128, 128)
81  , max_(255, 255, 255)
82  , min_()
83  , rep_(128, 128, 128)
84  {}
85 
86  /** Average color shade. */
87  color_t mid() const { return mid_; }
88 
89  /** Maximum color shade. */
90  color_t max() const { return max_; }
91 
92  /** Minimum color shade. */
93  color_t min() const { return min_; }
94 
95  /** High-contrast shade, intended for the minimap markers. */
96  color_t rep() const { return rep_; }
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  bool operator<(const color_range& b) const
104  {
105  if(mid_ != b.mid()) { return mid_.to_rgba_bytes() < b.mid().to_rgba_bytes(); }
106  if(max_ != b.max()) { return max_.to_rgba_bytes() < b.max().to_rgba_bytes(); }
107  if(min_ != b.min()) { return min_.to_rgba_bytes() < b.min().to_rgba_bytes(); }
108 
109  return rep_.to_rgba_bytes() < b.rep().to_rgba_bytes();
110  }
111 
112  /** Return a string describing the color range for debug output. */
113  std::string debug() const;
114 
115 private:
117 };
118 
119 /**
120  * Creates a reference color palette from a color range.
121  */
122 std::vector<color_t> palette(const color_range& cr);
123 
124 /**
125  * Converts a source palette using the specified color_range object.
126  * This holds the main interface for range-based team coloring. The output is used with the recolor_image()
127 * method to do the actual recoloring.
128  *
129  * @param new_rgb Specifies parameters for the conversion.
130  * @param old_rgb Source palette.
131  *
132  * @return A STL map of colors, with the keys being source palette elements, and the values
133  * are the result of applying the color range conversion on it.
134  */
135 color_range_map recolor_range(const color_range& new_rgb, const std::vector<color_t>& old_rgb);
color_range_map recolor_range(const color_range &new_rgb, const std::vector< color_t > &old_rgb)
Converts a source palette using the specified color_range object.
Definition: color_range.cpp:28
color_t min() const
Minimum color shade.
Definition: color_range.hpp:93
color_range(const std::vector< color_t > &v)
Constructor, which expects four reference RGB colors.
Definition: color_range.hpp:71
bool operator==(const color_range &b) const
Definition: color_range.hpp:98
color_range(color_t mid, color_t max={255, 255, 255}, color_t min={0, 0, 0}, color_t rep={128, 128, 128})
Constructor, which expects four reference RGB colors.
Definition: color_range.hpp:60
color_t min_
#define b
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
bool operator<(const color_range &b) const
std::vector< color_t > palette(const color_range &cr)
Creates a reference color palette from a color range.
Definition: color_range.cpp:83
color_t mid_
std::unordered_map< color_t, color_t > color_range_map
Definition: color_range.hpp:32
color_t max() const
Maximum color shade.
Definition: color_range.hpp:90
color_t max_
color_range()
Default constructor.
Definition: color_range.hpp:79
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:50
uint32_t to_rgba_bytes() const
Returns the stored color as a uint32_t, in RGBA format.
Definition: color.hpp:131
std::string debug() const
Return a string describing the color range for debug output.
color_t rep() const
High-contrast shade, intended for the minimap markers.
Definition: color_range.hpp:96
color_t rep_
color_t mid() const
Average color shade.
Definition: color_range.hpp:87