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