Gazebo Math

API Reference

7.5.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 <cctype>
21 #include <istream>
22 #include <ostream>
23 
24 #include <gz/math/Helpers.hh>
25 #include <gz/math/Vector3.hh>
26 #include <gz/math/config.hh>
27 
28 namespace gz::math
29 {
30  // Inline bracket to help doxygen filtering.
31  inline namespace GZ_MATH_VERSION_NAMESPACE {
32  //
40  class GZ_MATH_VISIBLE Color
41  {
43  public: static const Color &White;
45  public: static const Color &Black;
47  public: static const Color &Red;
49  public: static const Color &Green;
51  public: static const Color &Blue;
53  public: static const Color &Yellow;
55  public: static const Color &Magenta;
57  public: static const Color &Cyan;
58 
66  public: typedef unsigned int RGBA;
67 
75  public: typedef unsigned int BGRA;
76 
84  public: typedef unsigned int ARGB;
85 
93  public: typedef unsigned int ABGR;
94 
96  public: Color() = default;
97 
103  public: constexpr Color(const float _r, const float _g, const float _b,
104  const float _a = 1.0)
105  : r(_r), g(_g), b(_b), a(_a)
106  {
107  this->Clamp();
108  }
109 
112  public: Color(const Color &_clr) = default;
113 
115  public: ~Color() = default;
116 
119  public: void Reset();
120 
126  public: void Set(const float _r = 1, const float _g = 1,
127  const float _b = 1, const float _a = 1);
128 
132  public: Vector3f HSV() const;
133 
138  public: void SetFromHSV(const float _h, const float _s, const float _v);
139 
142  public: Vector3f YUV() const;
143 
148  public: void SetFromYUV(const float _y, const float _u, const float _v);
149 
153  public: Color &operator=(const Color &_pt) = default;
154 
160  public: float operator[](const unsigned int _index);
161 
167  public: float operator[](const unsigned int _index) const;
168 
171  public: RGBA AsRGBA() const;
172 
175  public: BGRA AsBGRA() const;
176 
179  public: ARGB AsARGB() const;
180 
183  public: ABGR AsABGR() const;
184 
187  public: void SetFromRGBA(const RGBA _v);
188 
191  public: void SetFromBGRA(const BGRA _v);
192 
195  public: void SetFromARGB(const ARGB _v);
196 
199  public: void SetFromABGR(const ABGR _v);
200 
204  public: Color operator+(const Color &_pt) const;
205 
209  public: Color operator+(const float &_v) const;
210 
214  public: const Color &operator+=(const Color &_pt);
215 
219  public: Color operator-(const Color &_pt) const;
220 
224  public: Color operator-(const float &_v) const;
225 
229  public: const Color &operator-=(const Color &_pt);
230 
234  public: const Color operator/(const Color &_pt) const;
235 
239  public: const Color operator/(const float &_v) const;
240 
244  public: const Color &operator/=(const Color &_pt);
245 
249  public: const Color operator*(const Color &_pt) const;
250 
254  public: const Color operator*(const float &_v) const;
255 
259  public: const Color &operator*=(const Color &_pt);
260 
264  public: bool operator==(const Color &_pt) const;
265 
269  public: bool operator!=(const Color &_pt) const;
270 
272  private: constexpr void Clamp()
273  {
274  // These comparisons are carefully written to handle NaNs correctly.
275  if (!(this->r >= 0)) { this->r = 0; }
276  if (!(this->g >= 0)) { this->g = 0; }
277  if (!(this->b >= 0)) { this->b = 0; }
278  if (!(this->a >= 0)) { this->a = 0; }
279  if (this->r > 1) { this->r = this->r/255.0f; }
280  if (this->g > 1) { this->g = this->g/255.0f; }
281  if (this->b > 1) { this->b = this->b/255.0f; }
282  if (this->a > 1) { this->a = 1; }
283  }
284 
289  public: friend std::ostream &operator<<(std::ostream &_out,
290  const Color &_color)
291  {
292  for (auto i : {0, 1, 2, 3})
293  {
294  if (i > 0)
295  _out << " ";
296 
297  appendToStream(_out, _color[i]);
298  }
299  return _out;
300  }
301 
306  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
307  {
308  // Skip white spaces
309  _in.setf(std::ios_base::skipws);
310  _in >> _pt.r >> _pt.g >> _pt.b;
311  // Since alpha is optional, check if it's there before parsing
312  while (_in.good() && std::isspace(_in.peek()))
313  {
314  _in.get();
315  }
316  if (_in.good())
317  {
318  _in >> _pt.a;
319  }
320  else if (!_in.fail())
321  {
322  _pt.a = 1.0;
323  }
324  return _in;
325  }
326 
329  public: float R() const;
330 
333  public: float G() const;
334 
337  public: float B() const;
338 
341  public: float A() const;
342 
345  public: float &R();
346 
349  public: float &G();
350 
353  public: float &B();
354 
357  public: float &A();
358 
361  public: void R(const float _r);
362 
365  public: void G(const float _g);
366 
369  public: void B(const float _b);
370 
373  public: void A(const float _a);
374 
376  private: float r = 0;
377 
379  private: float g = 0;
380 
382  private: float b = 0;
383 
385  private: float a = 1;
386  };
387  } // namespace GZ_MATH_VERSION_NAMESPACE
388 } // namespace gz::math
389 #endif // GZ_MATH_COLOR_HH_