Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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 
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  if (v.getRows() != 4) {
127  throw(vpException(vpException::dimensionError, "Bad vector dimension "));
128  }
129  const unsigned int index_0 = 0;
130  const unsigned int index_1 = 1;
131  const unsigned int index_2 = 2;
132  const unsigned int index_3 = 3;
133  R = static_cast<unsigned char>(v[index_0]);
134  G = static_cast<unsigned char>(v[index_1]);
135  B = static_cast<unsigned char>(v[index_2]);
136  A = static_cast<unsigned char>(v[index_3]);
137  return *this;
138 }
139 
145 bool vpRGBa::operator==(const vpRGBa &v) const
146 {
147  return (R == v.R) && (G == v.G) && (B == v.B) && (A == v.A);
148 }
154 bool vpRGBa::operator!=(const vpRGBa &v) const { return ((R != v.R) || (G != v.G) || (B != v.B) || (A != v.A)); }
155 
162 {
163  vpColVector n(4); // new color
164  const unsigned int index_0 = 0;
165  const unsigned int index_1 = 1;
166  const unsigned int index_2 = 2;
167  const unsigned int index_3 = 3;
168  n[index_0] = static_cast<double>(R) - static_cast<double>(v.R);
169  n[index_1] = static_cast<double>(G) - static_cast<double>(v.G);
170  n[index_2] = static_cast<double>(B) - static_cast<double>(v.B);
171  n[index_3] = static_cast<double>(A) - static_cast<double>(v.A);
172  return n;
173 }
174 
182 {
183  vpRGBa n; // new color
184  n.R = static_cast<unsigned char>(R + v.R);
185  n.G = static_cast<unsigned char>(G + v.G);
186  n.B = static_cast<unsigned char>(B + v.B);
187  n.A = static_cast<unsigned char>(A + v.A);
188  return n;
189 }
190 
197 {
198  vpColVector n(4); // new color
199  const unsigned int index_0 = 0;
200  const unsigned int index_1 = 1;
201  const unsigned int index_2 = 2;
202  const unsigned int index_3 = 3;
203  n[index_0] = R - v[index_0];
204  n[index_1] = G - v[index_1];
205  n[index_2] = B - v[index_2];
206  n[index_3] = A - v[index_3];
207  return n;
208 }
209 
216 {
217  vpColVector n(4); // new color
218  const unsigned int index_0 = 0;
219  const unsigned int index_1 = 1;
220  const unsigned int index_2 = 2;
221  const unsigned int index_3 = 3;
222  n[index_0] = R + v[index_0];
223  n[index_1] = G + v[index_1];
224  n[index_2] = B + v[index_2];
225  n[index_3] = A + v[index_3];
226  return n;
227 }
228 
234 vpColVector vpRGBa::operator*(const float &v) const
235 {
236  vpColVector n(4);
237  const unsigned int index_0 = 0;
238  const unsigned int index_1 = 1;
239  const unsigned int index_2 = 2;
240  const unsigned int index_3 = 3;
241  n[index_0] = R * v;
242  n[index_1] = G * v;
243  n[index_2] = B * v;
244  n[index_3] = A * v;
245  return n;
246 }
247 
253 vpColVector vpRGBa::operator*(const double &v) const
254 {
255  vpColVector n(4);
256  const unsigned int index_0 = 0;
257  const unsigned int index_1 = 1;
258  const unsigned int index_2 = 2;
259  const unsigned int index_3 = 3;
260  n[index_0] = R * v;
261  n[index_1] = G * v;
262  n[index_2] = B * v;
263  n[index_3] = A * v;
264  return n;
265 }
266 
267 bool vpRGBa::operator<(const vpRGBa &v) const
268 {
269  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
270  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
271 
272  return (gray1 < gray2);
273 }
274 
275 bool vpRGBa::operator>(const vpRGBa &v) const
276 {
277  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
278  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
279 
280  return (gray1 > gray2);
281 }
282 
290 vpRGBa operator*(const double &x, const vpRGBa &rgb)
291 {
292  vpRGBa rgba;
293  rgba.R = static_cast<unsigned char>(rgb.R * x);
294  rgba.G = static_cast<unsigned char>(rgb.G * x);
295  rgba.B = static_cast<unsigned char>(rgb.B * x);
296  rgba.A = rgb.A;
297  return rgba;
298 }
299 
325 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba)
326 {
327  os << "(" << static_cast<int>(rgba.R) << "," << static_cast<int>(rgba.G) << "," << static_cast<int>(rgba.B) << "," << static_cast<int>(rgba.A) << ")";
328  return os;
329 }
330 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
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:161
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:267
vpRGBa operator+(const vpRGBa &v) const
Definition: vpRGBa.cpp:181
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:275
vpRGBa & operator=(const unsigned char &v)
Definition: vpRGBa.cpp:50
vpColVector operator*(const float &v) const
Definition: vpRGBa.cpp:234
bool operator!=(const vpRGBa &v) const
Definition: vpRGBa.cpp:154
bool operator==(const vpRGBa &v) const
Definition: vpRGBa.cpp:145