Zivid C++ API 2.15.0+5fcc365b-1
Color.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of the Zivid API
3 *
4 * Copyright 2015-2025 (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
63 template<typename T>
64 std::string rgbaToString(T r, T g, T b, T a)
65 {
66 return rgbaIntsToString(static_cast<int>(r), static_cast<int>(g), static_cast<int>(b), static_cast<int>(a));
67 }
68
69 template<typename T>
70 std::string bgraToString(T b, T g, T r, T a)
71 {
72 return bgraIntsToString(static_cast<int>(b), static_cast<int>(g), static_cast<int>(r), static_cast<int>(a));
73 }
74 } // namespace Detail
75#endif
76
77 struct ColorBGRA;
78 struct ColorSRGB;
80
81 template<typename T>
83 {
85 using ValueType = T;
86
88 ColorRGBABase() = default;
89
91 ColorRGBABase(T red, T green, T blue, T alpha)
92 : r{ red }
93 , g{ green }
94 , b{ blue }
95 , a{ alpha }
96 {}
97
99 T r;
100
102 T g;
103
105 T b;
106
108 T a;
109
112
115
117 std::string toString() const
118 {
119 return Detail::rgbaToString(r, g, b, a);
120 }
121 };
122
124 template<typename T>
125 std::ostream &operator<<(std::ostream &stream, const ColorRGBABase<T> &color)
126 {
127 return stream << color.toString();
128 }
129
143 struct ColorSRGB : public ColorRGBABase<uint8_t>
144 {
146 };
147
149
161
172 struct ColorRGBA : public ColorRGBABase<uint8_t>
173 {
175 };
176
178
179 template<typename T>
181 {
183 using ValueType = T;
184
186 ColorBGRABase() = default;
187
189 ColorBGRABase(T blue, T green, T red, T alpha)
190 : b{ blue }
191 , g{ green }
192 , r{ red }
193 , a{ alpha }
194 {}
195
197 T b;
198
200 T g;
201
203 T r;
204
206 T a;
207
210
213
215 std::string toString() const
216 {
217 return Detail::bgraToString(b, g, r, a);
218 }
219 };
220
222 template<typename T>
223 std::ostream &operator<<(std::ostream &stream, const ColorBGRABase<T> &color)
224 {
225 return stream << color.toString();
226 }
227
238 struct ColorBGRA : public ColorBGRABase<uint8_t>
239 {
241 };
242
244
256 struct ColorBGRA_SRGB : public ColorBGRABase<uint8_t>
257 {
259 };
260
262
263} // namespace Zivid
#define ZIVID_STATIC_ASSERT_DATA_FORMAT_TYPE(Type, size)
Definition DataFormatUtils.h:48
#define ZIVID_UTILS_EXPORT
Definition UtilsExport.h:56
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:84
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream.
Definition Array2D.h:283
Definition Color.h:181
ColorBGRABase(T blue, T green, T red, T alpha)
Constructor.
Definition Color.h:189
ZIVID_UTILS_EXPORT bool operator!=(const ColorBGRABase &rhs) const
Check for inequality.
T g
Green channel.
Definition Color.h:200
ColorBGRABase()=default
Constructor.
T a
Alpha channel.
Definition Color.h:206
T ValueType
The type of the channel values.
Definition Color.h:183
T r
Red channel.
Definition Color.h:203
ZIVID_UTILS_EXPORT bool operator==(const ColorBGRABase &rhs) const
Check for equality.
T b
Blue channel.
Definition Color.h:197
std::string toString() const
Get string representation.
Definition Color.h:215
Color with 8-bit blue, green, red and alpha channels in the sRGB color space.
Definition Color.h:257
Color with 8-bit blue, green, red and alpha channels.
Definition Color.h:239
Definition Color.h:83
ZIVID_UTILS_EXPORT bool operator!=(const ColorRGBABase &rhs) const
Check for inequality.
T ValueType
The type of the channel values.
Definition Color.h:85
std::string toString() const
Get string representation.
Definition Color.h:117
T g
Green channel.
Definition Color.h:102
ZIVID_UTILS_EXPORT bool operator==(const ColorRGBABase &rhs) const
Check for equality.
ColorRGBABase()=default
Constructor.
T a
Alpha channel.
Definition Color.h:108
T r
Red channel.
Definition Color.h:99
T b
Blue channel.
Definition Color.h:105
ColorRGBABase(T red, T green, T blue, T alpha)
Constructor.
Definition Color.h:91
Color with 8-bit red, green, blue and alpha channels.
Definition Color.h:173
Color with 8-bit red, green, blue and alpha channels in the sRGB color space.
Definition Color.h:144