Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpRGBa.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * RGBA pixel.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
46 #include <visp3/core/vpColor.h>
47 #include <visp3/core/vpDebug.h>
48 #include <visp3/core/vpException.h>
49 #include <visp3/core/vpRGBa.h>
50 
56 vpRGBa &vpRGBa::operator=(const unsigned char &v)
57 {
58  this->R = v;
59  this->G = v;
60  this->B = v;
61  this->A = v;
62  return *this;
63 }
64 
69 {
70  this->R = v.R;
71  this->G = v.G;
72  this->B = v.B;
73  this->A = v.A;
74  return *this;
75 }
76 
77 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
78 
82 {
83  this->R = std::move(v.R);
84  this->G = std::move(v.G);
85  this->B = std::move(v.B);
86  this->A = std::move(v.A);
87  return *this;
88 }
89 #endif
90 
101 {
102  if (v.getRows() != 4) {
103  vpERROR_TRACE("Bad vector dimension ");
104  throw(vpException(vpException::dimensionError, "Bad vector dimension "));
105  }
106  R = (unsigned char)v[0];
107  G = (unsigned char)v[1];
108  B = (unsigned char)v[2];
109  A = (unsigned char)v[3];
110  return *this;
111 }
112 
119 {
120  if (R != v.R)
121  return false;
122  if (G != v.G)
123  return false;
124  if (B != v.B)
125  return false;
126  if (A != v.A)
127  return false;
128 
129  return true;
130 }
136 bool vpRGBa::operator!=(const vpRGBa &v) { return (R != v.R || G != v.G || B != v.B || A != v.A); }
137 
144 {
145  vpColVector n(4); // new color
146  n[0] = (double)R - (double)v.R;
147  n[1] = (double)G - (double)v.G;
148  n[2] = (double)B - (double)v.B;
149  n[3] = (double)A - (double)v.A;
150  return n;
151 }
152 
160 {
161  vpRGBa n; // new color
162  n.R = static_cast<unsigned char>(R + v.R);
163  n.G = static_cast<unsigned char>(G + v.G);
164  n.B = static_cast<unsigned char>(B + v.B);
165  n.A = static_cast<unsigned char>(A + v.A);
166  return n;
167 }
168 
175 {
176  vpColVector n(4); // new color
177  n[0] = R - v[0];
178  n[1] = G - v[1];
179  n[2] = B - v[2];
180  n[3] = A - v[3];
181  return n;
182 }
183 
190 {
191  vpColVector n(4); // new color
192  n[0] = R + v[0];
193  n[1] = G + v[1];
194  n[2] = B + v[2];
195  n[3] = A + v[3];
196  return n;
197 }
198 
204 vpColVector vpRGBa::operator*(const float &v) const
205 {
206  vpColVector n(4);
207  n[0] = R * v;
208  n[1] = G * v;
209  n[2] = B * v;
210  n[3] = A * v;
211  return n;
212 }
213 
219 vpColVector vpRGBa::operator*(const double &v) const
220 {
221  vpColVector n(4);
222  n[0] = R * v;
223  n[1] = G * v;
224  n[2] = B * v;
225  n[3] = A * v;
226  return n;
227 }
228 
229 bool vpRGBa::operator<(const vpRGBa &v) const
230 {
231  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
232  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
233 
234  return (gray1 < gray2);
235 }
236 
237 bool vpRGBa::operator>(const vpRGBa &v) const
238 {
239  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
240  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
241 
242  return (gray1 > gray2);
243 }
244 
245 vpRGBa operator*(const double &x, const vpRGBa &rgb) { return rgb * x; }
246 
269 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba)
270 {
271  os << "(" << (int)rgba.R << "," << (int)rgba.G << "," << (int)rgba.B << "," << (int)rgba.A << ")";
272  return os;
273 }
unsigned char B
Blue component.
Definition: vpRGBa.h:150
#define vpERROR_TRACE
Definition: vpDebug.h:393
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getRows() const
Definition: vpArray2D.h:289
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:159
unsigned char G
Green component.
Definition: vpRGBa.h:149
bool operator<(const vpRGBa &v) const
Definition: vpRGBa.cpp:229
vpColVector operator-(const vpRGBa &v) const
Definition: vpRGBa.cpp:143
bool operator>(const vpRGBa &v) const
Definition: vpRGBa.cpp:237
Definition: vpRGBa.h:66
bool operator==(const vpRGBa &v)
Definition: vpRGBa.cpp:118
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpRGBa &rgba)
Definition: vpRGBa.cpp:269
unsigned char A
Additionnal component.
Definition: vpRGBa.h:151
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:56
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
unsigned char R
Red component.
Definition: vpRGBa.h:148
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:204
bool operator!=(const vpRGBa &v)
Definition: vpRGBa.cpp:136