The Battle for Wesnoth  1.19.0-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 
16 #include "color.hpp"
17 
18 #include <iomanip>
19 #include <sstream>
20 
21 color_t color_t::from_rgba_string(const std::string& c)
22 {
23  if(c.empty()) {
24  return null_color();
25  }
26 
27  std::vector<std::string> fields = utils::split(c);
28 
29  // Allow either 3 (automatic opaque alpha) or 4 (explicit alpha) fields
30  if(fields.size() != 3 && fields.size() != 4) {
31  throw std::invalid_argument("Wrong number of components for RGBA color");
32  }
33 
34  return {
35  static_cast<uint8_t>(std::stoul(fields[0])),
36  static_cast<uint8_t>(std::stoul(fields[1])),
37  static_cast<uint8_t>(std::stoul(fields[2])),
38  static_cast<uint8_t>(fields.size() == 4 ? std::stoul(fields[3]) : ALPHA_OPAQUE)
39  };
40 }
41 
42 color_t color_t::from_rgb_string(const std::string& c)
43 {
44  if(c.empty()) {
45  return null_color();
46  }
47 
48  std::vector<std::string> fields = utils::split(c);
49 
50  if(fields.size() != 3) {
51  throw std::invalid_argument("Wrong number of components for RGB color");
52  }
53 
54  return {
55  static_cast<uint8_t>(std::stoul(fields[0])),
56  static_cast<uint8_t>(std::stoul(fields[1])),
57  static_cast<uint8_t>(std::stoul(fields[2])),
58  static_cast<uint8_t>(ALPHA_OPAQUE)
59  };
60 }
61 
62 color_t color_t::from_hex_string(const std::string& c)
63 {
64  if(c.length() != 6) {
65  throw std::invalid_argument("Color hex string should be exactly 6 digits");
66  }
67 
68  unsigned long temp_c = std::strtol(c.c_str(), nullptr, 16);
69 
70  return {
71  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 16),
72  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 8),
73  static_cast<uint8_t>((0x00FFFFFF & temp_c)),
75  };
76 }
77 
78 std::string color_t::to_hex_string() const
79 {
80  std::ostringstream h;
81 
82  h << "#"
83  << std::hex << std::setfill('0')
84  << std::setw(2) << static_cast<int>(r)
85  << std::setw(2) << static_cast<int>(g)
86  << std::setw(2) << static_cast<int>(b);
87 
88  if(a != ALPHA_OPAQUE) {
89  h << std::setw(2) << static_cast<int>(a);
90  }
91 
92  return h.str();
93 }
94 
95 std::string color_t::to_rgba_string() const
96 {
97  std::ostringstream color;
98 
99  color << static_cast<int>(r) << ','
100  << static_cast<int>(g) << ','
101  << static_cast<int>(b) << ','
102  << static_cast<int>(a);
103 
104  return color.str();
105 }
106 
107 std::string color_t::to_rgb_string() const
108 {
109  std::ostringstream color;
110 
111  color << static_cast<int>(r) << ','
112  << static_cast<int>(g) << ','
113  << static_cast<int>(b);
114 
115  return color.str();
116 }
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:42
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:78
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:107
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:21
std::string to_rgba_string() const
Returns the stored color as an "R,G,B,A" string.
Definition: color.cpp:95
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:62
mock_char c
#define h
#define a
#define b