A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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 
95  static Color from_bytes( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 );
96 
97  static Color from_hex( std::string const& hex_color, std::string const& prefix = "#" );
98 
99  // -------------------------------------------------------------------------
100  // Accessors
101  // -------------------------------------------------------------------------
102 
103  double r() const
104  {
105  return r_;
106  }
107 
108  double g() const
109  {
110  return g_;
111  }
112 
113  double b() const
114  {
115  return b_;
116  }
117 
118  double a() const
119  {
120  return a_;
121  }
122 
123  unsigned char r_byte() const
124  {
125  return to_byte_( r_ );
126  }
127 
128  unsigned char g_byte() const
129  {
130  return to_byte_( g_ );
131  }
132 
133  unsigned char b_byte() const
134  {
135  return to_byte_( b_ );
136  }
137 
138  unsigned char a_byte() const
139  {
140  return to_byte_( a_ );
141  }
142 
143  // -------------------------------------------------------------------------
144  // Modificators
145  // -------------------------------------------------------------------------
146 
147  void r (double value);
148 
149  void g (double value);
150 
151  void b (double value);
152 
153  void a (double value);
154 
155  void r_byte (unsigned char value)
156  {
157  r_ = from_byte_( value );
158  }
159 
160  void g_byte (unsigned char value)
161  {
162  g_ = from_byte_( value );
163  }
164 
165  void b_byte (unsigned char value)
166  {
167  b_ = from_byte_( value );
168  }
169 
170  void a_byte (unsigned char value)
171  {
172  a_ = from_byte_( value );
173  }
174 
175  // -------------------------------------------------------------------------
176  // Internal Functions
177  // -------------------------------------------------------------------------
178 
179 private:
180 
181  static unsigned char to_byte_( double v );
182  static double from_byte_( unsigned char v );
183 
184  // -------------------------------------------------------------------------
185  // Data Members
186  // -------------------------------------------------------------------------
187 
188 private:
189 
190  double r_;
191  double g_;
192  double b_;
193  double a_;
194 
195 };
196 
197 // =================================================================================================
198 // Basic Operators
199 // =================================================================================================
200 
201 inline void swap (Color& lhs, Color& rhs)
202 {
203  lhs.swap(rhs);
204 }
205 
206 inline bool operator == (Color const& lhs, Color const& rhs)
207 {
208  return lhs.r() == rhs.r() && lhs.g() == rhs.g() && lhs.b() == rhs.b() && lhs.a() == rhs.a();
209 }
210 
211 inline bool operator != (Color const& lhs, Color const& rhs)
212 {
213  return !(lhs == rhs);
214 }
215 
216 } // namespace utils
217 } // namespace genesis
218 
219 #endif // include guard
double g() const
Definition: color.hpp:108
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="#")
Definition: color.cpp:65
double r() const
Definition: color.hpp:103
static Color from_bytes(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: color.cpp:60
bool operator!=(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
void swap(Color &lhs, Color &rhs)
Definition: color.hpp:201
void swap(Color &other)
Definition: color.hpp:82
void g_byte(unsigned char value)
Definition: color.hpp:160
void swap(NexusTaxa &lhs, NexusTaxa &rhs)
Definition: taxa.hpp:207
unsigned char b_byte() const
Definition: color.hpp:133
void b_byte(unsigned char value)
Definition: color.hpp:165
unsigned char g_byte() const
Definition: color.hpp:128
unsigned char a_byte() const
Definition: color.hpp:138
double b() const
Definition: color.hpp:113
unsigned char r_byte() const
Definition: color.hpp:123
bool operator==(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
Color & operator=(Color const &)=default
void r_byte(unsigned char value)
Definition: color.hpp:155
double a() const
Definition: color.hpp:118
void a_byte(unsigned char value)
Definition: color.hpp:170
Color(double r, double g, double b)
Constructor for setting the RGB value.
Definition: color.hpp:65