alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@128:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@128:  *
alpar@440:  * Copyright (C) 2003-2009
alpar@128:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@128:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@128:  *
alpar@128:  * Permission to use, modify and distribute this software is granted
alpar@128:  * provided that this copyright notice appears in all copies. For
alpar@128:  * precise terms see the accompanying LICENSE file.
alpar@128:  *
alpar@128:  * This software is provided "AS IS" with no warranty of any kind,
alpar@128:  * express or implied, and with no claim as to its suitability for any
alpar@128:  * purpose.
alpar@128:  *
alpar@128:  */
alpar@128: 
alpar@128: #ifndef LEMON_COLOR_H
alpar@128: #define LEMON_COLOR_H
alpar@128: 
alpar@128: #include<vector>
alpar@128: #include<lemon/math.h>
alpar@128: #include<lemon/maps.h>
alpar@128: 
alpar@128: 
alpar@128: ///\ingroup misc
alpar@128: ///\file
alpar@128: ///\brief Tools to manage RGB colors.
alpar@128: 
alpar@128: namespace lemon {
alpar@128: 
alpar@128: 
alpar@128:   /// \addtogroup misc
alpar@128:   /// @{
alpar@128: 
alpar@128:   ///Data structure representing RGB colors.
alpar@128: 
alpar@128:   ///Data structure representing RGB colors.
alpar@128:   class Color
alpar@128:   {
alpar@128:     double _r,_g,_b;
alpar@128:   public:
alpar@128:     ///Default constructor
alpar@128:     Color() {}
alpar@128:     ///Constructor
alpar@128:     Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@128:     ///Set the red component
alpar@128:     double & red() {return _r;}
alpar@128:     ///Return the red component
alpar@128:     const double & red() const {return _r;}
alpar@128:     ///Set the green component
alpar@128:     double & green() {return _g;}
alpar@128:     ///Return the green component
alpar@128:     const double & green() const {return _g;}
alpar@128:     ///Set the blue component
alpar@128:     double & blue() {return _b;}
alpar@128:     ///Return the blue component
alpar@128:     const double & blue() const {return _b;}
alpar@128:     ///Set the color components
alpar@128:     void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@128:   };
alpar@128: 
alpar@128:   /// White color constant
alpar@209:   extern const Color WHITE;
alpar@128:   /// Black color constant
alpar@128:   extern const Color BLACK;
alpar@128:   /// Red color constant
alpar@128:   extern const Color RED;
alpar@128:   /// Green color constant
alpar@128:   extern const Color GREEN;
alpar@128:   /// Blue color constant
alpar@128:   extern const Color BLUE;
alpar@128:   /// Yellow color constant
alpar@128:   extern const Color YELLOW;
alpar@128:   /// Magenta color constant
alpar@128:   extern const Color MAGENTA;
alpar@128:   /// Cyan color constant
alpar@128:   extern const Color CYAN;
alpar@128:   /// Grey color constant
alpar@128:   extern const Color GREY;
alpar@128:   /// Dark red color constant
alpar@128:   extern const Color DARK_RED;
alpar@128:   /// Dark green color constant
alpar@128:   extern const Color DARK_GREEN;
alpar@128:   /// Drak blue color constant
alpar@128:   extern const Color DARK_BLUE;
alpar@128:   /// Dark yellow color constant
alpar@128:   extern const Color DARK_YELLOW;
alpar@128:   /// Dark magenta color constant
alpar@128:   extern const Color DARK_MAGENTA;
alpar@128:   /// Dark cyan color constant
alpar@128:   extern const Color DARK_CYAN;
alpar@128: 
kpeter@313:   ///Map <tt>int</tt>s to different <tt>Color</tt>s
alpar@128: 
alpar@128:   ///This map assigns one of the predefined \ref Color "Color"s to
alpar@128:   ///each <tt>int</tt>. It is possible to change the colors as well as
alpar@128:   ///their number. The integer range is cyclically mapped to the
alpar@128:   ///provided set of colors.
alpar@128:   ///
alpar@128:   ///This is a true \ref concepts::ReferenceMap "reference map", so
alpar@128:   ///you can also change the actual colors.
alpar@128: 
alpar@128:   class Palette : public MapBase<int,Color>
alpar@128:   {
alpar@128:     std::vector<Color> colors;
alpar@128:   public:
alpar@128:     ///Constructor
alpar@128: 
kpeter@206:     ///Constructor.
kpeter@206:     ///\param have_white Indicates whether white is among the
alpar@133:     ///provided initial colors (\c true) or not (\c false). If it is true,
alpar@133:     ///white will be assigned to \c 0.
kpeter@206:     ///\param num The number of the allocated colors. If it is \c -1,
alpar@133:     ///the default color configuration is set up (26 color plus optionaly the
alpar@128:     ///white).  If \c num is less then 26/27 then the default color
alpar@128:     ///list is cut. Otherwise the color list is filled repeatedly with
alpar@128:     ///the default color list.  (The colors can be changed later on.)
alpar@129:     Palette(bool have_white=false,int num=-1)
alpar@128:     {
alpar@128:       if (num==0) return;
alpar@128:       do {
alpar@128:         if(have_white) colors.push_back(Color(1,1,1));
alpar@128: 
alpar@128:         colors.push_back(Color(0,0,0));
alpar@128:         colors.push_back(Color(1,0,0));
alpar@128:         colors.push_back(Color(0,1,0));
alpar@128:         colors.push_back(Color(0,0,1));
alpar@128:         colors.push_back(Color(1,1,0));
alpar@128:         colors.push_back(Color(1,0,1));
alpar@128:         colors.push_back(Color(0,1,1));
alpar@209: 
alpar@128:         colors.push_back(Color(.5,0,0));
alpar@128:         colors.push_back(Color(0,.5,0));
alpar@128:         colors.push_back(Color(0,0,.5));
alpar@128:         colors.push_back(Color(.5,.5,0));
alpar@128:         colors.push_back(Color(.5,0,.5));
alpar@128:         colors.push_back(Color(0,.5,.5));
alpar@209: 
alpar@128:         colors.push_back(Color(.5,.5,.5));
alpar@128:         colors.push_back(Color(1,.5,.5));
alpar@128:         colors.push_back(Color(.5,1,.5));
alpar@128:         colors.push_back(Color(.5,.5,1));
alpar@128:         colors.push_back(Color(1,1,.5));
alpar@128:         colors.push_back(Color(1,.5,1));
alpar@128:         colors.push_back(Color(.5,1,1));
alpar@209: 
alpar@128:         colors.push_back(Color(1,.5,0));
alpar@128:         colors.push_back(Color(.5,1,0));
alpar@128:         colors.push_back(Color(1,0,.5));
alpar@128:         colors.push_back(Color(0,1,.5));
alpar@128:         colors.push_back(Color(0,.5,1));
alpar@128:         colors.push_back(Color(.5,0,1));
alpar@128:       } while(int(colors.size())<num);
alpar@128:       if(num>=0) colors.resize(num);
alpar@128:     }
alpar@128:     ///\e
alpar@128:     Color &operator[](int i)
alpar@128:     {
alpar@128:       return colors[i%colors.size()];
alpar@128:     }
alpar@128:     ///\e
alpar@128:     const Color &operator[](int i) const
alpar@128:     {
alpar@128:       return colors[i%colors.size()];
alpar@128:     }
alpar@128:     ///\e
alpar@128:     void set(int i,const Color &c)
alpar@128:     {
alpar@128:       colors[i%colors.size()]=c;
alpar@128:     }
kpeter@206:     ///Adds a new color to the end of the color list.
alpar@209:     void add(const Color &c)
alpar@128:     {
alpar@128:       colors.push_back(c);
alpar@128:     }
alpar@128: 
kpeter@206:     ///Sets the number of the existing colors.
alpar@128:     void resize(int s) { colors.resize(s);}
alpar@128:     ///Returns the number of the existing colors.
alpar@128:     int size() const { return int(colors.size());}
alpar@128:   };
alpar@128: 
alpar@133:   ///Returns a visibly distinct \ref Color
alpar@128: 
alpar@128:   ///Returns a \ref Color which is as different from the given parameter
alpar@128:   ///as it is possible.
alpar@209:   inline Color distantColor(const Color &c)
alpar@128:   {
alpar@128:     return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
alpar@128:   }
alpar@128:   ///Returns black for light colors and white for the dark ones.
alpar@128: 
alpar@128:   ///Returns black for light colors and white for the dark ones.
alpar@128:   inline Color distantBW(const Color &c){
alpar@128:     return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
alpar@128:   }
alpar@128: 
alpar@128:   /// @}
alpar@128: 
alpar@128: } //END OF NAMESPACE LEMON
alpar@128: 
alpar@128: #endif // LEMON_COLOR_H