ITK  5.4.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 RGBAPixel() { this->Fill(0); }
85#endif
89 template <typename TRGBAPixelValueType>
91 : BaseArray(r)
92 {}
94 : BaseArray(r)
95 {}
98#if defined(ITK_LEGACY_REMOVE)
100 RGBAPixel(std::nullptr_t) = delete;
101
103 explicit RGBAPixel(const ComponentType & r) { this->Fill(r); }
104#else
105 RGBAPixel(const ComponentType & r) { this->Fill(r); }
106#endif
110 RGBAPixel &
112
114 Self
115 operator+(const Self & r) const;
116 Self
117 operator-(const Self & r) const;
118 Self operator*(const ComponentType & r) const;
119 Self
120 operator/(const ComponentType & r) const;
124 const Self &
125 operator+=(const Self & r);
126 const Self &
127 operator-=(const Self & r);
128 const Self &
130 const Self &
135 bool
136 operator<(const Self & r) const;
137
138 bool
139 operator==(const Self & r) const;
140
142 static unsigned int
144 {
145 return 4;
146 }
147
149 ComponentType
150 GetNthComponent(int c) const
151 {
152 return this->operator[](c);
153 }
154
156 ComponentType
158 {
159 return static_cast<ComponentType>(
160 std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
161 static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
162 static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
163 }
164
166 void
168 {
169 this->operator[](c) = v;
170 }
171
173 void
175 {
176 this->operator[](0) = red;
177 }
178
180 void
182 {
183 this->operator[](1) = green;
184 }
185
187 void
189 {
190 this->operator[](2) = blue;
191 }
192
194 void
196 {
197 this->operator[](3) = alpha;
198 }
199
201 void
203 {
204 this->operator[](0) = red;
205 this->operator[](1) = green;
206 this->operator[](2) = blue;
207 this->operator[](3) = alpha;
208 }
212 const ComponentType &
213 GetRed() const
214 {
215 return this->operator[](0);
216 }
217
219 const ComponentType &
220 GetGreen() const
221 {
222 return this->operator[](1);
223 }
224
226 const ComponentType &
227 GetBlue() const
228 {
229 return this->operator[](2);
230 }
231
233 const ComponentType &
234 GetAlpha() const
235 {
236 return this->operator[](3);
237 }
238
240 LuminanceType
242};
243
244template <typename TComponent>
245std::ostream &
246operator<<(std::ostream & os, const RGBAPixel<TComponent> & c);
247
248template <typename TComponent>
249std::istream &
250operator>>(std::istream & is, RGBAPixel<TComponent> & c);
251
252template <typename T>
253inline void
255{
256 a.swap(b);
257}
258
259} // end namespace itk
260
261//
262// Numeric traits must be included after (optionally) including the explicit
263// instantiations control of this class, in case the implicit instantiation
264// needs to be disabled.
265//
266// NumericTraits must be included before (optionally) including the .hxx file,
267// in case the .hxx requires to use NumericTraits.
268//
270
271#ifndef ITK_MANUAL_INSTANTIATION
272# include "itkRGBAPixel.hxx"
273#endif
274
275#endif
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
void swap(FixedArray &other)
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:150
RGBAPixel & operator=(const ComponentType r[4])
RGBAPixel(const RGBAPixel< TRGBAPixelValueType > &r)
Definition: itkRGBAPixel.h:90
void SetAlpha(ComponentType alpha)
Definition: itkRGBAPixel.h:195
LuminanceType GetLuminance() const
const Self & operator+=(const Self &r)
RGBAPixel(const ComponentType &r)
Definition: itkRGBAPixel.h:103
Self operator+(const Self &r) const
Self operator-(const Self &r) const
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBAPixel.h:167
const ComponentType & GetAlpha() const
Definition: itkRGBAPixel.h:234
const ComponentType & GetRed() const
Definition: itkRGBAPixel.h:213
bool operator==(const Self &r) const
const ComponentType & GetGreen() const
Definition: itkRGBAPixel.h:220
void SetGreen(ComponentType green)
Definition: itkRGBAPixel.h:181
static unsigned int GetNumberOfComponents()
Definition: itkRGBAPixel.h:143
ComponentType GetScalarValue() const
Definition: itkRGBAPixel.h:157
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:188
void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
Definition: itkRGBAPixel.h:202
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:227
void SetRed(ComponentType red)
Definition: itkRGBAPixel.h:174
RGBAPixel(const ComponentType r[4])
Definition: itkRGBAPixel.h:93
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559