Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpRGBa.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
39
40#ifndef VP_RGBA_H
41#define VP_RGBA_H
42
43#include <assert.h>
44
45#include <visp3/core/vpConfig.h>
46#include <visp3/core/vpColVector.h>
47
48#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
49#include <type_traits>
50#endif
51
52
54#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
55template <typename T, bool>
56class vpHSV;
57#endif
73class VISP_EXPORT vpRGBa
74{
75public:
77
83 inline vpRGBa() : R(0), G(0), B(0), A(vpRGBa::alpha_default) { }
84
95 inline vpRGBa(unsigned char r, unsigned char g, unsigned char b, unsigned char a = vpRGBa::alpha_default)
96 : R(r), G(g), B(b), A(a)
97 { }
98
106 VP_EXPLICIT inline vpRGBa(unsigned char v) : R(v), G(v), B(v), A(v) { }
107
115 VP_EXPLICIT inline vpRGBa(unsigned int v)
116 {
117 assert(v < 256);
118 unsigned char v_uc = static_cast<unsigned char>(v);
119 R = v_uc;
120 G = v_uc;
121 B = v_uc;
122 A = v_uc;
123 }
124
132 VP_EXPLICIT inline vpRGBa(int v)
133 {
134 assert(v >=0 && v < 256);
135 unsigned char v_uc = static_cast<unsigned char>(v);
136 R = v_uc;
137 G = v_uc;
138 B = v_uc;
139 A = v_uc;
140 }
141
142#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
143#ifndef VISP_PYTHON_PREPROCESSOR_RUNNING
151 template <typename T, bool useFullScale, typename std::enable_if<std::is_same<T, unsigned char>::value, int>::type = 0>
152 VP_EXPLICIT vpRGBa(const vpHSV<T, useFullScale> &hsv)
153 {
155 }
156
157 template <typename T, bool useFullScale, typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0>
158 VP_EXPLICIT vpRGBa(const vpHSV<T, useFullScale> &hsv)
159 {
161 }
162#endif
163
174 template<typename T, bool useFullScale, typename U = float >
175 typename std::enable_if<std::is_same<U, float>::value &&std::is_same<T, unsigned char>::value, vpRGBa &>::type
177 {
178 vpHSV<U, useFullScale> hsv(other);
179 buildFrom(hsv);
180 return *this;
181 }
182
195 template<typename T, bool useFullScale>
196 typename std::enable_if<std::is_floating_point<T>::value, vpRGBa &>::type
198 {
199 T hue = hsv.H, saturation = hsv.S, value = hsv.V;
200 T h = hue * static_cast<T>(6.0);
201 T s = saturation;
202 T v = value;
203
204 if (vpMath::equal(h, static_cast<T>(6.0), std::numeric_limits<T>::epsilon())) {
205 h = 0.0;
206 }
207
208 T f = h - static_cast<int>(h);
209 T p = v * static_cast<T>(1.0 - s);
210 T q = v * static_cast<T>(1.0 - (s * f));
211 T t = v * static_cast<T>(1.0 - (s * (1.0 - f)));
212
213 const int val_2 = 2;
214 const int val_3 = 3;
215 const int val_4 = 4;
216 switch (static_cast<int>(h)) {
217 case 0:
218 hue = v;
219 saturation = t;
220 value = p;
221 break;
222
223 case 1:
224 hue = q;
225 saturation = v;
226 value = p;
227 break;
228
229 case val_2:
230 hue = p;
231 saturation = v;
232 value = t;
233 break;
234
235 case val_3:
236 hue = p;
237 saturation = q;
238 value = v;
239 break;
240
241 case val_4:
242 hue = t;
243 saturation = p;
244 value = v;
245 break;
246
247 default: // case 5:
248 hue = v;
249 saturation = p;
250 value = q;
251 break;
252 }
253
254 R = static_cast<unsigned char>(vpMath::round(hue * 255.0));
255 G = static_cast<unsigned char>(vpMath::round(saturation * 255.0));
256 B = static_cast<unsigned char>(vpMath::round(value * 255.0));
258 return *this;
259 }
260#endif
261
265#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
266 inline vpRGBa(const vpRGBa &v) = default;
267#else
268 inline vpRGBa(const vpRGBa &v) : R(v.R), G(v.G), B(v.B), A(v.A) { }
269#endif
270
279 VP_EXPLICIT inline vpRGBa(const vpColVector &v) : R(0), G(0), B(0), A(vpRGBa::alpha_default) { *this = v; }
280
281 // We cannot add here the following destructor without changing the
282 // hypothesis that the size of this class is 4. With the destructor it
283 // becomes 16 that does break a lot of things around image conversions
284 // virtual ~vpRGBa() {}; // Not to implement
285
286 vpRGBa &operator=(const unsigned char &v);
287 vpRGBa &operator=(const unsigned int &v);
288 vpRGBa &operator=(const int &v);
289#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
290 vpRGBa &operator=(vpRGBa &&v) = default;
291 vpRGBa &operator=(const vpRGBa &v) = default;
292#else
293 vpRGBa &operator=(const vpRGBa &v)
294 {
295 this->R = v.R;
296 this->G = v.G;
297 this->B = v.B;
298 this->A = v.A;
299 return *this;
300 }
301#endif
302 vpRGBa &operator=(const vpColVector &v);
303 bool operator==(const vpRGBa &v) const;
304 bool operator!=(const vpRGBa &v) const;
305
306 vpColVector operator-(const vpRGBa &v) const;
307 vpRGBa operator+(const vpRGBa &v) const;
308 vpColVector operator-(const vpColVector &v) const;
309 vpColVector operator+(const vpColVector &v) const;
310 vpColVector operator*(const float &v) const;
311 vpColVector operator*(const double &v) const;
312
313 bool operator<(const vpRGBa &v) const;
314 bool operator>(const vpRGBa &v) const;
315
316 friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRGBa &rgba);
317
318#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
322 static constexpr unsigned char nbChannels = 4;
323#endif
324public:
325 unsigned char R;
326 unsigned char G;
327 unsigned char B;
328 unsigned char A;
329
330 friend VISP_EXPORT vpRGBa operator*(const double &x, const vpRGBa &rgb);
331};
332
333#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
334static_assert(std::is_trivially_assignable_v<vpRGBa, vpRGBa>);
335static_assert(std::is_trivially_copyable_v<vpRGBa>);
336#endif
337
338END_VISP_NAMESPACE
339#endif
Implementation of column vector and the associated operations.
Class implementing the HSV pixel format.
Definition vpHSV.h:135
T V
Definition vpHSV.h:539
T S
Definition vpHSV.h:538
T H
Definition vpHSV.h:537
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
static int round(double x)
Definition vpMath.h:413
vpRGBa(const vpRGBa &v)=default
vpRGBa & operator=(const vpRGBa &v)=default
unsigned char B
Blue component.
Definition vpRGBa.h:327
VP_EXPLICIT vpRGBa(const vpColVector &v)
Definition vpRGBa.h:279
std::enable_if< std::is_floating_point< T >::value, vpRGBa & >::type buildFrom(const vpHSV< T, useFullScale > &hsv)
Build a vpRGBa object from a vpHSV<double> or vpHSV<float> object.
Definition vpRGBa.h:197
VP_EXPLICIT vpRGBa(const vpHSV< T, useFullScale > &hsv)
Construct a new vpRGBa object from an vpHSV pbject.
Definition vpRGBa.h:152
vpRGBa(unsigned char r, unsigned char g, unsigned char b, unsigned char a=vpRGBa::alpha_default)
Definition vpRGBa.h:95
VP_EXPLICIT vpRGBa(unsigned int v)
Definition vpRGBa.h:115
vpRGBa & operator=(vpRGBa &&v)=default
unsigned char R
Red component.
Definition vpRGBa.h:325
VP_EXPLICIT vpRGBa(int v)
Definition vpRGBa.h:132
std::enable_if< std::is_same< U, float >::value &&std::is_same< T, unsignedchar >::value, vpRGBa & >::type buildFrom(const vpHSV< T, useFullScale > &other)
Build a vpRGBa object from a vpHSV<unsigned char> object.
Definition vpRGBa.h:176
static constexpr unsigned char nbChannels
Number of channels a vpRGBa object is made of.
Definition vpRGBa.h:322
vpRGBa()
Definition vpRGBa.h:83
unsigned char G
Green component.
Definition vpRGBa.h:326
AlphaDefault
Definition vpRGBa.h:76
@ alpha_default
Definition vpRGBa.h:76
VP_EXPLICIT vpRGBa(unsigned char v)
Definition vpRGBa.h:106
unsigned char A
Additional component.
Definition vpRGBa.h:328
vpRGBa & operator=(const unsigned char &v)
Definition vpRGBa.cpp:53
vpColVector operator*(const float &v) const
Definition vpRGBa.cpp:212