Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpRGBf.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  * 32-bit floating point RGB pixel.
32  */
33 
40 #include <limits>
41 #include <visp3/core/vpColor.h>
42 #include <visp3/core/vpException.h>
43 #include <visp3/core/vpRGBf.h>
44 
45 BEGIN_VISP_NAMESPACE
52 {
53  this->R = v;
54  this->G = v;
55  this->B = v;
56  return *this;
57 }
58 
65 {
66  this->R = static_cast<float>(v);
67  this->G = static_cast<float>(v);
68  this->B = static_cast<float>(v);
69  return *this;
70 }
71 
76 {
77  this->R = v.R;
78  this->G = v.G;
79  this->B = v.B;
80  return *this;
81 }
82 
83 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
88 {
89  this->R = std::move(v.R);
90  this->G = std::move(v.G);
91  this->B = std::move(v.B);
92  return *this;
93 }
94 #endif
95 
105 {
106  const unsigned int rows_size = 3;
107  const unsigned int index_0 = 0;
108  const unsigned int index_1 = 1;
109  const unsigned int index_2 = 2;
110 
111  if (v.getRows() != rows_size) {
112  throw(vpException(vpException::dimensionError, "Bad vector dimension"));
113  }
114  R = static_cast<float>(v[index_0]);
115  G = static_cast<float>(v[index_1]);
116  B = static_cast<float>(v[index_2]);
117  return *this;
118 }
119 
125 bool vpRGBf::operator==(const vpRGBf &v) const
126 {
127  if (std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) {
128  return false;
129  }
130  if (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) {
131  return false;
132  }
133  if (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()) {
134  return false;
135  }
136 
137  return true;
138 }
144 bool vpRGBf::operator!=(const vpRGBf &v) const
145 {
146  return ((std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) ||
147  (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) ||
148  (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()));
149 }
150 
157 {
158  vpColVector n(3); // new color
159  const unsigned int index_0 = 0;
160  const unsigned int index_1 = 1;
161  const unsigned int index_2 = 2;
162  n[index_0] = static_cast<double>(R) - static_cast<double>(v.R);
163  n[index_1] = static_cast<double>(G) - static_cast<double>(v.G);
164  n[index_2] = static_cast<double>(B) - static_cast<double>(v.B);
165  return n;
166 }
167 
175 {
176  vpRGBf n; // new color
177  n.R = R + v.R;
178  n.G = G + v.G;
179  n.B = B + v.B;
180  return n;
181 }
182 
189 {
190  vpColVector n(3); // new color
191  const unsigned int index_0 = 0;
192  const unsigned int index_1 = 1;
193  const unsigned int index_2 = 2;
194  n[index_0] = R - v[index_0];
195  n[index_1] = G - v[index_1];
196  n[index_2] = B - v[index_2];
197  return n;
198 }
199 
206 {
207  vpColVector n(3); // new color
208  const unsigned int index_0 = 0;
209  const unsigned int index_1 = 1;
210  const unsigned int index_2 = 2;
211  n[index_0] = R + v[index_0];
212  n[index_1] = G + v[index_1];
213  n[index_2] = B + v[index_2];
214  return n;
215 }
216 
223 {
224  vpColVector n(3);
225  const unsigned int index_0 = 0;
226  const unsigned int index_1 = 1;
227  const unsigned int index_2 = 2;
228  n[index_0] = R * v;
229  n[index_1] = G * v;
230  n[index_2] = B * v;
231  return n;
232 }
233 
240 {
241  vpColVector n(3);
242  const unsigned int index_0 = 0;
243  const unsigned int index_1 = 1;
244  const unsigned int index_2 = 2;
245  n[index_0] = R * v;
246  n[index_1] = G * v;
247  n[index_2] = B * v;
248  return n;
249 }
250 
251 bool vpRGBf::operator<(const vpRGBf &v) const
252 {
253  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
254  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
255 
256  return (gray1 < gray2);
257 }
258 
259 bool vpRGBf::operator>(const vpRGBf &v) const
260 {
261  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
262  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
263 
264  return (gray1 > gray2);
265 }
266 
274 vpRGBf operator*(double x, const vpRGBf &rgb)
275 {
276  vpRGBf rgbf;
277  rgbf.R = static_cast<float>(rgb.R * x);
278  rgbf.G = static_cast<float>(rgb.G * x);
279  rgbf.B = static_cast<float>(rgb.B * x);
280  return rgbf;
281 }
282 
290 vpRGBf operator*(float x, const vpRGBf &rgb)
291 {
292  vpRGBf rgbf;
293  rgbf.R = rgb.R * x;
294  rgbf.G = rgb.G * x;
295  rgbf.B = rgb.B * x;
296  return rgbf;
297 }
298 
324 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBf &rgb)
325 {
326  os << "(" << rgb.R << "," << rgb.G << "," << rgb.B << ")";
327  return os;
328 }
329 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: vpRGBf.h:60
vpColVector operator*(float v) const
Definition: vpRGBf.cpp:222
float B
Blue component.
Definition: vpRGBf.h:142
vpColVector operator-(const vpRGBf &v) const
Definition: vpRGBf.cpp:156
bool operator>(const vpRGBf &v) const
Definition: vpRGBf.cpp:259
bool operator==(const vpRGBf &v) const
Definition: vpRGBf.cpp:125
vpRGBf & operator=(float v)
Definition: vpRGBf.cpp:51
vpRGBf operator+(const vpRGBf &v) const
Definition: vpRGBf.cpp:174
float G
Green component.
Definition: vpRGBf.h:141
bool operator<(const vpRGBf &v) const
Definition: vpRGBf.cpp:251
bool operator!=(const vpRGBf &v) const
Definition: vpRGBf.cpp:144
float R
Red component.
Definition: vpRGBf.h:140