Ignition Math

API Reference

6.10.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  //
41  class IGNITION_MATH_VISIBLE Color
42  {
44  public: static const Color White;
46  public: static const Color Black;
48  public: static const Color Red;
50  public: static const Color Green;
52  public: static const Color Blue;
54  public: static const Color Yellow;
56  public: static const Color Magenta;
58  public: static const Color Cyan;
59 
67  public: typedef unsigned int RGBA;
68 
76  public: typedef unsigned int BGRA;
77 
85  public: typedef unsigned int ARGB;
86 
94  public: typedef unsigned int ABGR;
95 
97  public: Color();
98 
104  public: Color(const float _r, const float _g, const float _b,
105  const float _a = 1.0);
106 
109  public: Color(const Color &_clr);
110 
112  public: virtual ~Color();
113 
116  public: void Reset();
117 
123  public: void Set(const float _r = 1, const float _g = 1,
124  const float _b = 1, const float _a = 1);
125 
129  public: Vector3f HSV() const;
130 
135  public: void SetFromHSV(const float _h, const float _s, const float _v);
136 
139  public: Vector3f YUV() const;
140 
145  public: void SetFromYUV(const float _y, const float _u, const float _v);
146 
150  public: Color &operator=(const Color &_pt);
151 
156  public: float operator[](const unsigned int _index);
157 
160  public: RGBA AsRGBA() const;
161 
164  public: BGRA AsBGRA() const;
165 
168  public: ARGB AsARGB() const;
169 
172  public: ABGR AsABGR() const;
173 
176  public: void SetFromRGBA(const RGBA _v);
177 
180  public: void SetFromBGRA(const BGRA _v);
181 
184  public: void SetFromARGB(const ARGB _v);
185 
188  public: void SetFromABGR(const ABGR _v);
189 
193  public: Color operator+(const Color &_pt) const;
194 
198  public: Color operator+(const float &_v) const;
199 
203  public: const Color &operator+=(const Color &_pt);
204 
208  public: Color operator-(const Color &_pt) const;
209 
213  public: Color operator-(const float &_v) const;
214 
218  public: const Color &operator-=(const Color &_pt);
219 
223  public: const Color operator/(const Color &_pt) const;
224 
228  public: const Color operator/(const float &_v) const;
229 
233  public: const Color &operator/=(const Color &_pt);
234 
238  public: const Color operator*(const Color &_pt) const;
239 
243  public: const Color operator*(const float &_v) const;
244 
248  public: const Color &operator*=(const Color &_pt);
249 
253  public: bool operator==(const Color &_pt) const;
254 
258  public: bool operator!=(const Color &_pt) const;
259 
261  private: void Clamp();
262 
267  public: friend std::ostream &operator<<(std::ostream &_out,
268  const Color &_pt)
269  {
270  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
271  return _out;
272  }
273 
278  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
279  {
280  // Skip white spaces
281  _in.setf(std::ios_base::skipws);
282  _in >> _pt.r >> _pt.g >> _pt.b;
283  // Since alpha is optional, check if it's there before parsing
284  while (_in.good() && std::isspace(_in.peek()))
285  {
286  _in.get();
287  }
288  if (_in.good())
289  {
290  _in >> _pt.a;
291  }
292  else if (!_in.fail())
293  {
294  _pt.a = 1.0;
295  }
296  return _in;
297  }
298 
301  public: float R() const;
302 
305  public: float G() const;
306 
309  public: float B() const;
310 
313  public: float A() const;
314 
317  public: float &R();
318 
321  public: float &G();
322 
325  public: float &B();
326 
329  public: float &A();
330 
333  public: void R(const float _r);
334 
337  public: void G(const float _g);
338 
341  public: void B(const float _b);
342 
345  public: void A(const float _a);
346 
348  private: float r = 0;
349 
351  private: float g = 0;
352 
354  private: float b = 0;
355 
357  private: float a = 1;
358  };
359  }
360  }
361 }
362 #endif
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Color.hh:267
T setf(T... args)
T good(T... args)
T fail(T... args)
unsigned int ARGB
A ARGB packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: Color.hh:85
static const Color Yellow
(1, 1, 0)
Definition: Color.hh:54
STL class.
unsigned int BGRA
A BGRA packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: Color.hh:76
static const Color Green
(0, 1, 0)
Definition: Color.hh:50
static const Color White
(1, 1, 1)
Definition: Color.hh:44
T get(T... args)
static const Color Red
(1, 0, 0)
Definition: Color.hh:48
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:52
static const Color Cyan
(0, 1, 1)
Definition: Color.hh:58
Definition: Angle.hh:42
unsigned int ABGR
A ABGR packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: Color.hh:94
STL class.
Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component i...
Definition: Color.hh:41
unsigned int RGBA
A RGBA packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: Color.hh:67
T isspace(T... args)
static const Color Magenta
(1, 0, 1)
Definition: Color.hh:56
static const Color Black
(0, 0, 0)
Definition: Color.hh:46