bpp-core3  3.0.0
ColorSet.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_GRAPHICS_COLORSET_H
6 #define BPP_GRAPHICS_COLORSET_H
7 
8 
9 #include "../Utils/MapTools.h"
10 #include "RgbColor.h"
11 
12 // From the STL:
13 #include <vector>
14 
15 namespace bpp
16 {
20 class ColorSet
21 {
22 public:
23  ColorSet() {}
24  virtual ~ColorSet() {}
25 
26 public:
34  virtual const RGBColor& getColor(const std::string& name) const = 0;
35 
43  virtual const RGBColor& getColor(unsigned int index) const = 0;
44 
48  virtual std::vector<std::string> getColorNames() const = 0;
49 
53  virtual size_t getNumberOfColors() const = 0;
54 };
55 
56 
63  public ColorSet
64 {
65 protected:
66  std::map<std::string, RGBColor> colors_;
67 
68 public:
69  AbstractColorSet() : colors_() {}
70  virtual ~AbstractColorSet() {}
71 
72 public:
73  const RGBColor& getColor(const std::string& name) const
74  {
75  std::map<std::string, RGBColor>::const_iterator it = colors_.find(name);
76  if (it != colors_.end()) return it->second;
77  else throw Exception("AbstractColorSet::getColor(name): no color with name " + name);
78  }
79 
80  const RGBColor& getColor(unsigned int index) const
81  {
82  if (index >= colors_.size()) throw IndexOutOfBoundsException("AbstractColorSet::getColor(index): invalid index.", index, 0, colors_.size() - 1);
83  std::map<std::string, RGBColor>::const_iterator it = colors_.begin();
84  for (unsigned int i = 0; i < index; i++) { it++;}
85  return it->second;
86  }
87 
88  std::vector<std::string> getColorNames() const { return MapTools::getKeys(colors_); }
89 
90  size_t getNumberOfColors() const { return colors_.size(); }
91 };
92 } // end of namespace bpp;
93 #endif // BPP_GRAPHICS_COLORSET_H
virtual ~ColorSet()
Definition: ColorSet.h:24
std::map< std::string, RGBColor > colors_
Definition: ColorSet.h:66
Partial implementation of the ColorSet interface.
Definition: ColorSet.h:62
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:21
const RGBColor & getColor(unsigned int index) const
Get the ith color object in the set.
Definition: ColorSet.h:80
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
Get a vector of all keys in a map.
Definition: MapTools.h:27
Specify a set of color definitions.
Definition: ColorSet.h:20
virtual size_t getNumberOfColors() const =0
const RGBColor & getColor(const std::string &name) const
Get the color object corresponding to a given name.
Definition: ColorSet.h:73
virtual std::vector< std::string > getColorNames() const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
size_t getNumberOfColors() const
Definition: ColorSet.h:90
virtual ~AbstractColorSet()
Definition: ColorSet.h:70
std::vector< std::string > getColorNames() const
Definition: ColorSet.h:88
virtual const RGBColor & getColor(const std::string &name) const =0
Get the color object corresponding to a given name.
Index out of bounds exception class.
Definition: Exceptions.h:131