The Battle for Wesnoth  1.15.0-dev
color.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
15 #include "color.hpp"
16 
17 #include <iomanip>
18 #include <sstream>
19 
20 color_t color_t::from_rgba_string(const std::string& c)
21 {
22  if(c.empty()) {
23  return null_color();
24  }
25 
26  std::vector<std::string> fields = utils::split(c);
27 
28  // Allow either 3 (automatic opaque alpha) or 4 (explicit alpha) fields
29  if(fields.size() != 3 && fields.size() != 4) {
30  throw std::invalid_argument("Wrong number of components for RGBA color");
31  }
32 
33  return {
34  static_cast<uint8_t>(std::stoul(fields[0])),
35  static_cast<uint8_t>(std::stoul(fields[1])),
36  static_cast<uint8_t>(std::stoul(fields[2])),
37  static_cast<uint8_t>(fields.size() == 4 ? std::stoul(fields[3]) : ALPHA_OPAQUE)
38  };
39 }
40 
41 color_t color_t::from_rgb_string(const std::string& c)
42 {
43  if(c.empty()) {
44  return null_color();
45  }
46 
47  std::vector<std::string> fields = utils::split(c);
48 
49  if(fields.size() != 3) {
50  throw std::invalid_argument("Wrong number of components for RGB color");
51  }
52 
53  return {
54  static_cast<uint8_t>(std::stoul(fields[0])),
55  static_cast<uint8_t>(std::stoul(fields[1])),
56  static_cast<uint8_t>(std::stoul(fields[2])),
57  static_cast<uint8_t>(ALPHA_OPAQUE)
58  };
59 }
60 
61 color_t color_t::from_hex_string(const std::string& c)
62 {
63  if(c.length() != 6) {
64  throw std::invalid_argument("Color hex string should be exactly 6 digits");
65  }
66 
67  unsigned long temp_c = std::strtol(c.c_str(), nullptr, 16);
68 
69  return {
70  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 16),
71  static_cast<uint8_t>((0x00FFFFFF & temp_c) >> 8),
72  static_cast<uint8_t>((0x00FFFFFF & temp_c)),
74  };
75 }
76 
78 {
79  return {
80  static_cast<uint8_t>((RGBA_RED_MASK & c) >> RGBA_RED_BITSHIFT),
81  static_cast<uint8_t>((RGBA_GREEN_MASK & c) >> RGBA_GREEN_BITSHIFT),
82  static_cast<uint8_t>((RGBA_BLUE_MASK & c) >> RGBA_BLUE_BITSHIFT),
83  static_cast<uint8_t>((RGBA_ALPHA_MASK & c) >> RGBA_ALPHA_BITSHIFT),
84  };
85 }
86 
88 {
89  return {
90  static_cast<uint8_t>((SDL_RED_MASK & c) >> SDL_RED_BITSHIFT),
91  static_cast<uint8_t>((SDL_GREEN_MASK & c) >> SDL_GREEN_BITSHIFT),
92  static_cast<uint8_t>((SDL_BLUE_MASK & c) >> SDL_BLUE_BITSHIFT),
93  static_cast<uint8_t>((SDL_ALPHA_MASK & c) >> SDL_ALPHA_BITSHIFT),
94  };
95 }
96 
97 std::string color_t::to_hex_string() const
98 {
99  std::ostringstream h;
100 
101  h << "#"
102  << std::hex << std::setfill('0')
103  << std::setw(2) << static_cast<int>(r)
104  << std::setw(2) << static_cast<int>(g)
105  << std::setw(2) << static_cast<int>(b);
106 
107  return h.str();
108 }
109 
110 std::string color_t::to_rgba_string() const
111 {
112  std::ostringstream color;
113 
114  color << static_cast<int>(r) << ','
115  << static_cast<int>(g) << ','
116  << static_cast<int>(b) << ','
117  << static_cast<int>(a);
118 
119  return color.str();
120 }
121 
122 std::string color_t::to_rgb_string() const
123 {
124  std::ostringstream color;
125 
126  color << static_cast<int>(r) << ','
127  << static_cast<int>(g) << ','
128  << static_cast<int>(b);
129 
130  return color.str();
131 }
std::string to_rgba_string() const
Returns the stored color as an "R,G,B,A" string.
Definition: color.cpp:110
const uint32_t RGBA_ALPHA_MASK
Definition: color.hpp:38
const uint32_t RGBA_GREEN_BITSHIFT
Definition: color.hpp:45
const uint32_t SDL_BLUE_MASK
Definition: color.hpp:31
const uint32_t RGBA_BLUE_BITSHIFT
Definition: color.hpp:46
const uint32_t SDL_ALPHA_MASK
Definition: color.hpp:28
const uint32_t SDL_GREEN_BITSHIFT
Definition: color.hpp:35
const uint32_t RGBA_RED_BITSHIFT
Definition: color.hpp:44
std::string to_rgb_string() const
Returns the stored color as an "R,G,B" string.
Definition: color.cpp:122
#define h
const uint32_t RGBA_RED_MASK
Definition: color.hpp:39
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
const uint32_t SDL_BLUE_BITSHIFT
Definition: color.hpp:36
const uint32_t SDL_RED_MASK
Definition: color.hpp:29
static color_t null_color()
Definition of a &#39;null&#39; color - fully transparent black.
Definition: color.hpp:236
const uint32_t RGBA_GREEN_MASK
Definition: color.hpp:40
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:61
const uint32_t SDL_RED_BITSHIFT
Definition: color.hpp:34
uint8_t r
Red value.
Definition: color.hpp:177
uint8_t a
Alpha value.
Definition: color.hpp:186
static color_t from_argb_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:87
std::string to_hex_string() const
Returns the stored color in rrggbb hex format.
Definition: color.cpp:97
static color_t from_rgba_bytes(uint32_t c)
Creates a new color_t object from a uint32_t variable.
Definition: color.cpp:77
const uint8_t ALPHA_OPAQUE
Definition: color.hpp:48
const uint32_t RGBA_ALPHA_BITSHIFT
Definition: color.hpp:43
const uint32_t SDL_ALPHA_BITSHIFT
Definition: color.hpp:33
const uint32_t SDL_GREEN_MASK
Definition: color.hpp:30
uint8_t g
Green value.
Definition: color.hpp:180
uint8_t b
Blue value.
Definition: color.hpp:183
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:20
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:41
const uint32_t RGBA_BLUE_MASK
Definition: color.hpp:41