Ignition Math

API Reference

6.4.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 
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 bracket to help doxygen filtering.
31  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
32  //
36  class IGNITION_MATH_VISIBLE Color
37  {
39  public: static const Color White;
41  public: static const Color Black;
43  public: static const Color Red;
45  public: static const Color Green;
47  public: static const Color Blue;
49  public: static const Color Yellow;
51  public: static const Color Magenta;
53  public: static const Color Cyan;
54 
57  public: typedef unsigned int RGBA;
58 
61  public: typedef unsigned int BGRA;
62 
65  public: typedef unsigned int ARGB;
66 
69  public: typedef unsigned int ABGR;
70 
72  public: Color();
73 
79  public: Color(const float _r, const float _g, const float _b,
80  const float _a = 1.0);
81 
84  public: Color(const Color &_clr);
85 
87  public: virtual ~Color();
88 
91  public: void Reset();
92 
98  public: void Set(const float _r = 1, const float _g = 1,
99  const float _b = 1, const float _a = 1);
100 
104  public: Vector3f HSV() const;
105 
110  public: void SetFromHSV(const float _h, const float _s, const float _v);
111 
114  public: Vector3f YUV() const;
115 
120  public: void SetFromYUV(const float _y, const float _u, const float _v);
121 
125  public: Color &operator=(const Color &_pt);
126 
131  public: float operator[](const unsigned int _index);
132 
135  public: RGBA AsRGBA() const;
136 
139  public: BGRA AsBGRA() const;
140 
143  public: ARGB AsARGB() const;
144 
147  public: ABGR AsABGR() const;
148 
151  public: void SetFromRGBA(const RGBA _v);
152 
155  public: void SetFromBGRA(const BGRA _v);
156 
159  public: void SetFromARGB(const ARGB _v);
160 
163  public: void SetFromABGR(const ABGR _v);
164 
168  public: Color operator+(const Color &_pt) const;
169 
173  public: Color operator+(const float &_v) const;
174 
178  public: const Color &operator+=(const Color &_pt);
179 
183  public: Color operator-(const Color &_pt) const;
184 
188  public: Color operator-(const float &_v) const;
189 
193  public: const Color &operator-=(const Color &_pt);
194 
198  public: const Color operator/(const Color &_pt) const;
199 
203  public: const Color operator/(const float &_v) const;
204 
208  public: const Color &operator/=(const Color &_pt);
209 
213  public: const Color operator*(const Color &_pt) const;
214 
218  public: const Color operator*(const float &_v) const;
219 
223  public: const Color &operator*=(const Color &_pt);
224 
228  public: bool operator==(const Color &_pt) const;
229 
233  public: bool operator!=(const Color &_pt) const;
234 
236  private: void Clamp();
237 
242  public: friend std::ostream &operator<<(std::ostream &_out,
243  const Color &_pt)
244  {
245  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
246  return _out;
247  }
248 
253  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
254  {
255  // Skip white spaces
256  _in.setf(std::ios_base::skipws);
257  _in >> _pt.r >> _pt.g >> _pt.b;
258  // Since alpha is optional, check if it's there before parsing
259  while (!_in.eof() && std::isspace(_in.peek()))
260  {
261  _in.get();
262  }
263  if (!_in.eof())
264  {
265  _in >> _pt.a;
266  }
267  else
268  {
269  _pt.a = 1.0;
270  }
271  return _in;
272  }
273 
276  public: float R() const;
277 
280  public: float G() const;
281 
284  public: float B() const;
285 
288  public: float A() const;
289 
292  public: float &R();
293 
296  public: float &G();
297 
300  public: float &B();
301 
304  public: float &A();
305 
308  public: void R(const float _r);
309 
312  public: void G(const float _g);
313 
316  public: void B(const float _b);
317 
320  public: void A(const float _a);
321 
323  private: float r = 0;
324 
326  private: float g = 0;
327 
329  private: float b = 0;
330 
332  private: float a = 1;
333  };
334  }
335  }
336 }
337 #endif
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Color.hh:242
T setf(T... args)
unsigned int ARGB
A ARGB packed value as an unsigned int.
Definition: Color.hh:65
static const Color Yellow
(1, 1, 0)
Definition: Color.hh:49
STL class.
unsigned int BGRA
A BGRA packed value as an unsigned int.
Definition: Color.hh:61
T eof(T... args)
static const Color Green
(0, 1, 0)
Definition: Color.hh:45
static const Color White
(1, 1, 1)
Definition: Color.hh:39
T get(T... args)
static const Color Red
(1, 0, 0)
Definition: Color.hh:43
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
T peek(T... args)
static const Color Blue
(0, 0, 1)
Definition: Color.hh:47
static const Color Cyan
(0, 1, 1)
Definition: Color.hh:53
Definition: Angle.hh:42
unsigned int ABGR
A ABGR packed value as an unsigned int.
Definition: Color.hh:69
STL class.
Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component i...
Definition: Color.hh:36
unsigned int RGBA
A RGBA packed value as an unsigned int.
Definition: Color.hh:57
T isspace(T... args)
static const Color Magenta
(1, 0, 1)
Definition: Color.hh:51
static const Color Black
(0, 0, 0)
Definition: Color.hh:41