Loading...
Searching...
No Matches
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
24#include <ignition/math/config.hh>
25
26namespace ignition
27{
28 namespace math
29 {
30 inline namespace IGNITION_MATH_VERSION_NAMESPACE
31 {
35 class IGNITION_MATH_VISIBLE Color
36 {
38 public: static const Color White;
40 public: static const Color Black;
42 public: static const Color Red;
44 public: static const Color Green;
46 public: static const Color Blue;
48 public: static const Color Yellow;
50 public: static const Color Magenta;
52 public: static const Color Cyan;
53
56 public: typedef unsigned int RGBA;
57
60 public: typedef unsigned int BGRA;
61
64 public: typedef unsigned int ARGB;
65
68 public: typedef unsigned int ABGR;
69
71 public: Color();
72
78 public: Color(const float _r, const float _g, const float _b,
79 const float _a = 1.0);
80
83 public: Color(const Color &_clr);
84
86 public: virtual ~Color();
87
90 public: void Reset();
91
97 public: void Set(const float _r = 1, const float _g = 1,
98 const float _b = 1, const float _a = 1);
99
103 public: Vector3f HSV() const;
104
109 public: void SetFromHSV(const float _h, const float _s, const float _v);
110
113 public: Vector3f YUV() const;
114
119 public: void SetFromYUV(const float _y, const float _u, const float _v);
120
124 public: Color &operator=(const Color &_pt);
125
130 public: float operator[](const unsigned int _index);
131
134 public: RGBA AsRGBA() const;
135
138 public: BGRA AsBGRA() const;
139
142 public: ARGB AsARGB() const;
143
146 public: ABGR AsABGR() const;
147
150 public: void SetFromRGBA(const RGBA _v);
151
154 public: void SetFromBGRA(const BGRA _v);
155
158 public: void SetFromARGB(const ARGB _v);
159
162 public: void SetFromABGR(const ABGR _v);
163
167 public: Color operator+(const Color &_pt) const;
168
172 public: Color operator+(const float &_v) const;
173
177 public: const Color &operator+=(const Color &_pt);
178
182 public: Color operator-(const Color &_pt) const;
183
187 public: Color operator-(const float &_v) const;
188
192 public: const Color &operator-=(const Color &_pt);
193
197 public: const Color operator/(const Color &_pt) const;
198
202 public: const Color operator/(const float &_v) const;
203
207 public: const Color &operator/=(const Color &_pt);
208
212 public: const Color operator*(const Color &_pt) const;
213
217 public: const Color operator*(const float &_v) const;
218
222 public: const Color &operator*=(const Color &_pt);
223
227 public: bool operator==(const Color &_pt) const;
228
232 public: bool operator!=(const Color &_pt) const;
233
235 private: void Clamp();
236
241 public: friend std::ostream &operator<<(std::ostream &_out,
242 const Color &_pt)
243 {
244 _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
245 return _out;
246 }
247
251 public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
252 {
253 // Skip white spaces
254 _in.setf(std::ios_base::skipws);
255 _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
256 return _in;
257 }
258
261 public: float R() const;
262
265 public: float G() const;
266
269 public: float B() const;
270
273 public: float A() const;
274
277 public: float &R();
278
281 public: float &G();
282
285 public: float &B();
286
289 public: float &A();
290
293 public: void R(const float _r);
294
297 public: void G(const float _g);
298
301 public: void B(const float _b);
302
305 public: void A(const float _a);
306
308 private: float r = 0;
309
311 private: float g = 0;
312
314 private: float b = 0;
315
317 private: float a = 1;
318 };
319 }
320 }
321}
322#endif
Defines a color using a red (R), green (G), blue (B), and alpha (A) component.
Definition Color.hh:36
float operator[](const unsigned int _index)
Array index operator.
float & A()
Get a mutable reference to the alpha value.
bool operator==(const Color &_pt) const
Equality operator.
Color(const Color &_clr)
Copy Constructor.
float & B()
Get a mutable reference to the blue value.
const Color operator*(const Color &_pt) const
Multiplication operator.
void G(const float _g)
Set the green value.
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
Addition operator (this + _pt)
void SetFromHSV(const float _h, const float _s, const float _v)
Set a color based on HSV values.
static const Color Green
(0, 1, 0)
Definition Color.hh:44
static const Color White
(1, 1, 1)
Definition Color.hh:38
void SetFromABGR(const ABGR _v)
Set from uint32 ABGR packed value.
static const Color Black
(0, 0, 0)
Definition Color.hh:40
void A(const float _a)
Set the alpha value.
const Color operator/(const Color &_pt) const
Division operator.
Vector3f HSV() const
Get the color in HSV colorspace.
void R(const float _r)
Set the red value.
bool operator!=(const Color &_pt) const
Inequality operator.
Color operator+(const float &_v) const
Add _v to all color components.
void Reset()
Reset the color to default values to red=0, green=0, blue=0, alpha=1.
static const Color Blue
(0, 0, 1)
Definition Color.hh:46
static const Color Red
(1, 0, 0)
Definition Color.hh:42
Color operator-(const float &_v) const
Subtract _v from all color components.
BGRA AsBGRA() const
Get as uint32 BGRA packed value.
void SetFromYUV(const float _y, const float _u, const float _v)
Set from yuv.
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition Color.hh:241
float & R()
Get a mutable reference to the red value.
RGBA AsRGBA() const
Get as uint32 RGBA packed value.
const Color & operator/=(const Color &_pt)
Division equal operator.
const Color & operator+=(const Color &_pt)
Addition equal operator.
Color(const float _r, const float _g, const float _b, const float _a=1.0)
Constructor.
static const Color Cyan
(0, 1, 1)
Definition Color.hh:52
static const Color Yellow
(1, 1, 0)
Definition Color.hh:48
ARGB AsARGB() const
Get as uint32 ARGB packed value.
ABGR AsABGR() const
Get as uint32 ABGR packed value.
Color & operator=(const Color &_pt)
Equal operator.
static const Color Magenta
(1, 0, 1)
Definition Color.hh:50
const Color operator*(const float &_v) const
Multiply all color components by _v.
const Color & operator*=(const Color &_pt)
Multiplication equal operator.
Vector3f YUV() const
Get the color in YUV colorspace.
void Set(const float _r=1, const float _g=1, const float _b=1, const float _a=1)
Set the contents of the vector.
const Color operator/(const float &_v) const
Divide all color component by _v.
Color operator-(const Color &_pt) const
Subtraction operator.
void B(const float _b)
Set the blue value.
void SetFromARGB(const ARGB _v)
Set from uint32 ARGB packed value.
float & G()
Get a mutable reference to the green value.
void SetFromRGBA(const RGBA _v)
Set from uint32 RGBA packed value.
The Vector3 class represents the generic vector containing 3 elements.
Definition Vector3.hh:40
Definition Angle.hh:40