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 itkColorTable_h
19 #define itkColorTable_h
20 
21 #include "itkObject.h"
22 #include "itkRGBPixel.h"
23 #include "itkObjectFactory.h"
24 
25 #include <string>
26 #include <vector>
27 namespace itk
28 {
29 /** \class ColorTable
30  *  \brief Define a color table for image visualisation.
31  *
32  * Generates color lookup tables of various types. The lookup table
33  * value range differs for integral and continuous data types. Consult
34  * the documentation for each lookup table generator.
35  * \ingroup DataRepresentation
36  * \ingroup ITKCommon
37  */
38 
39 template< typename TPixel >
40 class ITK_TEMPLATE_EXPORT ColorTable:public Object
41 {
42 public:
43   ITK_DISALLOW_COPY_AND_ASSIGN(ColorTable);
44 
45   /** Standard class type aliases. */
46   using Self = ColorTable;
47   using Superclass = Object;
48   using Pointer = SmartPointer< Self >;
49   using ConstPointer = SmartPointer< const Self >;
50 
51   /** Method for creation through the object factory. */
52   itkNewMacro(Self);
53 
54   /** Run-time type information (and related methods). */
55   itkTypeMacro(ColorTable, Object);
56 
57   /** Generate a lookup table of 8 discrete colors. The colors are Red,
58     * Purple, Aqua, Yellow, Green, Blue, Grey0.70, White. For integral
59     * pixel types, the color range is between NonpositiveMin() and max(). For
60     * continuous types, the range is 0.0 to 1.0.
61     */
62   void    UseDiscreteColors();
63 
64   /** Generate a lookuptable of n grayscale values. For integral pixel
65     * types, a ramp is generated from NonpositiveMin() to max() of the
66     * pixel type. For continuous pixel types, the range is 0.0 to 1.0.
67     */
68   void    UseGrayColors(unsigned int n = 256);
69 
70   /** Generate a lookup table of n values good for showing
71     * "temperatures".  For integral pixel types, the color range is
72     * between NonpositiveMin() and max(). For continuous types, the
73     * range is 0.0 to 1.0.
74     */
75   void    UseHeatColors(unsigned int n = 256);
76 
77   /** Generate a lookup table of n random values. For integral pixel
78     * types, the color range is between NonpositiveMin() and
79     * max(). For continuous types, the range is 0.0 to 1.0.
80     */
81   void    UseRandomColors(unsigned int n = 256);
82 
83   /** Get the number of colors in the lookup table. */
84   itkGetConstMacro(NumberOfColors, unsigned int);
85 
86   /** Get the color stored at a given index. */
87   RGBPixel< TPixel > GetColor(unsigned int colorId);
88 
89   /** Set the color at a given index. Optionally provide a name for
90     * the color. If a name is not provided, the name "UserDefined" is
91     * used.
92     */
93   bool    SetColor(unsigned int c, TPixel r, TPixel g, TPixel b,
94                    const char *name = "UserDefined");
95   bool    SetColor(unsigned int c, RGBPixel<TPixel> pixel,
96                    const char *name = "UserDefined");
97 
98   /** Given the position in the table and the color
99     * returns the value.
100     */
101   TPixel  GetColorComponent(unsigned int colorId, char rgb);
102 
103   /** Get the name of the color at a given index. */
104   std::string  GetColorName(unsigned int colorId);
105 
106   /** Find the color closest to a given pixel. Uses a L2 distance
107     * metric.
108      */
109   unsigned int GetClosestColorTableId(TPixel r, TPixel g, TPixel b);
110 
111 protected:
112   ColorTable() = default;
113   void PrintSelf(std::ostream & os, Indent indent) const override;
114 
115 private:
116   using ColorNameVectorType = std::vector< std::string >;
117   using ColorVectorType = std::vector< RGBPixel< TPixel > >;
118 
119   void DeleteColors();
120 
121   unsigned int m_NumberOfColors{ 0 };
122 
123   ColorNameVectorType         m_ColorName;
124   ColorVectorType             m_Color;
125 };
126 } // namespace itk
127 
128 #ifndef ITK_MANUAL_INSTANTIATION
129 #include "itkColorTable.hxx"
130 #endif
131 
132 #endif
133