The Battle for Wesnoth  1.17.0-dev
color.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 
79 {
80  return {
81  static_cast<uint8_t>((RGBA_RED_MASK & c) >> RGBA_RED_BITSHIFT),
82  static_cast<uint8_t>((RGBA_GREEN_MASK & c) >> RGBA_GREEN_BITSHIFT),
83  static_cast<uint8_t>((RGBA_BLUE_MASK & c) >> RGBA_BLUE_BITSHIFT),
84  static_cast<uint8_t>((RGBA_ALPHA_MASK & c) >> RGBA_ALPHA_BITSHIFT),
85  };
86 }
87 
89 {
90  return {
91  static_cast<uint8_t>((SDL_RED_MASK & c) >> SDL_RED_BITSHIFT),
92  static_cast<uint8_t>((SDL_GREEN_MASK & c) >> SDL_GREEN_BITSHIFT),
93  static_cast<uint8_t>((SDL_BLUE_MASK & c) >> SDL_BLUE_BITSHIFT),
94  static_cast<uint8_t>((SDL_ALPHA_MASK & c) >> SDL_ALPHA_BITSHIFT),
95  };
96 }
97 
98 std::string color_t::to_hex_string() const
99 {
100  std::ostringstream h;
101 
102  h << "#"
103  << std::hex << std::setfill('0')
104  << std::setw(2) << static_cast<int>(r)
105  << std::setw(2) << static_cast<int>(g)
106  << std::setw(2) << static_cast<int>(b);
107 
108  return h.str();
109 }
110 
111 std::string color_t::to_rgba_string() const
112 {
113  std::ostringstream color;
114 
115  color << static_cast<int>(r) << ','
116  << static_cast<int>(g) << ','
117  << static_cast<int>(b) << ','
118  << static_cast<int>(a);
119 
120  return color.str();
121 }
122 
123 std::string color_t::to_rgb_string() const
124 {
125  std::ostringstream color;
126 
127  color << static_cast<int>(r) << ','
128  << static_cast<int>(g) << ','
129  << static_cast<int>(b);
130 
131  return color.str();
132 }
std::string to_rgba_string() const
Returns the stored color as an "R,G,B,A" string.
Definition: color.cpp:111
const uint32_t RGBA_ALPHA_MASK
Definition: color.hpp:39
const uint32_t RGBA_GREEN_BITSHIFT
Definition: color.hpp:46
const uint32_t SDL_BLUE_MASK
Definition: color.hpp:32
const uint32_t RGBA_BLUE_BITSHIFT
Definition: color.hpp:47
const uint32_t SDL_ALPHA_MASK
Definition: color.hpp:29
const uint32_t SDL_GREEN_BITSHIFT
Definition: color.hpp:36
const uint32_t RGBA_RED_BITSHIFT
Definition: color.hpp:45
std::string to_rgb_string() const
Returns the stored color as an "R,G,B" string.
Definition: color.cpp:123
#define h
const uint32_t RGBA_RED_MASK
Definition: color.hpp:40
const uint32_t SDL_BLUE_BITSHIFT
Definition: color.hpp:37
const uint32_t SDL_RED_MASK
Definition: color.hpp:30
static color_t null_color()
Definition of a &#39;null&#39; color - fully transparent black.
Definition: color.hpp:237
const uint32_t RGBA_GREEN_MASK
Definition: color.hpp:41
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
const uint32_t SDL_RED_BITSHIFT
Definition: color.hpp:35
uint8_t r
Red value.
Definition: color.hpp:178
uint8_t a
Alpha value.
Definition: color.hpp:187
static color_t from_argb_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:88
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:98
static color_t from_rgba_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:78
const uint8_t ALPHA_OPAQUE
Definition: color.hpp:49
std::vector< std::string > split(const config_attribute_value &val)
const uint32_t RGBA_ALPHA_BITSHIFT
Definition: color.hpp:44
const uint32_t SDL_ALPHA_BITSHIFT
Definition: color.hpp:34
const uint32_t SDL_GREEN_MASK
Definition: color.hpp:31
uint8_t g
Green value.
Definition: color.hpp:181
uint8_t b
Blue value.
Definition: color.hpp:184
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
mock_char c
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
const uint32_t RGBA_BLUE_MASK
Definition: color.hpp:42