MeteoIODoc 20260620.13b5b0a5
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
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 <vector>
25
26namespace mio {
27
38class 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 constexpr unsigned int char_width = 6;
75 static constexpr unsigned int char_height = 10;
76
77 static constexpr unsigned int text_chars_nb = 9; //each label will contain 9 chars
78 static constexpr unsigned int char_space = 1; //horizontal space between two chars
79 static constexpr unsigned int text_width = text_chars_nb*(char_width+char_space); //whole text line
80 static constexpr unsigned int sample_width = char_width*1; //color sample 2 chars wide
81 static constexpr unsigned int sample_text_space = 6; //width of the color sample
82 static constexpr unsigned int legend_plot_space = char_width*1;
83 static constexpr unsigned int total_width = legend_plot_space+sample_width+sample_text_space+text_width;
84
85 static constexpr unsigned int interline = 5;
86 static constexpr unsigned int label_height = char_height+interline; //1 char + interline
87 static constexpr unsigned int nb_labels = 12+1; //every decile + 0 level
88 static constexpr unsigned int total_height = nb_labels*label_height+interline;
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
95namespace 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
180class Gradient {
181 public:
198
203 Gradient() : min(0.), max(0.), delta(0.), type(none), model(nullptr), 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
285class 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 override;
289};
290
292 public:
293 gr_blue_pink(const double& i_min, const double& i_max, const bool& i_autoscale);
294};
295
296class 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 override;
300 private:
301 //This gradient is interpolated in RGB color space
302 std::vector<double> 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
320class 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
331 public:
332 gr_terrain(const double& i_min, const double& i_max, const bool& i_autoscale);
333};
334
335class gr_slope : public Gradient_model {
336 public:
337 gr_slope(const double& i_min, const double& i_max, const bool& i_autoscale);
338};
339
340class gr_azi : public Gradient_model {
341 public:
342 gr_azi(const double& i_min, const double& i_max, const bool& i_autoscale);
343};
344
345class 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
The template class Array2D is a 2D Array (Matrix) able to hold any type of object as datatype....
Definition Array2D.h:65
Definition Graphics.h:133
double getInterpol(const double &val, const std::vector< double > &i_X, const std::vector< double > &i_Y) const
Definition Graphics.cc:488
virtual ~Gradient_model()
Definition Graphics.h:136
virtual void getColor(const double &val, double &r, double &g, double &b) const
Definition Graphics.cc:523
std::vector< double > v_s
Definition Graphics.h:147
void HSV2RGB(const double &h, const double &s, const double &v, unsigned char &r, unsigned char &g, unsigned char &b) const
Definition Graphics.cc:514
std::vector< double > v_h
Definition Graphics.h:147
Gradient_model(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition Graphics.h:137
std::vector< double > X
Definition Graphics.h:147
Gradient_model()
Definition Graphics.h:135
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
This converts numeric values into rgb values. The object is initialized with the range that the gradi...
Definition Graphics.h:180
Type
This enum provides names for possible color gradients.
Definition Graphics.h:183
@ heat
the traditional heat gradient (with all its associated shortcomings)
Definition Graphics.h:188
@ blue_pink
blue to pink gradient, isomorphic gradient
Definition Graphics.h:190
@ freeze
two, two-color gradients with a sharp transition at 0
Definition Graphics.h:189
@ terrain
suitable for DEM. if autoscale, then sea and snow line are turned off
Definition Graphics.h:185
@ bluewhitered
blue to white, then white to red
Definition Graphics.h:193
@ whitetoblk
white to black gradient
Definition Graphics.h:194
@ slope
suitable to represent slope
Definition Graphics.h:186
@ bg_isomorphic
drak-blue to light-green isomorphic gradient
Definition Graphics.h:192
@ pastel
same color scale as "slope" but linear
Definition Graphics.h:191
@ blue
white to slightly violet gradient. This is similar to the one used for the SLF avalanche bulletin
Definition Graphics.h:196
@ azi
suitable to represent slope azimuth. In autoscale, it becomes a two color gradient
Definition Graphics.h:187
@ none
no type selected
Definition Graphics.h:184
@ blktowhite
black to white gradient
Definition Graphics.h:195
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:317
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:436
Gradient()
Default Constructor. This should be followed by a call to set() before calling getColor.
Definition Graphics.h:203
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:358
~Gradient()
Definition Graphics.h:217
Gradient & operator=(const Gradient &source)
Definition Graphics.cc:303
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:344
static const unsigned char channel_max_color
nr of colors per channel of the generated gradients
Definition Graphics.h:272
This creates a legend as pixels in a Grid2DObject. This should be used with/by a plugin that would th...
Definition Graphics.h:38
static const double text_color
marker for solid text
Definition Graphics.h:64
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:210
static const double bg_color
marker for solid background
Definition Graphics.h:63
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:152
Definition Graphics.h:340
Definition Graphics.h:325
Definition Graphics.h:315
Definition Graphics.h:291
Definition Graphics.h:320
Definition Graphics.h:305
Definition Graphics.h:296
void getColor(const double &val, double &r, double &g, double &b) const override
Definition Graphics.cc:579
Definition Graphics.h:285
gr_heat(const double &i_min, const double &i_max, const bool &i_autoscale)
Definition Graphics.h:287
void getColor(const double &i_val, double &r, double &g, double &b) const override
Definition Graphics.cc:536
Definition Graphics.h:345
Definition Graphics.h:335
Definition Graphics.h:330
Definition Graphics.h:310
Definition Config.cc:34