MRPT  2.0.4
TColor.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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 #pragma once
10 
11 #include <mrpt/core/bits_math.h>
14 #include <cstdint>
15 #include <iosfwd>
16 #include <iostream>
17 
18 namespace mrpt::img
19 {
20 // Ensure 1-byte memory alignment, no additional stride bytes.
21 #pragma pack(push, 1)
22 
23 /** A RGB color - 8bit. Struct pack=1 is ensured.
24  * \ingroup mrpt_img_grp */
25 struct TColor
26 {
27  constexpr inline TColor() = default;
28  constexpr inline TColor(
29  uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255)
30  : R(r), G(g), B(b), A(alpha)
31  {
32  }
33 
34  constexpr inline explicit TColor(const unsigned int color_RGB_24bit)
35  : R(uint8_t(color_RGB_24bit >> 16)),
36  G(uint8_t(color_RGB_24bit >> 8)),
37  B(uint8_t(color_RGB_24bit)),
38  A(255)
39  {
40  }
41 
42  constexpr inline TColor(
43  const unsigned int color_RGB_24bit, const uint8_t alpha)
44  : R(uint8_t(color_RGB_24bit >> 16)),
45  G(uint8_t(color_RGB_24bit >> 8)),
46  B(uint8_t(color_RGB_24bit)),
47  A(alpha)
48  {
49  }
50 
51  uint8_t R{0}, G{0}, B{0}, A{255};
52 
53  /** Operator for implicit conversion into an int binary representation
54  * 0xRRGGBB */
55  inline operator unsigned int() const
56  {
57  return (((unsigned int)R) << 16) | (((unsigned int)G) << 8) | B;
58  }
59 
60  TColor(const TColor& other) { *this = other; }
61  TColor& operator=(const TColor& other);
62  TColor& operator+=(const TColor& other);
63  TColor& operator-=(const TColor& other);
64 
65  /** Predefined colors */
66  static constexpr TColor red() { return TColor(255, 0, 0); }
67  static constexpr TColor green() { return TColor(0, 255, 0); }
68  static constexpr TColor blue() { return TColor(0, 0, 255); }
69  static constexpr TColor black() { return TColor(0, 0, 0); }
70  static constexpr TColor white() { return TColor(255, 255, 255); }
71  static constexpr TColor gray() { return TColor(127, 127, 127); }
72 };
73 #pragma pack(pop)
74 
75 // Text streaming:
76 std::ostream& operator<<(std::ostream& o, const TColor& c);
77 // Binary streaming:
79  mrpt::serialization::CArchive& o, const TColor& c);
81  mrpt::serialization::CArchive& i, TColor& c);
82 
83 // Ensure 1-byte memory alignment, no additional stride bytes.
84 #pragma pack(push, 1)
85 
86 /** An RGBA color - floats in the range [0,1]
87  * \ingroup mrpt_img_grp */
88 struct TColorf
89 {
90  TColorf(float r = 0, float g = 0, float b = 0, float alpha = 1.0f)
91  : R(r), G(g), B(b), A(alpha)
92  {
93  }
94 
95  explicit TColorf(const TColor& col)
96  : R(u8tof(col.R)), G(u8tof(col.G)), B(u8tof(col.B)), A(u8tof(col.A))
97  {
98  }
99 
100  /** Returns the 0-255 integer version of this color: RGBA_u8 */
101  TColor asTColor() const
102  {
103  return TColor(
105  }
106 
107  float R, G, B, A;
108 };
109 #pragma pack(pop)
110 
111 /**\brief Pairwise addition of their corresponding RGBA members
112  */
113 TColor operator+(const TColor& first, const TColor& second);
114 /**\brief Pairwise substraction of their corresponding RGBA members
115  */
116 TColor operator-(const TColor& first, const TColor& second);
117 bool operator==(const TColor& first, const TColor& second);
118 // bool operator!=(const TColor& first, const TColor& second);
119 
120 // Text streaming:
121 std::ostream& operator<<(std::ostream& o, const TColorf& c);
122 // Binary streaming:
127 
128 } // namespace mrpt::img
129 
130 namespace mrpt::typemeta
131 {
132 // Specialization must occur in the same namespace
135 } // namespace mrpt::typemeta
mrpt::img::TColor::red
static constexpr TColor red()
Predefined colors.
Definition: TColor.h:66
mrpt::img::operator+
TColor operator+(const TColor &first, const TColor &second)
Pairwise addition of their corresponding RGBA members.
Definition: TColor.cpp:23
mrpt::typemeta
Definition: config/CConfigFileBase.h:21
mrpt::img::TColor::TColor
constexpr TColor(const unsigned int color_RGB_24bit)
Definition: TColor.h:34
mrpt::f2u8
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds)
Definition: core/include/mrpt/core/bits_math.h:193
mrpt::img::TColorf::R
float R
Definition: TColor.h:107
mrpt::img::TColorf::asTColor
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8
Definition: TColor.h:101
mrpt::img::TColor::R
uint8_t R
Definition: TColor.h:51
mrpt::img::TColorf::TColorf
TColorf(const TColor &col)
Definition: TColor.h:95
mrpt::img::TColor::operator=
TColor & operator=(const TColor &other)
mrpt::img::operator==
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:231
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::img::operator<<
std::ostream & operator<<(std::ostream &o, const TColor &c)
Definition: TColor.cpp:80
mrpt::img::operator-
TColor operator-(const TColor &first, const TColor &second)
Pairwise substraction of their corresponding RGBA members.
Definition: TColor.cpp:34
mrpt::img::TColor::green
static constexpr TColor green()
Definition: TColor.h:67
mrpt::img::TColorf::B
float B
Definition: TColor.h:107
mrpt::img::TColor::B
uint8_t B
Definition: TColor.h:51
MRPT_DECLARE_TTYPENAME_NO_NAMESPACE
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
TTypeName.h
mrpt::img
Definition: CCanvas.h:16
mrpt::img::TColor::white
static constexpr TColor white()
Definition: TColor.h:70
mrpt::img::TColor::TColor
constexpr TColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255)
Definition: TColor.h:28
mrpt::u8tof
float u8tof(const uint8_t v)
converts a uint8_t [0,255] into a float [0,1]
Definition: core/include/mrpt/core/bits_math.h:196
mrpt::img::TColor::gray
static constexpr TColor gray()
Definition: TColor.h:71
mrpt::img::TColorf
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:25
A
Definition: is_defined_unittest.cpp:13
mrpt::img::TColor::blue
static constexpr TColor blue()
Definition: TColor.h:68
bits_math.h
serialization_frwds.h
mrpt::img::TColor::black
static constexpr TColor black()
Definition: TColor.h:69
mrpt::img::TColorf::G
float G
Definition: TColor.h:107
mrpt::img::TColor::operator-=
TColor & operator-=(const TColor &other)
Definition: TColor.cpp:55
mrpt::img::TColor::G
uint8_t G
Definition: TColor.h:51
mrpt::img::TColor::TColor
constexpr TColor(const unsigned int color_RGB_24bit, const uint8_t alpha)
Definition: TColor.h:42
mrpt::img::operator>>
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &i, TColor &c)
Definition: TColor.cpp:98
mrpt::img::TColor::operator+=
TColor & operator+=(const TColor &other)
Definition: TColor.cpp:45
mrpt::img::TColor::TColor
TColor(const TColor &other)
Definition: TColor.h:60
mrpt::img::TColor::TColor
constexpr TColor()=default
mrpt::img::TColorf::A
float A
Definition: TColor.h:107
mrpt::img::TColorf::TColorf
TColorf(float r=0, float g=0, float b=0, float alpha=1.0f)
Definition: TColor.h:90



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sun Jul 19 17:54:30 UTC 2020