Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ColourGradient.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __COLOUR_GRADIENT_HH__
26 #define __COLOUR_GRADIENT_HH__
27 
28 // STL
29 #include <vector>
30 #include <list>
31 
32 // BIAS
33 #include <bias_config.h>
34 #include <Base/Debug/Error.hh>
35 
36 namespace BIAS
37 {
38  /**
39  * @brief Represents a colour gradient.
40  *
41  * The ColourGradient is a function [0,1] -> YOUR_COLOUR_SPACE.
42  *
43  * The gradient will consist of at least two colours: one colour on the
44  * "left" side (at position 0) and one on the "right" side (at position 1).
45  * Intermediate colours can be added with AddColour().
46  *
47  * To get a linearly interpolated colour at a certain position call
48  * GetColour().
49  *
50  * \ingroup g_utils
51  * @author rwulff 04/2010
52  */
53  template <class StorageType>
54  class BIASUtils_EXPORT ColourGradient
55  {
56 
57  public:
58 
59  /**
60  * @brief Different types of predefined color gradients that can be generated.
61  *
62  * Inspired by Matlab.
63  */
65  {
68  Hot,
70  Jet
71  };
72 
73  /**
74  * @brief Default Constructor. Initialises with gray-scale gradient from
75  * black to white, assuming 3 channels and unsigned char.
76  */
78 
79  /**
80  * @brief Constructor.
81  *
82  * @note Both colour vectors may not be empty and must have the same size.
83  *
84  * @param colourLeft
85  * the colour at position 0
86  * @param colourRight
87  * the colour at position 1
88  */
89  ColourGradient(std::vector<StorageType> colourLeft,
90  std::vector<StorageType> colourRight);
91 
92  /**
93  * Constructor that allows to generate from a predefined color map.
94  * The color maximas are set to max e.g. blue is (0 0 max)
95  */
96  ColourGradient(ColourGradientType mapType, const StorageType max = 255);
97 
98  /**
99  * @brief Adds a colour to this ColourGradient.
100  *
101  * If a colour already exists at the given pos, it is replaced by the new
102  * value.
103  *
104  * @param pos
105  * the position where the new colour is to be set, must be in [0,1]
106  * @param colour
107  * the new colour to set, must have the same size as the colours
108  * given to the constructor
109  */
110  void AddColour(float pos, const std::vector<StorageType> &colour);
111 
112  /**
113  * @brief Convenience wrapper
114  */
115  void AddRGBColour(float pos,
116  const StorageType r,
117  const StorageType g,
118  const StorageType b);
119 
120  /**
121  * @brief Returns the interpolated colour at a given position.
122  *
123  * The colour is interpolated linearly between the nearest colours that were
124  * added before.
125  *
126  * @param pos
127  * the position for which the interpolated colour should be returned,
128  * must be in [0,1]
129  * @param colour
130  * the returned interpolated colour
131  */
132  void GetColour(float pos, std::vector<StorageType> &colour);
133 
134  private:
135 
136  /**
137  * struct to hold a single step of the colour gradient
138  */
139  template <class StructStorageType>
140  struct GradientStep
141  {
142  float pos;
143  std::vector<StructStorageType> colour;
144  };
145 
146  // to ensure correct channel count
147  unsigned int numChannels_;
148 
149  // sorted list of the colours
150  std::list<GradientStep<StorageType> > gradientSteps_;
151 
152  }; // end of class
153 } // end of namespace
154 
155 #endif // __COLOUR_GRADIENT_HH__
Represents a colour gradient.
ColourGradientType
Different types of predefined color gradients that can be generated.