A library for working with phylogenetic and population genetic data.
v0.32.0
norm_logarithmic.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_COLOR_NORM_LOGARITHMIC_H_
2 #define GENESIS_UTILS_COLOR_NORM_LOGARITHMIC_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2023 Lucas Czech
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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
37 
38 #include <cmath>
39 #include <limits>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Color Normalization Logarithmic
47 // =================================================================================================
48 
54 {
55 public:
56 
57  // -------------------------------------------------------------------------
58  // Constructors and Rule of Five
59  // -------------------------------------------------------------------------
60 
65  : ColorNormalizationLinear( 1.0, 100.0 )
66  {}
67 
71  ColorNormalizationLogarithmic( double min, double max )
72  : ColorNormalizationLinear( min, max )
73  {}
74 
79  explicit ColorNormalizationLogarithmic( std::vector<double> const& values )
80  {
81  autoscale( values.begin(), values.end() );
82  }
83 
88  template <class ForwardIterator>
89  ColorNormalizationLogarithmic( ForwardIterator first, ForwardIterator last )
90  {
91  autoscale( first, last );
92  }
93 
94  virtual ~ColorNormalizationLogarithmic() override = default;
95 
98 
101 
102  // -------------------------------------------------------------------------
103  // Settings
104  // -------------------------------------------------------------------------
105 
106  double base() const
107  {
108  return base_;
109  }
110 
112  {
113  base_ = value;
114  return *this;
115  }
116 
117  bool exponential_labels() const
118  {
119  return exp_labels_;
120  }
121 
123  {
124  exp_labels_ = value;
125  return *this;
126  }
127 
128  // -------------------------------------------------------------------------
129  // Virtual Functions
130  // -------------------------------------------------------------------------
131 
132 protected:
133 
137  virtual bool is_valid_() const override
138  {
139  return 0.0 < min_value() && min_value() < max_value();
140  }
141 
145  virtual void is_valid_or_throw_() const override
146  {
147  if( min_value() >= max_value() ) {
148  throw std::runtime_error( "Invalid Color Normalization with min >= max" );
149  }
150  if( min_value() <= 0.0 ) {
151  throw std::runtime_error( "Invalid Color Normalization with min <= 0.0" );
152  }
153  }
154 
155  virtual double normalize_( double value ) const override
156  {
157  // Make sure that the norm is set up correctly.
159 
160  // Extreme cases.
161  if( value < min_value() ) {
162  return -1.0;
163  }
164  if( value > max_value() ) {
165  return 2.0;
166  }
167 
168  // Let's check these cases again.
169  assert( min_value() <= value && value <= max_value() );
170  assert( is_valid_() );
171 
172  // As we have 0 < min <= value, this must hold. Otherwise, log won't work.
173  assert( 0.0 < value );
174 
175  // Bring value into the range [ 0.0, 1.0 ].
176  auto const lg_min = std::log( min_value() ) / std::log( base_ );
177  auto const lg_max = std::log( max_value() ) / std::log( base_ );
178  auto const lg_val = std::log( value ) / std::log( base_ );
179  return ( lg_val - lg_min ) / ( lg_max - lg_min );
180  }
181 
182  // -------------------------------------------------------------------------
183  // Data Members
184  // -------------------------------------------------------------------------
185 
186 private:
187 
188  double base_ = 10.0;
189  bool exp_labels_ = false;
190 
191 };
192 
193 } // namespace utils
194 } // namespace genesis
195 
196 #endif // include guard
genesis::utils::ColorNormalizationLogarithmic
Color normalization for a logarithmic scale.
Definition: norm_logarithmic.hpp:52
genesis::utils::ColorNormalizationLogarithmic::ColorNormalizationLogarithmic
ColorNormalizationLogarithmic(double min, double max)
Constructor that sets min() and max() to the provided values.
Definition: norm_logarithmic.hpp:71
genesis::utils::ColorNormalizationLogarithmic::ColorNormalizationLogarithmic
ColorNormalizationLogarithmic(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_logarithmic.hpp:89
genesis::utils::ColorNormalizationLogarithmic::exponential_labels
ColorNormalizationLogarithmic & exponential_labels(bool value)
Definition: norm_logarithmic.hpp:122
genesis::utils::ColorNormalizationLogarithmic::base
double base() const
Definition: norm_logarithmic.hpp:106
genesis::utils::ColorNormalizationLogarithmic::ColorNormalizationLogarithmic
ColorNormalizationLogarithmic(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_logarithmic.hpp:79
genesis::utils::ColorNormalizationLogarithmic::~ColorNormalizationLogarithmic
virtual ~ColorNormalizationLogarithmic() override=default
genesis::utils::ColorNormalizationLogarithmic::exponential_labels
bool exponential_labels() const
Definition: norm_logarithmic.hpp:117
genesis::utils::ColorNormalizationLogarithmic::base
ColorNormalizationLogarithmic & base(double value)
Definition: norm_logarithmic.hpp:111
genesis::utils::ColorNormalizationLogarithmic::operator=
ColorNormalizationLogarithmic & operator=(ColorNormalizationLogarithmic const &)=default
genesis::utils::ColorNormalizationLogarithmic::is_valid_or_throw_
virtual void is_valid_or_throw_() const override
Throw if the ranges are incorrect.
Definition: norm_logarithmic.hpp:145
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::ColorNormalizationLogarithmic::is_valid_
virtual bool is_valid_() const override
Return whether the ranges are correct.
Definition: norm_logarithmic.hpp:137
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
color.hpp
Header of Color class.
norm_linear.hpp
tickmarks.hpp
genesis::utils::ColorNormalizationLogarithmic::normalize_
virtual double normalize_(double value) const override
Normalization function.
Definition: norm_logarithmic.hpp:155
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::ColorNormalizationLogarithmic::ColorNormalizationLogarithmic
ColorNormalizationLogarithmic()
Constructor that sets min == 1.0 and max == 100.0.
Definition: norm_logarithmic.hpp:64