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