Color.cxx

Go to the documentation of this file.
00001 
00012 #include "Color.h"
00013 
00014 using std::string;
00015 
00016 using namespace hippodraw;
00017 
00018 // Used to generate a new color for composite plotter.
00019 int Color::colorIndex=0;
00020 
00021 Color::ColorMapByName_t Color::s_color_by_name;
00022 std::vector < std::string > Color::s_color_names;
00023 
00024 Color::Color ( int red, int green, int blue )
00025   : m_red( red ), m_green( green ), m_blue( blue )
00026 {
00027 }
00028 
00029 Color::
00030 Color ( Color::Value value )
00031 {
00032   setColor ( value );
00033 }
00034 
00035 void
00036 Color::
00037 addColor ( const char * name, const Color & color )
00038 {
00039   string s ( name );
00040   s_color_by_name [ s ] = color;
00041 }
00042 
00043 void
00044 Color::
00045 setColor ( Color::Value value )
00046 {
00047   switch ( value ) {
00048   case Color::red :
00049     setColor ( "red" );
00050     break;
00051   case Color::green :
00052     setColor ( "green" );
00053     break;
00054   case Color::blue :
00055     setColor ( "blue" );
00056     break;
00057   case Color::cyan :
00058     setColor ( "cyan" );
00059     break;
00060   case Color::magenta :
00061     setColor ( "magenta" );
00062     break;
00063   case Color::yellow :
00064     setColor ( "yellow" );
00065     break;
00066   case Color::orange :
00067     setColor ( "orange" );
00068     break;
00069   case Color::black :
00070     setColor ( "black" );
00071     break;
00072   case Color::darkgray :
00073     setColor ( "darkgray" );
00074     break;
00075   case Color::lightgray :
00076     setColor ( "lightgray" );
00077     break;
00078   case Color::white :
00079     setColor ( "white" );
00080     break;
00081   default :
00082     setColor ( "black" );
00083     break;
00084   }
00085 }
00086 
00087 void
00088 Color::
00089 initColorByNameMap ()
00090 {
00091   addColor ( "red",       Color ( 255,   0,   0 ) );
00092   addColor ( "green",     Color (   0, 255,   0 ) );
00093   addColor ( "blue",      Color (   0,   0, 255 ) );
00094   addColor ( "cyan",      Color (   0, 255, 255 ) );
00095   addColor ( "magenta",   Color ( 255,   0, 255 ) );
00096   addColor ( "yellow",    Color ( 255, 255,   0 ) );
00097   addColor ( "orange",    Color ( 255, 165,   0 ) );
00098   addColor ( "black",     Color (   0,   0,   0 ) );
00099   addColor ( "darkgray",  Color ( 152, 152, 152 ) );
00100   addColor ( "lightgray", Color ( 211, 211, 211 ) );
00101   addColor ( "white",     Color ( 255, 255, 255 ) );
00102 }
00103 
00104 Color::
00105 Color ( const std::string & name )
00106 {
00107   setColor ( name );
00108 }
00109 
00110 void
00111 Color::
00112 setColor ( const std::string & name )
00113 {
00114   bool yes = isValid ( name );
00115   if ( yes ) {
00116     ColorMapByName_t :: const_iterator first = s_color_by_name.find ( name );
00117     const Color color = first -> second;
00118     m_red = color.m_red;
00119     m_green = color.m_green;
00120     m_blue = color.m_blue;
00121   }
00122   else { // black
00123     m_red = 0;
00124     m_green = 0;
00125     m_blue = 0; 
00126   }
00127 }
00128 
00129 bool
00130 Color::
00131 isValid ( const std::string & name )
00132 {
00133   if ( s_color_by_name.empty () == true ) {
00134     initColorByNameMap ();
00135   }
00136 
00137   ColorMapByName_t::const_iterator first = s_color_by_name.find ( name );
00138 
00139   return first != s_color_by_name.end ();
00140 }
00141 
00142 const std::vector < std::string > &
00143 Color::
00144 colorNames ()
00145 {
00146   // Make sure the list is up to date
00147   s_color_names.clear ();
00148   ColorMapByName_t::const_iterator it = s_color_by_name.begin();
00149   while ( it != s_color_by_name.end () ) {
00150     const string & name = (it++) -> first;
00151     s_color_names.push_back ( name );
00152   }
00153 
00154   return s_color_names;
00155 }
00156 
00157 void Color::setColor ( int red, int green, int blue )
00158 {
00159   m_red = red;
00160   m_green = green;
00161   m_blue = blue;
00162 }
00163 
00164 int Color::getRed () const
00165 {
00166   return m_red;
00167 }
00168 
00169 int Color::getGreen () const
00170 {
00171   return m_green;
00172 }
00173 
00174 int Color::getBlue () const
00175 {
00176   return m_blue;
00177 }
00178 
00179 // Get a color from red, green, blue, magenta and orange.
00180 // Use to plot multiple curves in a plotter.
00181 Color::Value Color::getColor()
00182 {
00183   Color::Value retColor;
00184 
00185   switch (colorIndex)
00186     {
00187     case 0: retColor= red; break;
00188     case 1: retColor= green; break;
00189     case 2: retColor= blue; break;
00190     case 3: retColor= magenta; break;
00191     default: retColor= orange;
00192     }
00193 
00194   if (colorIndex==4) colorIndex=colorIndex-4;
00195   else colorIndex++;
00196   
00197   return retColor;
00198 }
00199 
00200 
00201 bool
00202 Color::operator==(Color c)
00203 {
00204   return ((m_red == c.getRed()) && 
00205           (m_green == c.getGreen()) && 
00206           (m_blue == c.getBlue()));
00207 }
00208 

Generated for HippoDraw Class Library by doxygen