The Battle for Wesnoth  1.19.2+dev
color.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 
15 #include "color.hpp"
16 
18 #include "utils/from_chars.hpp"
19 
20 #include <iomanip>
21 #include <sstream>
22 
23 color_t color_t::from_rgba_string(const std::string& c)
24 {
25  if(c.empty()) {
26  return null_color();
27  }
28 
29  std::vector<std::string> fields = utils::split(c);
30 
31  // Allow either 3 (automatic opaque alpha) or 4 (explicit alpha) fields
32  if(fields.size() != 3 && fields.size() != 4) {
33  throw std::invalid_argument("Wrong number of components for RGBA color");
34  }
35 
36  return {
37  *utils::from_chars<uint8_t>(fields[0]),
38  *utils::from_chars<uint8_t>(fields[1]),
39  *utils::from_chars<uint8_t>(fields[2]),
40  fields.size() == 4 ? *utils::from_chars<uint8_t>(fields[3]) : ALPHA_OPAQUE
41  };
42 }
43 
44 color_t color_t::from_rgb_string(const std::string& c)
45 {
46  if(c.empty()) {
47  return null_color();
48  }
49 
50  std::vector<std::string> fields = utils::split(c);
51 
52  if(fields.size() != 3) {
53  throw std::invalid_argument("Wrong number of components for RGB color");
54  }
55 
56  return {
57  *utils::from_chars<uint8_t>(fields[0]),
58  *utils::from_chars<uint8_t>(fields[1]),
59  *utils::from_chars<uint8_t>(fields[2]),
61  };
62 }
63 
64 color_t color_t::from_hex_string(const std::string& c)
65 {
66  if(c.length() != 6) {
67  throw std::invalid_argument("Color hex string should be exactly 6 digits");
68  }
69 
70  if(std::any_of(c.begin(), c.end(), [](const char& ch) { return std::isxdigit(ch) == 0; })) {
71  throw std::invalid_argument("Color hex string contains invalid characters");
72  }
73 
74  auto temp_c = *utils::from_chars<uint32_t>(c, 16);
75 
76  return {
77  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 16),
78  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 8),
79  static_cast<uint8_t>((0x00FFFFFF & temp_c)),
81  };
82 }
83 
84 std::string color_t::to_hex_string() const
85 {
86  std::ostringstream h;
87 
88  h << "#"
89  << std::hex << std::setfill('0')
90  << std::setw(2) << static_cast<int>(r)
91  << std::setw(2) << static_cast<int>(g)
92  << std::setw(2) << static_cast<int>(b);
93 
94  if(a != ALPHA_OPAQUE) {
95  h << std::setw(2) << static_cast<int>(a);
96  }
97 
98  return h.str();
99 }
100 
101 std::string color_t::to_rgba_string() const
102 {
103  std::ostringstream color;
104 
105  color << static_cast<int>(r) << ','
106  << static_cast<int>(g) << ','
107  << static_cast<int>(b) << ','
108  << static_cast<int>(a);
109 
110  return color.str();
111 }
112 
113 std::string color_t::to_rgb_string() const
114 {
115  std::ostringstream color;
116 
117  color << static_cast<int>(r) << ','
118  << static_cast<int>(g) << ','
119  << static_cast<int>(b);
120 
121  return color.str();
122 }
double g
Definition: astarsearch.cpp:63
constexpr uint8_t ALPHA_OPAQUE
Definition: color.hpp:45
std::vector< std::string > split(const config_attribute_value &val)
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
static color_t from_rgb_string(const std::string &c)
Creates a new opaque color_t object from a string variable in "R,G,B" format.
Definition: color.cpp:44
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:84
static constexpr color_t null_color()
Definition of a 'null' color - fully transparent black.
Definition: color.hpp:251
std::string to_rgb_string() const
Returns the stored color as an "R,G,B" string.
Definition: color.cpp:113
static color_t from_rgba_string(const std::string &c)
Creates a new color_t object from a string variable in "R,G,B,A" format.
Definition: color.cpp:23
std::string to_rgba_string() const
Returns the stored color as an "R,G,B,A" string.
Definition: color.cpp:101
static color_t from_hex_string(const std::string &c)
Creates a new color_t object from a string variable in hex format.
Definition: color.cpp:64
mock_char c
#define h
#define a
#define b