ITK  5.3.0
Insight Toolkit
itkRGBPixel.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 itkRGBPixel_h
19 #define itkRGBPixel_h
20 
21 // Undefine an eventual RGBPixel macro
22 #ifdef RGBPixel
23 # undef RGBPixel
24 #endif
25 
26 #include "itkIndent.h"
27 #include "itkFixedArray.h"
28 #include "itkMath.h"
29 
30 namespace itk
31 {
57 template <typename TComponent = unsigned short>
58 class ITK_TEMPLATE_EXPORT RGBPixel : public FixedArray<TComponent, 3>
59 {
60 public:
62  using Self = RGBPixel;
64 
67 
69  static constexpr unsigned int Dimension = 3;
70 
72  static constexpr unsigned int Length = 3;
73 
75  using ComponentType = TComponent;
77 
80  RGBPixel() { this->Fill(0); }
81 
82 #if defined(ITK_LEGACY_REMOVE)
83 
84  explicit RGBPixel(const ComponentType & r) { this->Fill(r); }
85 
87  RGBPixel(std::nullptr_t) = delete;
88 #else
89 
92  RGBPixel(const ComponentType & r) { this->Fill(r); }
93 #endif
94 
96  template <typename TRGBPixelValueType>
98  : BaseArray(r)
99  {}
101  : BaseArray(r)
102  {}
106  template <typename TRGBPixelValueType>
107  Self &
109  {
110  BaseArray::operator=(r);
111  return *this;
112  }
115  Self &
116  operator=(const ComponentType r[3]);
117 
119  Self
120  operator+(const Self & r) const;
121  Self
122  operator-(const Self & r) const;
123  Self operator*(const ComponentType & r) const;
124  Self
125  operator/(const ComponentType & r) const;
129  const Self &
130  operator+=(const Self & r);
131  const Self &
132  operator-=(const Self & r);
133  const Self &
134  operator*=(const ComponentType & r);
135  const Self &
136  operator/=(const ComponentType & r);
140  bool
141  operator<(const Self & r) const;
142 
143  bool
144  operator==(const Self & r) const;
145 
147  static unsigned int
149  {
150  return 3;
151  }
152 
154  ComponentType
155  GetNthComponent(int c) const
156  {
157  return this->operator[](c);
158  }
159 
161  ComponentType
163  {
164  return static_cast<ComponentType>(
165  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
166  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
167  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
168  }
169 
171  void
172  SetNthComponent(int c, const ComponentType & v)
173  {
174  this->operator[](c) = v;
175  }
176 
178  void
180  {
181  this->operator[](0) = red;
182  }
183 
185  void
187  {
188  this->operator[](1) = green;
189  }
190 
192  void
194  {
195  this->operator[](2) = blue;
196  }
197 
199  void
201  {
202  this->operator[](0) = red;
203  this->operator[](1) = green;
204  this->operator[](2) = blue;
205  }
209  const ComponentType &
210  GetRed() const
211  {
212  return this->operator[](0);
213  }
214 
216  const ComponentType &
217  GetGreen() const
218  {
219  return this->operator[](1);
220  }
221 
223  const ComponentType &
224  GetBlue() const
225  {
226  return this->operator[](2);
227  }
228 
230  LuminanceType
231  GetLuminance() const;
232 };
233 
234 template <typename TComponent>
235 std::ostream &
236 operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
237 
238 template <typename TComponent>
239 std::istream &
240 operator>>(std::istream & is, RGBPixel<TComponent> & c);
241 
242 template <typename T>
243 inline void
245 {
246  a.swap(b);
247 }
248 
249 } // end namespace itk
250 
251 //
252 // Numeric traits must be included after (optionally) including the explicit
253 // instantiations control of this class, in case the implicit instantiation
254 // needs to be disabled.
255 //
256 // NumericTraits must be included before (optionally) including the .hxx file,
257 // in case the .hxx requires to use NumericTraits.
258 //
260 
261 
262 #ifndef ITK_MANUAL_INSTANTIATION
263 # include "itkRGBPixel.hxx"
264 #endif
265 
266 #endif
itk::RGBPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:148
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559
itk::RGBPixel::GetRed
const ComponentType & GetRed() const
Definition: itkRGBPixel.h:210
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::RGBPixel::Set
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:200
itk::RGBPixel::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
itk::RGBPixel::operator=
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:108
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
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:84
itk::RGBPixel::GetGreen
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:217
itk::RGBPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:162
itk::RGBPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:224
itkNumericTraitsRGBPixel.h
itkIndent.h
itk::RGBPixel::RGBPixel
RGBPixel()
Definition: itkRGBPixel.h:80
itkFixedArray.h
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:100
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:545
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::RGBPixel::SetGreen
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:186
itk::RGBPixel::RGBPixel
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:97
itk::RGBPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:172
itk::RGBPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:155
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RGBPixel::ComponentType
TComponent ComponentType
Definition: itkRGBPixel.h:75
itk::FixedArray< TComponent, 3 >::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:84
itk::RGBPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:179
AddImageFilter
Definition: itkAddImageFilter.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::RGBPixel::SetBlue
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:193
itkMath.h
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)