Gazebo Math

API Reference

8.1.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 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::math
29{
30 // Inline bracket to help doxygen filtering.
31 inline namespace GZ_MATH_VERSION_NAMESPACE {
32 //
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
105 public: 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
120 public: static constexpr Color UnclampedColor(const float _r,
121 const float _g,
122 const float _b,
123 const float _a)
124 {
125 auto clr = Color();
126 clr.r = _r;
127 clr.g = _g;
128 clr.b = _b;
129 clr.a = _a;
130 return clr;
131 }
132
135 public: void Reset();
136
142 public: void Set(const float _r = 1, const float _g = 1,
143 const float _b = 1, const float _a = 1);
144
148 public: Vector3f HSV() const;
149
154 public: void SetFromHSV(const float _h, const float _s, const float _v);
155
158 public: Vector3f YUV() const;
159
164 public: void SetFromYUV(const float _y, const float _u, const float _v);
165
171 public: float operator[](const unsigned int _index);
172
178 public: float operator[](const unsigned int _index) const;
179
182 public: RGBA AsRGBA() const;
183
186 public: BGRA AsBGRA() const;
187
190 public: ARGB AsARGB() const;
191
194 public: ABGR AsABGR() const;
195
198 public: void SetFromRGBA(const RGBA _v);
199
202 public: void SetFromBGRA(const BGRA _v);
203
206 public: void SetFromARGB(const ARGB _v);
207
210 public: void SetFromABGR(const ABGR _v);
211
215 public: Color operator+(const Color &_pt) const;
216
220 public: Color operator+(const float &_v) const;
221
225 public: const Color &operator+=(const Color &_pt);
226
230 public: Color operator-(const Color &_pt) const;
231
235 public: Color operator-(const float &_v) const;
236
240 public: const Color &operator-=(const Color &_pt);
241
245 public: const Color operator/(const Color &_pt) const;
246
250 public: const Color operator/(const float &_v) const;
251
255 public: const Color &operator/=(const Color &_pt);
256
260 public: const Color operator*(const Color &_pt) const;
261
265 public: const Color operator*(const float &_v) const;
266
270 public: const Color &operator*=(const Color &_pt);
271
275 public: bool operator==(const Color &_pt) const;
276
280 public: bool operator!=(const Color &_pt) const;
281
283 private: void Clamp();
284
290 const Color &_color)
291 {
292 for (auto i : {0, 1, 2, 3})
293 {
294 if (i > 0)
295 _out << " ";
296
298 }
299 return _out;
300 }
301
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_