Visual Servoing Platform  version 3.6.1 under development (2024-12-09)
vpRGBa.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
40 #include <visp3/core/vpColor.h>
41 #include <visp3/core/vpException.h>
42 #include <visp3/core/vpRGBa.h>
43 
44 BEGIN_VISP_NAMESPACE
50 vpRGBa &vpRGBa::operator=(const unsigned char &v)
51 {
52  this->R = v;
53  this->G = v;
54  this->B = v;
55  this->A = v;
56  return *this;
57 }
58 
64 vpRGBa &vpRGBa::operator=(const unsigned int &v)
65 {
66  assert(v < 256);
67  this->R = v;
68  this->G = v;
69  this->B = v;
70  this->A = v;
71  return *this;
72 }
73 
79 vpRGBa &vpRGBa::operator=(const int &v)
80 {
81  assert(v < 256);
82  this->R = v;
83  this->G = v;
84  this->B = v;
85  this->A = v;
86  return *this;
87 }
88 
93 {
94  this->R = v.R;
95  this->G = v.G;
96  this->B = v.B;
97  this->A = v.A;
98  return *this;
99 }
100 
101 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
106 {
107  this->R = std::move(v.R);
108  this->G = std::move(v.G);
109  this->B = std::move(v.B);
110  this->A = std::move(v.A);
111  return *this;
112 }
113 #endif
114 
125 {
126  const unsigned int val_4 = 4;
127  if (v.getRows() != val_4) {
128  throw(vpException(vpException::dimensionError, "Bad vector dimension "));
129  }
130  const unsigned int index_0 = 0;
131  const unsigned int index_1 = 1;
132  const unsigned int index_2 = 2;
133  const unsigned int index_3 = 3;
134  R = static_cast<unsigned char>(v[index_0]);
135  G = static_cast<unsigned char>(v[index_1]);
136  B = static_cast<unsigned char>(v[index_2]);
137  A = static_cast<unsigned char>(v[index_3]);
138  return *this;
139 }
140 
146 bool vpRGBa::operator==(const vpRGBa &v) const
147 {
148  return (R == v.R) && (G == v.G) && (B == v.B) && (A == v.A);
149 }
155 bool vpRGBa::operator!=(const vpRGBa &v) const { return ((R != v.R) || (G != v.G) || (B != v.B) || (A != v.A)); }
156 
163 {
164  vpColVector n(4); // new color
165  const unsigned int index_0 = 0;
166  const unsigned int index_1 = 1;
167  const unsigned int index_2 = 2;
168  const unsigned int index_3 = 3;
169  n[index_0] = static_cast<double>(R) - static_cast<double>(v.R);
170  n[index_1] = static_cast<double>(G) - static_cast<double>(v.G);
171  n[index_2] = static_cast<double>(B) - static_cast<double>(v.B);
172  n[index_3] = static_cast<double>(A) - static_cast<double>(v.A);
173  return n;
174 }
175 
183 {
184  vpRGBa n; // new color
185  n.R = static_cast<unsigned char>(R + v.R);
186  n.G = static_cast<unsigned char>(G + v.G);
187  n.B = static_cast<unsigned char>(B + v.B);
188  n.A = static_cast<unsigned char>(A + v.A);
189  return n;
190 }
191 
198 {
199  vpColVector n(4); // new color
200  const unsigned int index_0 = 0;
201  const unsigned int index_1 = 1;
202  const unsigned int index_2 = 2;
203  const unsigned int index_3 = 3;
204  n[index_0] = R - v[index_0];
205  n[index_1] = G - v[index_1];
206  n[index_2] = B - v[index_2];
207  n[index_3] = A - v[index_3];
208  return n;
209 }
210 
217 {
218  vpColVector n(4); // new color
219  const unsigned int index_0 = 0;
220  const unsigned int index_1 = 1;
221  const unsigned int index_2 = 2;
222  const unsigned int index_3 = 3;
223  n[index_0] = R + v[index_0];
224  n[index_1] = G + v[index_1];
225  n[index_2] = B + v[index_2];
226  n[index_3] = A + v[index_3];
227  return n;
228 }
229 
235 vpColVector vpRGBa::operator*(const float &v) const
236 {
237  vpColVector n(4);
238  const unsigned int index_0 = 0;
239  const unsigned int index_1 = 1;
240  const unsigned int index_2 = 2;
241  const unsigned int index_3 = 3;
242  n[index_0] = R * v;
243  n[index_1] = G * v;
244  n[index_2] = B * v;
245  n[index_3] = A * v;
246  return n;
247 }
248 
254 vpColVector vpRGBa::operator*(const double &v) const
255 {
256  vpColVector n(4);
257  const unsigned int index_0 = 0;
258  const unsigned int index_1 = 1;
259  const unsigned int index_2 = 2;
260  const unsigned int index_3 = 3;
261  n[index_0] = R * v;
262  n[index_1] = G * v;
263  n[index_2] = B * v;
264  n[index_3] = A * v;
265  return n;
266 }
267 
268 bool vpRGBa::operator<(const vpRGBa &v) const
269 {
270  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
271  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
272 
273  return (gray1 < gray2);
274 }
275 
276 bool vpRGBa::operator>(const vpRGBa &v) const
277 {
278  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
279  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
280 
281  return (gray1 > gray2);
282 }
283 
291 vpRGBa operator*(const double &x, const vpRGBa &rgb)
292 {
293  vpRGBa rgba;
294  rgba.R = static_cast<unsigned char>(rgb.R * x);
295  rgba.G = static_cast<unsigned char>(rgb.G * x);
296  rgba.B = static_cast<unsigned char>(rgb.B * x);
297  rgba.A = rgb.A;
298  return rgba;
299 }
300 
326 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba)
327 {
328  os << "(" << static_cast<int>(rgba.R) << "," << static_cast<int>(rgba.G) << "," << static_cast<int>(rgba.B) << "," << static_cast<int>(rgba.A) << ")";
329  return os;
330 }
331 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
unsigned int getRows() const
Definition: vpArray2D.h:347
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Definition: vpRGBa.h:65
vpColVector operator-(const vpRGBa &v) const
Definition: vpRGBa.cpp:162
unsigned char B
Blue component.
Definition: vpRGBa.h:169
unsigned char R
Red component.
Definition: vpRGBa.h:167
bool operator<(const vpRGBa &v) const
Definition: vpRGBa.cpp:268
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:182
unsigned char G
Green component.
Definition: vpRGBa.h:168
unsigned char A
Additionnal component.
Definition: vpRGBa.h:170
bool operator>(const vpRGBa &v) const
Definition: vpRGBa.cpp:276
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:50
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:235
bool operator!=(const vpRGBa &v) const
Definition: vpRGBa.cpp:155
bool operator==(const vpRGBa &v) const
Definition: vpRGBa.cpp:146