Tulip  4.9.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
ColorScale.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
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 ///@cond DOXYGEN_HIDDEN
20 
21 
22 #ifndef COLORSCALE_H_
23 #define COLORSCALE_H_
24 
25 #include <tulip/Observable.h>
26 #include <tulip/Color.h>
27 
28 #include <vector>
29 #include <map>
30 
31 namespace tlp {
32 
33 /**
34  * @brief This class represents a color scale to perform color mapping.
35  * The color scale can be either gradient or predefined colors steps.
36  * If the color scale is a gradient, returned colors are interpolated in function of the position.
37  * If the color scale isn't a gradient returned colors are the predefined colors steps.
38  * @code
39  * \// Creating the color scale.
40  * ColorScale colorScale;
41  * \// Color scale initialization : from blue to red with gradient.
42  * colorScale.setColorAtPos(0.0, Color(0,0,255));
43  * colorScale.setColorAtPos(1.0, Color(255,0,0));
44  *
45  * \// Get the color for the position 0.5, i.e. Color(127,0,127).
46  * colorScale.getColorAtPos(0.5);
47  * \// Reinitialize the color scale : from blue to red without gradient.
48  * vector<color> newColors;
49  * newColors.push_back(Color(0,0,255));
50  * newColors.push_back(Color(255,0,0));
51  * colorScale.setColorScale(newColors,false);
52  * \// Get the color for the position 0.3, i.e. Color(0,0,255).
53  * colorScale.getColorAtPos(0.3);
54  * \// Get the color for the position 0.7, i.e. Color(255,0,0).
55  * colorScale.getColorAtPos(0.7);
56  * @endcode
57  */
58 class TLP_SCOPE ColorScale : public Observable {
59 
60 public:
61 
62  /**
63  * Initializes a color scale with a default set of colors.
64  * @param gradient Specify if the color scale should be a gradient or not.
65  */
66  ColorScale(const bool gradient = true);
67 
68  /**
69  * Initializes a color scale with a set of colors passed as parameter
70  * @param colors a vector of colors defining the color scale (first color is at position 0, last color at position 1)
71  * @param gradient Specify if the color scale should be a gradient or not.
72  */
73  ColorScale(const std::vector<Color> &colors, const bool gradient = true);
74 
75 
76  ColorScale(const ColorScale& scale);
77  ColorScale& operator=(const ColorScale& scale);
78  virtual ~ColorScale();
79 
80  /**
81  @brief Gets the number of stops into the color scale.
82  */
83  unsigned int getStopsCount() {
84  return colorMap.size();
85  }
86 
87  /**
88  * @brief Configures the color scale.
89  * This method configures the color scale.
90  * If the scale was already configured the previous configuration is lost.
91  * @param colors The colors to use in the color scale.
92  * @param gradient If set to true, color scale is a gradient
93  */
94  virtual void setColorScale(const std::vector<Color> colors, const bool gradient = true);
95 
96  /**
97  * @brief Adds a color to the color scale at specific position.
98  * This method adds a color to the color scale at a specific position
99  * @param pos the position in the color scale (0.0 <= pos <= 1.0)
100  * @param color the color to add at the specified position
101  */
102  virtual void setColorAtPos(const float pos, const Color &color);
103 
104  /**
105  * @brief Returns the color for a given position in the color scale.
106  * This method computes the color associated to a specific position in the color scale and returns it.
107  * @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).
108  * @return The color corresponding to the position in the scale.
109  */
110  virtual Color getColorAtPos(const float pos) const;
111 
112  /**
113  * @brief Returns true is the color scale was initialized.
114  */
115  bool colorScaleInitialized() const {
116  return !colorMap.empty();
117  }
118  /**
119  * @brief Returns a map corresponding to the color scale.
120  * The index of the map is the position for the corresponding color in the color scale. The index is comprised between 0 and 1.
121  */
122  std::map<float, Color> getColorMap() const {
123  return colorMap;
124  }
125  /**
126  * @brief Set the map to configure position to color mapping.
127  * The values in the map must be between 0.0 and 1.0, other values will be ignored.
128  *
129  **/
130  void setColorMap(const std::map<float, Color>& colorMap);
131  /**
132  * @brief Returns true if the color scale is a gradient.
133  */
134  bool isGradient() const {
135  return gradient;
136  }
137  /**
138  * @brief Set the transparency of all the colors in the map
139  *
140  **/
141  void setColorMapTransparency(unsigned char transparency);
142  /**
143  * @brief test ColorScale equality
144  *
145  **/
146  bool operator==(const ColorScale& cs) const {
147  return (gradient == cs.gradient) && (colorMap == cs.colorMap);
148  }
149  /**
150  * @brief test ColorScale equality
151  *
152  **/
153  bool operator==(const std::vector<Color> &colors) const;
154 
155 protected:
156  std::map<float, Color> colorMap;
157  bool gradient;
158 };
159 
160 }
161 
162 #endif /* COLORSCALE_H_ */
163 ///@endcond