The Original CHomP Software
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
chomp::homology::ColorPalette Class Reference

Provides a palette of distinct RGB colors. More...

#include <colorpal.h>

Public Member Functions

 ColorPalette (int _n, bool grays=false, bool pycolors=false)
 The constructor of a palette of a prescribed size. More...
 
 ~ColorPalette ()
 The destructor. More...
 
int operator[] (int i) const
 Returns the color with the given number. More...
 

Private Member Functions

 ColorPalette (const ColorPalette &src)
 The copy constructor is not allowed. More...
 
ColorPaletteoperator= (const ColorPalette &src)
 The assignment operator is not allowed. More...
 

Static Private Member Functions

static int generateComponent (int bitMask)
 Generates a color component based on the bit mask. More...
 

Private Attributes

int n
 The number of colors in the palette. More...
 
int * colors
 The RBG colors in the palette. More...
 

Detailed Description

Provides a palette of distinct RGB colors.

The first color is black unless pycolors is set to true. If using the pycolors option, python-style color palette is provided. White is considered a background color and thus is not provided.

Definition at line 52 of file colorpal.h.

Constructor & Destructor Documentation

◆ ColorPalette() [1/2]

chomp::homology::ColorPalette::ColorPalette ( int  _n,
bool  grays = false,
bool  pycolors = false 
)
inline

The constructor of a palette of a prescribed size.

Definition at line 100 of file colorpal.h.

100 :
101 n (_n), colors (0)
102{
103 if (n <= 0)
104 return;
105 colors = new int [n];
106
107 if (grays)
108 {
109 for (int i = 0; i < n; ++ i)
110 {
111 int shade = i * 255 / n;
112 colors [i] = shade | (shade << 8) | (shade << 16);
113 }
114 return;
115 }
116
117 // prepare a collection of nice colors (previous version)
118 const int niceCountPrevious = 14;
119 const int niceColorsPrevious [niceCountPrevious] = {
120 0x000000, 0x0000FF, 0xFF0000, 0x00FF00,
121 0x00FFFF, 0xFF00FF, 0x7F007F, 0xFF7F00,
122 0x007F00, 0x7F7F7F, 0xAFAFAF, 0x00007F,
123 0x7F00FF, 0xFFFF00,
124 };
125
126 // prepare a collection of nice colors (Python-like version)
127 // (note: pycolors.py)
128 const int niceCountPython = 44;
129 const int niceColorsPython [niceCountPython] = {
130 0x1F77B4, 0x2CA02C, 0x9467BD, 0x8C564B,
131 0xE377C2, 0x7F7F7F, 0x17BECF, 0xBCBD22,
132 0xFF7F0E, 0xD62728, 0x1B9E77, 0xFFD92F,
133 0x5254A3, 0x637939, 0x8C6D31, 0xBD9E39,
134 0xE7BA52, 0xE7CB94, 0x9C9ED3, 0x8CA252,
135 0x6B6ECF, 0xB5CF8B, 0xCEDB9C, 0xD6616B,
136 0xE7969C, 0xA55194, 0xDE9ED6, 0x6BAED6,
137 0x9ECAE1, 0xE65503, 0xFDAE6B, 0x74C476,
138 0xA1D99B, 0x756BB1, 0x9E9AC8, 0xBCBDDC,
139 0x636363, 0x909090, 0xC7C7C7, 0x843C39,
140 0x7B4173, 0xCE6DBD, 0xAD494A, 0x393B79,
141 };
142
143 int niceCount = pycolors ? niceCountPython : niceCountPrevious;
144
145 int counter = 1;
146 int pos = 0;
147 while (pos < n)
148 {
149 // take a nice color if possible
150 if (pos < niceCount)
151 {
152 colors [pos] = (pycolors ? niceColorsPython :
153 niceColorsPrevious) [pos];
154 ++ pos;
155 continue;
156 }
157
158 // create a color based on the current counter
159 int red = generateComponent (counter >> 1);
160 int green = generateComponent (counter >> 2);
161 int blue = generateComponent (counter);
162 int color = (red << 16) | (green << 8) | blue;
163
164 // check whether this color has already appeared before
165 bool repeated = false;
166 for (int i = 0; i < pos; ++ i)
167 {
168 if (colors [i] == color)
169 {
170 repeated = true;
171 break;
172 }
173 }
174
175 // if the color is not repeated and it is not white then ok
176 if (!repeated && (color != 0xFFFFFF) && (color != 0xFFFF7F))
177 colors [pos ++] = color;
178 ++ counter;
179 }
180
181 return;
182} /* ColorPalette::ColorPalette */
static int generateComponent(int bitMask)
Generates a color component based on the bit mask.
Definition: colorpal.h:86
int * colors
The RBG colors in the palette.
Definition: colorpal.h:80
int n
The number of colors in the palette.
Definition: colorpal.h:77

References colors, generateComponent(), and n.

◆ ~ColorPalette()

chomp::homology::ColorPalette::~ColorPalette ( )
inline

The destructor.

Definition at line 184 of file colorpal.h.

185{
186 if (colors)
187 delete [] colors;
188 return;
189} /* ColorPalette::~ColorPalette */

References colors.

◆ ColorPalette() [2/2]

chomp::homology::ColorPalette::ColorPalette ( const ColorPalette src)
inlineprivate

The copy constructor is not allowed.

Definition at line 191 of file colorpal.h.

192{
193 return;
194} /* ColorPalette::ColorPalette */

Member Function Documentation

◆ generateComponent()

int chomp::homology::ColorPalette::generateComponent ( int  bitMask)
inlinestaticprivate

Generates a color component based on the bit mask.

The nonzero bits are every 3 locations in the mask.

Definition at line 86 of file colorpal.h.

87{
88 int mask = 0x100;
89 int result = 1;
90 while (mask && bitMask)
91 {
92 if (bitMask & 1)
93 result = mask;
94 mask >>= 1;
95 bitMask >>= 3;
96 }
97 return (result - 1) & 0xFF;
98} /* ColorPalette::generateComponent */

Referenced by ColorPalette().

◆ operator=()

ColorPalette & chomp::homology::ColorPalette::operator= ( const ColorPalette src)
inlineprivate

The assignment operator is not allowed.

Definition at line 196 of file colorpal.h.

197{
198 return *this;
199} /* ColorPalette::operator = */

◆ operator[]()

int chomp::homology::ColorPalette::operator[] ( int  i) const
inline

Returns the color with the given number.

The color is encoded as 0xRRGGBB.

Definition at line 201 of file colorpal.h.

202{
203 if ((i < 0) || (i >= n))
204 return 0;
205 else
206 return colors [i];
207} /* ColorPalette::operator [] */

References colors, and n.

Member Data Documentation

◆ colors

int* chomp::homology::ColorPalette::colors
private

The RBG colors in the palette.

Definition at line 80 of file colorpal.h.

Referenced by ColorPalette(), operator[](), and ~ColorPalette().

◆ n

int chomp::homology::ColorPalette::n
private

The number of colors in the palette.

Definition at line 77 of file colorpal.h.

Referenced by ColorPalette(), and operator[]().


The documentation for this class was generated from the following file: