ITK  5.2.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  * http://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 
80  RGBAPixel() { this->Fill(0); }
81  RGBAPixel(const RGBAPixel &) = default;
82  RGBAPixel &
83  operator=(const RGBAPixel &) = default;
84  RGBAPixel(RGBAPixel &&) = default;
85  RGBAPixel &
86  operator=(RGBAPixel &&) = default;
87  ~RGBAPixel() = default;
89 
91  template <typename TRGBAPixelValueType>
93  : BaseArray(r)
94  {}
96  : BaseArray(r)
97  {}
98  RGBAPixel(const ComponentType & r) { this->Fill(r); }
100 
102  RGBAPixel &
103  operator=(const ComponentType r[4]);
104 
106  Self
107  operator+(const Self & r) const;
108  Self
109  operator-(const Self & r) const;
110  Self operator*(const ComponentType & r) const;
111  Self
112  operator/(const ComponentType & r) const;
114 
116  const Self &
117  operator+=(const Self & r);
118  const Self &
119  operator-=(const Self & r);
120  const Self &
121  operator*=(const ComponentType & r);
122  const Self &
123  operator/=(const ComponentType & r);
125 
127  bool
128  operator<(const Self & r) const;
129 
130  bool
131  operator==(const Self & r) const;
132 
134  static unsigned int
136  {
137  return 4;
138  }
139 
141  ComponentType
142  GetNthComponent(int c) const
143  {
144  return this->operator[](c);
145  }
146 
148  ComponentType
150  {
151  return static_cast<ComponentType>(
152  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
153  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
154  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
155  }
156 
158  void
159  SetNthComponent(int c, const ComponentType & v)
160  {
161  this->operator[](c) = v;
162  }
163 
165  void
167  {
168  this->operator[](0) = red;
169  }
170 
172  void
174  {
175  this->operator[](1) = green;
176  }
177 
179  void
181  {
182  this->operator[](2) = blue;
183  }
184 
186  void
188  {
189  this->operator[](3) = alpha;
190  }
191 
193  void
195  {
196  this->operator[](0) = red;
197  this->operator[](1) = green;
198  this->operator[](2) = blue;
199  this->operator[](3) = alpha;
200  }
202 
204  const ComponentType &
205  GetRed() const
206  {
207  return this->operator[](0);
208  }
209 
211  const ComponentType &
212  GetGreen() const
213  {
214  return this->operator[](1);
215  }
216 
218  const ComponentType &
219  GetBlue() const
220  {
221  return this->operator[](2);
222  }
223 
225  const ComponentType &
226  GetAlpha() const
227  {
228  return this->operator[](3);
229  }
230 
232  LuminanceType
233  GetLuminance() const;
234 };
235 
236 template <typename TComponent>
237 std::ostream &
238 operator<<(std::ostream & os, const RGBAPixel<TComponent> & c);
239 
240 template <typename TComponent>
241 std::istream &
242 operator>>(std::istream & is, RGBAPixel<TComponent> & c);
243 
244 template <typename T>
245 inline void
247 {
248  a.swap(b);
249 }
250 
251 } // end namespace itk
252 
253 //
254 // Numeric traits must be included after (optionally) including the explicit
255 // instantiations control of this class, in case the implicit instantiation
256 // needs to be disabled.
257 //
258 // NumericTraits must be included before (optionally) including the .hxx file,
259 // in case the .hxx requires to use NumericTraits.
260 //
262 
263 #ifndef ITK_MANUAL_INSTANTIATION
264 # include "itkRGBAPixel.hxx"
265 #endif
266 
267 #endif
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:546
itk::RGBAPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBAPixel.h:219
itk::RGBAPixel::SetGreen
void SetGreen(ComponentType green)
Definition: itkRGBAPixel.h:173
itk::RGBAPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBAPixel.h:149
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::RGBAPixel::RGBAPixel
RGBAPixel(const RGBAPixel< TRGBAPixelValueType > &r)
Definition: itkRGBAPixel.h:92
itk::RGBAPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBAPixel.h:159
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:679
itkNumericTraitsRGBAPixel.h
itk::RGBAPixel::RGBAPixel
RGBAPixel(const ComponentType r[4])
Definition: itkRGBAPixel.h:95
itkIndent.h
itk::RGBAPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBAPixel.h:135
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &vct)
itkFixedArray.h
itk::RGBAPixel::RGBAPixel
RGBAPixel()
Definition: itkRGBAPixel.h:80
itk::RGBAPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBAPixel.h:166
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::RGBAPixel::RGBAPixel
RGBAPixel(const ComponentType &r)
Definition: itkRGBAPixel.h:98
itk::RGBAPixel::GetRed
const ComponentType & GetRed() const
Definition: itkRGBAPixel.h:205
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:275
itk::RGBAPixel::Set
void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
Definition: itkRGBAPixel.h:194
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:212
itk::RGBAPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBAPixel.h:142
itk::FixedArray< TComponent, 4 >::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:444
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:661
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::RGBAPixel::GetAlpha
const ComponentType & GetAlpha() const
Definition: itkRGBAPixel.h:226
itk::RGBAPixel::SetAlpha
void SetAlpha(ComponentType alpha)
Definition: itkRGBAPixel.h:187
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:180
itk::RGBAPixel< double >::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBAPixel.h:77
itk::RGBAPixel< double >::ComponentType
double ComponentType
Definition: itkRGBAPixel.h:76