MRPT  2.0.1
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
static constexpr TColor blue()
Definition: TColor.h:68
uint8_t B
Definition: TColor.h:51
uint8_t G
Definition: TColor.h:51
constexpr TColor(const unsigned int color_RGB_24bit, const uint8_t alpha)
Definition: TColor.h:42
TColor operator+(const TColor &first, const TColor &second)
Pairwise addition of their corresponding RGBA members.
Definition: TColor.cpp:23
TColor & operator-=(const TColor &other)
Definition: TColor.cpp:55
static constexpr TColor gray()
Definition: TColor.h:71
static constexpr TColor black()
Definition: TColor.h:69
TColor(const TColor &other)
Definition: TColor.h:60
constexpr TColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255)
Definition: TColor.h:28
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
static constexpr TColor green()
Definition: TColor.h:67
uint8_t R
Definition: TColor.h:51
bool operator==(const mrpt::img::TCamera &a, const mrpt::img::TCamera &b)
Definition: TCamera.cpp:231
static constexpr TColor red()
Predefined colors.
Definition: TColor.h:66
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds) ...
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
constexpr TColor(const unsigned int color_RGB_24bit)
Definition: TColor.h:34
TColorf(float r=0, float g=0, float b=0, float alpha=1.0f)
Definition: TColor.h:90
TColorf(const TColor &col)
Definition: TColor.h:95
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
float u8tof(const uint8_t v)
converts a uint8_t [0,255] into a float [0,1]
constexpr TColor()=default
TColor operator-(const TColor &first, const TColor &second)
Pairwise substraction of their corresponding RGBA members.
Definition: TColor.cpp:34
TColor & operator=(const TColor &other)
TColor & operator+=(const TColor &other)
Definition: TColor.cpp:45
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8.
Definition: TColor.h:101
A RGB color - 8bit.
Definition: TColor.h:25
std::ostream & operator<<(std::ostream &o, const TColor &c)
Definition: TColor.cpp:80
static constexpr TColor white()
Definition: TColor.h:70
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &i, TColor &c)
Definition: TColor.cpp:98



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020