Gazebo Math

API Reference

6.15.1
gz/math/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 GZ_MATH_COLOR_HH_
18 #define GZ_MATH_COLOR_HH_
19 
20 #include <iostream>
21 #include <cctype>
22 
23 #include <gz/math/Helpers.hh>
24 #include <gz/math/Vector3.hh>
25 #include <gz/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
Color & operator=(const Color &_pt)
Equal operator.
const Color operator*(const Color &_pt) const
Multiplication operator.
const Color & operator+=(const Color &_pt)
Addition equal operator.
Definition: gz/math/AdditivelySeparableScalarField3.hh:27
Defines a color using a red (R), green (G), blue (B), and alpha (A) component. Each color component i...
Definition: gz/math/Color.hh:41
float B() const
Get the blue value.
Color()
Constructor.
T peek(T... args)
T get(T... args)
void SetFromRGBA(const RGBA _v)
Set from uint32 RGBA packed value.
static const Color Black
(0, 0, 0)
Definition: gz/math/Color.hh:46
void SetFromHSV(const float _h, const float _s, const float _v)
Set a color based on HSV values.
void Reset()
Reset the color to default values to red=0, green=0, blue=0, alpha=1.
bool operator!=(const Color &_pt) const
Inequality operator.
friend std::istream & operator>>(std::istream &_in, Color &_pt)
Stream insertion operator.
Definition: gz/math/Color.hh:278
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: gz/math/Color.hh:267
static const Color Blue
(0, 0, 1)
Definition: gz/math/Color.hh:52
void SetFromARGB(const ARGB _v)
Set from uint32 ARGB packed value.
unsigned int ARGB
A ARGB packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: gz/math/Color.hh:85
STL class.
void Set(const float _r=1, const float _g=1, const float _b=1, const float _a=1)
Set the contents of the vector.
float operator[](const unsigned int _index)
Array index operator.
T setf(T... args)
ABGR AsABGR() const
Get as uint32 ABGR packed value.
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: gz/math/Vector3.hh:41
static const Color Green
(0, 1, 0)
Definition: gz/math/Color.hh:50
static const Color Cyan
(0, 1, 1)
Definition: gz/math/Color.hh:58
T good(T... args)
RGBA AsRGBA() const
Get as uint32 RGBA packed value.
static const Color Magenta
(1, 0, 1)
Definition: gz/math/Color.hh:56
float R() const
Get the red value.
T fail(T... args)
void SetFromBGRA(const BGRA _v)
Set from uint32 BGRA packed value.
const Color & operator-=(const Color &_pt)
Subtraction equal operator.
Color operator-(const Color &_pt) const
Subtraction operator.
Color operator+(const Color &_pt) const
Addition operator (this + _pt)
void SetFromABGR(const ABGR _v)
Set from uint32 ABGR packed value.
unsigned int ABGR
A ABGR packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: gz/math/Color.hh:94
const Color & operator/=(const Color &_pt)
Division equal operator.
unsigned int RGBA
A RGBA packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: gz/math/Color.hh:67
T isspace(T... args)
Vector3f HSV() const
Get the color in HSV colorspace.
static const Color Red
(1, 0, 0)
Definition: gz/math/Color.hh:48
virtual ~Color()
Destructor.
static const Color Yellow
(1, 1, 0)
Definition: gz/math/Color.hh:54
BGRA AsBGRA() const
Get as uint32 BGRA packed value.
STL class.
const Color & operator*=(const Color &_pt)
Multiplication equal operator.
const Color operator/(const Color &_pt) const
Division operator.
Vector3f YUV() const
Get the color in YUV colorspace.
ARGB AsARGB() const
Get as uint32 ARGB packed value.
bool operator==(const Color &_pt) const
Equality operator.
float G() const
Get the green value.
static const Color White
(1, 1, 1)
Definition: gz/math/Color.hh:44
void SetFromYUV(const float _y, const float _u, const float _v)
Set from yuv.
unsigned int BGRA
A BGRA packed value as an unsigned int Each 8 bits corresponds to a channel.
Definition: gz/math/Color.hh:76
float A() const
Get the alpha value.