ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkLabelToRGBFunctor.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 itkLabelToRGBFunctor_h
19#define itkLabelToRGBFunctor_h
20
21#include <vector>
22#include "itkNumericTraits.h"
23
24namespace itk::Functor
25{
47template <typename TLabel, typename TRGBPixel>
49{
50public:
52
54 : m_BackgroundValue(TLabel{})
55 {
56 using ValueType = typename TRGBPixel::ValueType;
57
58 // the following colors are from "R", and named:
59 // "red" "green3" "blue" "cyan"
60 //"magenta" "darkorange1" "darkgreen" "blueviolet"
61 //"brown4" "navy" "yellow4" "violetred1"
62 //"salmon4" "turquoise4" "sienna3" "darkorchid1"
63 //"springgreen4" "mediumvioletred" "orangered3" "lightseagreen"
64 //"slateblue" "deeppink1" "aquamarine4" "royalblue1"
65 //"tomato3" "mediumblue" "violetred4" "darkmagenta"
66 //"violet" "red4"
67 // They are a good selection of distinct colors for plotting and
68 // overlays.
69 constexpr size_t numColors = 30;
70 constexpr unsigned char colors[numColors][3] = {
71 { 255, 0, 0 }, { 0, 205, 0 }, { 0, 0, 255 }, { 0, 255, 255 }, { 255, 0, 255 }, { 255, 127, 0 },
72 { 0, 100, 0 }, { 138, 43, 226 }, { 139, 35, 35 }, { 0, 0, 128 }, { 139, 139, 0 }, { 255, 62, 150 },
73 { 139, 76, 57 }, { 0, 134, 139 }, { 205, 104, 57 }, { 191, 62, 255 }, { 0, 139, 69 }, { 199, 21, 133 },
74 { 205, 55, 0 }, { 32, 178, 170 }, { 106, 90, 205 }, { 255, 20, 147 }, { 69, 139, 116 }, { 72, 118, 255 },
75 { 205, 79, 57 }, { 0, 0, 205 }, { 139, 34, 82 }, { 139, 0, 139 }, { 238, 130, 238 }, { 139, 0, 0 }
76 };
77
78 for (auto & color : colors)
79 {
80 AddColor(color[0], color[1], color[2]);
81 }
82
83 // provide some default value for external use (outside
84 // LabelToRGBImageFilter)
85 // Inside LabelToRGBImageFilter, the values are always initialized
87 m_BackgroundColor.Fill(ValueType{});
88 }
89
90 inline TRGBPixel
91 operator()(const TLabel & p) const
92 {
93 // value is background
94 // return a gray pixel with the same intensity than the label pixel
95 if (p == m_BackgroundValue)
96 {
97 return m_BackgroundColor;
98 }
99
100 // else, return a colored pixel from the color table
101 return m_Colors[p % m_Colors.size()];
102 }
103
104 void
105 AddColor(unsigned char r, unsigned char g, unsigned char b)
106 {
107 TRGBPixel rgbPixel;
109
110 using ValueType = typename TRGBPixel::ValueType;
111
112 const ValueType m = NumericTraits<ValueType>::max();
113
114 rgbPixel[0] = static_cast<ValueType>(static_cast<double>(r) / 255 * m);
115 rgbPixel[1] = static_cast<ValueType>(static_cast<double>(g) / 255 * m);
116 rgbPixel[2] = static_cast<ValueType>(static_cast<double>(b) / 255 * m);
117 m_Colors.push_back(rgbPixel);
118 }
119
120 // Empty the color LUT
121 void
123 {
124 m_Colors.clear();
125 }
126
127 // Get number of colors in the LUT
128 [[nodiscard]] unsigned int
130 {
131 return static_cast<unsigned int>(m_Colors.size());
132 }
133
134 bool
135 operator==(const Self & other) const
136 {
138 m_Colors == other.m_Colors;
139 }
140
142
143 void
145 {
147 }
148
149 void
150 SetBackgroundColor(TRGBPixel rgb)
151 {
152 m_BackgroundColor = rgb;
153 }
154
156
157 std::vector<TRGBPixel> m_Colors;
158
160
162};
163} // namespace itk::Functor
164
165#endif
bool operator==(const Self &other) const
void AddColor(unsigned char r, unsigned char g, unsigned char b)
TRGBPixel operator()(const TLabel &p) const
static constexpr T max(const T &)
static void SetLength(T &m, const unsigned int s)