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 
25 namespace ignition
26 {
27  namespace math
28  {
33  {
35  public: static const Color White;
37  public: static const Color Black;
39  public: static const Color Red;
41  public: static const Color Green;
43  public: static const Color Blue;
45  public: static const Color Yellow;
47  public: static const Color Magenta;
49  public: static const Color Cyan;
50 
53  public: typedef unsigned int RGBA;
54 
57  public: typedef unsigned int BGRA;
58 
61  public: typedef unsigned int ARGB;
62 
65  public: typedef unsigned int ABGR;
66 
68  public: Color();
69 
75  public: Color(const float _r, const float _g, const float _b,
76  const float _a = 1.0);
77 
80  public: Color(const Color &_clr);
81 
83  public: virtual ~Color();
84 
87  public: void Reset();
88 
94  public: void Set(const float _r = 1, const float _g = 1,
95  const float _b = 1, const float _a = 1);
96 
100  public: Vector3f HSV() const;
101 
106  public: void SetFromHSV(const float _h, const float _s, const float _v);
107 
110  public: Vector3f YUV() const;
111 
116  public: void SetFromYUV(const float _y, const float _u, const float _v);
117 
121  public: Color &operator=(const Color &_pt);
122 
127  public: float operator[](const unsigned int _index);
128 
131  public: RGBA AsRGBA() const;
132 
135  public: BGRA AsBGRA() const;
136 
139  public: ARGB AsARGB() const;
140 
143  public: ABGR AsABGR() const;
144 
147  public: void SetFromRGBA(const RGBA _v);
148 
151  public: void SetFromBGRA(const BGRA _v);
152 
155  public: void SetFromARGB(const ARGB _v);
156 
159  public: void SetFromABGR(const ABGR _v);
160 
164  public: Color operator+(const Color &_pt) const;
165 
169  public: Color operator+(const float &_v) const;
170 
174  public: const Color &operator+=(const Color &_pt);
175 
179  public: Color operator-(const Color &_pt) const;
180 
184  public: Color operator-(const float &_v) const;
185 
189  public: const Color &operator-=(const Color &_pt);
190 
194  public: const Color operator/(const Color &_pt) const;
195 
199  public: const Color operator/(const float &_v) const;
200 
204  public: const Color &operator/=(const Color &_pt);
205 
209  public: const Color operator*(const Color &_pt) const;
210 
214  public: const Color operator*(const float &_v) const;
215 
219  public: const Color &operator*=(const Color &_pt);
220 
224  public: bool operator==(const Color &_pt) const;
225 
229  public: bool operator!=(const Color &_pt) const;
230 
232  private: void Clamp();
233 
238  public: friend std::ostream &operator<<(std::ostream &_out,
239  const Color &_pt)
240  {
241  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
242  return _out;
243  }
244 
248  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
249  {
250  // Skip white spaces
251  _in.setf(std::ios_base::skipws);
252  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
253  return _in;
254  }
255 
258  public: float R() const;
259 
262  public: float G() const;
263 
266  public: float B() const;
267 
270  public: float A() const;
271 
274  public: float &R();
275 
278  public: float &G();
279 
282  public: float &B();
283 
286  public: float &A();
287 
290  public: void R(const float _r);
291 
294  public: void G(const float _g);
295 
298  public: void B(const float _b);
299 
302  public: void A(const float _a);
303 
305  private: float r = 0;
306 
308  private: float g = 0;
309 
311  private: float b = 0;
312 
314  private: float a = 1;
315  };
316  }
317 }
318 #endif
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Color.hh:238
static const Color Red
(1, 0, 0)
Definition: Color.hh:39
unsigned int RGBA
Definition: Color.hh:53
static const Color White
(1, 1, 1)
Definition: Color.hh:35
static const Color Magenta
(1, 0, 1)
Definition: Color.hh:47
static const Color Blue
(0, 0, 1)
Definition: Color.hh:43
static const Color Yellow
(1, 1, 0)
Definition: Color.hh:45
static const Color Black
(0, 0, 0)
Definition: Color.hh:37
static const Color Cyan
(0, 1, 1)
Definition: Color.hh:49
static const Color Green
(0, 1, 0)
Definition: Color.hh:41
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: System.hh:59
unsigned int ARGB
Definition: Color.hh:61
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:36
unsigned int BGRA
Definition: Color.hh:57
unsigned int ABGR
Definition: Color.hh:65
Definition: Angle.hh:38
Defines a color using a red (R), green (G), blue (B), and alpha (A) component.
Definition: Color.hh:32