ITK  5.2.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  * 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 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 
79  RGBPixel() { this->Fill(0); }
80  RGBPixel(const RGBPixel &) = default;
81  RGBPixel(RGBPixel &&) = default;
82  RGBPixel &
83  operator=(const RGBPixel &) = default;
84  RGBPixel &
85  operator=(RGBPixel &&) = default;
86  ~RGBPixel() = default;
88 
90  RGBPixel(const ComponentType & r) { this->Fill(r); }
91 
93  template <typename TRGBPixelValueType>
95  : BaseArray(r)
96  {}
97  RGBPixel(const ComponentType r[3])
98  : BaseArray(r)
99  {}
101 
103  template <typename TRGBPixelValueType>
104  Self &
106  {
107  BaseArray::operator=(r);
108  return *this;
109  }
111 
112  Self &
113  operator=(const ComponentType r[3]);
114 
116  Self
117  operator+(const Self & r) const;
118  Self
119  operator-(const Self & r) const;
120  Self operator*(const ComponentType & r) const;
121  Self
122  operator/(const ComponentType & r) const;
124 
126  const Self &
127  operator+=(const Self & r);
128  const Self &
129  operator-=(const Self & r);
130  const Self &
131  operator*=(const ComponentType & r);
132  const Self &
133  operator/=(const ComponentType & r);
135 
137  bool
138  operator<(const Self & r) const;
139 
140  bool
141  operator==(const Self & r) const;
142 
144  static unsigned int
146  {
147  return 3;
148  }
149 
151  ComponentType
152  GetNthComponent(int c) const
153  {
154  return this->operator[](c);
155  }
156 
158  ComponentType
160  {
161  return static_cast<ComponentType>(
162  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
163  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
164  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
165  }
166 
168  void
169  SetNthComponent(int c, const ComponentType & v)
170  {
171  this->operator[](c) = v;
172  }
173 
175  void
177  {
178  this->operator[](0) = red;
179  }
180 
182  void
184  {
185  this->operator[](1) = green;
186  }
187 
189  void
191  {
192  this->operator[](2) = blue;
193  }
194 
196  void
198  {
199  this->operator[](0) = red;
200  this->operator[](1) = green;
201  this->operator[](2) = blue;
202  }
204 
206  const ComponentType &
207  GetRed() const
208  {
209  return this->operator[](0);
210  }
211 
213  const ComponentType &
214  GetGreen() const
215  {
216  return this->operator[](1);
217  }
218 
220  const ComponentType &
221  GetBlue() const
222  {
223  return this->operator[](2);
224  }
225 
227  LuminanceType
228  GetLuminance() const;
229 };
230 
231 template <typename TComponent>
232 std::ostream &
233 operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
234 
235 template <typename TComponent>
236 std::istream &
237 operator>>(std::istream & is, RGBPixel<TComponent> & c);
238 
239 template <typename T>
240 inline void
242 {
243  a.swap(b);
244 }
245 
246 } // end namespace itk
247 
248 //
249 // Numeric traits must be included after (optionally) including the explicit
250 // instantiations control of this class, in case the implicit instantiation
251 // needs to be disabled.
252 //
253 // NumericTraits must be included before (optionally) including the .hxx file,
254 // in case the .hxx requires to use NumericTraits.
255 //
257 
258 
259 #ifndef ITK_MANUAL_INSTANTIATION
260 # include "itkRGBPixel.hxx"
261 #endif
262 
263 #endif
itk::RGBPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:145
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:546
itk::RGBPixel::GetRed
const ComponentType & GetRed() const
Definition: itkRGBPixel.h:207
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::RGBPixel::Set
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:197
itk::RGBPixel< TPixel >::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
itk::RGBPixel::operator=
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:105
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
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:90
itk::RGBPixel::GetGreen
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:214
itk::RGBPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:159
itk::RGBPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:221
itkNumericTraitsRGBPixel.h
itkIndent.h
itk::RGBPixel::RGBPixel
RGBPixel()
Definition: itkRGBPixel.h:79
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &vct)
itkFixedArray.h
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:97
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
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::RGBPixel::SetGreen
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:183
itk::RGBPixel::RGBPixel
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:94
itk::RGBPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:169
itk::RGBPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:152
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RGBPixel< TPixel >::ComponentType
TPixel ComponentType
Definition: itkRGBPixel.h:75
itk::FixedArray< TComponent, 3 >::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::RGBPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:176
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::RGBPixel::SetBlue
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:190
itkMath.h