The Battle for Wesnoth  1.19.1+dev
text_formatting.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 #include "font/text_formatting.hpp"
15 
16 #include "formatter.hpp"
17 #include "game_config.hpp"
18 #include "gettext.hpp"
19 #include "color.hpp"
20 
21 
22 namespace font {
23 
24 std::string span_color(const color_t& color)
25 {
26  return formatter() << "<span color='" << color.to_hex_string() << "'>";
27 }
28 
29 std::string span_color(const color_t& color, const std::string& data)
30 {
31  return span_color(color) + data + "</span>";
32 }
33 
34 std::string get_pango_color_from_id(const std::string& id)
35 {
36  const auto color = game_config::team_rgb_colors.find(id);
37  if(color != game_config::team_rgb_colors.end()) {
38  return (color->second[0]).to_hex_string();
39  }
40 
41  return "";
42 }
43 
44 std::string get_color_string_pango(const std::string& id)
45 {
46  const auto name = game_config::team_rgb_name.find(id);
47  if(name != game_config::team_rgb_name.end()) {
48  return formatter() << "<span color='" << get_pango_color_from_id(id) << "'>" << name->second << "</span>";
49  }
50 
51  return _("Invalid Color");
52 }
53 
54 }
std::ostringstream wrapper.
Definition: formatter.hpp:40
static std::string _(const char *str)
Definition: gettext.hpp:93
Collection of helper functions relating to Pango formatting.
std::string get_pango_color_from_id(const std::string &id)
Returns a hex color string from a color range.
std::string get_color_string_pango(const std::string &id)
Returns the name of a color range, colored with its own color.
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
std::map< std::string, t_string, std::less<> > team_rgb_name
std::map< std::string, std::vector< color_t >, std::less<> > team_rgb_colors
std::string_view data
Definition: picture.cpp:194
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:84