Ignition Math

API Reference

6.8.0
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 #include <cctype>
22 
23 #include <ignition/math/Helpers.hh>
24 #include <ignition/math/Vector3.hh>
25 #include <ignition/math/config.hh>
26 
27 namespace ignition
28 {
29  namespace math
30  {
31  // Inline bracket to help doxygen filtering.
32  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
33  //
37  class IGNITION_MATH_VISIBLE Color
38  {
40  public: static const Color White;
42  public: static const Color Black;
44  public: static const Color Red;
46  public: static const Color Green;
48  public: static const Color Blue;
50  public: static const Color Yellow;
52  public: static const Color Magenta;
54  public: static const Color Cyan;
55 
58  public: typedef unsigned int RGBA;
59 
62  public: typedef unsigned int BGRA;
63 
66  public: typedef unsigned int ARGB;
67 
70  public: typedef unsigned int ABGR;
71 
73  public: Color();
74 
80  public: Color(const float _r, const float _g, const float _b,
81  const float _a = 1.0);
82 
85  public: Color(const Color &_clr);
86 
88  public: virtual ~Color();
89 
92  public: void Reset();
93 
99  public: void Set(const float _r = 1, const float _g = 1,
100  const float _b = 1, const float _a = 1);
101 
105  public: Vector3f HSV() const;
106 
111  public: void SetFromHSV(const float _h, const float _s, const float _v);
112 
115  public: Vector3f YUV() const;
116 
121  public: void SetFromYUV(const float _y, const float _u, const float _v);
122 
126  public: Color &operator=(const Color &_pt);
127 
132  public: float operator[](const unsigned int _index);
133 
136  public: RGBA AsRGBA() const;
137 
140  public: BGRA AsBGRA() const;
141 
144  public: ARGB AsARGB() const;
145 
148  public: ABGR AsABGR() const;
149 
152  public: void SetFromRGBA(const RGBA _v);
153 
156  public: void SetFromBGRA(const BGRA _v);
157 
160  public: void SetFromARGB(const ARGB _v);
161 
164  public: void SetFromABGR(const ABGR _v);
165 
169  public: Color operator+(const Color &_pt) const;
170 
174  public: Color operator+(const float &_v) const;
175 
179  public: const Color &operator+=(const Color &_pt);
180 
184  public: Color operator-(const Color &_pt) const;
185 
189  public: Color operator-(const float &_v) const;
190 
194  public: const Color &operator-=(const Color &_pt);
195 
199  public: const Color operator/(const Color &_pt) const;
200 
204  public: const Color operator/(const float &_v) const;
205 
209  public: const Color &operator/=(const Color &_pt);
210 
214  public: const Color operator*(const Color &_pt) const;
215 
219  public: const Color operator*(const float &_v) const;
220 
224  public: const Color &operator*=(const Color &_pt);
225 
229  public: bool operator==(const Color &_pt) const;
230 
234  public: bool operator!=(const Color &_pt) const;
235 
237  private: void Clamp();
238 
243  public: friend std::ostream &operator<<(std::ostream &_out,
244  const Color &_pt)
245  {
246  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
247  return _out;
248  }
249 
254  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
255  {
256  // Skip white spaces
257  _in.setf(std::ios_base::skipws);
258  _in >> _pt.r >> _pt.g >> _pt.b;
259  // Since alpha is optional, check if it's there before parsing
260  while (_in.good() && std::isspace(_in.peek()))
261  {
262  _in.get();
263  }
264  if (_in.good())
265  {
266  _in >> _pt.a;
267  }
268  else if (!_in.fail())
269  {
270  _pt.a = 1.0;
271  }
272  return _in;
273  }
274 
277  public: float R() const;
278 
281  public: float G() const;
282 
285  public: float B() const;
286 
289  public: float A() const;
290 
293  public: float &R();
294 
297  public: float &G();
298 
301  public: float &B();
302 
305  public: float &A();
306 
309  public: void R(const float _r);
310 
313  public: void G(const float _g);
314 
317  public: void B(const float _b);
318 
321  public: void A(const float _a);
322 
324  private: float r = 0;
325 
327  private: float g = 0;
328 
330  private: float b = 0;
331 
333  private: float a = 1;
334  };
335  }
336  }
337 }
338 #endif
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Color.hh:243
T setf(T... args)
T good(T... args)
T fail(T... args)
unsigned int ARGB
A ARGB packed value as an unsigned int.
Definition: Color.hh:66
static const Color Yellow
(1, 1, 0)
Definition: Color.hh:50
STL class.
unsigned int BGRA
A BGRA packed value as an unsigned int.
Definition: Color.hh:62
static const Color Green
(0, 1, 0)
Definition: Color.hh:46
static const Color White
(1, 1, 1)
Definition: Color.hh:40
T get(T... args)
static const Color Red
(1, 0, 0)
Definition: Color.hh:44
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:41
T peek(T... args)
static const Color Blue
(0, 0, 1)
Definition: Color.hh:48
static const Color Cyan
(0, 1, 1)
Definition: Color.hh:54
Definition: Angle.hh:42
unsigned int ABGR
A ABGR packed value as an unsigned int.
Definition: Color.hh:70
STL class.
Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component i...
Definition: Color.hh:37
unsigned int RGBA
A RGBA packed value as an unsigned int.
Definition: Color.hh:58
T isspace(T... args)
static const Color Magenta
(1, 0, 1)
Definition: Color.hh:52
static const Color Black
(0, 0, 0)
Definition: Color.hh:42