-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
text_formatting.cpp
69 lines (53 loc) · 1.78 KB
/
text_formatting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright (C) 2003 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "font/text_formatting.hpp"
#include "formatter.hpp"
#include "game_config.hpp"
#include "gettext.hpp"
#include <iomanip>
namespace font {
std::string unit32_to_pango_color(uint32_t rgb)
{
std::ostringstream h;
// Must match what the pango expects
h << "#"
<< std::hex << std::setfill('0') << std::setw(2) << ((rgb & 0xFF0000) >> 16)
<< std::hex << std::setfill('0') << std::setw(2) << ((rgb & 0x00FF00) >> 8)
<< std::hex << std::setfill('0') << std::setw(2) << ( rgb & 0x0000FF);
return h.str();
}
std::string color2hexa(const SDL_Color &color)
{
char buf[7];
sprintf(buf, "%02x%02x%02x", color.r, color.g, color.b);
return buf;
}
std::string span_color(const SDL_Color &color)
{
return "<span foreground=\"#" + font::color2hexa(color) + "\">";
}
std::string get_pango_color_from_id(const std::string& id)
{
const auto color = game_config::team_rgb_colors.find(id);
if(color != game_config::team_rgb_colors.end()) {
return unit32_to_pango_color(color->second[0]);
}
return "";
}
std::string get_color_string_pango(const std::string& id)
{
const auto name = game_config::team_rgb_name.find(id);
if(name != game_config::team_rgb_name.end()) {
return formatter() << "<span color='" << get_pango_color_from_id(id) << "'>" << name->second << "</span>";
}
return _("Invalid Color");
}
}