Color.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_COLOR_HH_
18 #define IGNITION_MATH_COLOR_HH_
19 
20 #include <iostream>
21 
22 #include <ignition/math/Helpers.hh>
23 #include <ignition/math/Vector3.hh>
24 #include <ignition/math/config.hh>
25 
26 namespace ignition
27 {
28  namespace math
29  {
30  inline namespace IGNITION_MATH_VERSION_NAMESPACE
31  {
35  class IGNITION_MATH_VISIBLE Color
36  {
38  public: static const Color White;
40  public: static const Color Black;
42  public: static const Color Red;
44  public: static const Color Green;
46  public: static const Color Blue;
48  public: static const Color Yellow;
50  public: static const Color Magenta;
52  public: static const Color Cyan;
53 
56  public: typedef unsigned int RGBA;
57 
60  public: typedef unsigned int BGRA;
61 
64  public: typedef unsigned int ARGB;
65 
68  public: typedef unsigned int ABGR;
69 
71  public: Color();
72 
78  public: Color(const float _r, const float _g, const float _b,
79  const float _a = 1.0);
80 
83  public: Color(const Color &_clr);
84 
86  public: virtual ~Color();
87 
90  public: void Reset();
91 
97  public: void Set(const float _r = 1, const float _g = 1,
98  const float _b = 1, const float _a = 1);
99 
103  public: Vector3f HSV() const;
104 
109  public: void SetFromHSV(const float _h, const float _s, const float _v);
110 
113  public: Vector3f YUV() const;
114 
119  public: void SetFromYUV(const float _y, const float _u, const float _v);
120 
124  public: Color &operator=(const Color &_pt);
125 
130  public: float operator[](const unsigned int _index);
131 
134  public: RGBA AsRGBA() const;
135 
138  public: BGRA AsBGRA() const;
139 
142  public: ARGB AsARGB() const;
143 
146  public: ABGR AsABGR() const;
147 
150  public: void SetFromRGBA(const RGBA _v);
151 
154  public: void SetFromBGRA(const BGRA _v);
155 
158  public: void SetFromARGB(const ARGB _v);
159 
162  public: void SetFromABGR(const ABGR _v);
163 
167  public: Color operator+(const Color &_pt) const;
168 
172  public: Color operator+(const float &_v) const;
173 
177  public: const Color &operator+=(const Color &_pt);
178 
182  public: Color operator-(const Color &_pt) const;
183 
187  public: Color operator-(const float &_v) const;
188 
192  public: const Color &operator-=(const Color &_pt);
193 
197  public: const Color operator/(const Color &_pt) const;
198 
202  public: const Color operator/(const float &_v) const;
203 
207  public: const Color &operator/=(const Color &_pt);
208 
212  public: const Color operator*(const Color &_pt) const;
213 
217  public: const Color operator*(const float &_v) const;
218 
222  public: const Color &operator*=(const Color &_pt);
223 
227  public: bool operator==(const Color &_pt) const;
228 
232  public: bool operator!=(const Color &_pt) const;
233 
235  private: void Clamp();
236 
241  public: friend std::ostream &operator<<(std::ostream &_out,
242  const Color &_pt)
243  {
244  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
245  return _out;
246  }
247 
251  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
252  {
253  // Skip white spaces
254  _in.setf(std::ios_base::skipws);
255  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
256  return _in;
257  }
258 
261  public: float R() const;
262 
265  public: float G() const;
266 
269  public: float B() const;
270 
273  public: float A() const;
274 
277  public: float &R();
278 
281  public: float &G();
282 
285  public: float &B();
286 
289  public: float &A();
290 
293  public: void R(const float _r);
294 
297  public: void G(const float _g);
298 
301  public: void B(const float _b);
302 
305  public: void A(const float _a);
306 
308  private: float r = 0;
309 
311  private: float g = 0;
312 
314  private: float b = 0;
315 
317  private: float a = 1;
318  };
319  }
320  }
321 }
322 #endif
Helpers.hh
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Blue
static const Color Blue
(0, 0, 1)
Definition: Color.hh:46
ignition
Definition: Angle.hh:39
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::White
static const Color White
(1, 1, 1)
Definition: Color.hh:38
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Cyan
static const Color Cyan
(0, 1, 1)
Definition: Color.hh:52
Vector3.hh
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::operator<<
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Color.hh:241
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Red
static const Color Red
(1, 0, 0)
Definition: Color.hh:42
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color
Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component i...
Definition: Color.hh:35
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Yellow
static const Color Yellow
(1, 1, 0)
Definition: Color.hh:48
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Black
static const Color Black
(0, 0, 0)
Definition: Color.hh:40
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Magenta
static const Color Magenta
(1, 0, 1)
Definition: Color.hh:50
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Vector3
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Vector3.hh:39
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::BGRA
unsigned int BGRA
Definition: Color.hh:60
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::RGBA
unsigned int RGBA
Definition: Color.hh:56
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::ABGR
unsigned int ABGR
Definition: Color.hh:68
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::Green
static const Color Green
(0, 1, 0)
Definition: Color.hh:44
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::Color::ARGB
unsigned int ARGB
Definition: Color.hh:64