colorpal.h

Go to the documentation of this file.
00001 
00002 
00003 
00015 
00016 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00017 //
00018 // This file is part of my research software package.  This is free software;
00019 // you can redistribute it and/or modify it under the terms of the GNU
00020 // General Public License as published by the Free Software Foundation;
00021 // either version 2 of the License, or (at your option) any later version.
00022 //
00023 // This software is distributed in the hope that it will be useful,
00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 // GNU General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License along
00029 // with this software; see the file "license.txt".  If not, write to the
00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00031 // MA 02111-1307, USA.
00032 
00033 // Started on February 14, 2007. Last revision: February 14, 2008.
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 // ----------------- Color Palette ------------------
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 }; /* ColorPalette */
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 } /* ColorPalette::generateComponent */
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                 // take a nice color if possible
00129                 if (pos < niceCount)
00130                 {
00131                         colors [pos] = niceColors [pos];
00132                         ++ pos;
00133                         continue;
00134                 }
00135 
00136                 // create a color based on the current counter
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                 // check whether this color has already appeared before
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                 // if the color is not repeated and it is not white then ok
00154                 if (!repeated && (color != 0xFFFFFF) && (color != 0xFFFF7F))
00155                         colors [pos ++] = color;
00156                 ++ counter;
00157         }
00158 
00159         return;
00160 } /* ColorPalette::ColorPalette */
00161 
00162 inline ColorPalette::~ColorPalette ()
00163 {
00164         if (colors)
00165                 delete [] colors;
00166         return;
00167 } /* ColorPalette::~ColorPalette */
00168 
00169 inline ColorPalette::ColorPalette (const ColorPalette &)
00170 {
00171         return;
00172 } /* ColorPalette::ColorPalette */
00173 
00174 inline ColorPalette &ColorPalette::operator = (const ColorPalette &)
00175 {
00176         return *this;
00177 } /* ColorPalette::operator = */
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 } /* ColorPalette::operator [] */
00186 
00187 
00188 } // namespace homology
00189 } // namespace chomp
00190 
00191 #endif // _CHOMP_BITMAPS_COLORPAL_H_
00192 
00194