A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
map.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_MAP_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_MAP_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 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 
35 
36 #include <cmath>
37 #include <limits>
38 #include <vector>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Forward Declarations
45 // =================================================================================================
46 
47 class ColorNormalization;
48 
49 // =================================================================================================
50 // Color Map
51 // =================================================================================================
52 
61 class ColorMap
62 {
63 public:
64 
65  // -------------------------------------------------------------------------
66  // Constructors and Rule of Five
67  // -------------------------------------------------------------------------
68 
69  ColorMap() = default;
70 
71  ColorMap( std::vector<Color> const& colors )
72  {
73  palette( colors );
74  }
75 
76  ~ColorMap() = default;
77 
78  ColorMap(ColorMap const&) = default;
79  ColorMap(ColorMap&&) = default;
80 
81  ColorMap& operator= (ColorMap const&) = default;
82  ColorMap& operator= (ColorMap&&) = default;
83 
84  // -------------------------------------------------------------------------
85  // Accessors
86  // -------------------------------------------------------------------------
87 
91  Color mask_color() const
92  {
93  return mask_color_;
94  }
95 
102  {
103  return over_color_;
104  }
105 
112  {
113  return under_color_;
114  }
115 
121  bool clip_under() const
122  {
123  return clip_under_;
124  }
125 
131  bool clip_over() const
132  {
133  return clip_over_;
134  }
135 
139  bool reverse() const
140  {
141  return reverse_;
142  }
143 
144  // -------------------------------------------------------------------------
145  // Modificators
146  // -------------------------------------------------------------------------
147 
152  {
153  mask_color_ = value;
154  return *this;
155  }
156 
161  {
162  over_color_ = value;
163  return *this;
164  }
165 
170  {
171  under_color_ = value;
172  return *this;
173  }
174 
178  ColorMap& clip( bool value )
179  {
180  clip_under_ = value;
181  clip_over_ = value;
182  return *this;
183  }
184 
188  ColorMap& clip_under( bool value )
189  {
190  clip_under_ = value;
191  return *this;
192  }
193 
197  ColorMap& clip_over( bool value )
198  {
199  clip_over_ = value;
200  return *this;
201  }
202 
203  ColorMap& reverse( bool value )
204  {
205  reverse_ = value;
206  return *this;
207  }
208 
209  ColorMap& palette( std::vector<Color> const& value )
210  {
211  palette_ = value;
212  return *this;
213  }
214 
215  // -------------------------------------------------------------------------
216  // Palette
217  // -------------------------------------------------------------------------
218 
222  std::vector<Color> const& palette() const
223  {
224  return palette_;
225  }
226 
230  bool empty() const
231  {
232  return palette_.empty();
233  }
234 
238  size_t size() const
239  {
240  return palette_.size();
241  }
242 
249  Color color( size_t index ) const
250  {
251  if( palette_.size() == 0 ) {
252  return Color();
253  }
254  return get_entry_( index % palette_.size() );
255  }
256 
267  std::vector<Color> color_list( size_t n = 256 ) const;
268 
269  // -------------------------------------------------------------------------
270  // Mapping
271  // -------------------------------------------------------------------------
272 
282  Color operator() ( double value ) const;
283 
289  std::vector<Color> operator() ( std::vector<double> const& values ) const;
290 
296  template <class ForwardIterator>
297  std::vector<Color> operator()( ForwardIterator first, ForwardIterator last ) const
298  {
299  std::vector<Color> result;
300  while( first != last ) {
301  result.push_back( operator()( *first ) );
302  ++first;
303  }
304  return result;
305  }
306 
310  Color operator() ( ColorNormalization const& norm, double value ) const;
311 
315  std::vector<Color> operator() ( ColorNormalization const& norm, std::vector<double> const& values ) const;
316 
320  template <class ForwardIterator>
321  std::vector<Color> operator()(
322  ColorNormalization const& norm,
323  ForwardIterator first,
324  ForwardIterator last
325  ) const {
326  std::vector<Color> result;
327  while( first != last ) {
328  result.push_back( operator()( norm, *first ) );
329  ++first;
330  }
331  return result;
332  }
333 
334  // -------------------------------------------------------------------------
335  // Internal Functions
336  // -------------------------------------------------------------------------
337 
338 private:
339 
343  Color get_entry_( size_t index ) const
344  {
345  if( reverse_ ) {
346  index = palette_.size() - index - 1;
347  }
348  return palette_[ index ];
349  }
350 
351  // -------------------------------------------------------------------------
352  // Data Members
353  // -------------------------------------------------------------------------
354 
355 private:
356 
357  Color mask_color_ = { 1.0, 1.0, 0.0 };
358  Color over_color_ = { 0.0, 1.0, 1.0 };
359  Color under_color_ = { 1.0, 0.0, 1.0 };
360 
361  bool clip_under_ = false;
362  bool clip_over_ = false;
363  bool reverse_ = false;
364 
365  std::vector<Color> palette_;
366 
367 };
368 
369 } // namespace utils
370 } // namespace genesis
371 
372 #endif // include guard
bool clip_over() const
Clip (clamp) values greater than max() to be inside [ min, max ].
Definition: map.hpp:131
ColorMap & under_color(Color value)
Color that indicates values less than min().
Definition: map.hpp:169
bool empty() const
Return whether the Palette is empty, that is, no colors were set.
Definition: map.hpp:230
size_t size() const
Return the size of the map, that is, the number of colors in the list.
Definition: map.hpp:238
ColorMap & over_color(Color value)
Color that indicates values greater than max().
Definition: map.hpp:160
Store a list of colors and offer them as a map for values in range [ 0.0, 1.0 ].
Definition: map.hpp:61
Color color(size_t index) const
Return a particular color from the palette, module the palette size.
Definition: map.hpp:249
ColorMap & clip_under(bool value)
Clip (clamp) values less than min() to be inside [ min, max ].
Definition: map.hpp:188
Header of Color class.
Color over_color() const
Color that indicates values greater than max().
Definition: map.hpp:101
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
ColorMap(std::vector< Color > const &colors)
Definition: map.hpp:71
ColorMap & reverse(bool value)
Definition: map.hpp:203
bool reverse() const
Use the palette colors in reverse, back to front.
Definition: map.hpp:139
std::vector< Color > const & palette() const
Get the color list currently in use.
Definition: map.hpp:222
std::vector< Color > operator()(ColorNormalization const &norm, ForwardIterator first, ForwardIterator last) const
Return the mapped colors for a range of values, normalized by norm.
Definition: map.hpp:321
Base class for color normalization.
bool clip_under() const
Clip (clamp) values less than min() to be inside [ min, max ].
Definition: map.hpp:121
ColorMap & operator=(ColorMap const &)=default
Color under_color() const
Color that indicates values less than min().
Definition: map.hpp:111
ColorMap & clip_over(bool value)
Clip (clamp) values greater than max() to be inside [ min, max ].
Definition: map.hpp:197
ColorMap & clip(bool value)
Definition: map.hpp:178
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
std::vector< Color > operator()(ForwardIterator first, ForwardIterator last) const
Return the mapped colors for a range of values.
Definition: map.hpp:297
ColorMap & palette(std::vector< Color > const &value)
Definition: map.hpp:209
Color mask_color() const
Color that indicates values equal to mask_value() or non-finite values.
Definition: map.hpp:91
ColorMap & mask_color(Color value)
Color that indicates values equal to mask_value() or non-finite values.
Definition: map.hpp:151