ITK  5.4.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 #ifdef ITK_FUTURE_LEGACY_REMOVE
81  RGBPixel() = default;
82 #else
83  RGBPixel() { this->Fill(0); }
84 #endif
85 
87 #if defined(ITK_LEGACY_REMOVE)
88 
89  explicit RGBPixel(const ComponentType & r) { this->Fill(r); }
90 
92  RGBPixel(std::nullptr_t) = delete;
93 #else
94 
97  RGBPixel(const ComponentType & r) { this->Fill(r); }
98 #endif
99 
101  template <typename TRGBPixelValueType>
103  : BaseArray(r)
104  {}
106  : BaseArray(r)
107  {}
111  template <typename TRGBPixelValueType>
112  Self &
114  {
115  BaseArray::operator=(r);
116  return *this;
117  }
120  Self &
121  operator=(const ComponentType r[3]);
122 
124  Self
125  operator+(const Self & r) const;
126  Self
127  operator-(const Self & r) const;
128  Self operator*(const ComponentType & r) const;
129  Self
130  operator/(const ComponentType & r) const;
134  const Self &
135  operator+=(const Self & r);
136  const Self &
137  operator-=(const Self & r);
138  const Self &
139  operator*=(const ComponentType & r);
140  const Self &
141  operator/=(const ComponentType & r);
145  bool
146  operator<(const Self & r) const;
147 
148  bool
149  operator==(const Self & r) const;
150 
152  static unsigned int
154  {
155  return 3;
156  }
157 
159  ComponentType
160  GetNthComponent(int c) const
161  {
162  return this->operator[](c);
163  }
164 
166  ComponentType
168  {
169  return static_cast<ComponentType>(
170  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
171  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
172  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
173  }
174 
176  void
177  SetNthComponent(int c, const ComponentType & v)
178  {
179  this->operator[](c) = v;
180  }
181 
183  void
185  {
186  this->operator[](0) = red;
187  }
188 
190  void
192  {
193  this->operator[](1) = green;
194  }
195 
197  void
199  {
200  this->operator[](2) = blue;
201  }
202 
204  void
206  {
207  this->operator[](0) = red;
208  this->operator[](1) = green;
209  this->operator[](2) = blue;
210  }
214  const ComponentType &
215  GetRed() const
216  {
217  return this->operator[](0);
218  }
219 
221  const ComponentType &
222  GetGreen() const
223  {
224  return this->operator[](1);
225  }
226 
228  const ComponentType &
229  GetBlue() const
230  {
231  return this->operator[](2);
232  }
233 
235  LuminanceType
236  GetLuminance() const;
237 };
238 
239 template <typename TComponent>
240 std::ostream &
241 operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
242 
243 template <typename TComponent>
244 std::istream &
245 operator>>(std::istream & is, RGBPixel<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 
267 #ifndef ITK_MANUAL_INSTANTIATION
268 # include "itkRGBPixel.hxx"
269 #endif
270 
271 #endif
itk::RGBPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:153
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:215
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:205
itk::RGBPixel::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
itk::RGBPixel::operator=
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:113
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:89
itk::RGBPixel::GetGreen
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:222
itk::RGBPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:167
itk::RGBPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:229
itkNumericTraitsRGBPixel.h
itkIndent.h
itkFixedArray.h
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:105
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:191
itk::RGBPixel::RGBPixel
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:102
itk::RGBPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:177
itk::RGBPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:160
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:433
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
itk::RGBPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:184
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::RGBPixel::SetBlue
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:198
itkMath.h
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)