Visual Servoing Platform  version 3.5.1 under development (2023-05-31)
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)
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 
118 bool vpRGBa::operator==(const vpRGBa &v) const
119 {
120  return R == v.R && G == v.G && B == v.B && A == v.A;
121 }
127 bool vpRGBa::operator!=(const vpRGBa &v) const { return (R != v.R || G != v.G || B != v.B || A != v.A); }
128 
135 {
136  vpColVector n(4); // new color
137  n[0] = (double)R - (double)v.R;
138  n[1] = (double)G - (double)v.G;
139  n[2] = (double)B - (double)v.B;
140  n[3] = (double)A - (double)v.A;
141  return n;
142 }
143 
151 {
152  vpRGBa n; // new color
153  n.R = static_cast<unsigned char>(R + v.R);
154  n.G = static_cast<unsigned char>(G + v.G);
155  n.B = static_cast<unsigned char>(B + v.B);
156  n.A = static_cast<unsigned char>(A + v.A);
157  return n;
158 }
159 
166 {
167  vpColVector n(4); // new color
168  n[0] = R - v[0];
169  n[1] = G - v[1];
170  n[2] = B - v[2];
171  n[3] = A - v[3];
172  return n;
173 }
174 
181 {
182  vpColVector n(4); // new color
183  n[0] = R + v[0];
184  n[1] = G + v[1];
185  n[2] = B + v[2];
186  n[3] = A + v[3];
187  return n;
188 }
189 
195 vpColVector vpRGBa::operator*(const float &v) const
196 {
197  vpColVector n(4);
198  n[0] = R * v;
199  n[1] = G * v;
200  n[2] = B * v;
201  n[3] = A * v;
202  return n;
203 }
204 
210 vpColVector vpRGBa::operator*(const double &v) const
211 {
212  vpColVector n(4);
213  n[0] = R * v;
214  n[1] = G * v;
215  n[2] = B * v;
216  n[3] = A * v;
217  return n;
218 }
219 
220 bool vpRGBa::operator<(const vpRGBa &v) const
221 {
222  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
223  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
224 
225  return (gray1 < gray2);
226 }
227 
228 bool vpRGBa::operator>(const vpRGBa &v) const
229 {
230  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
231  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
232 
233  return (gray1 > gray2);
234 }
235 
236 vpRGBa operator*(const double &x, const vpRGBa &rgb) { return rgb * x; }
237 
260 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba)
261 {
262  os << "(" << (int)rgba.R << "," << (int)rgba.G << "," << (int)rgba.B << "," << (int)rgba.A << ")";
263  return os;
264 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:499
unsigned int getRows() const
Definition: vpArray2D.h:292
Implementation of column vector and the associated operations.
Definition: vpColVector.h:172
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ dimensionError
Bad dimension.
Definition: vpException.h:95
VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
Definition: vpRGBa.h:67
vpColVector operator-(const vpRGBa &v) const
Definition: vpRGBa.cpp:134
unsigned char B
Blue component.
Definition: vpRGBa.h:146
unsigned char R
Red component.
Definition: vpRGBa.h:144
bool operator<(const vpRGBa &v) const
Definition: vpRGBa.cpp:220
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:56
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:150
unsigned char G
Green component.
Definition: vpRGBa.h:145
unsigned char A
Additionnal component.
Definition: vpRGBa.h:147
bool operator>(const vpRGBa &v) const
Definition: vpRGBa.cpp:228
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:195
bool operator!=(const vpRGBa &v) const
Definition: vpRGBa.cpp:127
bool operator==(const vpRGBa &v) const
Definition: vpRGBa.cpp:118
#define vpERROR_TRACE
Definition: vpDebug.h:393