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