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