A library for working with phylogenetic and population genetic data.
v0.27.0
norm_diverging.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_NORM_DIVERGING_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_NORM_DIVERGING_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 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 
37 
38 #include <cmath>
39 #include <limits>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Color Normalization Diverging
47 // =================================================================================================
48 
73 {
74 public:
75 
76  // -------------------------------------------------------------------------
77  // Constructors and Rule of Five
78  // -------------------------------------------------------------------------
79 
84  : ColorNormalizationLinear( -1.0, 1.0 )
85  , mid_value_( 0.0 )
86  {}
87 
92  ColorNormalizationDiverging( double min, double max )
93  : ColorNormalizationDiverging( min, ( min + max ) / 2.0, max )
94  {}
95 
99  ColorNormalizationDiverging( double min, double mid, double max )
100  : ColorNormalizationLinear( min, max )
101  , mid_value_( mid )
102  {
104  }
105 
110  explicit ColorNormalizationDiverging( std::vector<double> const& values )
111  {
112  autoscale( values.begin(), values.end() );
113  }
114 
119  template <class ForwardIterator>
120  ColorNormalizationDiverging( ForwardIterator first, ForwardIterator last )
121  {
122  autoscale( first, last );
123  }
124 
125  virtual ~ColorNormalizationDiverging() override = default;
126 
129 
132 
133  // -------------------------------------------------------------------------
134  // Accessors
135  // -------------------------------------------------------------------------
136 
140  double mid_value() const
141  {
142  return mid_value_;
143  }
144 
145  // -------------------------------------------------------------------------
146  // Modificators
147  // -------------------------------------------------------------------------
148 
156  {
157  // Set the min and max so that they are symmetric around center.
158  auto const dist = std::max(
159  std::fabs( center - min_value() ),
160  std::fabs( center - max_value() )
161  );
162  min_value( center - dist );
163  max_value( center + dist );
164  mid_value_ = center;
165 
166  return *this;
167  }
168 
173  {
174  mid_value_ = value;
175  return *this;
176  }
177 
178  // -------------------------------------------------------------------------
179  // Virtual Functions
180  // -------------------------------------------------------------------------
181 
185  virtual bool is_valid_() const override
186  {
187  return min_value() < mid_value_ && mid_value_ < max_value();
188  }
189 
190 protected:
191 
195  virtual void is_valid_or_throw_() const override
196  {
197  if( min_value() >= max_value() ) {
198  throw std::runtime_error( "Invalid Color Normalization with min >= max." );
199  }
200  if( min_value() >= mid_value() ) {
201  throw std::runtime_error( "Invalid Color Normalization with min >= mid." );
202  }
203  if( mid_value() >= max_value() ) {
204  throw std::runtime_error( "Invalid Color Normalization with mid >= max." );
205  }
206  }
207 
208  virtual double normalize_( double value ) const override
209  {
210  // Already checked by base class.
211  assert( min_value() <= value && value <= max_value() );
212  assert( is_valid_() );
213 
214  // Bring value into the range [ 0.0, 1.0 ].
215  double pos = 0.0;
216  if( value < mid_value_ ) {
217  pos = ( value - min_value() ) / ( mid_value_ - min_value() );
218  pos /= 2.0;
219  } else {
220  pos = ( value - mid_value_ ) / ( max_value() - mid_value_ );
221  pos /= 2.0;
222  pos += 0.5;
223  }
224  return pos;
225  }
226 
227  virtual void update_hook_( double min, double max ) override
228  {
229  mid_value_ = ( min + max ) / 2.0;
230  }
231 
232  // -------------------------------------------------------------------------
233  // Data Members
234  // -------------------------------------------------------------------------
235 
236 private:
237 
238  double mid_value_ = 0.0;
239 
240 };
241 
242 } // namespace utils
243 } // namespace genesis
244 
245 #endif // include guard
genesis::utils::ColorNormalizationDiverging::update_hook_
virtual void update_hook_(double min, double max) override
Called whenever the min and max are set automatically. Gives derived classes a chance to update their...
Definition: norm_diverging.hpp:227
genesis::utils::ColorNormalizationDiverging::ColorNormalizationDiverging
ColorNormalizationDiverging(ForwardIterator first, ForwardIterator last)
Constructor that sets min() and max() to the min and max of the provided range, and mid() to their mi...
Definition: norm_diverging.hpp:120
genesis::utils::ColorNormalizationDiverging
Color normalization for a diverging scale.
Definition: norm_diverging.hpp:71
genesis::utils::ColorNormalizationDiverging::ColorNormalizationDiverging
ColorNormalizationDiverging(double min, double max)
Constructor that sets min() and max() to the provided values, and mid() to their midpoint.
Definition: norm_diverging.hpp:92
genesis::utils::ColorNormalizationDiverging::ColorNormalizationDiverging
ColorNormalizationDiverging()
Constructor that sets min == -1.0, mid = 0.0 and max == 1.0.
Definition: norm_diverging.hpp:83
genesis::utils::ColorNormalizationDiverging::make_centric
ColorNormalizationDiverging & make_centric(double center=0.0)
Make the range symmetric around a center value.
Definition: norm_diverging.hpp:155
genesis::utils::ColorNormalizationLinear::min_value
double min_value() const
Minimum value, that is, where to begin the color scale.
Definition: norm_linear.hpp:115
genesis::utils::ColorNormalizationLinear
Default Color normalization, using a sequential linear scaling in the range [ min,...
Definition: norm_linear.hpp:59
genesis::utils::ColorNormalizationDiverging::is_valid_
virtual bool is_valid_() const override
Return whether the ranges are correct.
Definition: norm_diverging.hpp:185
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::ColorNormalizationDiverging::mid_value
double mid_value() const
Mid-point value, that is, where the middle value of a diverging_color() is.
Definition: norm_diverging.hpp:140
genesis::utils::ColorNormalizationDiverging::operator=
ColorNormalizationDiverging & operator=(ColorNormalizationDiverging const &)=default
genesis::utils::ColorNormalizationDiverging::is_valid_or_throw_
virtual void is_valid_or_throw_() const override
Throw if the ranges are incorrect.
Definition: norm_diverging.hpp:195
functions.hpp
Color operators and functions.
color.hpp
Header of Color class.
norm_linear.hpp
genesis::utils::ColorNormalizationDiverging::~ColorNormalizationDiverging
virtual ~ColorNormalizationDiverging() override=default
genesis::utils::ColorNormalizationDiverging::ColorNormalizationDiverging
ColorNormalizationDiverging(double min, double mid, double max)
Constructor that sets min(), mid() and max() to the provided values, in that order.
Definition: norm_diverging.hpp:99
genesis::utils::ColorNormalizationLinear::max_value
double max_value() const
Minimum value, that is, where to end the color scale.
Definition: norm_linear.hpp:123
genesis::utils::ColorNormalizationLinear::autoscale
ColorNormalizationLinear & autoscale(std::vector< double > const &values)
Definition: norm_linear.hpp:146
genesis::utils::ColorNormalizationDiverging::mid_value
ColorNormalizationDiverging & mid_value(double value)
Mid-point value, that is, where the middle value of a diverging_color() is.
Definition: norm_diverging.hpp:172
genesis::utils::ColorNormalizationDiverging::normalize_
virtual double normalize_(double value) const override
Normalization function.
Definition: norm_diverging.hpp:208
genesis::utils::ColorNormalizationDiverging::ColorNormalizationDiverging
ColorNormalizationDiverging(std::vector< double > const &values)
Constructor that sets min() and max() to the min and max of the provided values, and mid() to their m...
Definition: norm_diverging.hpp:110