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 
30 namespace itk
31 {
58 template <typename TComponent = unsigned short>
59 class ITK_TEMPLATE_EXPORT RGBAPixel : public FixedArray<TComponent, 4>
60 {
61 public:
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  RGBAPixel() { this->Fill(0); }
82 
84  template <typename TRGBAPixelValueType>
86  : BaseArray(r)
87  {}
89  : BaseArray(r)
90  {}
93 #if defined(ITK_LEGACY_REMOVE)
94 
95  RGBAPixel(std::nullptr_t) = delete;
96 
98  explicit RGBAPixel(const ComponentType & r) { this->Fill(r); }
99 #else
100  RGBAPixel(const ComponentType & r) { this->Fill(r); }
101 #endif
102 
105  RGBAPixel &
106  operator=(const ComponentType r[4]);
107 
109  Self
110  operator+(const Self & r) const;
111  Self
112  operator-(const Self & r) const;
113  Self operator*(const ComponentType & r) const;
114  Self
115  operator/(const ComponentType & r) const;
119  const Self &
120  operator+=(const Self & r);
121  const Self &
122  operator-=(const Self & r);
123  const Self &
124  operator*=(const ComponentType & r);
125  const Self &
126  operator/=(const ComponentType & r);
130  bool
131  operator<(const Self & r) const;
132 
133  bool
134  operator==(const Self & r) const;
135 
137  static unsigned int
139  {
140  return 4;
141  }
142 
144  ComponentType
145  GetNthComponent(int c) const
146  {
147  return this->operator[](c);
148  }
149 
151  ComponentType
153  {
154  return static_cast<ComponentType>(
155  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
156  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
157  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
158  }
159 
161  void
162  SetNthComponent(int c, const ComponentType & v)
163  {
164  this->operator[](c) = v;
165  }
166 
168  void
170  {
171  this->operator[](0) = red;
172  }
173 
175  void
177  {
178  this->operator[](1) = green;
179  }
180 
182  void
184  {
185  this->operator[](2) = blue;
186  }
187 
189  void
191  {
192  this->operator[](3) = alpha;
193  }
194 
196  void
198  {
199  this->operator[](0) = red;
200  this->operator[](1) = green;
201  this->operator[](2) = blue;
202  this->operator[](3) = alpha;
203  }
207  const ComponentType &
208  GetRed() const
209  {
210  return this->operator[](0);
211  }
212 
214  const ComponentType &
215  GetGreen() const
216  {
217  return this->operator[](1);
218  }
219 
221  const ComponentType &
222  GetBlue() const
223  {
224  return this->operator[](2);
225  }
226 
228  const ComponentType &
229  GetAlpha() const
230  {
231  return this->operator[](3);
232  }
233 
235  LuminanceType
236  GetLuminance() const;
237 };
238 
239 template <typename TComponent>
240 std::ostream &
241 operator<<(std::ostream & os, const RGBAPixel<TComponent> & c);
242 
243 template <typename TComponent>
244 std::istream &
245 operator>>(std::istream & is, RGBAPixel<TComponent> & c);
246 
247 template <typename T>
248 inline void
250 {
251  a.swap(b);
252 }
253 
254 } // end namespace itk
255 
256 //
257 // Numeric traits must be included after (optionally) including the explicit
258 // instantiations control of this class, in case the implicit instantiation
259 // needs to be disabled.
260 //
261 // NumericTraits must be included before (optionally) including the .hxx file,
262 // in case the .hxx requires to use NumericTraits.
263 //
265 
266 #ifndef ITK_MANUAL_INSTANTIATION
267 # include "itkRGBAPixel.hxx"
268 #endif
269 
270 #endif
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559
itk::RGBAPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBAPixel.h:222
itk::RGBAPixel::SetGreen
void SetGreen(ComponentType green)
Definition: itkRGBAPixel.h:176
itk::RGBAPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBAPixel.h:152
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:268
itk::RGBAPixel::RGBAPixel
RGBAPixel(const RGBAPixel< TRGBAPixelValueType > &r)
Definition: itkRGBAPixel.h:85
itk::RGBAPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBAPixel.h:162
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itkNumericTraitsRGBAPixel.h
itk::RGBAPixel::RGBAPixel
RGBAPixel(const ComponentType r[4])
Definition: itkRGBAPixel.h:88
itkIndent.h
itk::RGBAPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBAPixel.h:138
itkFixedArray.h
itk::RGBAPixel::RGBAPixel
RGBAPixel()
Definition: itkRGBAPixel.h:81
itk::RGBAPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBAPixel.h:169
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:545
itk::RGBAPixel::RGBAPixel
RGBAPixel(const ComponentType &r)
Definition: itkRGBAPixel.h:98
itk::RGBAPixel::GetRed
const ComponentType & GetRed() const
Definition: itkRGBAPixel.h:208
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::RGBAPixel::Set
void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
Definition: itkRGBAPixel.h:197
itk::RGBAPixel
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:59
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RGBAPixel::GetGreen
const ComponentType & GetGreen() const
Definition: itkRGBAPixel.h:215
itk::RGBAPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBAPixel.h:145
itk::FixedArray< TComponent, 4 >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:416
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:85
AddImageFilter
Definition: itkAddImageFilter.h:80
itk::RGBAPixel::GetAlpha
const ComponentType & GetAlpha() const
Definition: itkRGBAPixel.h:229
itk::RGBAPixel::SetAlpha
void SetAlpha(ComponentType alpha)
Definition: itkRGBAPixel.h:190
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itkMath.h
itk::RGBAPixel::SetBlue
void SetBlue(ComponentType blue)
Definition: itkRGBAPixel.h:183
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
itk::RGBAPixel< double >::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBAPixel.h:77
itk::RGBAPixel< double >::ComponentType
double ComponentType
Definition: itkRGBAPixel.h:76