A toolkit for working with phylogenetic data.
v0.24.0
color.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 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(
50  ! std::isfinite(r) || ! std::isfinite(g) || ! std::isfinite(b) || ! std::isfinite(a) ||
51  r < 0.0 || r > 1.0 || g < 0.0 || g > 1.0 || b < 0.0 || b > 1.0 || a < 0.0 || a > 1.0
52  ) {
53  throw std::invalid_argument(
54  "Color can only be constructed with values in range [ 0.0, 1.0 ]."
55  );
56  }
57 }
58 
59 // =================================================================================================
60 // Factories
61 // =================================================================================================
62 
63 Color Color::from_bytes( unsigned char r, unsigned char g, unsigned char b, unsigned char a )
64 {
65  return color_from_bytes( r, g, b, a );
66 }
67 
68 Color Color::from_hex( std::string const& hex_color, std::string const& prefix )
69 {
70  return color_from_hex( hex_color, prefix );
71 }
72 
73 // =================================================================================================
74 // Modificators
75 // =================================================================================================
76 
77 void Color::r (double value)
78 {
79  if( ! std::isfinite(value) || value < 0.0 || value > 1.0 ) {
80  throw std::invalid_argument(
81  "Color can only be used with values in range [ 0.0, 1.0 ]."
82  );
83  }
84  r_ = value;
85 }
86 
87 void Color::g (double value)
88 {
89  if( ! std::isfinite(value) || value < 0.0 || value > 1.0 ) {
90  throw std::invalid_argument(
91  "Color can only be used with values in range [ 0.0, 1.0 ]."
92  );
93  }
94  g_ = value;
95 }
96 
97 void Color::b (double value)
98 {
99  if( ! std::isfinite(value) || value < 0.0 || value > 1.0 ) {
100  throw std::invalid_argument(
101  "Color can only be used with values in range [ 0.0, 1.0 ]."
102  );
103  }
104  b_ = value;
105 }
106 
107 void Color::a (double value)
108 {
109  if( ! std::isfinite(value) || value < 0.0 || value > 1.0 ) {
110  throw std::invalid_argument(
111  "Color can only be used with values in range [ 0.0, 1.0 ]."
112  );
113  }
114  a_ = value;
115 }
116 
117 // =================================================================================================
118 // Internal Functions
119 // =================================================================================================
120 
121 unsigned char Color::to_byte_( double v )
122 {
123  assert( std::isfinite(v) && 0.0 <= v && v <= 1.0 );
124  return static_cast<unsigned char>( std::round( 255.0 * v ));
125 }
126 
127 double Color::from_byte_( unsigned char v )
128 {
129  return static_cast<double>( v ) / 255.0;
130 }
131 
132 } // namespace utils
133 } // namespace genesis
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
double a() const
Definition: color.hpp:124
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...
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Header of Color class.
double b() const
Definition: color.hpp:119
double g() const
Definition: color.hpp:114
double r() const
Definition: color.hpp:109
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]".
Color operators and functions.