MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Graphics.h
Go to the documentation of this file.
1 /***********************************************************************************/
2 /* Copyright 2011 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3 /***********************************************************************************/
4 /* This file is part of MeteoIO.
5  MeteoIO is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser 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  MeteoIO 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef GRAPHICS_H
19 #define GRAPHICS_H
20 
22 
23 #include <string>
24 #include <vector>
25 
26 namespace mio {
27 
38 class Legend {
39  public:
46  Legend(const unsigned int &height, const double &minimum, const double &maximum);
47 
53  static unsigned int getLegendWidth();
54 
61  const Array2D<double> getLegend() const;
62 
63  static const double bg_color;
64  static const double text_color;
65 
66  private:
67  Array2D<double> grid;
68  void simpleLegend(const unsigned int &height, const double &minimum, const double &maximum);
69  void smartLegend(const unsigned int &height, const double &minimum, const double &maximum);
70  void drawLegend(const unsigned int &height, const double &minimum, const double &maximum);
71  void writeLine(const double& val, const unsigned int& px_row);
72  void writeChar(const unsigned int i_char[10][6], const unsigned int& px_col, const unsigned int& px_row);
73 
74  static const unsigned int char_width;
75  static const unsigned int char_height;
76 
77  static const unsigned int text_chars_nb;
78  static const unsigned int char_space;
79  static const unsigned int text_width;
80  static const unsigned int sample_width;
81  static const unsigned int sample_text_space;
82  static const unsigned int legend_plot_space;
83  static const unsigned int total_width;
84 
85  static const unsigned int interline;
86  static const unsigned int label_height;
87  static const unsigned int nb_labels;
88  static const unsigned int total_height;
89 
90  static const unsigned int font_0[10][6], font_1[10][6], font_2[10][6], font_3[10][6], font_4[10][6];
91  static const unsigned int font_5[10][6], font_6[10][6], font_7[10][6], font_8[10][6], font_9[10][6];
92  static const unsigned int font_plus[10][6], font_minus[10][6], font_dot[10][6], font_E[10][6];
93 };
94 
95 namespace Color {
109  void RGBtoHSV(const double& r, const double& g, const double& b, double &h, double &s, double &v);
110 
124  void HSVtoRGB(const double& h, const double& s, const double& v, double &r, double &g, double &b);
125 }
126 
128 // Gradient class
130 //This class is the base class for the various gradients.
131 //DO NOT USE pure white in a gradient, since this might be interpreted as a transparent pixel!
132 //Gradients scale between 0 and 1, but might receive some out of range values for special effects (below sea level, above snow line, etc)
134  public:
135  Gradient_model() : X(), v_h(), v_s(), v_v() {} //do not use this constructor!
136  virtual ~Gradient_model() {}
137  Gradient_model(const double& i_min, const double& i_max, const bool& i_autoscale) : X(), v_h(), v_s(), v_v() { (void)i_min; (void)i_max; (void)i_autoscale;}
138  //setBgColor()
139  //setFgColor()
140 
141  //val MUST be between 0 and 1
142  virtual void getColor(const double &val, double &r, double &g, double &b) const;
143  protected:
144  double getInterpol(const double& val, const std::vector<double>& i_X, const std::vector<double>& i_Y) const;
145  void HSV2RGB(const double& h, const double& s, const double& v, unsigned char &r, unsigned char &g, unsigned char &b) const;
146 
147  std::vector<double> X, v_h,v_s,v_v;
148 };
149 
180 class Gradient {
181  public:
183  typedef enum TYPE {
187  azi,
197  } Type;
198 
203  Gradient() : min(0.), max(0.), delta(0.), type(none), model(NULL), nr_unique_cols(0), autoscale(true) {}
204 
213  Gradient(const Type& i_type, const double& min_val, const double &max_val, const bool& i_autoscale);
214 
215  Gradient(const Gradient& c);
216 
217  ~Gradient() {delete model;}
218 
227  void set(const Type& i_type, const double& min_val, const double &max_val, const bool& i_autoscale);
228  //setBgColor()
229  //setFgColor()
230 
240  void setNrOfLevels(const unsigned char& i_nr_unique_levels);
241 
251  void getColor(const double &val, unsigned char &r, unsigned char &g, unsigned char &b, bool &a) const;
252 
259  void getColor(const double& val, unsigned char& index) const;
260 
268  void getPalette(std::vector<unsigned char> &palette, size_t &nr_colors) const;
269 
270  Gradient& operator=(const Gradient& source);
271 
272  static const unsigned char channel_max_color;
273  private:
274  void setModel(const Type& i_type);
275 
276  double min, max, delta;
277  Type type;
278  Gradient_model *model;
279  unsigned char nr_unique_cols;
280  static const unsigned char reserved_idx;
281  static const unsigned char reserved_cols;
282  bool autoscale;
283 };
284 
285 class gr_heat : public Gradient_model {
286  public:
287  gr_heat(const double& i_min, const double& i_max, const bool& i_autoscale) {(void)i_min; (void)i_max; (void)i_autoscale;}
288  void getColor(const double &i_val, double &r, double &g, double &b) const;
289 };
290 
291 class gr_blue_pink : public Gradient_model {
292  public:
293  gr_blue_pink(const double& i_min, const double& i_max, const bool& i_autoscale);
294 };
295 
296 class gr_freeze : public Gradient_model {
297  public:
298  gr_freeze(const double& i_min, const double& i_max, const bool& i_autoscale);
299  void getColor(const double &val, double &r, double &g, double &b) const;
300  private:
301  //This gradient is interpolated in RGB color space
302  std::vector<double> X, v_r, v_g, v_b;
303 };
304 
306  public:
307  gr_bluewhitered(const double& i_min, const double& i_max, const bool& i_autoscale);
308 };
309 
311  public:
312  gr_whitetoblk(const double& i_min, const double& i_max, const bool& i_autoscale);
313 };
314 
316  public:
317  gr_blktowhite(const double& i_min, const double& i_max, const bool& i_autoscale);
318 };
319 
320 class gr_blue : public Gradient_model {
321  public:
322  gr_blue(const double& i_min, const double& i_max, const bool& i_autoscale);
323 };
324 
326  public:
327  gr_bg_isomorphic(const double& i_min, const double& i_max, const bool& i_autoscale);
328 };
329 
330 class gr_terrain : public Gradient_model {
331  public:
332  gr_terrain(const double& i_min, const double& i_max, const bool& i_autoscale);
333 };
334 
335 class gr_slope : public Gradient_model {
336  public:
337  gr_slope(const double& i_min, const double& i_max, const bool& i_autoscale);
338 };
339 
340 class gr_azi : public Gradient_model {
341  public:
342  gr_azi(const double& i_min, const double& i_max, const bool& i_autoscale);
343 };
344 
345 class gr_pastel : public Gradient_model {
346  public:
347  gr_pastel(const double& i_min, const double& i_max, const bool& i_autoscale);
348 };
349 
350 } //namespace
351 #endif
static const double text_color
marker for solid text
Definition: Graphics.h:64
Definition: Graphics.h:133
~Gradient()
Definition: Graphics.h:217
gr_blktowhite(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:600
Definition: Graphics.h:315
gr_whitetoblk(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:595
void HSVtoRGB(const double &h, const double &s, const double &v, double &r, double &g, double &b)
convert HSV to RGB. This converts Hue-Saturation-Value to Red-Green-Blue values. See https://secure...
Definition: Graphics.cc:252
drak-blue to light-green isomorphic gradient
Definition: Graphics.h:192
suitable to represent slope
Definition: Graphics.h:186
same color scale as "slope" but linear
Definition: Graphics.h:191
blue to white, then white to red
Definition: Graphics.h:193
Definition: Graphics.h:330
void getColor(const double &val, unsigned char &r, unsigned char &g, unsigned char &b, bool &a) const
Get RGB values for a given numeric value See class description for more explanations on the implement...
Definition: Graphics.cc:362
Gradient_model(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.h:137
no type selected
Definition: Graphics.h:184
void set(const Type &i_type, const double &min_val, const double &max_val, const bool &i_autoscale)
Setter See class description for more...
Definition: Graphics.cc:321
suitable for DEM. if autoscale, then sea and snow line are turned off
Definition: Graphics.h:185
gr_bg_isomorphic(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:616
gr_pastel(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:623
std::vector< double > v_h
Definition: Graphics.h:147
void getColor(const double &i_val, double &r, double &g, double &b) const
Definition: Graphics.cc:539
Definition: Graphics.h:291
suitable to represent slope azimuth. In autoscale, it becomes a two color gradient ...
Definition: Graphics.h:187
const Array2D< double > getLegend() const
Get the legend in an array The legend is coded as values between min and max (+background and text co...
Definition: Graphics.cc:213
gr_azi(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:668
gr_slope(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:657
Definition: Graphics.h:310
void getColor(const double &val, double &r, double &g, double &b) const
Definition: Graphics.cc:582
black to white gradient
Definition: Graphics.h:195
static const double bg_color
marker for solid background
Definition: Graphics.h:63
white to slightly violet gradient. This is similar to the one used for the SLF avalanche bulletin ...
Definition: Graphics.h:196
gr_blue(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:605
gr_blue_pink(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:553
void RGBtoHSV(const double &r, const double &g, const double &b, double &h, double &s, double &v)
convert RGB to HSV. This converts Red-Green-Blue values to Hue-Saturation-Value. See https://secure...
Definition: Graphics.cc:223
double getInterpol(const double &val, const std::vector< double > &i_X, const std::vector< double > &i_Y) const
Definition: Graphics.cc:491
Gradient_model()
Definition: Graphics.h:135
gr_bluewhitered(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:589
Type
This enum provides names for possible color gradients.
Definition: Graphics.h:183
std::vector< double > v_s
Definition: Graphics.h:147
static unsigned int getLegendWidth()
Get the actual width of the legend This is constant but depends on various parameters of the legend: ...
Definition: Graphics.cc:155
std::vector< double > v_v
control points: vector of X and associated hues, saturations and values. They must be in X ascending ...
Definition: Graphics.h:147
void setNrOfLevels(const unsigned char &i_nr_unique_levels)
Set a reduced number of levels for the gradient The given argument is an upper bound for the number o...
Definition: Graphics.cc:348
This converts numeric values into rgb values. The object is initialized with the range that the gradi...
Definition: Graphics.h:180
Legend(const unsigned int &height, const double &minimum, const double &maximum)
Constructor.
Definition: Graphics.cc:68
Definition: Graphics.h:325
Definition: Graphics.h:305
Definition: Graphics.h:345
gr_freeze(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:561
Definition: Graphics.h:320
Gradient & operator=(const Gradient &source)
Definition: Graphics.cc:307
the traditional heat gradient (with all its associated shortcomings)
Definition: Graphics.h:188
Definition: Graphics.h:285
This creates a legend as pixels in a Grid2DObject. This should be used with/by a plugin that would th...
Definition: Graphics.h:38
white to black gradient
Definition: Graphics.h:194
Gradient()
Default Constructor. This should be followed by a call to set() before calling getColor.
Definition: Graphics.h:203
std::vector< double > X
Definition: Graphics.h:147
virtual ~Gradient_model()
Definition: Graphics.h:136
static const unsigned char channel_max_color
nr of colors per channel of the generated gradients
Definition: Graphics.h:272
blue to pink gradient, isomorphic gradient
Definition: Graphics.h:190
Definition: Graphics.h:340
Definition: Graphics.h:335
Definition: Graphics.h:296
virtual void getColor(const double &val, double &r, double &g, double &b) const
Definition: Graphics.cc:526
void getPalette(std::vector< unsigned char > &palette, size_t &nr_colors) const
Get palette colors for the selected gradient When building an indexed image, one needs to first retri...
Definition: Graphics.cc:439
gr_terrain(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:633
void HSV2RGB(const double &h, const double &s, const double &v, unsigned char &r, unsigned char &g, unsigned char &b) const
Definition: Graphics.cc:517
two, two-color gradients with a sharp transition at 0
Definition: Graphics.h:189
gr_heat(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.h:287