The Original CHomP Software
colorpal.h
Go to the documentation of this file.
1
3
15
16// Copyright (C) 1997-2020 by Pawel Pilarczyk.
17//
18// This file is part of my research software package. This is free software:
19// you can redistribute it and/or modify it under the terms of the GNU
20// General Public License as published by the Free Software Foundation,
21// either version 3 of the License, or (at your option) any later version.
22//
23// This software is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26// GNU General Public License for more details.
27//
28// You should have received a copy of the GNU General Public License
29// along with this software; see the file "license.txt". If not,
30// please, see <https://www.gnu.org/licenses/>.
31
32// Started on February 14, 2007. Last revision: February 20, 2022.
33
34
35#ifndef _CHOMP_BITMAPS_COLORPAL_H_
36#define _CHOMP_BITMAPS_COLORPAL_H_
37
38#include "chomp/system/config.h"
39
40namespace chomp {
41namespace homology {
42
43
44// --------------------------------------------------
45// ----------------- Color Palette ------------------
46// --------------------------------------------------
47
53{
54public:
56 ColorPalette (int _n, bool grays = false, bool pycolors = false);
57
60
63 int operator [] (int i) const;
64
65private:
67 ColorPalette (const ColorPalette &src);
68
71
74 static int generateComponent (int bitMask);
75
77 int n;
78
80 int *colors;
81
82}; /* ColorPalette */
83
84// --------------------------------------------------
85
86inline int ColorPalette::generateComponent (int bitMask)
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 */
99
100inline ColorPalette::ColorPalette (int _n, bool grays, bool pycolors):
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 */
183
185{
186 if (colors)
187 delete [] colors;
188 return;
189} /* ColorPalette::~ColorPalette */
190
192{
193 return;
194} /* ColorPalette::ColorPalette */
195
197{
198 return *this;
199} /* ColorPalette::operator = */
200
201inline int ColorPalette::operator [] (int i) const
202{
203 if ((i < 0) || (i >= n))
204 return 0;
205 else
206 return colors [i];
207} /* ColorPalette::operator [] */
208
209
210} // namespace homology
211} // namespace chomp
212
213#endif // _CHOMP_BITMAPS_COLORPAL_H_
214
216
Provides a palette of distinct RGB colors.
Definition: colorpal.h:53
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
ColorPalette(int _n, bool grays=false, bool pycolors=false)
The constructor of a palette of a prescribed size.
Definition: colorpal.h:100
int operator[](int i) const
Returns the color with the given number.
Definition: colorpal.h:201
~ColorPalette()
The destructor.
Definition: colorpal.h:184
ColorPalette & operator=(const ColorPalette &src)
The assignment operator is not allowed.
Definition: colorpal.h:196
int n
The number of colors in the palette.
Definition: colorpal.h:77
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51