Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpRGBf.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  * 32-bit floating point RGB pixel.
33  *
34 *****************************************************************************/
35 
42 #include <limits>
43 #include <visp3/core/vpColor.h>
44 #include <visp3/core/vpDebug.h>
45 #include <visp3/core/vpException.h>
46 #include <visp3/core/vpRGBf.h>
47 
54 {
55  this->R = v;
56  this->G = v;
57  this->B = v;
58  return *this;
59 }
60 
65 {
66  this->R = v.R;
67  this->G = v.G;
68  this->B = v.B;
69  return *this;
70 }
71 
72 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
77 {
78  this->R = std::move(v.R);
79  this->G = std::move(v.G);
80  this->B = std::move(v.B);
81  return *this;
82 }
83 #endif
84 
94 {
95  if (v.getRows() != 3) {
96  vpERROR_TRACE("Bad vector dimension");
97  throw(vpException(vpException::dimensionError, "Bad vector dimension"));
98  }
99  R = static_cast<float>(v[0]);
100  G = static_cast<float>(v[1]);
101  B = static_cast<float>(v[2]);
102  return *this;
103 }
104 
110 bool vpRGBf::operator==(const vpRGBf &v) const
111 {
112  if (std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) {
113  return false;
114  }
115  if (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) {
116  return false;
117  }
118  if (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()) {
119  return false;
120  }
121 
122  return true;
123 }
129 bool vpRGBf::operator!=(const vpRGBf &v) const
130 {
131  return ((std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) ||
132  (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) ||
133  (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()));
134 }
135 
142 {
143  vpColVector n(3); // new color
144  n[0] = static_cast<double>(R) - static_cast<double>(v.R);
145  n[1] = static_cast<double>(G) - static_cast<double>(v.G);
146  n[2] = static_cast<double>(B) - static_cast<double>(v.B);
147  return n;
148 }
149 
157 {
158  vpRGBf n; // new color
159  n.R = R + v.R;
160  n.G = G + v.G;
161  n.B = B + v.B;
162  return n;
163 }
164 
171 {
172  vpColVector n(3); // new color
173  n[0] = R - v[0];
174  n[1] = G - v[1];
175  n[2] = B - v[2];
176  return n;
177 }
178 
185 {
186  vpColVector n(3); // new color
187  n[0] = R + v[0];
188  n[1] = G + v[1];
189  n[2] = B + v[2];
190  return n;
191 }
192 
199 {
200  vpColVector n(3);
201  n[0] = R * v;
202  n[1] = G * v;
203  n[2] = B * v;
204  return n;
205 }
206 
213 {
214  vpColVector n(3);
215  n[0] = R * v;
216  n[1] = G * v;
217  n[2] = B * v;
218  return n;
219 }
220 
221 bool vpRGBf::operator<(const vpRGBf &v) const
222 {
223  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
224  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
225 
226  return (gray1 < gray2);
227 }
228 
229 bool vpRGBf::operator>(const vpRGBf &v) const
230 {
231  double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
232  double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
233 
234  return (gray1 > gray2);
235 }
236 
237 vpRGBf operator*(double x, const vpRGBf &rgb) { return rgb * x; }
238 
260 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBf &rgb)
261 {
262  os << "(" << rgb.R << "," << rgb.G << "," << rgb.B << ")";
263  return os;
264 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:600
unsigned int getRows() const
Definition: vpArray2D.h:337
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: vpRGBf.h:57
vpColVector operator*(float v) const
Definition: vpRGBf.cpp:198
float B
Blue component.
Definition: vpRGBf.h:126
vpRGBf & operator=(float v)
Definition: vpRGBf.cpp:53
vpColVector operator-(const vpRGBf &v) const
Definition: vpRGBf.cpp:141
bool operator>(const vpRGBf &v) const
Definition: vpRGBf.cpp:229
bool operator==(const vpRGBf &v) const
Definition: vpRGBf.cpp:110
vpRGBf operator+(const vpRGBf &v) const
Definition: vpRGBf.cpp:156
float G
Green component.
Definition: vpRGBf.h:125
bool operator<(const vpRGBf &v) const
Definition: vpRGBf.cpp:221
bool operator!=(const vpRGBf &v) const
Definition: vpRGBf.cpp:129
float R
Red component.
Definition: vpRGBf.h:124
#define vpERROR_TRACE
Definition: vpDebug.h:382