ITK  6.0.0
Insight Toolkit
itkRGBAPixel.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkRGBAPixel_h
19#define itkRGBAPixel_h
20
21// Undefine an eventual RGBAPixel macro
22#ifdef RGBAPixel
23# undef RGBAPixel
24#endif
25
26#include "itkIndent.h"
27#include "itkFixedArray.h"
28#include "itkMath.h"
29
30namespace itk
31{
58template <typename TComponent = unsigned short>
59class ITK_TEMPLATE_EXPORT RGBAPixel : public FixedArray<TComponent, 4>
60{
61public:
63 using Self = RGBAPixel;
65
67 static constexpr unsigned int Dimension = 4;
68
70 static constexpr unsigned int Length = 4;
71
74
76 using ComponentType = TComponent;
78
81#ifdef ITK_FUTURE_LEGACY_REMOVE
82 RGBAPixel() = default;
83#else
84 constexpr RGBAPixel()
86 {
87 // `: Superclass(Superclass())` is a workaround for an old compiler bug. A simple `: Superclass()` triggered
88 // warnings from GCC 9.4.0 saying: "warning: '<anonymous>' may be used uninitialized in this function
89 // [-Wmaybe-uninitialized]".
90 }
91#endif
92
94 template <typename TRGBAPixelValueType>
96 : BaseArray(r)
97 {}
99 : BaseArray(r)
100 {}
103#if defined(ITK_LEGACY_REMOVE)
105 RGBAPixel(std::nullptr_t) = delete;
106
108 explicit RGBAPixel(const ComponentType & r) { this->Fill(r); }
109#else
110 RGBAPixel(const ComponentType & r) { this->Fill(r); }
111#endif
115 RGBAPixel &
117
119 Self
120 operator+(const Self & r) const;
121 Self
122 operator-(const Self & r) const;
123 Self
124 operator*(const ComponentType & r) const;
125 Self
126 operator/(const ComponentType & r) const;
130 const Self &
131 operator+=(const Self & r);
132 const Self &
133 operator-=(const Self & r);
134 const Self &
136 const Self &
141 bool
142 operator<(const Self & r) const;
143
144 bool
145 operator==(const Self & r) const;
146
148 static unsigned int
150 {
151 return 4;
152 }
153
155 ComponentType
156 GetNthComponent(int c) const
157 {
158 return this->operator[](c);
159 }
160
162 ComponentType
164 {
165 return static_cast<ComponentType>(
166 std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
167 static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
168 static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
169 }
170
172 void
174 {
175 this->operator[](c) = v;
176 }
177
179 void
181 {
182 this->operator[](0) = red;
183 }
184
186 void
188 {
189 this->operator[](1) = green;
190 }
191
193 void
195 {
196 this->operator[](2) = blue;
197 }
198
200 void
202 {
203 this->operator[](3) = alpha;
204 }
205
207 void
209 {
210 this->operator[](0) = red;
211 this->operator[](1) = green;
212 this->operator[](2) = blue;
213 this->operator[](3) = alpha;
214 }
218 const ComponentType &
219 GetRed() const
220 {
221 return this->operator[](0);
222 }
223
225 const ComponentType &
226 GetGreen() const
227 {
228 return this->operator[](1);
229 }
230
232 const ComponentType &
233 GetBlue() const
234 {
235 return this->operator[](2);
236 }
237
239 const ComponentType &
240 GetAlpha() const
241 {
242 return this->operator[](3);
243 }
244
246 LuminanceType
248};
249
250template <typename TComponent>
251std::ostream &
252operator<<(std::ostream & os, const RGBAPixel<TComponent> & c);
253
254template <typename TComponent>
255std::istream &
256operator>>(std::istream & is, RGBAPixel<TComponent> & c);
257
258template <typename T>
259inline void
261{
262 a.swap(b);
263}
264
265} // end namespace itk
266
267//
268// Numeric traits must be included after (optionally) including the explicit
269// instantiations control of this class, in case the implicit instantiation
270// needs to be disabled.
271//
272// NumericTraits must be included before (optionally) including the .hxx file,
273// in case the .hxx requires to use NumericTraits.
274//
276
277#ifndef ITK_MANUAL_INSTANTIATION
278# include "itkRGBAPixel.hxx"
279#endif
280
281#endif
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:60
RGBAPixel(std::nullptr_t)=delete
ComponentType GetNthComponent(int c) const
Definition: itkRGBAPixel.h:156
RGBAPixel & operator=(const ComponentType r[4])
RGBAPixel(const RGBAPixel< TRGBAPixelValueType > &r)
Definition: itkRGBAPixel.h:95
void SetAlpha(ComponentType alpha)
Definition: itkRGBAPixel.h:201
LuminanceType GetLuminance() const
const Self & operator+=(const Self &r)
RGBAPixel(const ComponentType &r)
Definition: itkRGBAPixel.h:108
Self operator+(const Self &r) const
Self operator-(const Self &r) const
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBAPixel.h:173
const ComponentType & GetAlpha() const
Definition: itkRGBAPixel.h:240
const ComponentType & GetRed() const
Definition: itkRGBAPixel.h:219
bool operator==(const Self &r) const
const ComponentType & GetGreen() const
Definition: itkRGBAPixel.h:226
void SetGreen(ComponentType green)
Definition: itkRGBAPixel.h:187
static unsigned int GetNumberOfComponents()
Definition: itkRGBAPixel.h:149
ComponentType GetScalarValue() const
Definition: itkRGBAPixel.h:163
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBAPixel.h:77
Self operator/(const ComponentType &r) const
const Self & operator/=(const ComponentType &r)
void SetBlue(ComponentType blue)
Definition: itkRGBAPixel.h:194
void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
Definition: itkRGBAPixel.h:208
TComponent ComponentType
Definition: itkRGBAPixel.h:76
const Self & operator-=(const Self &r)
Self operator*(const ComponentType &r) const
const Self & operator*=(const ComponentType &r)
RGBAPixel()=default
const ComponentType & GetBlue() const
Definition: itkRGBAPixel.h:233
void SetRed(ComponentType red)
Definition: itkRGBAPixel.h:180
RGBAPixel(const ComponentType r[4])
Definition: itkRGBAPixel.h:98
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:566