Debian Codegraph @

libs/img/src/TColor.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include "img-precomp.h" // Precompiled headers
11 //
12 #include <mrpt/img/TColor.h>
13 #include <mrpt/serialization/CArchive.h>
14 #include <mrpt/system/os.h>
15
16 using namespace mrpt::img;
17 using mrpt::serialization::CArchive;
18
19 // Check "#pragma pack(push, 1)" is doing its job:
20 static_assert(sizeof(TColor) == 4 * sizeof(uint8_t));
21 static_assert(sizeof(TColorf) == 4 * sizeof(float));
22
23 TColor mrpt::img::operator+(const TColor& first, const TColor& second)
24 {
25 TColor ret;
26 ret.R = first.R + second.R;
27 ret.G = first.G + second.G;
28 ret.B = first.B + second.B;
29 ret.A = first.A + second.A;
30
31 return ret;
32 }
33
34 TColor mrpt::img::operator-(const TColor& first, const TColor& second)
35 {
36 TColor ret;
37 ret.R = first.R - second.R;
38 ret.G = first.G - second.G;
39 ret.B = first.B - second.B;
40 ret.A = first.A - second.A;
41
42 return ret;
43 }
44
45 TColor& TColor::operator+=(const TColor& other)
46 {
47 this->R += other.R;
48 this->G += other.G;
49 this->B += other.B;
50 this->A += other.A;
51
52 return *this;
53 }
54
55 TColor& TColor::operator-=(const TColor& other)
56 {
57 this->R -= other.R;
58 this->G -= other.G;
59 this->B -= other.B;
60 this->A -= other.A;
61
62 return *this;
63 }
64
65 TColor& TColor::operator=(const TColor& other) = default;
66
67 bool mrpt::img::operator==(const TColor& first, const TColor& second)
68 {
69 bool ret =
70 first.R == second.R && first.G == second.G && first.B == second.B && first.A == second.A;
71
72 return ret;
73 }
74
75 // bool operator!=(const TColor& first, const TColor& second) {
76 // return (!(first == second));
77 //}
78
79 // Text streaming:
80 std::ostream& mrpt::img::operator<<(std::ostream& o, const TColor& c)
81 {
82 char buf[200];
83 mrpt::system::os::sprintf(
84 buf, sizeof(buf), "RGBA=[%u,%u,%u,%u]", static_cast<unsigned int>(c.R),
85 static_cast<unsigned int>(c.G), static_cast<unsigned int>(c.B),
86 static_cast<unsigned int>(c.A));
87 o << buf;
88 return o;
89 }
90
91 // Binary streaming:
92 CArchive& mrpt::img::operator<<(CArchive& o, const TColor& c)
93 {
94 o << c.R << c.G << c.B << c.A;
95 return o;
96 }
97
98 CArchive& mrpt::img::operator>>(CArchive& i, TColor& c)
99 {
100 i >> c.R >> c.G >> c.B >> c.A;
101 return i;
102 }
103
104 // Text streaming:
105 std::ostream& mrpt::img::operator<<(std::ostream& o, const TColorf& c)
106 {
107 char buf[200];
108 mrpt::system::os::sprintf(buf, sizeof(buf), "RGBAf=[%f,%f,%f,%f]", c.R, c.G, c.B, c.A);
109 o << buf;
110 return o;
111 }
112
113 // Binary streaming:
114 CArchive& mrpt::img::operator<<(CArchive& o, const TColorf& c)
115 {
116 o << c.R << c.G << c.B << c.A;
117 return o;
118 }
119
120 CArchive& mrpt::img::operator>>(CArchive& i, TColorf& c)
121 {
122 i >> c.R >> c.G >> c.B >> c.A;
123 return i;
124 }