17 #ifndef __itkColorTable_txx
18 #define __itkColorTable_txx
21 #include "itkNumericTraits.h"
22 #include "vnl/vnl_sample.h"
29 template<
class TPixel>
38 template<
class TPixel>
45 template<
class TPixel>
56 if(m_ColorName !=
NULL)
59 for(i=0; i < m_NumberOfColors; i++)
61 delete [] m_ColorName[i];
63 delete [] m_ColorName;
68 template<
class TPixel>
73 Superclass::PrintSelf(os, indent);
75 os << indent <<
"m_NumberOfColors = " << m_NumberOfColors << std::endl;
76 os << indent <<
"m_Color = " << m_Color << std::endl;
77 for (
unsigned int i = 0; i < m_NumberOfColors; i++)
79 os << indent <<
"m_ColorName[" << i <<
"] = "
80 << m_ColorName[i] << std::endl;
84 template<
class TPixel>
93 m_ColorName =
new char*[m_NumberOfColors];
94 for(
unsigned int i=0; i<m_NumberOfColors; i++)
96 m_ColorName[i] =
new char [80];
99 m_Color[0].
Set((TPixel)0.9,(TPixel)0.0,(TPixel)0.0);
100 sprintf(m_ColorName[0],
"Red");
102 m_Color[1].Set((TPixel)0.8,(TPixel)0.0,(TPixel)0.8);
103 sprintf(m_ColorName[1],
"Purple");
105 m_Color[2].Set((TPixel)0.0,(TPixel)0.8,(TPixel)0.8);
106 sprintf(m_ColorName[2],
"Aqua");
108 m_Color[3].Set((TPixel)0.8,(TPixel)0.8,(TPixel)0.0);
109 sprintf(m_ColorName[3],
"Yellow");
111 m_Color[4].Set((TPixel)0.0,(TPixel)0.9,(TPixel)0.0);
112 sprintf(m_ColorName[4],
"Green");
114 m_Color[5].Set((TPixel)0.0,(TPixel)0.0,(TPixel)0.9);
115 sprintf(m_ColorName[5],
"Blue");
117 m_Color[6].Set((TPixel)0.7,(TPixel)0.7,(TPixel)0.7);
118 sprintf(m_ColorName[6],
"Grey0.70");
120 m_Color[7].Set((TPixel)1.0,(TPixel)1.0,(TPixel)1.8);
121 sprintf(m_ColorName[7],
"White");
124 template<
class TPixel>
131 this->DeleteColors();
133 m_NumberOfColors = n;
135 m_ColorName =
new char * [m_NumberOfColors];
136 for(i=0; i<m_NumberOfColors; i++)
138 m_ColorName[i] =
new char [80];
141 typename NumericTraits<TPixel>::RealType range =
142 NumericTraits<TPixel>::max() - NumericTraits<TPixel>::NonpositiveMin();
143 typename NumericTraits<TPixel>::RealType delta = range / (n - 1);
147 gray = NumericTraits<TPixel>::NonpositiveMin()
148 +
static_cast<TPixel
>(i * delta);
149 m_Color[i].Set(gray, gray, gray);
150 sprintf(m_ColorName[i],
"Gray%.02f", static_cast<float>(gray));
154 template<
class TPixel>
161 this->DeleteColors();
163 m_NumberOfColors = n;
165 m_ColorName =
new char * [m_NumberOfColors];
166 for(i=0; i<m_NumberOfColors; i++)
168 m_ColorName[i] =
new char [80];
171 for(i=0; i<n/2.0; i++)
173 m_Color[i].
SetRed( (i+1)/(TPixel)(n/2.0+1) );
174 m_Color[i].SetGreen( 0 );
175 m_Color[i].SetBlue ( 0 );
176 sprintf(m_ColorName[i],
"Heat%.02f", i/(
float)n);
181 m_Color[(int)(i+n/2.0)].SetRed(static_cast<TPixel>(1));
182 m_Color[(int)(i+n/2.0)].SetGreen((i+1)/(TPixel)(n/2.0+1));
183 m_Color[(int)(i+n/2.0)].SetBlue((i+1)/(TPixel)(n/2.0+1));
184 sprintf(m_ColorName[(
int)(i+n/2.0)],
"Heat%.02f", (i+n/2.0)/(
float)n);
188 template<
class TPixel>
194 this->DeleteColors();
196 m_NumberOfColors = n;
198 m_ColorName =
new char * [m_NumberOfColors];
199 for(i=0; i<m_NumberOfColors; i++)
201 m_ColorName[i] =
new char [80];
207 r =
static_cast<TPixel
>(vnl_sample_uniform(NumericTraits<TPixel>
208 ::NonpositiveMin(), NumericTraits<TPixel>::max()));
210 g =
static_cast<TPixel
>(vnl_sample_uniform(NumericTraits<TPixel>
211 ::NonpositiveMin(), NumericTraits<TPixel>::max()));
212 m_Color[i].SetGreen( g );
213 b =
static_cast<TPixel
>(vnl_sample_uniform(NumericTraits<TPixel>
214 ::NonpositiveMin(), NumericTraits<TPixel>::max()));
215 m_Color[i].SetBlue( b );
216 sprintf(m_ColorName[i],
"Random(%.02f,%.02f,%.02f)",
217 static_cast<float>(r),
218 static_cast<float>(g),
219 static_cast<float>(b));
223 template<
class TPixel>
228 itkWarningMacro(<<
"Call to itkColorTable.size() is being depreciated. " \
229 <<
"Use itkColorTable.GetNumberOfColors() instead.");
230 return m_NumberOfColors;
233 template<
class TPixel>
236 ::SetColor(
unsigned int c, TPixel r, TPixel g, TPixel b,
const char *name)
238 if(c < m_NumberOfColors)
240 m_Color[c].SetRed( r );
241 m_Color[c].SetGreen( g );
242 m_Color[c].SetBlue ( b );
243 strcpy(m_ColorName[c], name);
249 template<
class TPixel>
254 if(c < m_NumberOfColors)
264 template<
class TPixel>
269 itkWarningMacro(<<
"Call to itkColorTable.color(unsigned int colorID) " \
270 <<
"is being depreciated. " \
271 <<
"Use itkColorTable.GetColor(unsigned int colorID) instead.");
272 return this->GetColor(c);
275 template<
class TPixel>
280 if(c < m_NumberOfColors)
286 return m_Color[c].GetRed();
290 return m_Color[c].GetGreen();
294 return m_Color[c].GetBlue();
308 template<
class TPixel>
313 itkWarningMacro(<<
"Call to itkColorTable.color(unsigned int colorID, "
314 <<
"char rgb) is being depreciated. "
315 <<
"Use itkColorTable.GetColorComponent("
316 <<
"unsigned int colorID, char rgb) instead.");
317 return this->GetColorComponent(c, rgb);
320 template<
class TPixel>
325 if(c<m_NumberOfColors)
327 return m_ColorName[c];
335 template<
class TPixel>
340 itkWarningMacro(<<
"Call to itkColorTable.colorName(unsigned int colorID)" \
341 <<
"is being depreciated." \
342 <<
"Use itkColorTable.GetColorName(unsigned int colorID)"
344 return this->GetColorName(c);
347 template<
class TPixel>
353 double bestMatch = 0;
354 unsigned int bestMatchColor = 0;
355 for(
unsigned int i=0; i<m_NumberOfColors; i++)
357 match = (r - (double)m_Color[i].GetRed())
358 * (r - (
double)m_Color[i].GetRed());
359 match += (g - (double)m_Color[i].GetGreen())
360 * (g - (
double)m_Color[i].GetGreen());
361 match += (b - (double)m_Color[i].GetGreen())
362 * (b - (
double)m_Color[i].GetBlue());
363 if(i == 0 || match < bestMatch)
369 return bestMatchColor;