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