A toolkit for working with phylogenetic data.
v0.24.0
color.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <string>
35 #include <utility> // swap
36 
37 namespace genesis {
38 namespace utils {
39 
40 // =================================================================================================
41 // Color
42 // =================================================================================================
43 
47 class Color
48 {
49 public:
50 
51  // -------------------------------------------------------------------------
52  // Constructors and Rule of Five
53  // -------------------------------------------------------------------------
54 
59  : r_( 0.0 ), g_( 0.0 ), b_( 0.0 ), a_( 1.0 )
60  {}
61 
65  Color( double r, double g, double b )
66  : Color( r, g, b, 1.0 )
67  {}
68 
72  Color( double r, double g, double b, double a );
73 
74  ~Color() = default;
75 
76  Color(Color const&) = default;
77  Color(Color&&) = default;
78 
79  Color& operator= (Color const&) = default;
80  Color& operator= (Color&&) = default;
81 
82  void swap (Color& other)
83  {
84  using std::swap;
85  swap(r_, other.r_);
86  swap(g_, other.g_);
87  swap(b_, other.b_);
88  swap(a_, other.a_);
89  }
90 
91  // -------------------------------------------------------------------------
92  // Factories
93  // -------------------------------------------------------------------------
94 
98  static Color from_bytes( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 );
99 
103  static Color from_hex( std::string const& hex_color, std::string const& prefix = "#" );
104 
105  // -------------------------------------------------------------------------
106  // Accessors
107  // -------------------------------------------------------------------------
108 
109  double r() const
110  {
111  return r_;
112  }
113 
114  double g() const
115  {
116  return g_;
117  }
118 
119  double b() const
120  {
121  return b_;
122  }
123 
124  double a() const
125  {
126  return a_;
127  }
128 
129  unsigned char r_byte() const
130  {
131  return to_byte_( r_ );
132  }
133 
134  unsigned char g_byte() const
135  {
136  return to_byte_( g_ );
137  }
138 
139  unsigned char b_byte() const
140  {
141  return to_byte_( b_ );
142  }
143 
144  unsigned char a_byte() const
145  {
146  return to_byte_( a_ );
147  }
148 
149  // -------------------------------------------------------------------------
150  // Modificators
151  // -------------------------------------------------------------------------
152 
153  void r (double value);
154 
155  void g (double value);
156 
157  void b (double value);
158 
159  void a (double value);
160 
161  void r_byte (unsigned char value)
162  {
163  r_ = from_byte_( value );
164  }
165 
166  void g_byte (unsigned char value)
167  {
168  g_ = from_byte_( value );
169  }
170 
171  void b_byte (unsigned char value)
172  {
173  b_ = from_byte_( value );
174  }
175 
176  void a_byte (unsigned char value)
177  {
178  a_ = from_byte_( value );
179  }
180 
181  // -------------------------------------------------------------------------
182  // Internal Functions
183  // -------------------------------------------------------------------------
184 
185 private:
186 
187  static unsigned char to_byte_( double v );
188  static double from_byte_( unsigned char v );
189 
190  // -------------------------------------------------------------------------
191  // Data Members
192  // -------------------------------------------------------------------------
193 
194 private:
195 
196  double r_;
197  double g_;
198  double b_;
199  double a_;
200 
201 };
202 
203 // =================================================================================================
204 // Basic Operators
205 // =================================================================================================
206 
207 inline void swap (Color& lhs, Color& rhs)
208 {
209  lhs.swap(rhs);
210 }
211 
212 inline bool operator == (Color const& lhs, Color const& rhs)
213 {
214  // We consider two colors equal if their byte representations are equal.
215  // This should be good enough for all practical purposes.
216  return (
217  ( lhs.r_byte() == rhs.r_byte() ) &&
218  ( lhs.g_byte() == rhs.g_byte() ) &&
219  ( lhs.b_byte() == rhs.b_byte() ) &&
220  ( lhs.a_byte() == rhs.a_byte() )
221  );
222 }
223 
224 inline bool operator != (Color const& lhs, Color const& rhs)
225 {
226  return !(lhs == rhs);
227 }
228 
229 } // namespace utils
230 } // namespace genesis
231 
232 #endif // include guard
Color()
Default constructor. Sets the color to black.
Definition: color.hpp:58
static Color from_hex(std::string const &hex_color, std::string const &prefix="#")
Create a Color given a hex color string in the format "#003366[ff]".
Definition: color.cpp:68
static Color from_bytes(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Create a Color given three or four values in the range [ 0, 255 ] for each of the components red...
Definition: color.cpp:63
void swap(Color &lhs, Color &rhs)
Definition: color.hpp:207
void swap(Color &other)
Definition: color.hpp:82
void g_byte(unsigned char value)
Definition: color.hpp:166
double a() const
Definition: color.hpp:124
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
unsigned char b_byte() const
Definition: color.hpp:139
double b() const
Definition: color.hpp:119
void b_byte(unsigned char value)
Definition: color.hpp:171
bool operator==(JsonDocument::const_reference lhs, JsonDocument::const_reference rhs)
double g() const
Definition: color.hpp:114
double r() const
Definition: color.hpp:109
unsigned char r_byte() const
Definition: color.hpp:129
unsigned char a_byte() const
Definition: color.hpp:144
Color & operator=(Color const &)=default
void r_byte(unsigned char value)
Definition: color.hpp:161
unsigned char g_byte() const
Definition: color.hpp:134
bool operator!=(JsonDocument const &lhs, JsonDocument const &rhs)
void a_byte(unsigned char value)
Definition: color.hpp:176
Color(double r, double g, double b)
Constructor for setting the RGB value.
Definition: color.hpp:65