The Battle for Wesnoth  1.19.27+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 <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 using color_mapping = std::unordered_map<color_t, color_t>;
26 
27 /**
28  * Representing the "team color" information, this C++ class contains a subset
29  * of WML's [color_range] tag. The other content of the [color_range] tags is
30  * parsed by the game_config class, and the data is held by several globals
31  * declared in game_config.hpp.
32  *
33  * The main definition is made of three reference RGB colors, used for
34  * calculating conversions from a source/key palette with the external
35  * generate_color_mapping() method.
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  *
44  * Some further colors are included because this mixes the responsibilities of
45  * being the "team color" class.
46  *
47  * 4) A color with high contrast to terrain colors, so that it can
48  * be used for the markers on the mini-map.
49  * (default: same as the provided average shade, or gray #808080)
50  * 5) A color with high contrast to dark backgrounds, used for text.
51  */
53 {
54 public:
55  /**
56  * Constructor, which expects four reference RGB colors.
57  * @param mid Average color shade.
58  * @param max Maximum (highlight) color shade
59  * @param min Minimum color shade
60  * @param rep High-contrast to terrain
61  * @param font High-contrast to a dark background
62  */
64  : mid_(mid)
65  , max_(max)
66  , min_(min)
67  , rep_(rep)
68  , font_color_(font)
69  {}
70 
71  /**
72  * Constructor, matching the WML tag's split of the rgb attribute in one argument,
73  * and the color for UI text in a different argument.
74  */
75  color_range(const std::vector<color_t>& v, const std::optional<color_t>& font)
76  : mid_(v.size() ? v[0] : color_t(128, 128, 128))
77  , max_(v.size() > 1 ? v[1] : color_t(255, 255, 255))
78  , min_(v.size() > 2 ? v[2] : color_t(0 , 0 , 0 ))
79  , rep_(v.size() > 3 ? v[3] : mid_)
80  , font_color_(font ? *font : mid_)
81  {}
82 
83  /** Default constructor. */
85  : mid_(128, 128, 128)
86  , max_(255, 255, 255)
87  , min_()
88  , rep_(128, 128, 128)
89  , font_color_(mid_)
90  {}
91 
92  /** Average color shade. */
93  color_t mid() const { return mid_; }
94 
95  /** Maximum color shade. */
96  color_t max() const { return max_; }
97 
98  /** Minimum color shade. */
99  color_t min() const { return min_; }
100 
101  /** High-contrast against typical terrain colors, intended for the minimap markers. */
102  color_t rep() const { return rep_; }
103 
104  /** High-contrast to dark backgrounds, intended for the text in the chat log, etc. */
105  color_t font_color() const { return font_color_; }
106 
107  bool operator==(const color_range& b) const
108  {
109  return mid_ == b.mid() && max_ == b.max() && min_ == b.min()
110  && rep_ == b.rep() && font_color_ == b.font_color();
111  }
112 
113  /** Return a string describing the color range for debug output. */
114  std::string debug() const;
115 
116 private:
118 };
119 
120 /**
121  * Creates a reference color palette from a color range.
122  */
123 std::vector<color_t> generate_reference_palette(const color_range& cr);
124 
125 /**
126  * Converts a source palette using the specified color_range object.
127  * This holds the main interface for range-based team coloring. The output is used with the recolor_image()
128 * method to do the actual recoloring.
129  *
130  * @param new_rgb Specifies parameters for the conversion.
131  * @param old_rgb Source palette.
132  *
133  * @return A STL map of colors, with the keys being source palette elements, and the values
134  * are the result of applying the color range conversion on it.
135  */
136 color_mapping generate_color_mapping(const color_range& new_rgb, const std::vector<color_t>& old_rgb);
Representing the "team color" information, this C++ class contains a subset of WML's [color_range] ta...
Definition: color_range.hpp:53
color_t max() const
Maximum color shade.
Definition: color_range.hpp:96
color_t font_color() const
High-contrast to dark backgrounds, intended for the text in the chat log, etc.
color_t max_
bool operator==(const color_range &b) const
color_t font_color_
color_t rep() const
High-contrast against typical terrain colors, intended for the minimap markers.
color_t min_
color_range(color_t mid, color_t max, color_t min, color_t rep, color_t font)
Constructor, which expects four reference RGB colors.
Definition: color_range.hpp:63
color_range()
Default constructor.
Definition: color_range.hpp:84
color_t mid_
color_t mid() const
Average color shade.
Definition: color_range.hpp:93
color_t rep_
color_t min() const
Minimum color shade.
Definition: color_range.hpp:99
std::string debug() const
Return a string describing the color range for debug output.
color_range(const std::vector< color_t > &v, const std::optional< color_t > &font)
Constructor, matching the WML tag's split of the rgb attribute in one argument, and the color for UI ...
Definition: color_range.hpp:75
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:25
Graphical text output.
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