Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
39
40#include <limits>
41#include <visp3/core/vpColor.h>
42#include <visp3/core/vpException.h>
43#include <visp3/core/vpRGBf.h>
44
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
81{
82 const unsigned int rows_size = 3;
83 const unsigned int index_0 = 0;
84 const unsigned int index_1 = 1;
85 const unsigned int index_2 = 2;
86
87 if (v.getRows() != rows_size) {
88 throw(vpException(vpException::dimensionError, "Bad vector dimension"));
89 }
90 R = static_cast<float>(v[index_0]);
91 G = static_cast<float>(v[index_1]);
92 B = static_cast<float>(v[index_2]);
93 return *this;
94}
95
101bool vpRGBf::operator==(const vpRGBf &v) const
102{
103 if (std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) {
104 return false;
105 }
106 if (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) {
107 return false;
108 }
109 if (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()) {
110 return false;
111 }
112
113 return true;
114}
115
120bool vpRGBf::operator!=(const vpRGBf &v) const
121{
122 return ((std::fabs(R - v.R) > std::numeric_limits<float>::epsilon()) ||
123 (std::fabs(G - v.G) > std::numeric_limits<float>::epsilon()) ||
124 (std::fabs(B - v.B) > std::numeric_limits<float>::epsilon()));
125}
126
133{
134 vpColVector n(3); // new color
135 const unsigned int index_0 = 0;
136 const unsigned int index_1 = 1;
137 const unsigned int index_2 = 2;
138 n[index_0] = static_cast<double>(R) - static_cast<double>(v.R);
139 n[index_1] = static_cast<double>(G) - static_cast<double>(v.G);
140 n[index_2] = static_cast<double>(B) - static_cast<double>(v.B);
141 return n;
142}
143
151{
152 vpRGBf n; // new color
153 n.R = R + v.R;
154 n.G = G + v.G;
155 n.B = B + v.B;
156 return n;
157}
158
165{
166 vpColVector n(3); // new color
167 const unsigned int index_0 = 0;
168 const unsigned int index_1 = 1;
169 const unsigned int index_2 = 2;
170 n[index_0] = R - v[index_0];
171 n[index_1] = G - v[index_1];
172 n[index_2] = B - v[index_2];
173 return n;
174}
175
182{
183 vpColVector n(3); // new color
184 const unsigned int index_0 = 0;
185 const unsigned int index_1 = 1;
186 const unsigned int index_2 = 2;
187 n[index_0] = R + v[index_0];
188 n[index_1] = G + v[index_1];
189 n[index_2] = B + v[index_2];
190 return n;
191}
192
199{
200 vpColVector n(3);
201 const unsigned int index_0 = 0;
202 const unsigned int index_1 = 1;
203 const unsigned int index_2 = 2;
204 n[index_0] = R * v;
205 n[index_1] = G * v;
206 n[index_2] = B * v;
207 return n;
208}
209
216{
217 vpColVector n(3);
218 const unsigned int index_0 = 0;
219 const unsigned int index_1 = 1;
220 const unsigned int index_2 = 2;
221 n[index_0] = R * v;
222 n[index_1] = G * v;
223 n[index_2] = B * v;
224 return n;
225}
226
227bool vpRGBf::operator<(const vpRGBf &v) const
228{
229 double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
230 double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
231
232 return (gray1 < gray2);
233}
234
235bool vpRGBf::operator>(const vpRGBf &v) const
236{
237 double gray1 = (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
238 double gray2 = (0.2126 * v.R) + (0.7152 * v.G) + (0.0722 * v.B);
239
240 return (gray1 > gray2);
241}
242
250vpRGBf operator*(double x, const vpRGBf &rgb)
251{
252 vpRGBf rgbf;
253 rgbf.R = static_cast<float>(rgb.R * x);
254 rgbf.G = static_cast<float>(rgb.G * x);
255 rgbf.B = static_cast<float>(rgb.B * x);
256 return rgbf;
257}
258
266vpRGBf operator*(float x, const vpRGBf &rgb)
267{
268 vpRGBf rgbf;
269 rgbf.R = rgb.R * x;
270 rgbf.G = rgb.G * x;
271 rgbf.B = rgb.B * x;
272 return rgbf;
273}
274
300VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBf &rgb)
301{
302 os << "(" << rgb.R << "," << rgb.G << "," << rgb.B << ")";
303 return os;
304}
305END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
vpColVector operator*(float v) const
Definition vpRGBf.cpp:198
vpRGBf()
Definition vpRGBf.h:75
float B
Blue component.
Definition vpRGBf.h:161
vpColVector operator-(const vpRGBf &v) const
Definition vpRGBf.cpp:132
bool operator>(const vpRGBf &v) const
Definition vpRGBf.cpp:235
bool operator==(const vpRGBf &v) const
Definition vpRGBf.cpp:101
vpRGBf & operator=(float v)
Definition vpRGBf.cpp:51
vpRGBf operator+(const vpRGBf &v) const
Definition vpRGBf.cpp:150
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpRGBf &rgb)
Definition vpRGBf.cpp:300
float G
Green component.
Definition vpRGBf.h:160
bool operator<(const vpRGBf &v) const
Definition vpRGBf.cpp:227
bool operator!=(const vpRGBf &v) const
Definition vpRGBf.cpp:120
float R
Red component.
Definition vpRGBf.h:159