colorpal.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// @file colorpal.h
00004 ///
00005 /// Color palette for plotting Morse sets.
00006 /// This file contains the definition of a simple class "ColorPalette"
00007 /// which provides a specified number of distinct colors, for example,
00008 /// for the purpose of plotting cubical sets with different colors.
00009 ///
00010 /// @author Pawel Pilarczyk
00011 ///
00012 /////////////////////////////////////////////////////////////////////////////
00013 
00014 // Copyright (C) 1997-2008 by Pawel Pilarczyk.
00015 //
00016 // This file is part of my research software package.  This is free software;
00017 // you can redistribute it and/or modify it under the terms of the GNU
00018 // General Public License as published by the Free Software Foundation;
00019 // either version 2 of the License, or (at your option) any later version.
00020 //
00021 // This software is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 // GNU General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License along
00027 // with this software; see the file "license.txt".  If not, write to the
00028 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00029 // MA 02111-1307, USA.
00030 
00031 // Started on February 14, 2008. Last revision: February 14, 2008.
00032 
00033 
00034 #ifndef _PWP_COLORPAL_H_
00035 #define _PWP_COLORPAL_H_
00036 
00037 
00038 // --------------------------------------------------
00039 // ----------------- Color Palette ------------------
00040 // --------------------------------------------------
00041 
00042 /// Provides a palette of distinct RGB colors. The first color is black.
00043 /// White is considered a background color and thus is not provided.
00044 class ColorPalette
00045 {
00046 public:
00047         /// The constructor of a palette of a prescribed size.
00048         ColorPalette (int _n, bool grays = false);
00049 
00050         /// The destructor.
00051         ~ColorPalette ();
00052 
00053         /// Returns the color with the given number.
00054         /// The color is encoded as 0xRRGGBB.
00055         int operator [] (int i) const;
00056 
00057 private:
00058         /// The copy constructor is not allowed.
00059         ColorPalette (const ColorPalette &src);
00060 
00061         /// The assignment operator is not allowed.
00062         ColorPalette &operator = (const ColorPalette &src);
00063 
00064         /// Generates a color component based on the bit mask.
00065         /// The nonzero bits are every 3 locations in the mask.
00066         static int generateComponent (int bitMask);
00067 
00068         /// The number of colors in the palette.
00069         int n;
00070 
00071         /// The RBG colors in the palette.
00072         int *colors;
00073 
00074 }; /* ColorPalette */
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 } /* ColorPalette::generateComponent */
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                 // take a nice color if possible
00122                 if (pos < niceCount)
00123                 {
00124                         colors [pos] = niceColors [pos];
00125                         ++ pos;
00126                         continue;
00127                 }
00128 
00129                 // create a color based on the current counter
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                 // check whether this color has already appeared before
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                 // if the color is not repeated and it is not white then ok
00147                 if (!repeated && (color != 0xFFFFFF) && (color != 0xFFFF7F))
00148                         colors [pos ++] = color;
00149                 ++ counter;
00150         }
00151 
00152         return;
00153 } /* ColorPalette::ColorPalette */
00154 
00155 inline ColorPalette::~ColorPalette ()
00156 {
00157         if (colors)
00158                 delete [] colors;
00159         return;
00160 } /* ColorPalette::~ColorPalette */
00161 
00162 inline ColorPalette::ColorPalette (const ColorPalette &)
00163 {
00164         return;
00165 } /* ColorPalette::ColorPalette */
00166 
00167 inline ColorPalette &ColorPalette::operator = (const ColorPalette &)
00168 {
00169         return *this;
00170 } /* ColorPalette::operator = */
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 } /* ColorPalette::operator [] */
00179 
00180 
00181 #endif // _PWP_COLORPAL_H_
00182 

Generated on Sun Mar 28 17:47:57 2010 for The Conley-Morse Graphs Software by  doxygen 1.5.3