Zivid C++ API 2.18.0+1b44dbef-1
Color.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of the Zivid API
3 *
4 * Copyright 2015-2026 (C) Zivid AS
5 * All rights reserved.
6 *
7 * Zivid Software License, v1.0
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of Zivid AS nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without specific
21 * prior written permission.
22 *
23 * 4. This software, with or without modification, must not be used with any
24 * other 3D camera than from Zivid AS.
25 *
26 * 5. Any software provided in binary form under this license must not be
27 * reverse engineered, decompiled, modified and/or disassembled.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
41 * Info: http://www.zivid.com
42 ******************************************************************************/
43
44#pragma once
45
48
49#include <cstdint>
50#include <iosfwd>
51#include <string>
52#include <type_traits>
53
54namespace Zivid
55{
56#ifndef NO_DOC
58 namespace Detail
59 {
60 ZIVID_UTILS_EXPORT std::string rgbaIntsToString(int r, int g, int b, int a);
61 ZIVID_UTILS_EXPORT std::string bgraIntsToString(int b, int g, int r, int a);
62 ZIVID_UTILS_EXPORT std::string rgbIntsToString(int r, int g, int b);
63 ZIVID_UTILS_EXPORT std::string bgrIntsToString(int b, int g, int r);
64
65 template<typename T>
66 std::string rgbaToString(T r, T g, T b, T a)
67 {
68 return rgbaIntsToString(static_cast<int>(r), static_cast<int>(g), static_cast<int>(b), static_cast<int>(a));
69 }
70
71 template<typename T>
72 std::string bgraToString(T b, T g, T r, T a)
73 {
74 return bgraIntsToString(static_cast<int>(b), static_cast<int>(g), static_cast<int>(r), static_cast<int>(a));
75 }
76
77 template<typename T>
78 std::string rgbToString(T r, T g, T b)
79 {
80 return rgbIntsToString(static_cast<int>(r), static_cast<int>(g), static_cast<int>(b));
81 }
82
83 template<typename T>
84 std::string bgrToString(T b, T g, T r)
85 {
86 return bgrIntsToString(static_cast<int>(b), static_cast<int>(g), static_cast<int>(r));
87 }
88 } // namespace Detail
89#endif
90
91 struct ColorBGRA;
92 struct ColorSRGB;
94 struct ColorRGB_SRGB;
95 struct ColorBGR_SRGB;
96
97 template<typename T>
99 {
101 using ValueType = T;
102
104 ColorRGBABase() = default;
105
107 ColorRGBABase(T red, T green, T blue, T alpha)
108 : r{ red }
109 , g{ green }
110 , b{ blue }
111 , a{ alpha }
112 {}
113
115 T r;
116
118 T g;
119
121 T b;
122
124 T a;
125
128
131
133 std::string toString() const
134 {
135 return Detail::rgbaToString(r, g, b, a);
136 }
137 };
138
140 template<typename T>
141 std::ostream &operator<<(std::ostream &stream, const ColorRGBABase<T> &color)
142 {
143 return stream << color.toString();
144 }
145
159 struct ColorSRGB : public ColorRGBABase<uint8_t>
160 {
162 };
163
165
177
188 struct ColorRGBA : public ColorRGBABase<uint8_t>
189 {
191 };
192
194
195 template<typename T>
197 {
199 using ValueType = T;
200
202 ColorBGRABase() = default;
203
205 ColorBGRABase(T blue, T green, T red, T alpha)
206 : b{ blue }
207 , g{ green }
208 , r{ red }
209 , a{ alpha }
210 {}
211
213 T b;
214
216 T g;
217
219 T r;
220
222 T a;
223
226
229
231 std::string toString() const
232 {
233 return Detail::bgraToString(b, g, r, a);
234 }
235 };
236
238 template<typename T>
239 std::ostream &operator<<(std::ostream &stream, const ColorBGRABase<T> &color)
240 {
241 return stream << color.toString();
242 }
243
254 struct ColorBGRA : public ColorBGRABase<uint8_t>
255 {
257 };
258
260
272 struct ColorBGRA_SRGB : public ColorBGRABase<uint8_t>
273 {
275 };
276
278
284 struct ColorRGBAf : public ColorRGBABase<float>
285 {
287 };
288
290
291 template<typename T>
293 {
295 using ValueType = T;
296
298 ColorRGBBase() = default;
299
301 ColorRGBBase(T red, T green, T blue)
302 : r{ red }
303 , g{ green }
304 , b{ blue }
305 {}
306
308 T r;
309
311 T g;
312
314 T b;
315
318
321
323 std::string toString() const
324 {
325 return Detail::rgbToString(r, g, b);
326 }
327 };
328
330 template<typename T>
331 std::ostream &operator<<(std::ostream &stream, const ColorRGBBase<T> &color)
332 {
333 return stream << color.toString();
334 }
335
345 struct ColorRGB : public ColorRGBBase<uint8_t>
346 {
348 };
349
351
360 struct ColorRGB_SRGB : public ColorRGBBase<uint8_t>
361 {
363 };
364
366
367 template<typename T>
369 {
371 using ValueType = T;
372
374 ColorBGRBase() = default;
375
377 ColorBGRBase(T blue, T green, T red)
378 : b{ blue }
379 , g{ green }
380 , r{ red }
381 {}
382
384 T b;
385
387 T g;
388
390 T r;
391
394
397
399 std::string toString() const
400 {
401 return Detail::bgrToString(b, g, r);
402 }
403 };
404
406 template<typename T>
407 std::ostream &operator<<(std::ostream &stream, const ColorBGRBase<T> &color)
408 {
409 return stream << color.toString();
410 }
411
421 struct ColorBGR : public ColorBGRBase<uint8_t>
422 {
424 };
425
427
436 struct ColorBGR_SRGB : public ColorBGRBase<uint8_t>
437 {
439 };
440
442} // namespace Zivid
#define ZIVID_STATIC_ASSERT_DATA_FORMAT_TYPE(Type, size)
Definition DataFormatUtils.h:48
#define ZIVID_UTILS_EXPORT
Definition UtilsExport.h:56
Definition EnvironmentInfo.h:74
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:85
ColorSRGB ColorRGBA_SRGB
Color with 8-bit red, green, blue and alpha channels in the sRGB color space.
Definition Color.h:93
std::ostream & operator<<(std::ostream &stream, const Array1D< T > &array)
Serialize array information to a stream.
Definition Array1D.h:183
Definition Color.h:197
ColorBGRABase(T blue, T green, T red, T alpha)
Constructor.
Definition Color.h:205
ZIVID_UTILS_EXPORT bool operator!=(const ColorBGRABase &rhs) const
Check for inequality.
T g
Green channel.
Definition Color.h:216
ColorBGRABase()=default
Constructor.
T a
Alpha channel.
Definition Color.h:222
T ValueType
The type of the channel values.
Definition Color.h:199
T r
Red channel.
Definition Color.h:219
ZIVID_UTILS_EXPORT bool operator==(const ColorBGRABase &rhs) const
Check for equality.
T b
Blue channel.
Definition Color.h:213
std::string toString() const
Get string representation.
Definition Color.h:231
Color with 8-bit blue, green, red and alpha channels in the sRGB color space.
Definition Color.h:273
ColorBGRABase()=default
Constructor.
Color with 8-bit blue, green, red and alpha channels.
Definition Color.h:255
ColorBGRABase()=default
Constructor.
Definition Color.h:369
T b
Blue channel.
Definition Color.h:384
std::string toString() const
Get string representation.
Definition Color.h:399
ColorBGRBase(T blue, T green, T red)
Constructor.
Definition Color.h:377
ZIVID_UTILS_EXPORT bool operator==(const ColorBGRBase &rhs) const
Check for equality.
ColorBGRBase()=default
Constructor.
T g
Green channel.
Definition Color.h:387
T ValueType
The type of the channel values.
Definition Color.h:371
ZIVID_UTILS_EXPORT bool operator!=(const ColorBGRBase &rhs) const
Check for inequality.
T r
Red channel.
Definition Color.h:390
Color with 8-bit blue, green and red channels (no alpha) in the sRGB color space.
Definition Color.h:437
ColorBGRBase()=default
Constructor.
Color with 8-bit blue, green and red channels (no alpha)
Definition Color.h:422
ColorBGRBase()=default
Constructor.
Definition Color.h:99
ZIVID_UTILS_EXPORT bool operator!=(const ColorRGBABase &rhs) const
Check for inequality.
T ValueType
The type of the channel values.
Definition Color.h:101
std::string toString() const
Get string representation.
Definition Color.h:133
T g
Green channel.
Definition Color.h:118
ZIVID_UTILS_EXPORT bool operator==(const ColorRGBABase &rhs) const
Check for equality.
ColorRGBABase()=default
Constructor.
T a
Alpha channel.
Definition Color.h:124
T r
Red channel.
Definition Color.h:115
T b
Blue channel.
Definition Color.h:121
ColorRGBABase(T red, T green, T blue, T alpha)
Constructor.
Definition Color.h:107
Color with 8-bit red, green, blue and alpha channels.
Definition Color.h:189
ColorRGBABase()=default
Constructor.
Color with 32-bit red, green, blue and alpha channels.
Definition Color.h:285
ColorRGBABase()=default
Constructor.
Definition Color.h:293
T r
Red channel.
Definition Color.h:308
T g
Green channel.
Definition Color.h:311
std::string toString() const
Get string representation.
Definition Color.h:323
ZIVID_UTILS_EXPORT bool operator!=(const ColorRGBBase &rhs) const
Check for inequality.
ColorRGBBase()=default
Constructor.
ColorRGBBase(T red, T green, T blue)
Constructor.
Definition Color.h:301
T ValueType
The type of the channel values.
Definition Color.h:295
ZIVID_UTILS_EXPORT bool operator==(const ColorRGBBase &rhs) const
Check for equality.
T b
Blue channel.
Definition Color.h:314
Color with 8-bit red, green and blue channels (no alpha) in the sRGB color space.
Definition Color.h:361
ColorRGBBase()=default
Constructor.
Color with 8-bit red, green and blue channels (no alpha)
Definition Color.h:346
ColorRGBBase()=default
Constructor.
Color with 8-bit red, green, blue and alpha channels in the sRGB color space.
Definition Color.h:160
ColorRGBABase()=default
Constructor.