Go to the documentation of this file.00001
00002
00003
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _CHOMP_BITMAPS_COLORPAL_H_
00037 #define _CHOMP_BITMAPS_COLORPAL_H_
00038
00039 #include "chomp/system/config.h"
00040
00041 namespace chomp {
00042 namespace homology {
00043
00044
00045
00046
00047
00048
00051 class ColorPalette
00052 {
00053 public:
00055 ColorPalette (int _n, bool grays = false);
00056
00058 ~ColorPalette ();
00059
00062 int operator [] (int i) const;
00063
00064 private:
00066 ColorPalette (const ColorPalette &src);
00067
00069 ColorPalette &operator = (const ColorPalette &src);
00070
00073 static int generateComponent (int bitMask);
00074
00076 int n;
00077
00079 int *colors;
00080
00081 };
00082
00083
00084
00085 inline int ColorPalette::generateComponent (int bitMask)
00086 {
00087 int mask = 0x100;
00088 int result = 1;
00089 while (mask && bitMask)
00090 {
00091 if (bitMask & 1)
00092 result = mask;
00093 mask >>= 1;
00094 bitMask >>= 3;
00095 }
00096 return (result - 1) & 0xFF;
00097 }
00098
00099 inline ColorPalette::ColorPalette (int _n, bool grays):
00100 n (_n), colors (0)
00101 {
00102 if (n <= 0)
00103 return;
00104 colors = new int [n];
00105
00106 if (grays)
00107 {
00108 for (int i = 0; i < n; ++ i)
00109 {
00110 int shade = i * 255 / n;
00111 colors [i] = shade | (shade << 8) | (shade << 16);
00112 }
00113 return;
00114 }
00115
00116 const int niceCount = 14;
00117 int niceColors [niceCount] = {
00118 0x000000, 0x0000FF, 0xFF0000, 0x00FF00,
00119 0x00FFFF, 0xFF00FF, 0x7F007F, 0xFF7F00,
00120 0x007F00, 0x7F7F7F, 0xAFAFAF, 0x00007F,
00121 0x7F00FF, 0xFFFF00,
00122 };
00123
00124 int counter = 1;
00125 int pos = 0;
00126 while (pos < n)
00127 {
00128
00129 if (pos < niceCount)
00130 {
00131 colors [pos] = niceColors [pos];
00132 ++ pos;
00133 continue;
00134 }
00135
00136
00137 int red = generateComponent (counter >> 1);
00138 int green = generateComponent (counter >> 2);
00139 int blue = generateComponent (counter);
00140 int color = (red << 16) | (green << 8) | blue;
00141
00142
00143 bool repeated = false;
00144 for (int i = 0; i < pos; ++ i)
00145 {
00146 if (colors [i] == color)
00147 {
00148 repeated = true;
00149 break;
00150 }
00151 }
00152
00153
00154 if (!repeated && (color != 0xFFFFFF) && (color != 0xFFFF7F))
00155 colors [pos ++] = color;
00156 ++ counter;
00157 }
00158
00159 return;
00160 }
00161
00162 inline ColorPalette::~ColorPalette ()
00163 {
00164 if (colors)
00165 delete [] colors;
00166 return;
00167 }
00168
00169 inline ColorPalette::ColorPalette (const ColorPalette &)
00170 {
00171 return;
00172 }
00173
00174 inline ColorPalette &ColorPalette::operator = (const ColorPalette &)
00175 {
00176 return *this;
00177 }
00178
00179 inline int ColorPalette::operator [] (int i) const
00180 {
00181 if ((i < 0) || (i >= n))
00182 return 0;
00183 else
00184 return colors [i];
00185 }
00186
00187
00188 }
00189 }
00190
00191 #endif // _CHOMP_BITMAPS_COLORPAL_H_
00192
00194