bpp-core3  3.0.0
ColorManager.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_COLORMANAGER_H
6 #define BPP_GRAPHICS_COLORMANAGER_H
7 
8 
9 #include "../Text/TextTools.h"
10 #include "ColorTools.h"
11 #include "RgbColor.h"
12 
13 // From the STL:
14 #include <vector>
15 
16 namespace bpp
17 {
23 template<class CodeType>
25 {
26 public:
28  virtual ~ColorManager() {}
29 
30 public:
35  virtual CodeType getCode(const RGBColor& color) = 0;
36 
42  virtual const RGBColor& getColor(CodeType& code) const = 0;
43 
47  virtual const std::vector<CodeType>& getCodes() const = 0;
48 
52  virtual const std::vector<RGBColor>& getColors() const = 0;
53 
57  virtual size_t getNumberOfColors() const = 0;
58 };
59 
60 
68  public ColorManager<unsigned int>
69 {
70 protected:
71  unsigned int currentCode_;
72  std::vector<RGBColor> colors_;
73  std::vector<unsigned int> codes_;
74 
75 public:
77  currentCode_(31),
78  colors_(),
79  codes_()
80  {
81  // Add "official" color codes, from 0 to 31:
82  codes_.push_back(0); colors_.push_back(ColorTools::BLACK);
83  codes_.push_back(1); colors_.push_back(ColorTools::BLUE);
84  codes_.push_back(2); colors_.push_back(ColorTools::GREEN);
85  codes_.push_back(3); colors_.push_back(ColorTools::CYAN);
86  codes_.push_back(4); colors_.push_back(ColorTools::RED);
87  codes_.push_back(5); colors_.push_back(ColorTools::MAGENTA);
88  codes_.push_back(6); colors_.push_back(ColorTools::YELLOW);
89  codes_.push_back(7); colors_.push_back(ColorTools::WHITE);
90  codes_.push_back(8); colors_.push_back(RGBColor(0, 0, 140));
91  codes_.push_back(9); colors_.push_back(RGBColor(0, 0, 173));
92  codes_.push_back(10); colors_.push_back(RGBColor(0, 0, 206));
93  codes_.push_back(11); colors_.push_back(RGBColor(132, 207, 205));
94  codes_.push_back(12); colors_.push_back(RGBColor(0, 142, 0));
95  codes_.push_back(13); colors_.push_back(RGBColor(0, 174, 0));
96  codes_.push_back(14); colors_.push_back(RGBColor(0, 207, 0));
97  codes_.push_back(15); colors_.push_back(RGBColor(0, 142, 140));
98  codes_.push_back(16); colors_.push_back(RGBColor(0, 174, 173));
99  codes_.push_back(17); colors_.push_back(RGBColor(0, 207, 206));
100  codes_.push_back(18); colors_.push_back(RGBColor(140, 0, 0));
101  codes_.push_back(19); colors_.push_back(RGBColor(173, 0, 0));
102  codes_.push_back(20); colors_.push_back(RGBColor(206, 0, 0));
103  codes_.push_back(21); colors_.push_back(RGBColor(140, 0, 140));
104  codes_.push_back(22); colors_.push_back(RGBColor(173, 0, 173));
105  codes_.push_back(23); colors_.push_back(RGBColor(206, 0, 206));
106  codes_.push_back(24); colors_.push_back(RGBColor(132, 48, 0));
107  codes_.push_back(25); colors_.push_back(RGBColor(156, 65, 0));
108  codes_.push_back(26); colors_.push_back(RGBColor(189, 97, 0));
109  codes_.push_back(27); colors_.push_back(RGBColor(255, 130, 132));
110  codes_.push_back(28); colors_.push_back(RGBColor(255, 158, 156));
111  codes_.push_back(29); colors_.push_back(RGBColor(255, 190, 189));
112  codes_.push_back(30); colors_.push_back(RGBColor(255, 223, 222));
113  codes_.push_back(31); colors_.push_back(RGBColor(255, 215, 0));
114  }
115  virtual ~XFigColorManager() {}
116 
117 public:
118  unsigned int getCode(const RGBColor& color)
119  {
120  for (unsigned int i = 0; i < colors_.size(); i++)
121  {
122  if (colors_[i] == color)
123  {
124  return codes_[i];
125  }
126  }
127  currentCode_++;
128  colors_.push_back(color);
129  codes_.push_back(currentCode_);
130  return currentCode_;
131  }
132  const RGBColor& getColor(unsigned int& code) const
133  {
134  for (unsigned int i = 0; i < codes_.size(); i++)
135  {
136  if (codes_[i] == code)
137  {
138  return colors_[i];
139  }
140  }
141  throw Exception("XFigColorManager::getColor. No color associated to this code: " + TextTools::toString(code));
142  }
143  const std::vector<unsigned int>& getCodes() const { return codes_; }
144  const std::vector<RGBColor>& getColors() const { return colors_; }
145  size_t getNumberOfColors() const { return colors_.size(); }
146 };
147 } // end of namespace bpp;
148 #endif // BPP_GRAPHICS_COLORMANAGER_H
virtual const std::vector< RGBColor > & getColors() const =0
static const RGBColor CYAN
Definition: ColorTools.h:89
unsigned int currentCode_
Definition: ColorManager.h:71
virtual ~ColorManager()
Definition: ColorManager.h:28
static const RGBColor BLUE
Definition: ColorTools.h:85
static const RGBColor RED
Definition: ColorTools.h:83
static const RGBColor YELLOW
Definition: ColorTools.h:88
static const RGBColor BLACK
Definition: ColorTools.h:86
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:21
const std::vector< unsigned int > & getCodes() const
Definition: ColorManager.h:143
static const RGBColor GREEN
Definition: ColorTools.h:84
virtual size_t getNumberOfColors() const =0
Associate special colors to a code.
Definition: ColorManager.h:24
const std::vector< RGBColor > & getColors() const
Definition: ColorManager.h:144
Color manager for the XFig format.
Definition: ColorManager.h:67
const RGBColor & getColor(unsigned int &code) const
Definition: ColorManager.h:132
std::vector< unsigned int > codes_
Definition: ColorManager.h:73
unsigned int getCode(const RGBColor &color)
Definition: ColorManager.h:118
virtual const RGBColor & getColor(CodeType &code) const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
virtual CodeType getCode(const RGBColor &color)=0
virtual const std::vector< CodeType > & getCodes() const =0
std::vector< RGBColor > colors_
Definition: ColorManager.h:72
size_t getNumberOfColors() const
Definition: ColorManager.h:145
static const RGBColor MAGENTA
Definition: ColorTools.h:90
static const RGBColor WHITE
Definition: ColorTools.h:87
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
virtual ~XFigColorManager()
Definition: ColorManager.h:115