ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkLabelToRGBFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 itkLabelToRGBFunctor_h
19 #define itkLabelToRGBFunctor_h
20 
21 #include <vector>
22 #include "itkNumericTraits.h"
23 
24 namespace itk
25 {
26 namespace Functor
27 {
49 template< typename TLabel, typename TRGBPixel >
51 {
52 public:
53 
55 
57  {
58  using ValueType = typename TRGBPixel::ValueType;
59 
60  // the following colors are from "R", and named:
61  // "red" "green3" "blue" "cyan"
62  //"magenta" "darkorange1" "darkgreen" "blueviolet"
63  //"brown4" "navy" "yellow4" "violetred1"
64  //"salmon4" "turquoise4" "sienna3" "darkorchid1"
65  //"springgreen4" "mediumvioletred" "orangered3" "lightseagreen"
66  //"slateblue" "deeppink1" "aquamarine4" "royalblue1"
67  //"tomato3" "mediumblue" "violetred4" "darkmagenta"
68  //"violet" "red4"
69  // They are a good selection of distinct colors for plotting and
70  // overlays.
71  constexpr size_t numColors = 30;
72  constexpr unsigned char colors[numColors][3] = {
73  {255, 0, 0}, {0, 205, 0}, {0, 0, 255}, {0, 255, 255},
74  {255, 0, 255}, {255, 127, 0}, {0, 100, 0}, {138, 43, 226},
75  {139, 35, 35}, {0, 0, 128}, {139, 139, 0}, {255, 62, 150},
76  {139, 76, 57}, {0, 134, 139}, {205, 104, 57}, {191, 62, 255},
77  {0, 139, 69}, {199, 21, 133}, {205, 55, 0}, {32, 178, 170},
78  {106, 90, 205}, {255, 20, 147}, {69, 139, 116}, {72, 118, 255},
79  {205, 79, 57}, {0, 0, 205}, {139, 34, 82}, {139, 0, 139},
80  {238, 130, 238}, {139, 0, 0}
81  };
82 
83  for (auto & color : colors)
84  {
85  AddColor(color[0], color[1], color[2]);
86  }
87 
88  // provide some default value for external use (outside
89  // LabelToRGBImageFilter)
90  // Inside LabelToRGBImageFilter, the values are always initialized
94  }
95 
96  inline TRGBPixel operator()(const TLabel & p) const
97  {
98  // value is background
99  // return a gray pixel with the same intensity than the label pixel
100  if ( p == m_BackgroundValue )
101  {
102  return m_BackgroundColor;
103  }
104 
105  // else, return a colored pixel from the color table
106  return m_Colors[p % m_Colors.size()];
107  }
108 
109  void AddColor(unsigned char r, unsigned char g, unsigned char b)
110  {
111  TRGBPixel rgbPixel;
113 
114  using ValueType = typename TRGBPixel::ValueType;
115 
116  ValueType m = NumericTraits<ValueType>::max();
117 
118  rgbPixel[0] = static_cast< ValueType >( static_cast< double >( r ) / 255 * m );
119  rgbPixel[1] = static_cast< ValueType >( static_cast< double >( g ) / 255 * m );
120  rgbPixel[2] = static_cast< ValueType >( static_cast< double >( b ) / 255 * m );
121  m_Colors.push_back(rgbPixel);
122  }
123 
124  // Empty the color LUT
125  void ResetColors()
126  {
127  m_Colors.clear();
128  }
129 
130  // Get number of colors in the LUT
131  unsigned int GetNumberOfColors() const
132  {
133  return static_cast<unsigned int>( m_Colors.size() );
134  }
135 
136  bool operator!=(const Self & l) const
137  {
140  || m_Colors.size() != l.m_Colors.size() )
141  {
142  return true;
143  }
144 
145  // We need to check each color to see if it's different
146  for ( typename std::vector< TRGBPixel >::size_type i = 0; i < m_Colors.size(); ++i )
147  {
148  if ( m_Colors[i] != l.m_Colors[i] )
149  {
150  return true;
151  }
152  }
153  return false;
154  }
155 
156  bool operator==(const Self & other) const
157  {
158  return !( *this != other );
159  }
160 
161  void SetBackgroundValue(TLabel v)
162  {
163  m_BackgroundValue = v;
164  }
165 
166  void SetBackgroundColor(TRGBPixel rgb)
167  {
168  m_BackgroundColor = rgb;
169  }
170 
171  ~LabelToRGBFunctor() = default;
172 
173  std::vector< TRGBPixel > m_Colors;
174 
175  TRGBPixel m_BackgroundColor;
176 
178 };
179 } // end namespace Functor
180 } // end namespace itk
181 
182 #endif
Define numeric traits for std::vector.
bool operator==(const Self &other) const
bool operator!=(const Self &l) const
std::vector< TRGBPixel > m_Colors
Functor for converting labels into RGB triplets.
unsigned int GetNumberOfColors() const
TRGBPixel operator()(const TLabel &p) const
void AddColor(unsigned char r, unsigned char g, unsigned char b)