bpp-core3  3.0.0
ColorSet.h
Go to the documentation of this file.
1 //
2 // File: ColorSet.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2008-04-14 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (November 17, 2008)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_GRAPHICS_COLORSET_H
42 #define BPP_GRAPHICS_COLORSET_H
43 
44 
45 #include "../Utils/MapTools.h"
46 #include "RgbColor.h"
47 
48 // From the STL:
49 #include <vector>
50 
51 namespace bpp
52 {
56 class ColorSet
57 {
58 public:
59  ColorSet() {}
60  virtual ~ColorSet() {}
61 
62 public:
70  virtual const RGBColor& getColor(const std::string& name) const = 0;
71 
79  virtual const RGBColor& getColor(unsigned int index) const = 0;
80 
84  virtual std::vector<std::string> getColorNames() const = 0;
85 
89  virtual size_t getNumberOfColors() const = 0;
90 };
91 
92 
99  public ColorSet
100 {
101 protected:
102  std::map<std::string, RGBColor> colors_;
103 
104 public:
106  virtual ~AbstractColorSet() {}
107 
108 public:
109  const RGBColor& getColor(const std::string& name) const
110  {
111  std::map<std::string, RGBColor>::const_iterator it = colors_.find(name);
112  if (it != colors_.end()) return it->second;
113  else throw Exception("AbstractColorSet::getColor(name): no color with name " + name);
114  }
115 
116  const RGBColor& getColor(unsigned int index) const
117  {
118  if (index >= colors_.size()) throw IndexOutOfBoundsException("AbstractColorSet::getColor(index): invalid index.", index, 0, colors_.size() - 1);
119  std::map<std::string, RGBColor>::const_iterator it = colors_.begin();
120  for (unsigned int i = 0; i < index; i++) { it++;}
121  return it->second;
122  }
123 
124  std::vector<std::string> getColorNames() const { return MapTools::getKeys(colors_); }
125 
126  size_t getNumberOfColors() const { return colors_.size(); }
127 };
128 } // end of namespace bpp;
129 #endif // BPP_GRAPHICS_COLORSET_H
Partial implementation of the ColorSet interface.
Definition: ColorSet.h:100
const RGBColor & getColor(const std::string &name) const
Get the color object corresponding to a given name.
Definition: ColorSet.h:109
std::map< std::string, RGBColor > colors_
Definition: ColorSet.h:102
const RGBColor & getColor(unsigned int index) const
Get the ith color object in the set.
Definition: ColorSet.h:116
size_t getNumberOfColors() const
Definition: ColorSet.h:126
virtual ~AbstractColorSet()
Definition: ColorSet.h:106
std::vector< std::string > getColorNames() const
Definition: ColorSet.h:124
Specify a set of color definitions.
Definition: ColorSet.h:57
virtual const RGBColor & getColor(const std::string &name) const =0
Get the color object corresponding to a given name.
virtual const RGBColor & getColor(unsigned int index) const =0
Get the ith color object in the set.
virtual size_t getNumberOfColors() const =0
virtual std::vector< std::string > getColorNames() const =0
virtual ~ColorSet()
Definition: ColorSet.h:60
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Index out of bounds exception class.
Definition: Exceptions.h:170
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
Get a vector of all keys in a map.
Definition: MapTools.h:63
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:60