X-Boost  2.3.8
HeatmapColorGenerator.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef HEATMAPCOLORGENERATOR_H
22 #define HEATMAPCOLORGENERATOR_H
23 
27 namespace heatmap {
28 
29  namespace detail {
30 
31  struct RGB8{
32  RGB8(){}
33  RGB8(unsigned char i) : R(i), G(i), B(i) {}
34  RGB8(unsigned char r,unsigned char g, unsigned char b):R(r),G(g),B(b){}
35  unsigned char R,G,B;
36 
37  };
38 
39  inline RGB8 hsvToRGB(double h, // degrees [0, 360]
40  double s, // [0, 1]
41  double v) { // [0, 1]
42  if (s == 0) { // grey
43  return RGB8(v*255);
44  }
45  double h_floor = 0.0,
46  h_frac = modf(h/60,&h_floor);
47  double p = v*(1.0-s),
48  q = v*(1.0-s*h_frac),
49  t = v*(1.0-s*(1.0-h_frac));
50  p *= 255; q *= 255; t *= 255; v *= 255;
51  switch (static_cast<int>(h_floor)) {
52  case 0: return RGB8(v,t,p);
53  case 1: return RGB8(q,v,p);
54  case 2: return RGB8(p,v,t);
55  case 3: return RGB8(p,q,v);
56  case 4: return RGB8(t,p,v);
57  default: // case 5:
58  return RGB8(v,p,q);
59  };
60  }
61 
62  }
63 
64  template <class T>
65  class BlueToRed {
66  public:
67  BlueToRed(const T max, const T min = T()) : m_max(max), m_min(min), m_invRange(1.0/(max-min)) {}
68 
69  detail::RGB8 operator()(const T value) const {
70  double val = (value-m_min)*m_invRange;
71  if (val < 0.0) val = 0.0;
72  if (val > 1.0) val = 1.0;
73  return detail::hsvToRGB(240.0*(1.0-val),1.0,0.2+0.3*val);
74  }
75 
76  T min() const { return m_min; }
77  T max() const { return m_max; }
78 
79  private:
80  T m_max, m_min;
81  double m_invRange;
82  };
83 
84 }
85 
86 template <class T, template <class> class H = heatmap::BlueToRed>
87 struct HeatmapColorGenerator : public H<T> {
88  HeatmapColorGenerator(const T max, const T min = T()) : H<T>(max,min) {}
89 };
90 
91 #endif
Definition: HeatmapColorGenerator.h:31
Definition: HeatmapColorGenerator.h:87
Definition: HeatmapColorGenerator.h:65