Orfeo Toolbox  3.16
itkColorTable.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkColorTable.txx,v $
5  Language: C++
6  Date: $Date: 2008-05-26 02:36:52 $
7  Version: $Revision: 1.21 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkColorTable_txx
18 #define __itkColorTable_txx
19 
20 #include "itkColorTable.h"
21 #include "itkNumericTraits.h"
22 #include "vnl/vnl_sample.h"
23 #include <stdio.h>
24 #include <cstring>
25 
26 namespace itk
27 {
28 
29 template<class TPixel>
32 {
33  m_NumberOfColors = 0;
34  m_Color = NULL;
35  m_ColorName = NULL;
36 }
37 
38 template<class TPixel>
41 {
42  this->DeleteColors();
43 }
44 
45 template<class TPixel>
46 void
49 {
50  if(m_Color != NULL)
51  {
52  delete [] m_Color;
53  }
54  m_Color = NULL;
55 
56  if(m_ColorName != NULL)
57  {
58  unsigned int i;
59  for(i=0; i < m_NumberOfColors; i++)
60  {
61  delete [] m_ColorName[i];
62  }
63  delete [] m_ColorName;
64  }
65  m_ColorName = NULL;
66 }
67 
68 template<class TPixel>
69 void
71 ::PrintSelf(std::ostream & os, Indent indent) const
72 {
73  Superclass::PrintSelf(os, indent);
74 
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++)
78  {
79  os << indent << "m_ColorName[" << i << "] = "
80  << m_ColorName[i] << std::endl;
81  }
82 }
83 
84 template<class TPixel>
85 void
88 {
89  this->DeleteColors();
90 
91  m_NumberOfColors = 8;
92  m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
93  m_ColorName = new char*[m_NumberOfColors];
94  for(unsigned int i=0; i<m_NumberOfColors; i++)
95  {
96  m_ColorName[i] = new char [80];
97  }
98 
99  m_Color[0].Set((TPixel)0.9,(TPixel)0.0,(TPixel)0.0);
100  sprintf(m_ColorName[0], "Red");
101 
102  m_Color[1].Set((TPixel)0.8,(TPixel)0.0,(TPixel)0.8);
103  sprintf(m_ColorName[1], "Purple");
104 
105  m_Color[2].Set((TPixel)0.0,(TPixel)0.8,(TPixel)0.8);
106  sprintf(m_ColorName[2], "Aqua");
107 
108  m_Color[3].Set((TPixel)0.8,(TPixel)0.8,(TPixel)0.0);
109  sprintf(m_ColorName[3], "Yellow");
110 
111  m_Color[4].Set((TPixel)0.0,(TPixel)0.9,(TPixel)0.0);
112  sprintf(m_ColorName[4], "Green");
113 
114  m_Color[5].Set((TPixel)0.0,(TPixel)0.0,(TPixel)0.9);
115  sprintf(m_ColorName[5], "Blue");
116 
117  m_Color[6].Set((TPixel)0.7,(TPixel)0.7,(TPixel)0.7);
118  sprintf(m_ColorName[6], "Grey0.70");
119 
120  m_Color[7].Set((TPixel)1.0,(TPixel)1.0,(TPixel)1.8);
121  sprintf(m_ColorName[7], "White");
122 }
123 
124 template<class TPixel>
125 void
127 ::UseGrayColors(unsigned int n)
128 {
129  unsigned int i;
130 
131  this->DeleteColors();
132 
133  m_NumberOfColors = n;
134  m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
135  m_ColorName = new char * [m_NumberOfColors];
136  for(i=0; i<m_NumberOfColors; i++)
137  {
138  m_ColorName[i] = new char [80];
139  }
140 
141  typename NumericTraits<TPixel>::RealType range =
142  NumericTraits<TPixel>::max() - NumericTraits<TPixel>::NonpositiveMin();
143  typename NumericTraits<TPixel>::RealType delta = range / (n - 1);
144  TPixel gray;
145  for(i=0; i<n; i++)
146  {
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));
151  }
152 }
153 
154 template<class TPixel>
155 void
157 ::UseHeatColors(unsigned int n)
158 {
159  unsigned int i;
160 
161  this->DeleteColors();
162 
163  m_NumberOfColors = n;
164  m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
165  m_ColorName = new char * [m_NumberOfColors];
166  for(i=0; i<m_NumberOfColors; i++)
167  {
168  m_ColorName[i] = new char [80];
169  }
170 
171  for(i=0; i<n/2.0; i++)
172  {
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);
177  }
178 
179  for(i=0; i<n/2; i++)
180  {
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);
185  }
186 }
187 
188 template<class TPixel>
189 void
191 ::UseRandomColors(unsigned int n)
192 {
193  unsigned int i;
194  this->DeleteColors();
195 
196  m_NumberOfColors = n;
197  m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
198  m_ColorName = new char * [m_NumberOfColors];
199  for(i=0; i<m_NumberOfColors; i++)
200  {
201  m_ColorName[i] = new char [80];
202  }
203 
204  TPixel r, g, b;
205  for(i=0; i<n; i++)
206  {
207  r = static_cast<TPixel>(vnl_sample_uniform(NumericTraits<TPixel>
208  ::NonpositiveMin(), NumericTraits<TPixel>::max()));
209  m_Color[i].SetRed( r );
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));
220  }
221 }
222 
223 template<class TPixel>
224 unsigned int
226 ::size(void)
227 {
228  itkWarningMacro(<< "Call to itkColorTable.size() is being depreciated. " \
229  << "Use itkColorTable.GetNumberOfColors() instead.");
230  return m_NumberOfColors;
231 }
232 
233 template<class TPixel>
234 bool
236 ::SetColor(unsigned int c, TPixel r, TPixel g, TPixel b, const char *name)
237 {
238  if(c < m_NumberOfColors)
239  {
240  m_Color[c].SetRed( r );
241  m_Color[c].SetGreen( g );
242  m_Color[c].SetBlue ( b );
243  strcpy(m_ColorName[c], name);
244  return true;
245  }
246  return false;
247 }
248 
249 template<class TPixel>
252 ::GetColor(unsigned int c)
253 {
254  if(c < m_NumberOfColors)
255  {
256  return & m_Color[c];
257  }
258  else
259  {
260  return NULL;
261  }
262 }
263 
264 template<class TPixel>
267 ::color(unsigned int c)
268 {
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);
273 }
274 
275 template<class TPixel>
276 TPixel
278 ::GetColorComponent(unsigned int c, char rgb)
279 {
280  if(c < m_NumberOfColors)
281  {
282  switch(rgb)
283  {
284  case 'r' :
285  {
286  return m_Color[c].GetRed();
287  }
288  case 'g' :
289  {
290  return m_Color[c].GetGreen();
291  }
292  case 'b' :
293  {
294  return m_Color[c].GetBlue();
295  }
296  default:
297  {
298  return 0;
299  }
300  }
301  }
302  else
303  {
304  return 0;
305  }
306 }
307 
308 template<class TPixel>
309 TPixel
311 ::color(unsigned int c, char rgb)
312 {
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);
318 }
319 
320 template<class TPixel>
321 char *
323 ::GetColorName(unsigned int c)
324 {
325  if(c<m_NumberOfColors)
326  {
327  return m_ColorName[c];
328  }
329  else
330  {
331  return NULL;
332  }
333 }
334 
335 template<class TPixel>
336 char *
338 ::colorName(unsigned int c)
339 {
340  itkWarningMacro(<< "Call to itkColorTable.colorName(unsigned int colorID)" \
341  << "is being depreciated." \
342  << "Use itkColorTable.GetColorName(unsigned int colorID)"
343  << "instead.");
344  return this->GetColorName(c);
345 }
346 
347 template<class TPixel>
348 unsigned int
350 ::GetClosestColorTableId(TPixel r, TPixel g, TPixel b)
351 {
352  double match;
353  double bestMatch = 0;
354  unsigned int bestMatchColor = 0;
355  for(unsigned int i=0; i<m_NumberOfColors; i++)
356  {
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)
364  {
365  bestMatch = match;
366  bestMatchColor = i;
367  }
368  }
369  return bestMatchColor;
370 }
371 
372 } // namespace itk
373 
374 
375 #endif

Generated at Sat Feb 2 2013 23:32:28 for Orfeo Toolbox with doxygen 1.8.1.1