MeteoIODoc  2.10.0
Graphics.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-or-later
2 /***********************************************************************************/
3 /* Copyright 2011 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4 /***********************************************************************************/
5 /* This file is part of MeteoIO.
6  MeteoIO is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  MeteoIO is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GRAPHICS_H
20 #define GRAPHICS_H
21 
23 
24 #include <string>
25 #include <vector>
26 
27 namespace mio {
28 
39 class Legend {
40  public:
47  Legend(const unsigned int &height, const double &minimum, const double &maximum);
48 
54  static unsigned int getLegendWidth();
55 
62  const Array2D<double> getLegend() const;
63 
64  static const double bg_color;
65  static const double text_color;
66 
67  private:
68  Array2D<double> grid;
69  void simpleLegend(const unsigned int &height, const double &minimum, const double &maximum);
70  void smartLegend(const unsigned int &height, const double &minimum, const double &maximum);
71  void drawLegend(const unsigned int &height, const double &minimum, const double &maximum);
72  void writeLine(const double& val, const unsigned int& px_row);
73  void writeChar(const unsigned int i_char[10][6], const unsigned int& px_col, const unsigned int& px_row);
74 
75  static const unsigned int char_width;
76  static const unsigned int char_height;
77 
78  static const unsigned int text_chars_nb;
79  static const unsigned int char_space;
80  static const unsigned int text_width;
81  static const unsigned int sample_width;
82  static const unsigned int sample_text_space;
83  static const unsigned int legend_plot_space;
84  static const unsigned int total_width;
85 
86  static const unsigned int interline;
87  static const unsigned int label_height;
88  static const unsigned int nb_labels;
89  static const unsigned int total_height;
90 
91  static const unsigned int font_0[10][6], font_1[10][6], font_2[10][6], font_3[10][6], font_4[10][6];
92  static const unsigned int font_5[10][6], font_6[10][6], font_7[10][6], font_8[10][6], font_9[10][6];
93  static const unsigned int font_plus[10][6], font_minus[10][6], font_dot[10][6], font_E[10][6];
94 };
95 
96 namespace Color {
110  void RGBtoHSV(const double& r, const double& g, const double& b, double &h, double &s, double &v);
111 
125  void HSVtoRGB(const double& h, const double& s, const double& v, double &r, double &g, double &b);
126 }
127 
129 // Gradient class
131 //This class is the base class for the various gradients.
132 //DO NOT USE pure white in a gradient, since this might be interpreted as a transparent pixel!
133 //Gradients scale between 0 and 1, but might receive some out of range values for special effects (below sea level, above snow line, etc)
135  public:
136  Gradient_model() : X(), v_h(), v_s(), v_v() {} //do not use this constructor!
137  virtual ~Gradient_model() {}
138  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;}
139  //setBgColor()
140  //setFgColor()
141 
142  //val MUST be between 0 and 1
143  virtual void getColor(const double &val, double &r, double &g, double &b) const;
144  protected:
145  double getInterpol(const double& val, const std::vector<double>& i_X, const std::vector<double>& i_Y) const;
146  void HSV2RGB(const double& h, const double& s, const double& v, unsigned char &r, unsigned char &g, unsigned char &b) const;
147 
148  std::vector<double> X, v_h, v_s, v_v;
149 };
150 
181 class Gradient {
182  public:
184  typedef enum TYPE {
188  azi,
197  blue
198  } Type;
199 
204  Gradient() : min(0.), max(0.), delta(0.), type(none), model(nullptr), nr_unique_cols(0), autoscale(true) {}
205 
214  Gradient(const Type& i_type, const double& min_val, const double &max_val, const bool& i_autoscale);
215 
216  Gradient(const Gradient& c);
217 
218  ~Gradient() {delete model;}
219 
228  void set(const Type& i_type, const double& min_val, const double &max_val, const bool& i_autoscale);
229  //setBgColor()
230  //setFgColor()
231 
241  void setNrOfLevels(const unsigned char& i_nr_unique_levels);
242 
252  void getColor(const double &val, unsigned char &r, unsigned char &g, unsigned char &b, bool &a) const;
253 
260  void getColor(const double& val, unsigned char& index) const;
261 
269  void getPalette(std::vector<unsigned char> &palette, size_t &nr_colors) const;
270 
271  Gradient& operator=(const Gradient& source);
272 
273  static const unsigned char channel_max_color;
274  private:
275  void setModel(const Type& i_type);
276 
277  double min, max, delta;
278  Type type;
279  Gradient_model *model;
280  unsigned char nr_unique_cols;
281  static const unsigned char reserved_idx;
282  static const unsigned char reserved_cols;
283  bool autoscale;
284 };
285 
286 class gr_heat : public Gradient_model {
287  public:
288  gr_heat(const double& i_min, const double& i_max, const bool& i_autoscale) {(void)i_min; (void)i_max; (void)i_autoscale;}
289  void getColor(const double &i_val, double &r, double &g, double &b) const;
290 };
291 
292 class gr_blue_pink : public Gradient_model {
293  public:
294  gr_blue_pink(const double& i_min, const double& i_max, const bool& i_autoscale);
295 };
296 
297 class gr_freeze : public Gradient_model {
298  public:
299  gr_freeze(const double& i_min, const double& i_max, const bool& i_autoscale);
300  void getColor(const double &val, double &r, double &g, double &b) const;
301  private:
302  //This gradient is interpolated in RGB color space
303  std::vector<double> v_r, v_g, v_b;
304 };
305 
307  public:
308  gr_bluewhitered(const double& i_min, const double& i_max, const bool& i_autoscale);
309 };
310 
312  public:
313  gr_whitetoblk(const double& i_min, const double& i_max, const bool& i_autoscale);
314 };
315 
317  public:
318  gr_blktowhite(const double& i_min, const double& i_max, const bool& i_autoscale);
319 };
320 
321 class gr_blue : public Gradient_model {
322  public:
323  gr_blue(const double& i_min, const double& i_max, const bool& i_autoscale);
324 };
325 
327  public:
328  gr_bg_isomorphic(const double& i_min, const double& i_max, const bool& i_autoscale);
329 };
330 
331 class gr_terrain : public Gradient_model {
332  public:
333  gr_terrain(const double& i_min, const double& i_max, const bool& i_autoscale);
334 };
335 
336 class gr_slope : public Gradient_model {
337  public:
338  gr_slope(const double& i_min, const double& i_max, const bool& i_autoscale);
339 };
340 
341 class gr_azi : public Gradient_model {
342  public:
343  gr_azi(const double& i_min, const double& i_max, const bool& i_autoscale);
344 };
345 
346 class gr_pastel : public Gradient_model {
347  public:
348  gr_pastel(const double& i_min, const double& i_max, const bool& i_autoscale);
349 };
350 
351 } //namespace
352 #endif
Definition: Graphics.h:134
double getInterpol(const double &val, const std::vector< double > &i_X, const std::vector< double > &i_Y) const
Definition: Graphics.cc:501
virtual ~Gradient_model()
Definition: Graphics.h:137
virtual void getColor(const double &val, double &r, double &g, double &b) const
Definition: Graphics.cc:536
std::vector< double > v_s
Definition: Graphics.h:148
void HSV2RGB(const double &h, const double &s, const double &v, unsigned char &r, unsigned char &g, unsigned char &b) const
Definition: Graphics.cc:527
std::vector< double > v_h
Definition: Graphics.h:148
Gradient_model(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.h:138
std::vector< double > X
Definition: Graphics.h:148
Gradient_model()
Definition: Graphics.h:136
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:148
This converts numeric values into rgb values. The object is initialized with the range that the gradi...
Definition: Graphics.h:181
Type
This enum provides names for possible color gradients.
Definition: Graphics.h:184
@ heat
the traditional heat gradient (with all its associated shortcomings)
Definition: Graphics.h:189
@ blue_pink
blue to pink gradient, isomorphic gradient
Definition: Graphics.h:191
@ freeze
two, two-color gradients with a sharp transition at 0
Definition: Graphics.h:190
@ terrain
suitable for DEM. if autoscale, then sea and snow line are turned off
Definition: Graphics.h:186
@ bluewhitered
blue to white, then white to red
Definition: Graphics.h:194
@ whitetoblk
white to black gradient
Definition: Graphics.h:195
@ slope
suitable to represent slope
Definition: Graphics.h:187
@ bg_isomorphic
drak-blue to light-green isomorphic gradient
Definition: Graphics.h:193
@ pastel
same color scale as "slope" but linear
Definition: Graphics.h:192
@ blue
white to slightly violet gradient. This is similar to the one used for the SLF avalanche bulletin
Definition: Graphics.h:197
@ azi
suitable to represent slope azimuth. In autoscale, it becomes a two color gradient
Definition: Graphics.h:188
@ none
no type selected
Definition: Graphics.h:185
@ blktowhite
black to white gradient
Definition: Graphics.h:196
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:331
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:449
Gradient()
Default Constructor. This should be followed by a call to set() before calling getColor.
Definition: Graphics.h:204
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:372
~Gradient()
Definition: Graphics.h:218
Gradient & operator=(const Gradient &source)
Definition: Graphics.cc:317
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:358
static const unsigned char channel_max_color
nr of colors per channel of the generated gradients
Definition: Graphics.h:273
This creates a legend as pixels in a Grid2DObject. This should be used with/by a plugin that would th...
Definition: Graphics.h:39
static const double text_color
marker for solid text
Definition: Graphics.h:65
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:224
static const double bg_color
marker for solid background
Definition: Graphics.h:64
Legend(const unsigned int &height, const double &minimum, const double &maximum)
Constructor.
Definition: Graphics.cc:69
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:166
Definition: Graphics.h:341
gr_azi(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:678
Definition: Graphics.h:326
gr_bg_isomorphic(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:626
Definition: Graphics.h:316
gr_blktowhite(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:610
Definition: Graphics.h:292
gr_blue_pink(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:563
Definition: Graphics.h:321
gr_blue(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:615
Definition: Graphics.h:306
gr_bluewhitered(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:599
Definition: Graphics.h:297
gr_freeze(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:571
void getColor(const double &val, double &r, double &g, double &b) const
Definition: Graphics.cc:592
Definition: Graphics.h:286
void getColor(const double &i_val, double &r, double &g, double &b) const
Definition: Graphics.cc:549
gr_heat(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.h:288
Definition: Graphics.h:346
gr_pastel(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:633
Definition: Graphics.h:336
gr_slope(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:667
Definition: Graphics.h:331
gr_terrain(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:643
Definition: Graphics.h:311
gr_whitetoblk(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition: Graphics.cc:605
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:234
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:262
Definition: Config.cc:30