Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes

chomp::homology::ColorPalette Class Reference

Provides a palette of distinct RGB colors. More...

#include <colorpal.h>

List of all members.

Public Member Functions

 ColorPalette (int _n, bool grays=false)
 The constructor of a palette of a prescribed size.
 ~ColorPalette ()
 The destructor.
int operator[] (int i) const
 Returns the color with the given number.

Private Member Functions

 ColorPalette (const ColorPalette &src)
 The copy constructor is not allowed.
ColorPaletteoperator= (const ColorPalette &src)
 The assignment operator is not allowed.

Static Private Member Functions

static int generateComponent (int bitMask)
 Generates a color component based on the bit mask.

Private Attributes

int n
 The number of colors in the palette.
int * colors
 The RBG colors in the palette.

Detailed Description

Provides a palette of distinct RGB colors.

The first color is black. White is considered a background color and thus is not provided.

Definition at line 51 of file colorpal.h.


Constructor & Destructor Documentation

chomp::homology::ColorPalette::ColorPalette ( int  _n,
bool  grays = false 
) [inline]

The constructor of a palette of a prescribed size.

Definition at line 99 of file colorpal.h.

References colors, generateComponent(), and n.

                                                    :
        n (_n), colors (0)
{
        if (n <= 0)
                return;
        colors = new int [n];

        if (grays)
        {
                for (int i = 0; i < n; ++ i)
                {
                        int shade = i * 255 / n;
                        colors [i] = shade | (shade << 8) | (shade << 16);
                }
                return;
        }

        const int niceCount = 14;
        int niceColors [niceCount] = {
                0x000000, 0x0000FF, 0xFF0000, 0x00FF00,
                0x00FFFF, 0xFF00FF, 0x7F007F, 0xFF7F00,
                0x007F00, 0x7F7F7F, 0xAFAFAF, 0x00007F,
                0x7F00FF, 0xFFFF00,
        };

        int counter = 1;
        int pos = 0;
        while (pos < n)
        {
                // take a nice color if possible
                if (pos < niceCount)
                {
                        colors [pos] = niceColors [pos];
                        ++ pos;
                        continue;
                }

                // create a color based on the current counter
                int red = generateComponent (counter >> 1);
                int green = generateComponent (counter >> 2);
                int blue = generateComponent (counter);
                int color = (red << 16) | (green << 8) | blue;

                // check whether this color has already appeared before
                bool repeated = false;
                for (int i = 0; i < pos; ++ i)
                {
                        if (colors [i] == color)
                        {
                                repeated = true;
                                break;
                        }
                }

                // if the color is not repeated and it is not white then ok
                if (!repeated && (color != 0xFFFFFF) && (color != 0xFFFF7F))
                        colors [pos ++] = color;
                ++ counter;
        }

        return;
} /* ColorPalette::ColorPalette */

chomp::homology::ColorPalette::~ColorPalette (  )  [inline]

The destructor.

Definition at line 162 of file colorpal.h.

References colors.

{
        if (colors)
                delete [] colors;
        return;
} /* ColorPalette::~ColorPalette */

chomp::homology::ColorPalette::ColorPalette ( const ColorPalette src  )  [inline, private]

The copy constructor is not allowed.

Definition at line 169 of file colorpal.h.

{
        return;
} /* ColorPalette::ColorPalette */


Member Function Documentation

int chomp::homology::ColorPalette::generateComponent ( int  bitMask  )  [inline, static, private]

Generates a color component based on the bit mask.

The nonzero bits are every 3 locations in the mask.

Definition at line 85 of file colorpal.h.

Referenced by ColorPalette().

{
        int mask = 0x100;
        int result = 1;
        while (mask && bitMask)
        {
                if (bitMask & 1)
                        result = mask;
                mask >>= 1;
                bitMask >>= 3;
        }
        return (result - 1) & 0xFF;
} /* ColorPalette::generateComponent */

ColorPalette & chomp::homology::ColorPalette::operator= ( const ColorPalette src  )  [inline, private]

The assignment operator is not allowed.

Definition at line 174 of file colorpal.h.

{
        return *this;
} /* ColorPalette::operator = */

int chomp::homology::ColorPalette::operator[] ( int  i  )  const [inline]

Returns the color with the given number.

The color is encoded as 0xRRGGBB.

Definition at line 179 of file colorpal.h.

References colors, and n.

{
        if ((i < 0) || (i >= n))
                return 0;
        else
                return colors [i];
} /* ColorPalette::operator [] */


Member Data Documentation

The RBG colors in the palette.

Definition at line 79 of file colorpal.h.

Referenced by ColorPalette(), operator[](), and ~ColorPalette().

The number of colors in the palette.

Definition at line 76 of file colorpal.h.

Referenced by ColorPalette(), and operator[]().


The documentation for this class was generated from the following file: