A library for working with phylogenetic and population genetic data.
v0.27.0
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-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 
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  explicit 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 
101  Color const& mask_color() const
102  {
103  return mask_color_;
104  }
105 
111  Color const& over_color() const
112  {
113  return over_color_;
114  }
115 
121  Color const& under_color() const
122  {
123  return under_color_;
124  }
125 
131  bool clip_under() const
132  {
133  return clip_under_;
134  }
135 
141  bool clip_over() const
142  {
143  return clip_over_;
144  }
145 
149  bool reverse() const
150  {
151  return reverse_;
152  }
153 
154  // -------------------------------------------------------------------------
155  // Modificators
156  // -------------------------------------------------------------------------
157 
162  {
163  mask_color_ = value;
164  return *this;
165  }
166 
171  {
172  over_color_ = value;
173  return *this;
174  }
175 
180  {
181  under_color_ = value;
182  return *this;
183  }
184 
188  ColorMap& clip( bool value )
189  {
190  clip_under_ = value;
191  clip_over_ = value;
192  return *this;
193  }
194 
198  ColorMap& clip_under( bool value )
199  {
200  clip_under_ = value;
201  return *this;
202  }
203 
207  ColorMap& clip_over( bool value )
208  {
209  clip_over_ = value;
210  return *this;
211  }
212 
213  ColorMap& reverse( bool value )
214  {
215  reverse_ = value;
216  return *this;
217  }
218 
219  ColorMap& palette( std::vector<Color> const& value )
220  {
221  palette_ = value;
222  return *this;
223  }
224 
225  // -------------------------------------------------------------------------
226  // Palette
227  // -------------------------------------------------------------------------
228 
232  std::vector<Color> const& palette() const
233  {
234  return palette_;
235  }
236 
240  bool empty() const
241  {
242  return palette_.empty();
243  }
244 
248  size_t size() const
249  {
250  return palette_.size();
251  }
252 
259  Color color( size_t index ) const
260  {
261  if( palette_.size() == 0 ) {
262  return Color();
263  }
264  return get_entry_( index % palette_.size() );
265  }
266 
277  std::vector<Color> color_list( size_t n = 256 ) const;
278 
279  // -------------------------------------------------------------------------
280  // Mapping
281  // -------------------------------------------------------------------------
282 
292  Color operator() ( double value ) const;
293 
299  std::vector<Color> operator() ( std::vector<double> const& values ) const;
300 
306  template <class ForwardIterator>
307  std::vector<Color> operator()( ForwardIterator first, ForwardIterator last ) const
308  {
309  std::vector<Color> result;
310  while( first != last ) {
311  result.push_back( operator()( *first ) );
312  ++first;
313  }
314  return result;
315  }
316 
320  Color operator() ( ColorNormalization const& norm, double value ) const;
321 
325  std::vector<Color> operator() ( ColorNormalization const& norm, std::vector<double> const& values ) const;
326 
330  template <class ForwardIterator>
331  std::vector<Color> operator()(
332  ColorNormalization const& norm,
333  ForwardIterator first,
334  ForwardIterator last
335  ) const {
336  std::vector<Color> result;
337  while( first != last ) {
338  result.push_back( operator()( norm, *first ) );
339  ++first;
340  }
341  return result;
342  }
343 
344  // -------------------------------------------------------------------------
345  // Internal Functions
346  // -------------------------------------------------------------------------
347 
348 private:
349 
353  Color get_entry_( size_t index ) const
354  {
355  if( reverse_ ) {
356  index = palette_.size() - index - 1;
357  }
358  return palette_[ index ];
359  }
360 
361  // -------------------------------------------------------------------------
362  // Data Members
363  // -------------------------------------------------------------------------
364 
365 private:
366 
367  Color mask_color_ = { 1.0, 1.0, 0.0 };
368  Color over_color_ = { 0.0, 1.0, 1.0 };
369  Color under_color_ = { 1.0, 0.0, 1.0 };
370 
371  bool clip_under_ = false;
372  bool clip_over_ = false;
373  bool reverse_ = false;
374 
375  std::vector<Color> palette_;
376 
377 };
378 
379 } // namespace utils
380 } // namespace genesis
381 
382 #endif // include guard
genesis::utils::ColorMap::operator=
ColorMap & operator=(ColorMap const &)=default
genesis::utils::ColorMap::mask_color
ColorMap & mask_color(Color value)
Color that indicates values equal to ColorNormalization::mask_value() or non-finite values.
Definition: map.hpp:161
genesis::utils::ColorMap
Store a list of colors and offer them as a map for values in range [ 0.0, 1.0 ].
Definition: map.hpp:61
genesis::utils::Color
Definition: color.hpp:47
genesis::utils::ColorMap::color_list
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
genesis::utils::ColorMap::reverse
bool reverse() const
Use the palette colors in reverse, back to front.
Definition: map.hpp:149
genesis::utils::ColorMap::under_color
ColorMap & under_color(Color value)
Color that indicates values less than min().
Definition: map.hpp:179
genesis::utils::ColorNormalization
Base class for color normalization.
Definition: normalization.hpp:52
genesis::utils::ColorMap::over_color
ColorMap & over_color(Color value)
Color that indicates values greater than max().
Definition: map.hpp:170
genesis::utils::ColorMap::reverse
ColorMap & reverse(bool value)
Definition: map.hpp:213
genesis::utils::ColorMap::clip_over
bool clip_over() const
Clip (clamp) values greater than max() to be inside [ min, max ].
Definition: map.hpp:141
genesis::utils::ColorMap::operator()
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
genesis::utils::ColorMap::over_color
Color const & over_color() const
Color that indicates values greater than max().
Definition: map.hpp:111
genesis::utils::ColorMap::~ColorMap
~ColorMap()=default
genesis::utils::ColorMap::clip_under
ColorMap & clip_under(bool value)
Clip (clamp) values less than min() to be inside [ min, max ].
Definition: map.hpp:198
genesis::utils::ColorMap::under_color
Color const & under_color() const
Color that indicates values less than min().
Definition: map.hpp:121
genesis::utils::ColorMap::operator()
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:331
genesis::utils::ColorMap::clip_over
ColorMap & clip_over(bool value)
Clip (clamp) values greater than max() to be inside [ min, max ].
Definition: map.hpp:207
genesis::utils::ColorMap::clip
ColorMap & clip(bool value)
Definition: map.hpp:188
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::ColorMap::operator()
std::vector< Color > operator()(ForwardIterator first, ForwardIterator last) const
Return the mapped colors for a range of values.
Definition: map.hpp:307
color.hpp
Header of Color class.
genesis::utils::ColorMap::clip_under
bool clip_under() const
Clip (clamp) values less than min() to be inside [ min, max ].
Definition: map.hpp:131
genesis::utils::ColorMap::palette
std::vector< Color > const & palette() const
Get the color list currently in use.
Definition: map.hpp:232
genesis::utils::ColorMap::ColorMap
ColorMap(std::vector< Color > const &colors)
Definition: map.hpp:71
genesis::utils::ColorMap::empty
bool empty() const
Return whether the Palette is empty, that is, no colors were set.
Definition: map.hpp:240
genesis::utils::ColorMap::palette
ColorMap & palette(std::vector< Color > const &value)
Definition: map.hpp:219
genesis::utils::ColorMap::mask_color
Color const & mask_color() const
Color that indicates values equal to ColorNormalization::mask_value() or non-finite values.
Definition: map.hpp:101
genesis::utils::ColorMap::size
size_t size() const
Return the size of the map, that is, the number of colors in the list.
Definition: map.hpp:248
genesis::utils::ColorMap::ColorMap
ColorMap()=default
genesis::utils::ColorMap::color
Color color(size_t index) const
Return a particular color from the palette, module the palette size.
Definition: map.hpp:259