A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
color.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
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 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <cassert>
36 #include <cmath>
37 #include <stdexcept>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Constructors and Rule of Five
44 // =================================================================================================
45 
46 Color::Color( double r, double g, double b, double a )
47  : r_( r ), g_( g ), b_( b ), a_( a )
48 {
49  if( r < 0.0 || r > 1.0 || g < 0.0 || g > 1.0 || b < 0.0 || b > 1.0 || a < 0.0 || a > 1.0 ) {
50  throw std::invalid_argument(
51  "Color can only be constructed with values in range [ 0.0, 1.0 ]."
52  );
53  }
54 }
55 
56 // =================================================================================================
57 // Factories
58 // =================================================================================================
59 
60 Color Color::from_bytes( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
61 {
62  return color_from_bytes( r, g, b, a );
63 }
64 
65 Color Color::from_hex( std::string const& hex_color, std::string const& prefix )
66 {
67  return color_from_hex( hex_color, prefix );
68 }
69 
70 // =================================================================================================
71 // Modificators
72 // =================================================================================================
73 
74 void Color::r (double value)
75 {
76  if( value < 0.0 || value > 1.0 ) {
77  throw std::invalid_argument(
78  "Color can only be used with values in range [ 0.0, 1.0 ]."
79  );
80  }
81  r_ = value;
82 }
83 
84 void Color::g (double value)
85 {
86  if( value < 0.0 || value > 1.0 ) {
87  throw std::invalid_argument(
88  "Color can only be used with values in range [ 0.0, 1.0 ]."
89  );
90  }
91  g_ = value;
92 }
93 
94 void Color::b (double value)
95 {
96  if( value < 0.0 || value > 1.0 ) {
97  throw std::invalid_argument(
98  "Color can only be used with values in range [ 0.0, 1.0 ]."
99  );
100  }
101  b_ = value;
102 }
103 
104 void Color::a (double value)
105 {
106  if( value < 0.0 || value > 1.0 ) {
107  throw std::invalid_argument(
108  "Color can only be used with values in range [ 0.0, 1.0 ]."
109  );
110  }
111  a_ = value;
112 }
113 
114 // =================================================================================================
115 // Internal Functions
116 // =================================================================================================
117 
118 unsigned char Color::to_byte_( double v )
119 {
120  assert( 0.0 <= v && v <= 1.0 );
121  return static_cast<unsigned char>( std::round( 255.0 * v ));
122 }
123 
124 double Color::from_byte_( unsigned char v )
125 {
126  return static_cast<double>( v ) / 255.0;
127 }
128 
129 } // namespace utils
130 } // namespace genesis
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
Color color_from_bytes(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Create a Color given three or four values in the range [ 0, 255 ] for each of the components red...
Color 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]".
Header of Color class.
double b() const
Definition: color.hpp:113
Color operators and functions.
double a() const
Definition: color.hpp:118