Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpRGBa.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * RGBA pixel.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
46 #include <visp3/core/vpRGBa.h>
47 #include <visp3/core/vpColor.h>
48 #include <visp3/core/vpDebug.h>
49 #include <visp3/core/vpException.h>
50 
51 
57 vpRGBa &
58 vpRGBa::operator=(const unsigned char &v)
59 {
60  this->R = v;
61  this->G = v;
62  this->B = v;
63  this->A = v;
64  return *this;
65 }
66 
70 vpRGBa &
72 {
73  this->R = v.R;
74  this->G = v.G;
75  this->B = v.B;
76  this->A = v.A;
77  return *this;
78 }
79 
80 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
81 
84 vpRGBa &
86 {
87  this->R = std::move(v.R);
88  this->G = std::move(v.G);
89  this->B = std::move(v.B);
90  this->A = std::move(v.A);
91  return *this;
92 }
93 #endif
94 
104 vpRGBa &
106 {
107  if (v.getRows() != 4) {
108  vpERROR_TRACE("Bad vector dimension ") ;
109  throw(vpException(vpException::dimensionError, "Bad vector dimension "));
110  }
111  R = (unsigned char)v[0];
112  G = (unsigned char)v[1];
113  B = (unsigned char)v[2];
114  A = (unsigned char)v[3];
115  return *this;
116 }
117 
124 {
125  if (R != v.R)
126  return false;
127  if (G != v.G)
128  return false;
129  if (B != v.B)
130  return false;
131  if (A != v.A)
132  return false;
133 
134  return true ;
135 }
142 {
143  return (R != v.R || G != v.G || B != v.B || A != v.A);
144 }
145 
152 vpRGBa::operator-(const vpRGBa &v) const
153 {
154  vpColVector n(4); // new color
155  n[0] = (double)R - (double)v.R;
156  n[1] = (double)G - (double)v.G;
157  n[2] = (double)B - (double)v.B;
158  n[3] = (double)A - (double)v.A;
159  return n;
160 }
161 
168 vpRGBa
169 vpRGBa::operator+(const vpRGBa &v) const
170 {
171  vpRGBa n; // new color
172  n.R = static_cast<unsigned char>( R + v.R );
173  n.G = static_cast<unsigned char>( G + v.G );
174  n.B = static_cast<unsigned char>( B + v.B );
175  n.A = static_cast<unsigned char>( A + v.A );
176  return n;
177 }
178 
186 {
187  vpColVector n(4); // new color
188  n[0] = R - v[0];
189  n[1] = G - v[1];
190  n[2] = B - v[2];
191  n[3] = A - v[3];
192  return n;
193 }
194 
202 {
203  vpColVector n(4); // new color
204  n[0] = R + v[0];
205  n[1] = G + v[1];
206  n[2] = B + v[2];
207  n[3] = A + v[3];
208  return n;
209 }
210 
217 vpRGBa::operator*(const float &v) const
218 {
219  vpColVector n(4);
220  n[0] = R * v;
221  n[1] = G * v;
222  n[2] = B * v;
223  n[3] = A * v;
224  return n;
225 }
226 
233 vpRGBa::operator*(const double &v) const
234 {
235  vpColVector n(4);
236  n[0] = R * v;
237  n[1] = G * v;
238  n[2] = B * v;
239  n[3] = A * v;
240  return n;
241 }
242 
243 bool
244 vpRGBa::operator<(const vpRGBa &v) const
245 {
246  double gray1 = 0.2126*R+0.7152*G+0.0722*B;
247  double gray2 = 0.2126*v.R+0.7152*v.G+0.0722*v.B;
248 
249  return (gray1 < gray2);
250 }
251 
252 bool
253 vpRGBa::operator>(const vpRGBa &v) const
254 {
255  double gray1 = 0.2126*R+0.7152*G+0.0722*B;
256  double gray2 = 0.2126*v.R+0.7152*v.G+0.0722*v.B;
257 
258  return (gray1 > gray2);
259 }
260 
261 vpRGBa operator*(const double &x, const vpRGBa &rgb)
262 {
263  return rgb*x;
264 }
265 
288 VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRGBa& rgba)
289 {
290  os << "(" << (int)rgba.R << "," << (int)rgba.G << "," << (int)rgba.B << "," << (int)rgba.A << ")";
291  return os;
292 }
unsigned char B
Blue component.
Definition: vpRGBa.h:155
vpColVector operator*(const double &x, const vpColVector &v)
#define vpERROR_TRACE
Definition: vpDebug.h:391
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:169
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned char G
Green component.
Definition: vpRGBa.h:154
Definition: vpRGBa.h:66
bool operator>(const vpRGBa &v) const
Definition: vpRGBa.cpp:253
bool operator==(const vpRGBa &v)
Definition: vpRGBa.cpp:123
unsigned char A
Additionnal component.
Definition: vpRGBa.h:156
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:58
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
bool operator<(const vpRGBa &v) const
Definition: vpRGBa.cpp:244
unsigned char R
Red component.
Definition: vpRGBa.h:153
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:217
vpColVector operator-(const vpRGBa &v) const
Definition: vpRGBa.cpp:152
bool operator!=(const vpRGBa &v)
Definition: vpRGBa.cpp:141