A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
map.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
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 
35 
36 #include <algorithm>
37 #include <cassert>
38 #include <stdexcept>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Palette
45 // =================================================================================================
46 
47 std::vector<Color> ColorMap::color_list( size_t n ) const
48 {
49  // Result.
50  std::vector<Color> result;
51  result.reserve( n );
52 
53  // Boundary checks.
54  if( n == 0 ) {
55  return palette_;
56  }
57  if( n == 1 ) {
58  return { operator()( 0.5 ) };
59  }
60 
61  for( size_t i = 0; i < n; ++i ) {
62  auto const pos = static_cast<double>( i ) / static_cast<double>( n - 1 );
63  result.push_back( operator()( pos ));
64  }
65  return result;
66 }
67 
68 Color ColorMap::operator() ( double value ) const
69 {
70  // Extreme cases check.
71  if( palette_.size() == 0 ) {
72  return Color( 0.0, 0.0, 0.0 );
73  }
74  if( palette_.size() == 1 ) {
75  return palette_[0];
76  }
77 
78  // Boundary checks.
79  if( ! std::isfinite( value ) ) {
80  return mask_color_;
81  }
82  if( clip_under_ ) {
83  value = std::max( 0.0, value );
84  }
85  if( clip_over_ ) {
86  value = std::min( value, 1.0 );
87  }
88  if( value < 0.0 ) {
89  return under_color_;
90  }
91  if( value > 1.0 ) {
92  return over_color_;
93  }
94 
95  // Check again.
96  assert( value >= 0.0 && value <= 1.0 );
97  assert( palette_.size() > 0 );
98 
99  // Bring value into the correct range within the palette size.
100  auto const val_entry = static_cast<double>( palette_.size() - 1 ) * value;
101  assert( val_entry >= 0 );
102  assert( val_entry < static_cast<double>( palette_.size() ));
103 
104  // Get the values at the two colors next to the entry.
105  // auto const low_value = min_ + (( std::floor( val_entry ) / max_entry ) * ( max_ - min_ ));
106  // auto const high_value = min_ + (( std::ceil( val_entry ) / max_entry ) * ( max_ - min_ ));
107  // auto const interval_len = ( max_ - min_ ) / max_entry;
108 
109  // Get the color indices next to our chosen value.
110  auto const low_idx = static_cast<size_t>( std::floor( val_entry ) );
111  auto const high_idx = static_cast<size_t>( std::ceil( val_entry ) );
112  if( low_idx == high_idx ) {
113  return get_entry_( low_idx );
114  }
115  assert( low_idx < high_idx );
116 
117  // Get the fraction between the two entries that our value sits on.
118  auto const fraction = val_entry - std::floor( val_entry );
119  assert( fraction >= 0.0 && fraction <= 1.0 );
120 
121  // Return the interpolated result.
122  return interpolate( get_entry_( low_idx ), get_entry_( high_idx ), fraction );
123 }
124 
125 std::vector<Color> ColorMap::operator() ( std::vector<double> const& values ) const
126 {
127  using it_type = std::vector<double>::const_iterator;
128  return operator()<it_type>( values.begin(), values.end() );
129 }
130 
131 Color ColorMap::operator() ( ColorNormalization const& norm, double value ) const
132 {
133  return operator()( norm( value ) );
134 }
135 
136 std::vector<Color> ColorMap::operator() (
137  ColorNormalization const& norm, std::vector<double> const& values
138 ) const {
139  using it_type = std::vector<double>::const_iterator;
140  return operator()<it_type>( norm, values.begin(), values.end() );
141 }
142 
143 
144 } // namespace utils
145 } // namespace genesis
std::vector< Color > color_list(size_t n=256) const
Get a color list based on the palette, containing n colors sampled at equal distance across the palet...
Definition: map.cpp:47
Color interpolate(Color const &color1, Color const &color2, double fraction)
Linearily interpolate between two Colors.
Base class for color normalization.
Color operators and functions.
Color operator()(double value) const
Return an interpolated color for a value in the range [ 0.0, 1.0 ], representing a position in the pa...
Definition: map.cpp:68