Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces 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 1 and Inria Bordeaux - Sud Ouest
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. If the scale was previously configured the old configuration is lost.
90  * @param colors The colors to use in the color scale.
91  * @param gradient If set to true, color scale is a gradient
92  */
93  virtual void setColorScale(const std::vector<Color> colors, const bool gradient = true);
94 
95  /**
96  * @brief Adds a color to the color scale at specific position.
97  * This method adds a color to the color scale at a specific position
98  * @param pos the position in the color scale (0.0 <= pos <= 1.0)
99  * @param color the color to add at the specified position
100  */
101  virtual void setColorAtPos(const float pos, const Color &color);
102 
103  /**
104  * @brief Returns the color for a given position in the color scale.
105  * This method computes the color associated to a specific position in the color scale and returns it.
106  * @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).
107  * @return The color corresponding to the position in the scale.
108  */
109  virtual Color getColorAtPos(const float pos) const;
110 
111  /**
112  * @brief Returns true is the color scale was initialized.
113  */
114  bool colorScaleInitialized() const {
115  return colorScaleSet;
116  }
117  /**
118  * @brief Returns a map corresponding to the color scale.
119  * The index of the map is the position for the corresponding color in the color scale. The index is comprised between 0 and 1.
120  */
121  std::map<float, Color> getColorMap() const {
122  return colorMap;
123  }
124  /**
125  * @brief Set the map to configure position to color mapping.
126  * The values in the map must be between 0.0 and 1.0, other values will be ignored.
127  *
128  **/
129  void setColorMap(const std::map<float, Color>& colorMap);
130  /**
131  * @brief Returns true if the color scale is a gradient.
132  */
133  bool isGradient() const {
134  return gradient;
135  }
136 
137 protected:
138 
139  std::map<float, Color> colorMap;
140  bool gradient;
141  bool colorScaleSet;
142 
143 };
144 
145 }
146 
147 #endif /* COLORSCALE_H_ */
148 ///@endcond