A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
palette.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_PALETTE_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_PALETTE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 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 <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 // Color Palette
45 // =================================================================================================
46 
51 {
52 public:
53 
54  // -------------------------------------------------------------------------
55  // Constructors and Rule of Five
56  // -------------------------------------------------------------------------
57 
58  ColorPalette() = default;
59 
60  ColorPalette( std::vector<Color> const& colors )
61  {
62  palette( colors );
63  }
64 
65  ~ColorPalette() = default;
66 
67  ColorPalette(ColorPalette const&) = default;
68  ColorPalette(ColorPalette&&) noexcept = default;
69 
70  ColorPalette& operator= (ColorPalette const&) = default;
71  ColorPalette& operator= (ColorPalette&&) noexcept = default;
72 
73  // -------------------------------------------------------------------------
74  // Accessors
75  // -------------------------------------------------------------------------
76 
77  double min() const
78  {
79  return min_;
80  }
81 
82  double mid() const
83  {
84  return mid_;
85  }
86 
87  double max() const
88  {
89  return max_;
90  }
91 
102  double mask_value() const
103  {
104  return mask_value_;
105  }
106 
111  {
112  return mask_color_;
113  }
114 
121  {
122  return over_color_;
123  }
124 
131  {
132  return under_color_;
133  }
134 
141  bool clip() const
142  {
143  return clip_;
144  }
145 
149  bool reverse() const
150  {
151  return reverse_;
152  }
153 
154  std::vector<Color> const& palette() const
155  {
156  return palette_;
157  }
158 
159  // -------------------------------------------------------------------------
160  // Modificators
161  // -------------------------------------------------------------------------
162 
163  ColorPalette& range( double min, double max );
164 
165  ColorPalette& range( double min, double mid, double max );
166 
176  template <class ForwardIterator>
177  void range( ForwardIterator first, ForwardIterator last )
178  {
179  // New values, so that we first do not override the current ones.
180  auto min = std::numeric_limits<double>::max();
181  auto max = std::numeric_limits<double>::lowest();
182 
183  double sum = 0.0;
184  size_t cnt = 0;
185 
186  while( first != last ) {
187  if( ! std::isfinite( *first ) || *first == mask_value_ ) {
188  continue;
189  }
190  if( *first < min ) {
191  min = *first;
192  }
193  if( *first > max ) {
194  max = *first;
195  }
196 
197  sum += *first;
198  ++cnt;
199 
200  ++first;
201  }
202 
203  // Only update if we found values.
204  if( cnt > 0 ) {
205  min_ = min;
206  max_ = max;
207 
208  // mid_ = ( min + max ) / 2.0;
209  mid_ = sum / static_cast<double>( cnt );
210  }
211  }
212 
213  ColorPalette& min( double value )
214  {
215  min_ = value;
216  return *this;
217  }
218 
219  ColorPalette& mid( double value )
220  {
221  mid_ = value;
222  return *this;
223  }
224 
225  ColorPalette& max( double value )
226  {
227  max_ = value;
228  return *this;
229  }
230 
231  ColorPalette& mask_value( double value )
232  {
233  mask_value_ = value;
234  return *this;
235  }
236 
238  {
239  mask_color_ = value;
240  return *this;
241  }
242 
244  {
245  over_color_ = value;
246  return *this;
247  }
248 
250  {
251  under_color_ = value;
252  return *this;
253  }
254 
255  ColorPalette& clip( bool value )
256  {
257  clip_ = value;
258  return *this;
259  }
260 
261  ColorPalette& reverse( bool value )
262  {
263  reverse_ = value;
264  return *this;
265  }
266 
267  ColorPalette& palette( std::vector<Color> const& value )
268  {
269  palette_ = value;
270  return *this;
271  }
272 
273  // -------------------------------------------------------------------------
274  // Palette
275  // -------------------------------------------------------------------------
276 
277  Color qualitative_color( size_t index ) const;
278 
279  Color sequential_color( double value ) const;
280 
303  Color diverging_color( double value ) const;
304 
305  std::vector<Color> qualitative_colors( std::vector<size_t> const& values ) const;
306 
307  std::vector<Color> sequential_colors( std::vector<double> const& values ) const;
308 
309  std::vector<Color> diverging_colors( std::vector<double> const& values ) const;
310 
311  // -------------------------------------------------------------------------
312  // Internal Functions
313  // -------------------------------------------------------------------------
314 
315 private:
316 
320  Color get_entry_( size_t index ) const
321  {
322  if( reverse_ ) {
323  index = palette_.size() - index - 1;
324  }
325  return palette_[ index ];
326  }
327 
336  std::pair<Color, bool> boundary_checks_( double& value ) const;
337 
342  Color get_interpolated_color_( double value ) const;
343 
344  // -------------------------------------------------------------------------
345  // Data Members
346  // -------------------------------------------------------------------------
347 
348 private:
349 
350  double min_ = 0.0;
351  double mid_ = 0.5;
352  double max_ = 1.0;
353 
354  double mask_value_ = std::numeric_limits<double>::quiet_NaN();
355 
356  Color mask_color_ = { 1.0, 1.0, 0.0 };
357  Color over_color_ = { 0.0, 1.0, 1.0 };
358  Color under_color_ = { 1.0, 0.0, 1.0 };
359 
360  bool clip_ = false;
361  bool reverse_ = false;
362 
363  std::vector<Color> palette_;
364 
365 };
366 
367 } // namespace utils
368 } // namespace genesis
369 
370 #endif // include guard
void range(ForwardIterator first, ForwardIterator last)
Set the min and max of the Palette to the min and max valid values that are found in the range [ firs...
Definition: palette.hpp:177
ColorPalette & mask_value(double value)
Definition: palette.hpp:231
ColorPalette & mid(double value)
Definition: palette.hpp:219
std::vector< Color > qualitative_colors(std::vector< size_t > const &values) const
Definition: palette.cpp:113
Color qualitative_color(size_t index) const
Definition: palette.cpp:67
double sum(const Histogram &h)
ColorPalette & under_color(Color value)
Definition: palette.hpp:249
ColorPalette & palette(std::vector< Color > const &value)
Definition: palette.hpp:267
Header of Color class.
bool reverse() const
Use the palette colors in reverse, back to front.
Definition: palette.hpp:149
Color mask_color() const
Color that indicates values equal to mask_value().
Definition: palette.hpp:110
ColorPalette & clip(bool value)
Definition: palette.hpp:255
ColorPalette & mask_color(Color value)
Definition: palette.hpp:237
ColorPalette(std::vector< Color > const &colors)
Definition: palette.hpp:60
ColorPalette & min(double value)
Definition: palette.hpp:213
Color sequential_color(double value) const
Definition: palette.cpp:75
bool clip() const
Clip values to be inside [ min max ].
Definition: palette.hpp:141
ColorPalette & over_color(Color value)
Definition: palette.hpp:243
Color diverging_color(double value) const
Get a color by using the palette for a diverging scale.
Definition: palette.cpp:90
ColorPalette & reverse(bool value)
Definition: palette.hpp:261
ColorPalette & range(double min, double max)
Definition: palette.cpp:47
std::vector< Color > const & palette() const
Definition: palette.hpp:154
Color under_color() const
Color that indicates values less than min().
Definition: palette.hpp:130
std::vector< Color > diverging_colors(std::vector< double > const &values) const
Definition: palette.cpp:131
Color over_color() const
Color that indicates values greater than max().
Definition: palette.hpp:120
std::vector< Color > sequential_colors(std::vector< double > const &values) const
Definition: palette.cpp:122
double mask_value() const
Mask value that identifies invalid values.
Definition: palette.hpp:102
ColorPalette & max(double value)
Definition: palette.hpp:225