Tulip  5.2.0
Large graphs analysis and drawing
ColorScale.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip 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.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef COLORSCALE_H_
21 #define COLORSCALE_H_
22 
23 #include <tulip/Observable.h>
24 #include <tulip/Color.h>
25 
26 #include <vector>
27 #include <map>
28 
29 namespace tlp {
30 
31 /**
32  * @brief This class represents a color scale to perform color mapping.
33  * The color scale can be either a gradient or defined by colors associated to consecutive
34  * intervals.
35  * If the color scale is a gradient, returned colors are interpolated in function of a position
36  * between 0.0 and 1.0.
37  * If the color scale is not a gradient returned colors are computed according to the interval the
38  * position belongs to.
39  * @code
40  * // Creates the color scale.
41  * tlp::ColorScale colorScale;
42  * // Clears the color scale as the default constructor initializes a non empty one
43  * colorScale.clear();
44  * // Color scale initialization : from blue to red with gradient.
45  * colorScale.setColorAtPos(0.0, tlp::Color::Blue);
46  * colorScale.setColorAtPos(1.0, tlp::Color::Red);
47  *
48  * // Gets the color for the position 0.5, i.e. tlp::Color(127,0,127).
49  * tlp::Color color = colorScale.getColorAtPos(0.5);
50  * // Reinitializes the color scale : from blue to red without gradient.
51  * std::vector<tlp::Color> newColors;
52  * newColors.push_back(tlp::Color::Blue);
53  * newColors.push_back(tlp::Color::Red);
54  * colorScale.setColorScale(newColors, false);
55  * // Gets the color for the position 0.3, i.e. tlp::Color(0,0,255).
56  * color = colorScale.getColorAtPos(0.3);
57  * // Gets the color for the position 0.7, i.e. tlp::Color(255,0,0).
58  * color = colorScale.getColorAtPos(0.7);
59  * @endcode
60  *
61  */
62 class TLP_SCOPE ColorScale : public Observable {
63 
64 public:
65  /**
66  * Initializes a color scale with a default set of colors.
67  *
68  */
69  ColorScale();
70 
71  /**
72  * Initializes a color scale with a set of colors passed as parameter.
73  * @param colors a vector of colors defining the color scale (first color is at position 0.0, last
74  * color at position 1.0)
75  * @param gradient specifies if the color scale should be a gradient or not
76  *
77  */
78  ColorScale(const std::vector<Color> &colors, const bool gradient = true);
79 
80  /**
81  * Initializes a color scale with a map of stop points and colors passed as parameter.
82  * @since Tulip 4.10
83  * @param colorMap a map of stop points and colors defining the color scale (The keys of the map
84  * must be between 0.0 and 1.0, other ones will be ignored.)
85  * @param gradient specifies if the color scale should be a gradient or not
86  *
87  */
88  ColorScale(const std::map<float, Color> &colorMap, const bool gradient = true);
89 
90  ColorScale(const ColorScale &scale);
91 
92  ColorScale &operator=(const ColorScale &scale);
93 
94  ~ColorScale() override;
95 
96  /**
97  * @brief Clears the color scale.
98  *
99  * @since Tulip 4.10
100  *
101  */
102  void clear() {
103  colorMap.clear();
104  }
105 
106  /**
107  * @brief Gets the number of stops points into the color scale.
108  *
109  */
110  unsigned int getStopsCount() {
111  return colorMap.size();
112  }
113 
114  /**
115  * @brief Configures the color scale with regular stop points.
116  *
117  * This method configures the color scale from a vector of colors and
118  * associates regular stop points to them.
119  *
120  * @warning If the scale was already configured the previous configuration is lost.
121  *
122  * @param colors the colors to use in the color scale
123  * @param gradient if set to true, color scale is a gradient
124  *
125  */
126  virtual void setColorScale(const std::vector<Color> colors, const bool gradient = true);
127 
128  /**
129  * @brief Adds a color to the color scale at specific position.
130  *
131  * This method adds a color to the color scale at a specific position.
132  * @param pos the position in the color scale (0.0 <= pos <= 1.0)
133  * @param color the color to add at the specified position
134  *
135  */
136  virtual void setColorAtPos(const float pos, const Color &color);
137 
138  /**
139  * @brief Returns the color for a given position in the color scale.
140  *
141  * This method computes the color associated to a specific position in the color scale and returns
142  * it.
143  * @param pos This value defines the position of the color in the scale and must be between 0.0
144  * and 1.0 (it will be clamped otherwise)
145  * @return the color corresponding to the position in the scale
146  *
147  */
148  virtual Color getColorAtPos(const float pos) const;
149 
150  /**
151  * @brief Returns true is the color scale was initialized.
152  *
153  */
154  bool colorScaleInitialized() const {
155  return !colorMap.empty();
156  }
157 
158  /**
159  * @brief Returns a map corresponding to the color scale.
160  * The index of the map is the position for the corresponding color in the color scale. The index
161  * is comprised between 0 and 1.
162  *
163  */
164  std::map<float, Color> getColorMap() const {
165  return colorMap;
166  }
167 
168  /**
169  * @brief Sets the map of stop points and colors used to perform color mapping.
170  *
171  * @warning The keys of the map must be between 0.0 and 1.0, other values will be ignored.
172  *
173  */
174  void setColorMap(const std::map<float, Color> &colorMap);
175 
176  /**
177  * @brief Returns true if the color scale is a gradient.
178  *
179  */
180  bool isGradient() const {
181  return gradient;
182  }
183 
184  /**
185  * @brief specify whether the color scale must be considered as a gradient
186  */
187  void setGradient(const bool g) {
188  gradient = g;
189  }
190 
191  /**
192  * @brief Sets the transparency of all the colors in the underlying map.
193  *
194  */
195  void setColorMapTransparency(unsigned char transparency);
196 
197  /**
198  * @brief Tests color scale equality with another one.
199  *
200  */
201  bool operator==(const ColorScale &cs) const {
202  return (gradient == cs.gradient) && (colorMap == cs.colorMap);
203  }
204 
205  /**
206  * @brief Tests color scale equality with a regular one defined by a vector of colors.
207  *
208  */
209  bool operator==(const std::vector<Color> &colors) const;
210 
211  /**
212  * @brief Tests if the color scale has regular stop points, meaning the distance between each
213  * consecutive stop is constant.
214  *
215  * @since Tulip 4.10
216  *
217  */
218  bool hasRegularStops() const;
219 
220 protected:
221  std::map<float, Color> colorMap;
222  bool gradient;
223 };
224 }
225 
226 #endif /* COLORSCALE_H_ */
unsigned int getStopsCount()
Gets the number of stops points into the color scale.
Definition: ColorScale.h:110
void clear()
Clears the color scale.
Definition: ColorScale.h:102
This class represents a color scale to perform color mapping. The color scale can be either a gradien...
Definition: ColorScale.h:62
void setGradient(const bool g)
specify whether the color scale must be considered as a gradient
Definition: ColorScale.h:187
bool isGradient() const
Returns true if the color scale is a gradient.
Definition: ColorScale.h:180
bool colorScaleInitialized() const
Returns true is the color scale was initialized.
Definition: ColorScale.h:154
The Observable class is the base of Tulip&#39;s observation system.
Definition: Observable.h:141
std::map< float, Color > getColorMap() const
Returns a map corresponding to the color scale. The index of the map is the position for the correspo...
Definition: ColorScale.h:164
bool operator==(const ColorScale &cs) const
Tests color scale equality with another one.
Definition: ColorScale.h:201