KWStyle - itkColorTable.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkColorTable.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:34 $
7   Version:   $Revision: 1.4 $
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 DEF #ifndef _itkColorTable_txx
18 DEF #define _itkColorTable_txx
19
20 #include "itkColorTable.h"
21 #include "vnl/vnl_sample.h"
22 #include <stdio.h>
23
24 namespace itk
25 {
26
27 template<class TPixel>
28 ColorTable<TPixel>
29 ::ColorTable()
30 {
31   m_NumberOfColors = 0;
32   m_Color = NULL;
33   m_ColorName = NULL;
34 }
35
36 template<class TPixel>
37 ColorTable<TPixel>
38 ::~ColorTable()
39 IND **{
40   this->DeleteColors();
41 IND **}
42
43 template<class TPixel>
44 void
45 ColorTable<TPixel>
46 ::DeleteColors()
47 {
48   if(m_Color != NULL)
49     {
50     delete [] m_Color;
51     }
52   m_Color = NULL;
53
54   if(m_ColorName != NULL) 
55     {
56     unsigned int i;
57     for(i=0; i < m_NumberOfColors; i++)
58       {
59       delete [] m_ColorName[i];
60       }
61     delete [] m_ColorName;
62     }
63   m_ColorName = NULL;
64 }
65
66 template<class TPixel>
67 void
68 ColorTable<TPixel>
69 ::PrintSelf(std::ostream & os, Indent indent) const
70 IND **{
71   Superclass::PrintSelf(os, indent);
72
73   os << indent << "m_NumberOfColors = " << m_NumberOfColors << std::endl;
74   os << indent << "m_Color = " << m_Color << std::endl;
75   for (unsigned int i = 0; i < m_NumberOfColors; i++)
76     {
77 LEN     os << indent << "m_ColorName[" << i << "] = " << m_ColorName[i] << std::endl;
78     }
79 IND **}
80
81 template<class TPixel>
82 void 
83 ColorTable<TPixel>
84 ::UseDiscreteColors(void)
85 IND **{
86   this->DeleteColors();
87
88   m_NumberOfColors = 8;
89   m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
90   m_ColorName = new char*[m_NumberOfColors];
91   for(unsigned int i=0; i<m_NumberOfColors; i++)
92     {
93     m_ColorName[i] = new char [80];
94     }
95
96   m_Color[0].Set((TPixel)0.9,(TPixel)0.0,(TPixel)0.0);
97   sprintf(m_ColorName[0], "Red");
98
99   m_Color[1].Set((TPixel)0.8,(TPixel)0.0,(TPixel)0.8);
100   sprintf(m_ColorName[1], "Purple");
101
102   m_Color[2].Set((TPixel)0.0,(TPixel)0.8,(TPixel)0.8);
103   sprintf(m_ColorName[2], "Aqua");
104
105   m_Color[3].Set((TPixel)0.8,(TPixel)0.8,(TPixel)0.0);
106   sprintf(m_ColorName[3], "Yellow");
107
108   m_Color[4].Set((TPixel)0.0,(TPixel)0.9,(TPixel)0.0);
109   sprintf(m_ColorName[4], "Green");
110     
111   m_Color[5].Set((TPixel)0.0,(TPixel)0.0,(TPixel)0.9);
112   sprintf(m_ColorName[5], "Blue");
113
114   m_Color[6].Set((TPixel)0.7,(TPixel)0.7,(TPixel)0.7);
115   sprintf(m_ColorName[6], "Grey0.70");
116
117   m_Color[7].Set((TPixel)1.0,(TPixel)1.0,(TPixel)1.8);
118   sprintf(m_ColorName[7], "White");
119 IND **}
120
121 template<class TPixel>
122 void 
123 ColorTable<TPixel>
124 ::UseGrayColors(unsigned int n)
125 {
126   unsigned int i;
127
128   this->DeleteColors();
129
130   m_NumberOfColors = n;
131   m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
132   m_ColorName = new char * [m_NumberOfColors];
133   for(i=0; i<m_NumberOfColors; i++)
134     {
135     m_ColorName[i] = new char [80];
136     }
137
138   typename NumericTraits<TPixel>::RealType range =
139 IND ****NumericTraits<TPixel>::max() - NumericTraits<TPixel>::NonpositiveMin();
140   typename NumericTraits<TPixel>::RealType delta = range / (n - 1);  
141   TPixel gray;
142   for(i=0; i<n; i++) 
143     {
144     gray = NumericTraits<TPixel>::NonpositiveMin()
145 IND ******+ static_cast<TPixel>(i * delta);
146     m_Color[i].Set(gray, gray, gray);
147     sprintf(m_ColorName[i], "Gray%.02f", static_cast<float>(gray));
148     }
149 IND **}
150
151 template<class TPixel>
152 void
153 ColorTable<TPixel>
154 ::UseHeatColors(unsigned int n)
155 {
156   unsigned int i;
157
158   this->DeleteColors();
159
160   m_NumberOfColors = n;
161   m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
162   m_ColorName = new char * [m_NumberOfColors];
163   for(i=0; i<m_NumberOfColors; i++)
164     {
165     m_ColorName[i] = new char [80];
166     }
167
168   for(i=0; i<n/2.0; i++) 
169     {
170     m_Color[i].SetRed( (i+1)/(TPixel)(n/2.0+1) );
171     m_Color[i].SetGreen( 0 );
172     m_Color[i].SetBlue ( 0 );
173     sprintf(m_ColorName[i], "Heat%.02f", i/(float)n);
174     }
175
176   for(i=0; i<n/2; i++) 
177     {
178     m_Color[(int)(i+n/2.0)].SetRed(static_cast<TPixel>(1));
179     m_Color[(int)(i+n/2.0)].SetGreen((i+1)/(TPixel)(n/2.0+1));
180     m_Color[(int)(i+n/2.0)].SetBlue((i+1)/(TPixel)(n/2.0+1));
181     sprintf(m_ColorName[(int)(i+n/2.0)], "Heat%.02f", (i+n/2.0)/(float)n);
182     }
183 IND **}
184
185 template<class TPixel>
186 void
187 ColorTable<TPixel>
188 ::UseRandomColors(unsigned int n)
189 {
190   unsigned int i;
191   this->DeleteColors();
192
193   m_NumberOfColors = n;
194   m_Color = new RGBPixel<TPixel>[m_NumberOfColors];
195   m_ColorName = new char * [m_NumberOfColors];
196   for(i=0; i<m_NumberOfColors; i++)
197     {
198     m_ColorName[i] = new char [80];
199     }
200
201   TPixel r, g, b;
202   for(i=0; i<n; i++) 
203     {
204 LEN     r = static_cast<TPixel>(vnl_sample_uniform(NumericTraits<TPixel>::NonpositiveMin(), NumericTraits<TPixel>::max()));
205     m_Color[i].SetRed( r );
206 LEN     g = static_cast<TPixel>(vnl_sample_uniform(NumericTraits<TPixel>::NonpositiveMin(), NumericTraits<TPixel>::max()));
207     m_Color[i].SetGreen( g );
208 LEN     b = static_cast<TPixel>(vnl_sample_uniform(NumericTraits<TPixel>::NonpositiveMin(), NumericTraits<TPixel>::max()));
209     m_Color[i].SetBlue( b );
210     sprintf(m_ColorName[i], "Random(%.02f,%.02f,%.02f)",
211 IND ************static_cast<float>(r),
212 IND ************static_cast<float>(g),
213 IND ************static_cast<float>(b));
214     }
215 IND **}
216
217 template<class TPixel>
218 unsigned int
219 ColorTable<TPixel>
220 ::size(void)
221 IND **{
222   itkWarningMacro(<< "Call to itkColorTable.size() is being depreciated. " \
223                   << "Use itkColorTable.GetNumberOfColors() instead.");
224   return m_NumberOfColors;
225 IND **}
226
227 template<class TPixel>
228 bool 
229 ColorTable<TPixel>
230 ::SetColor(unsigned int c, TPixel r, TPixel g, TPixel b, const char *name)
231 IND **{
232   if(c < m_NumberOfColors)
233     {
234     m_Color[c].SetRed( r );
235     m_Color[c].SetGreen( g );
236     m_Color[c].SetBlue ( b );
237     strcpy(m_ColorName[c], name);
238     return true;
239     }
240   return false;
241 IND **}
242
243 template<class TPixel>
244 RGBPixel<TPixel> * 
245 ColorTable<TPixel>
246 ::GetColor(unsigned int c)
247 IND **{
248   if(c < m_NumberOfColors)
249     {
250     return & m_Color[c];
251     }
252   else
253     {
254     return NULL;
255     }
256 IND **}
257
258 template<class TPixel>
259 RGBPixel<TPixel>* 
260 ColorTable<TPixel>
261 ::color(unsigned int c)
262 IND **{
263   itkWarningMacro(<< "Call to itkColorTable.color(unsigned int colorID) " \
264                   << "is being depreciated. " \
265 LEN                   << "Use itkColorTable.GetColor(unsigned int colorID) instead.");
266   return this->GetColor(c);
267 IND **}
268
269 template<class TPixel>
270 TPixel
271 ColorTable<TPixel>
272 ::GetColorComponent(unsigned int c, char rgb)
273 IND **{
274   if(c < m_NumberOfColors)
275     {
276     switch(rgb) 
277       {
278       case 'r' : 
279         {
280         return m_Color[c].GetRed();
281         }
282       case 'g' : 
283         {
284         return m_Color[c].GetGreen();
285         }
286       case 'b' : 
287         {
288         return m_Color[c].GetBlue();
289         }
290       default: 
291         {
292         return 0;
293         }
294       }
295     }
296   else
297     {
298     return 0;
299     }
300 IND **}
301
302 template<class TPixel>
303 TPixel
304 ColorTable<TPixel>
305 ::color(unsigned int c, char rgb)
306 IND **{
307 LEN   itkWarningMacro(<< "Call to itkColorTable.color(unsigned int colorID, char rgb) " \
308                   << "is being depreciated. " \
309 LEN                   << "Use itkColorTable.GetColorComponent(unsigned int colorID, char rgb) instead.");
310   return this->GetColorComponent(c, rgb);
311 IND **}
312
313 template<class TPixel>
314 char * 
315 ColorTable<TPixel>
316 ::GetColorName(unsigned int c)
317 IND **{
318   if(c<m_NumberOfColors)
319     {
320     return m_ColorName[c];
321     }
322   else
323     {
324     return NULL;
325     }
326 IND **}
327
328 template<class TPixel>
329 char * 
330 ColorTable<TPixel>
331 ::colorName(unsigned int c)
332 IND **{
333   itkWarningMacro(<< "Call to itkColorTable.colorName(unsigned int colorID) " \
334                   << "is being depreciated. " \
335 LEN                   << "Use itkColorTable.GetColorName(unsigned int colorID) instead.");
336   return this->GetColorName(c);
337 IND **}
338
339 template<class TPixel>
340 unsigned int 
341 ColorTable<TPixel>
342 ::GetClosestColorTableId(TPixel r, TPixel g, TPixel b)
343 IND **{
344   double match;
345   double bestMatch = 0; 
346   unsigned int bestMatchColor = 0;
347   for(unsigned int i=0; i<m_NumberOfColors; i++)
348     {
349     match = (r - (double)m_Color[i].GetRed()) 
350 IND ************* (r - (double)m_Color[i].GetRed());
351     match += (g - (double)m_Color[i].GetGreen()) 
352 IND ************* (g - (double)m_Color[i].GetGreen());
353     match += (b - (double)m_Color[i].GetGreen()) 
354 IND ************* (b - (double)m_Color[i].GetBlue());
355     if(i == 0 || match < bestMatch)
356       {
357       bestMatch = match;
358       bestMatchColor = i;
359       }
360     }
361   return bestMatchColor;
362 IND **}
363
364 // namespace itk
365
366
367 #endif
368

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:02PM
© Kitware Inc.