Gazebo Math

API Reference

7.4.0
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
28namespace gz
29{
30 namespace math
31 {
32 // Inline bracket to help doxygen filtering.
33 inline namespace GZ_MATH_VERSION_NAMESPACE {
34 //
42 class GZ_MATH_VISIBLE Color
43 {
45 public: static const Color &White;
47 public: static const Color &Black;
49 public: static const Color &Red;
51 public: static const Color &Green;
53 public: static const Color &Blue;
55 public: static const Color &Yellow;
57 public: static const Color &Magenta;
59 public: static const Color &Cyan;
60
68 public: typedef unsigned int RGBA;
69
77 public: typedef unsigned int BGRA;
78
86 public: typedef unsigned int ARGB;
87
95 public: typedef unsigned int ABGR;
96
98 public: Color() = default;
99
105 public: constexpr Color(const float _r, const float _g, const float _b,
106 const float _a = 1.0)
107 : r(_r), g(_g), b(_b), a(_a)
108 {
109 this->Clamp();
110 }
111
114 public: Color(const Color &_clr) = default;
115
117 public: ~Color() = default;
118
121 public: void Reset();
122
128 public: void Set(const float _r = 1, const float _g = 1,
129 const float _b = 1, const float _a = 1);
130
134 public: Vector3f HSV() const;
135
140 public: void SetFromHSV(const float _h, const float _s, const float _v);
141
144 public: Vector3f YUV() const;
145
150 public: void SetFromYUV(const float _y, const float _u, const float _v);
151
155 public: Color &operator=(const Color &_pt) = default;
156
162 public: float operator[](const unsigned int _index);
163
169 public: float operator[](const unsigned int _index) const;
170
173 public: RGBA AsRGBA() const;
174
177 public: BGRA AsBGRA() const;
178
181 public: ARGB AsARGB() const;
182
185 public: ABGR AsABGR() const;
186
189 public: void SetFromRGBA(const RGBA _v);
190
193 public: void SetFromBGRA(const BGRA _v);
194
197 public: void SetFromARGB(const ARGB _v);
198
201 public: void SetFromABGR(const ABGR _v);
202
206 public: Color operator+(const Color &_pt) const;
207
211 public: Color operator+(const float &_v) const;
212
216 public: const Color &operator+=(const Color &_pt);
217
221 public: Color operator-(const Color &_pt) const;
222
226 public: Color operator-(const float &_v) const;
227
231 public: const Color &operator-=(const Color &_pt);
232
236 public: const Color operator/(const Color &_pt) const;
237
241 public: const Color operator/(const float &_v) const;
242
246 public: const Color &operator/=(const Color &_pt);
247
251 public: const Color operator*(const Color &_pt) const;
252
256 public: const Color operator*(const float &_v) const;
257
261 public: const Color &operator*=(const Color &_pt);
262
266 public: bool operator==(const Color &_pt) const;
267
271 public: bool operator!=(const Color &_pt) const;
272
274 private: constexpr void Clamp()
275 {
276 // These comparisons are carefully written to handle NaNs correctly.
277 if (!(this->r >= 0)) { this->r = 0; }
278 if (!(this->g >= 0)) { this->g = 0; }
279 if (!(this->b >= 0)) { this->b = 0; }
280 if (!(this->a >= 0)) { this->a = 0; }
281 if (this->r > 1) { this->r = this->r/255.0f; }
282 if (this->g > 1) { this->g = this->g/255.0f; }
283 if (this->b > 1) { this->b = this->b/255.0f; }
284 if (this->a > 1) { this->a = 1; }
285 }
286
291 public: friend std::ostream &operator<<(std::ostream &_out,
292 const Color &_color)
293 {
294 for (auto i : {0, 1, 2, 3})
295 {
296 if (i > 0)
297 _out << " ";
298
299 appendToStream(_out, _color[i]);
300 }
301 return _out;
302 }
303
308 public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
309 {
310 // Skip white spaces
311 _in.setf(std::ios_base::skipws);
312 _in >> _pt.r >> _pt.g >> _pt.b;
313 // Since alpha is optional, check if it's there before parsing
314 while (_in.good() && std::isspace(_in.peek()))
315 {
316 _in.get();
317 }
318 if (_in.good())
319 {
320 _in >> _pt.a;
321 }
322 else if (!_in.fail())
323 {
324 _pt.a = 1.0;
325 }
326 return _in;
327 }
328
331 public: float R() const;
332
335 public: float G() const;
336
339 public: float B() const;
340
343 public: float A() const;
344
347 public: float &R();
348
351 public: float &G();
352
355 public: float &B();
356
359 public: float &A();
360
363 public: void R(const float _r);
364
367 public: void G(const float _g);
368
371 public: void B(const float _b);
372
375 public: void A(const float _a);
376
378 private: float r = 0;
379
381 private: float g = 0;
382
384 private: float b = 0;
385
387 private: float a = 1;
388 };
389 }
390 }
391}
392#endif