Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
vpRGBa.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
42 #include <visp3/core/vpColor.h>
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpException.h>
45 #include <visp3/core/vpRGBa.h>
46 
52 vpRGBa &vpRGBa::operator=(const unsigned char &v)
53 {
54  this->R = v;
55  this->G = v;
56  this->B = v;
57  this->A = v;
58  return *this;
59 }
60 
65 {
66  this->R = v.R;
67  this->G = v.G;
68  this->B = v.B;
69  this->A = v.A;
70  return *this;
71 }
72 
73 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
78 {
79  this->R = std::move(v.R);
80  this->G = std::move(v.G);
81  this->B = std::move(v.B);
82  this->A = std::move(v.A);
83  return *this;
84 }
85 #endif
86 
97 {
98  if (v.getRows() != 4) {
99  vpERROR_TRACE("Bad vector dimension ");
100  throw(vpException(vpException::dimensionError, "Bad vector dimension "));
101  }
102  R = (unsigned char)v[0];
103  G = (unsigned char)v[1];
104  B = (unsigned char)v[2];
105  A = (unsigned char)v[3];
106  return *this;
107 }
108 
114 bool vpRGBa::operator==(const vpRGBa &v) const
115 {
116  return R == v.R && G == v.G && B == v.B && A == v.A;
117 }
123 bool vpRGBa::operator!=(const vpRGBa &v) const { return (R != v.R || G != v.G || B != v.B || A != v.A); }
124 
131 {
132  vpColVector n(4); // new color
133  n[0] = (double)R - (double)v.R;
134  n[1] = (double)G - (double)v.G;
135  n[2] = (double)B - (double)v.B;
136  n[3] = (double)A - (double)v.A;
137  return n;
138 }
139 
147 {
148  vpRGBa n; // new color
149  n.R = static_cast<unsigned char>(R + v.R);
150  n.G = static_cast<unsigned char>(G + v.G);
151  n.B = static_cast<unsigned char>(B + v.B);
152  n.A = static_cast<unsigned char>(A + v.A);
153  return n;
154 }
155 
162 {
163  vpColVector n(4); // new color
164  n[0] = R - v[0];
165  n[1] = G - v[1];
166  n[2] = B - v[2];
167  n[3] = A - v[3];
168  return n;
169 }
170 
177 {
178  vpColVector n(4); // new color
179  n[0] = R + v[0];
180  n[1] = G + v[1];
181  n[2] = B + v[2];
182  n[3] = A + v[3];
183  return n;
184 }
185 
191 vpColVector vpRGBa::operator*(const float &v) const
192 {
193  vpColVector n(4);
194  n[0] = R * v;
195  n[1] = G * v;
196  n[2] = B * v;
197  n[3] = A * v;
198  return n;
199 }
200 
206 vpColVector vpRGBa::operator*(const double &v) const
207 {
208  vpColVector n(4);
209  n[0] = R * v;
210  n[1] = G * v;
211  n[2] = B * v;
212  n[3] = A * v;
213  return n;
214 }
215 
216 bool vpRGBa::operator<(const vpRGBa &v) const
217 {
218  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
219  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
220 
221  return (gray1 < gray2);
222 }
223 
224 bool vpRGBa::operator>(const vpRGBa &v) const
225 {
226  double gray1 = 0.2126 * R + 0.7152 * G + 0.0722 * B;
227  double gray2 = 0.2126 * v.R + 0.7152 * v.G + 0.0722 * v.B;
228 
229  return (gray1 > gray2);
230 }
231 
232 vpRGBa operator*(const double &x, const vpRGBa &rgb) { return rgb * x; }
233 
256 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba)
257 {
258  os << "(" << (int)rgba.R << "," << (int)rgba.G << "," << (int)rgba.B << "," << (int)rgba.A << ")";
259  return os;
260 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:523
unsigned int getRows() const
Definition: vpArray2D.h:284
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
Definition: vpRGBa.h:61
vpColVector operator-(const vpRGBa &v) const
Definition: vpRGBa.cpp:130
unsigned char B
Blue component.
Definition: vpRGBa.h:139
unsigned char R
Red component.
Definition: vpRGBa.h:137
bool operator<(const vpRGBa &v) const
Definition: vpRGBa.cpp:216
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:52
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:146
unsigned char G
Green component.
Definition: vpRGBa.h:138
unsigned char A
Additionnal component.
Definition: vpRGBa.h:140
bool operator>(const vpRGBa &v) const
Definition: vpRGBa.cpp:224
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:191
bool operator!=(const vpRGBa &v) const
Definition: vpRGBa.cpp:123
bool operator==(const vpRGBa &v) const
Definition: vpRGBa.cpp:114
#define vpERROR_TRACE
Definition: vpDebug.h:382