bpp-core3  3.0.0
ColorManager.h
Go to the documentation of this file.
1 //
2 // File: ColorManager.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2006-05-08 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (November 17, 2004)
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_COLORMANAGER_H
42 #define BPP_GRAPHICS_COLORMANAGER_H
43 
44 
45 #include "../Text/TextTools.h"
46 #include "ColorTools.h"
47 #include "RgbColor.h"
48 
49 // From the STL:
50 #include <vector>
51 
52 namespace bpp
53 {
59 template<class CodeType>
61 {
62 public:
64  virtual ~ColorManager() {}
65 
66 public:
71  virtual CodeType getCode(const RGBColor& color) = 0;
72 
78  virtual const RGBColor& getColor(CodeType& code) const = 0;
79 
83  virtual const std::vector<CodeType>& getCodes() const = 0;
84 
88  virtual const std::vector<RGBColor>& getColors() const = 0;
89 
93  virtual size_t getNumberOfColors() const = 0;
94 };
95 
96 
104  public ColorManager<unsigned int>
105 {
106 protected:
107  unsigned int currentCode_;
108  std::vector<RGBColor> colors_;
109  std::vector<unsigned int> codes_;
110 
111 public:
113  currentCode_(31),
114  colors_(),
115  codes_()
116  {
117  // Add "official" color codes, from 0 to 31:
118  codes_.push_back(0); colors_.push_back(ColorTools::BLACK);
119  codes_.push_back(1); colors_.push_back(ColorTools::BLUE);
120  codes_.push_back(2); colors_.push_back(ColorTools::GREEN);
121  codes_.push_back(3); colors_.push_back(ColorTools::CYAN);
122  codes_.push_back(4); colors_.push_back(ColorTools::RED);
123  codes_.push_back(5); colors_.push_back(ColorTools::MAGENTA);
124  codes_.push_back(6); colors_.push_back(ColorTools::YELLOW);
125  codes_.push_back(7); colors_.push_back(ColorTools::WHITE);
126  codes_.push_back(8); colors_.push_back(RGBColor(0, 0, 140));
127  codes_.push_back(9); colors_.push_back(RGBColor(0, 0, 173));
128  codes_.push_back(10); colors_.push_back(RGBColor(0, 0, 206));
129  codes_.push_back(11); colors_.push_back(RGBColor(132, 207, 205));
130  codes_.push_back(12); colors_.push_back(RGBColor(0, 142, 0));
131  codes_.push_back(13); colors_.push_back(RGBColor(0, 174, 0));
132  codes_.push_back(14); colors_.push_back(RGBColor(0, 207, 0));
133  codes_.push_back(15); colors_.push_back(RGBColor(0, 142, 140));
134  codes_.push_back(16); colors_.push_back(RGBColor(0, 174, 173));
135  codes_.push_back(17); colors_.push_back(RGBColor(0, 207, 206));
136  codes_.push_back(18); colors_.push_back(RGBColor(140, 0, 0));
137  codes_.push_back(19); colors_.push_back(RGBColor(173, 0, 0));
138  codes_.push_back(20); colors_.push_back(RGBColor(206, 0, 0));
139  codes_.push_back(21); colors_.push_back(RGBColor(140, 0, 140));
140  codes_.push_back(22); colors_.push_back(RGBColor(173, 0, 173));
141  codes_.push_back(23); colors_.push_back(RGBColor(206, 0, 206));
142  codes_.push_back(24); colors_.push_back(RGBColor(132, 48, 0));
143  codes_.push_back(25); colors_.push_back(RGBColor(156, 65, 0));
144  codes_.push_back(26); colors_.push_back(RGBColor(189, 97, 0));
145  codes_.push_back(27); colors_.push_back(RGBColor(255, 130, 132));
146  codes_.push_back(28); colors_.push_back(RGBColor(255, 158, 156));
147  codes_.push_back(29); colors_.push_back(RGBColor(255, 190, 189));
148  codes_.push_back(30); colors_.push_back(RGBColor(255, 223, 222));
149  codes_.push_back(31); colors_.push_back(RGBColor(255, 215, 0));
150  }
151  virtual ~XFigColorManager() {}
152 
153 public:
154  unsigned int getCode(const RGBColor& color)
155  {
156  for (unsigned int i = 0; i < colors_.size(); i++)
157  {
158  if (colors_[i] == color)
159  {
160  return codes_[i];
161  }
162  }
163  currentCode_++;
164  colors_.push_back(color);
165  codes_.push_back(currentCode_);
166  return currentCode_;
167  }
168  const RGBColor& getColor(unsigned int& code) const
169  {
170  for (unsigned int i = 0; i < codes_.size(); i++)
171  {
172  if (codes_[i] == code)
173  {
174  return colors_[i];
175  }
176  }
177  throw Exception("XFigColorManager::getColor. No color associated to this code: " + TextTools::toString(code));
178  }
179  const std::vector<unsigned int>& getCodes() const { return codes_; }
180  const std::vector<RGBColor>& getColors() const { return colors_; }
181  size_t getNumberOfColors() const { return colors_.size(); }
182 };
183 } // end of namespace bpp;
184 #endif // BPP_GRAPHICS_COLORMANAGER_H
Associate special colors to a code.
Definition: ColorManager.h:61
virtual const std::vector< CodeType > & getCodes() const =0
virtual ~ColorManager()
Definition: ColorManager.h:64
virtual size_t getNumberOfColors() const =0
virtual const RGBColor & getColor(CodeType &code) const =0
virtual const std::vector< RGBColor > & getColors() const =0
virtual CodeType getCode(const RGBColor &color)=0
static const RGBColor BLACK
Definition: ColorTools.h:122
static const RGBColor CYAN
Definition: ColorTools.h:125
static const RGBColor WHITE
Definition: ColorTools.h:123
static const RGBColor YELLOW
Definition: ColorTools.h:124
static const RGBColor MAGENTA
Definition: ColorTools.h:126
static const RGBColor RED
Definition: ColorTools.h:119
static const RGBColor BLUE
Definition: ColorTools.h:121
static const RGBColor GREEN
Definition: ColorTools.h:120
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:60
Color manager for the XFig format.
Definition: ColorManager.h:105
const std::vector< unsigned int > & getCodes() const
Definition: ColorManager.h:179
const std::vector< RGBColor > & getColors() const
Definition: ColorManager.h:180
const RGBColor & getColor(unsigned int &code) const
Definition: ColorManager.h:168
std::vector< unsigned int > codes_
Definition: ColorManager.h:109
std::vector< RGBColor > colors_
Definition: ColorManager.h:108
virtual ~XFigColorManager()
Definition: ColorManager.h:151
unsigned int currentCode_
Definition: ColorManager.h:107
size_t getNumberOfColors() const
Definition: ColorManager.h:181
unsigned int getCode(const RGBColor &color)
Definition: ColorManager.h:154
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153