The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
color.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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/charconv.hpp"
19
20#include <iomanip>
21#include <sstream>
22
24{
25 if(c.empty()) {
26 return null_color();
27 }
28
29 std::vector<std::string_view> fields = utils::split_view(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
45{
46 if(c.empty()) {
47 return null_color();
48 }
49
50 std::vector<std::string_view> fields = utils::split_view(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
65{
66 if(c[0] == '#') {
67 c = c.substr(1);
68 }
69
70 if(c.length() != 6) {
71 throw std::invalid_argument("Color hex string should be exactly 6 digits (leading '#' optional)");
72 }
73
74 if(std::any_of(c.begin(), c.end(), [](const char& ch) { return std::isxdigit(ch) == 0; })) {
75 throw std::invalid_argument("Color hex string contains invalid characters");
76 }
77
78 auto temp_c = *utils::from_chars<uint32_t>(c, 16);
79
80 return {
81 static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 16),
82 static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 8),
83 static_cast<uint8_t>((0x00FFFFFF & temp_c)),
85 };
86}
87
88std::string color_t::to_hex_string() const
89{
90 std::ostringstream h;
91
92 h << "#"
93 << std::hex << std::setfill('0')
94 << std::setw(2) << static_cast<int>(r)
95 << std::setw(2) << static_cast<int>(g)
96 << std::setw(2) << static_cast<int>(b);
97
98 if(a != ALPHA_OPAQUE) {
99 h << std::setw(2) << static_cast<int>(a);
100 }
101
102 return h.str();
103}
104
105std::string color_t::to_rgba_string() const
106{
107 std::ostringstream color;
108
109 color << static_cast<int>(r) << ','
110 << static_cast<int>(g) << ','
111 << static_cast<int>(b) << ','
112 << static_cast<int>(a);
113
114 return color.str();
115}
116
117std::string color_t::to_rgb_string() const
118{
119 std::ostringstream color;
120
121 color << static_cast<int>(r) << ','
122 << static_cast<int>(g) << ','
123 << static_cast<int>(b);
124
125 return color.str();
126}
constexpr uint8_t ALPHA_OPAQUE
Definition color.hpp:37
std::vector< std::string_view > split_view(std::string_view s, const char sep, const int flags)
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
static color_t from_hex_string(std::string_view c)
Creates a new color_t object from a string variable in hex format.
Definition color.cpp:64
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition color.cpp:88
static color_t from_rgb_string(std::string_view c)
Creates a new opaque color_t object from a string variable in "R,G,B" format.
Definition color.cpp:44
static constexpr color_t null_color()
Definition of a 'null' color - fully transparent black.
Definition color.hpp:245
std::string to_rgb_string() const
Returns the stored color as an "R,G,B" string.
Definition color.cpp:117
static color_t from_rgba_string(std::string_view 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:105
#define g
#define h
#define c
#define b