bpp-core3  3.0.0
RgbColor.h
Go to the documentation of this file.
1 //
2 // File: RgbColor.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2006-03-16 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (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_RGBCOLOR_H
42 #define BPP_GRAPHICS_RGBCOLOR_H
43 
44 
45 
46 // From the STL:
47 #include <cmath>
48 
49 #include "../Text/TextTools.h"
50 #include "../Exceptions.h"
51 #include "../Clonable.h"
52 
53 namespace bpp
54 {
58 class RGBColor :
59  public virtual Clonable
60 {
61 protected:
62  unsigned int red_;
63  unsigned int green_;
64  unsigned int blue_;
65 
66 public:
67  RGBColor(unsigned int red, unsigned int green, unsigned int blue) : red_(red), green_(green), blue_(blue) {}
68  RGBColor() : red_(0), green_(0), blue_(0) {}
69  virtual ~RGBColor() {}
70 
71  RGBColor* clone() const { return new RGBColor(*this); }
72 
73 public:
74  bool operator==(const RGBColor& color) const
75  {
76  return red_ == color.red_ && green_ == color.green_ && blue_ == color.blue_;
77  }
78 
86  bool operator<(const RGBColor& color) const
87  {
88  return toHex() < color.toHex();
89  }
90 
94  std::string toHex() const
95  {
96  std::string hex = "#";
97  hex += decToHex(red_);
98  hex += decToHex(green_);
99  hex += decToHex(blue_);
100  return hex;
101  }
102 
106  const unsigned int& operator[](unsigned int i) const
107  {
108  if (i == 0) return red_;
109  if (i == 1) return green_;
110  if (i == 2) return blue_;
111  throw Exception("Invalid color index");
112  }
113 
117  unsigned int& operator[](unsigned int i)
118  {
119  if (i == 0) return red_;
120  if (i == 1) return green_;
121  if (i == 2) return blue_;
122  throw Exception("Invalid color index");
123  }
124 
128  std::string toString() const
129  {
130  return "[R" + TextTools::toString(red_) + ",G" + TextTools::toString(green_) + ",B" + TextTools::toString(blue_) + "]";
131  }
132 
133 protected:
134  static std::string decToHex(unsigned int dec)
135  {
136  std::string hexa = "0123456789ABCDEF";
137  std::string hex = "";
138  while (dec > 15)
139  {
140  unsigned int tmp = dec - static_cast<unsigned int>(floor(static_cast<double>(dec) / 16.) * 16);
141  hex = hexa[tmp] + hex;
142  dec = static_cast<unsigned int>(floor(static_cast<double>(dec) / 16.));
143  }
144  hex = hexa[dec] + hex;
145  if (hex.size() == 1) hex = "0" + hex;
146  return hex;
147  }
148 };
149 } // end of namespace bpp;
150 #endif // BPP_GRAPHICS_RGBCOLOR_H
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
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
static std::string decToHex(unsigned int dec)
Definition: RgbColor.h:134
const unsigned int & operator[](unsigned int i) const
Access to each color componant: 0=red, 1=green, 2=blue.
Definition: RgbColor.h:106
std::string toHex() const
Get the HTML-like, hexadecimal description of this color.
Definition: RgbColor.h:94
unsigned int blue_
Definition: RgbColor.h:64
bool operator<(const RGBColor &color) const
Comparison operator (for sorting purposes).
Definition: RgbColor.h:86
RGBColor * clone() const
Create a copy of this object and send a pointer to it.
Definition: RgbColor.h:71
bool operator==(const RGBColor &color) const
Definition: RgbColor.h:74
std::string toString() const
Get a string description of the color, e.g. [R255,G0,B255].
Definition: RgbColor.h:128
virtual ~RGBColor()
Definition: RgbColor.h:69
unsigned int green_
Definition: RgbColor.h:63
unsigned int & operator[](unsigned int i)
Access to each color componant: 0=red, 1=green, 2=blue.
Definition: RgbColor.h:117
unsigned int red_
Definition: RgbColor.h:62
RGBColor(unsigned int red, unsigned int green, unsigned int blue)
Definition: RgbColor.h:67
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153