bpp-core3  3.0.0
RgbColor.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_RGBCOLOR_H
6 #define BPP_GRAPHICS_RGBCOLOR_H
7 
8 
9 // From the STL:
10 #include <cmath>
11 
12 #include "../Text/TextTools.h"
13 #include "../Exceptions.h"
14 #include "../Clonable.h"
15 
16 namespace bpp
17 {
21 class RGBColor :
22  public virtual Clonable
23 {
24 protected:
25  unsigned int red_;
26  unsigned int green_;
27  unsigned int blue_;
28 
29 public:
30  RGBColor(unsigned int red, unsigned int green, unsigned int blue) : red_(red), green_(green), blue_(blue) {}
31  RGBColor() : red_(0), green_(0), blue_(0) {}
32  virtual ~RGBColor() {}
33 
34  RGBColor* clone() const { return new RGBColor(*this); }
35 
36 public:
37  bool operator==(const RGBColor& color) const
38  {
39  return red_ == color.red_ && green_ == color.green_ && blue_ == color.blue_;
40  }
41 
49  bool operator<(const RGBColor& color) const
50  {
51  return toHex() < color.toHex();
52  }
53 
57  std::string toHex() const
58  {
59  std::string hex = "#";
60  hex += decToHex(red_);
61  hex += decToHex(green_);
62  hex += decToHex(blue_);
63  return hex;
64  }
65 
69  const unsigned int& operator[](unsigned int i) const
70  {
71  if (i == 0) return red_;
72  if (i == 1) return green_;
73  if (i == 2) return blue_;
74  throw Exception("Invalid color index");
75  }
76 
80  unsigned int& operator[](unsigned int i)
81  {
82  if (i == 0) return red_;
83  if (i == 1) return green_;
84  if (i == 2) return blue_;
85  throw Exception("Invalid color index");
86  }
87 
91  std::string toString() const
92  {
93  return "[R" + TextTools::toString(red_) + ",G" + TextTools::toString(green_) + ",B" + TextTools::toString(blue_) + "]";
94  }
95 
96 protected:
97  static std::string decToHex(unsigned int dec)
98  {
99  std::string hexa = "0123456789ABCDEF";
100  std::string hex = "";
101  while (dec > 15)
102  {
103  unsigned int tmp = dec - static_cast<unsigned int>(floor(static_cast<double>(dec) / 16.) * 16);
104  hex = hexa[tmp] + hex;
105  dec = static_cast<unsigned int>(floor(static_cast<double>(dec) / 16.));
106  }
107  hex = hexa[dec] + hex;
108  if (hex.size() == 1) hex = "0" + hex;
109  return hex;
110  }
111 };
112 } // end of namespace bpp;
113 #endif // BPP_GRAPHICS_RGBCOLOR_H
std::string toString() const
Get a string description of the color, e.g. [R255,G0,B255].
Definition: RgbColor.h:91
bool operator<(const RGBColor &color) const
Comparison operator (for sorting purposes).
Definition: RgbColor.h:49
std::string toHex() const
Get the HTML-like, hexadecimal description of this color.
Definition: RgbColor.h:57
static std::string decToHex(unsigned int dec)
Definition: RgbColor.h:97
bool operator==(const RGBColor &color) const
Definition: RgbColor.h:37
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:21
const unsigned int & operator[](unsigned int i) const
Access to each color componant: 0=red, 1=green, 2=blue.
Definition: RgbColor.h:69
unsigned int blue_
Definition: RgbColor.h:27
unsigned int green_
Definition: RgbColor.h:26
RGBColor(unsigned int red, unsigned int green, unsigned int blue)
Definition: RgbColor.h:30
RGBColor * clone() const
Create a copy of this object and send a pointer to it.
Definition: RgbColor.h:34
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
unsigned int red_
Definition: RgbColor.h:25
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
unsigned int & operator[](unsigned int i)
Access to each color componant: 0=red, 1=green, 2=blue.
Definition: RgbColor.h:80
virtual ~RGBColor()
Definition: RgbColor.h:32