lemon/color.h
changeset 2547 f393a8162688
parent 2391 14a343be7a5a
child 2553 bfced05fa852
equal deleted inserted replaced
8:554882226d12 9:8dddf9194fa7
    31 ///\author Alpar Juttner
    31 ///\author Alpar Juttner
    32 
    32 
    33 namespace lemon {
    33 namespace lemon {
    34 
    34 
    35 
    35 
    36 /// \addtogroup misc
    36   /// \addtogroup misc
    37 /// @{
    37   /// @{
    38 
    38 
    39 ///Data structure representing RGB colors.
    39   ///Data structure representing RGB colors.
    40 
    40 
    41 ///Data structure representing RGB colors.
    41   ///Data structure representing RGB colors.
    42 class Color
    42   class Color
    43 {
    43   {
    44   double _r,_g,_b;
    44     double _r,_g,_b;
    45 public:
    45   public:
    46   ///Default constructor
    46     ///Default constructor
    47   Color() {}
    47     Color() {}
    48   ///Constructor
    48     ///Constructor
    49   Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
    49     Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
    50   ///Set the red component
    50     ///Set the red component
    51   double & red() {return _r;}
    51     double & red() {return _r;}
    52   ///Return the red component
    52     ///Return the red component
    53   const double & red() const {return _r;}
    53     const double & red() const {return _r;}
    54   ///Set the green component
    54     ///Set the green component
    55   double & green() {return _g;}
    55     double & green() {return _g;}
    56   ///Return the green component
    56     ///Return the green component
    57   const double & green() const {return _g;}
    57     const double & green() const {return _g;}
    58   ///Set the blue component
    58     ///Set the blue component
    59   double & blue() {return _b;}
    59     double & blue() {return _b;}
    60   ///Return the blue component
    60     ///Return the blue component
    61   const double & blue() const {return _b;}
    61     const double & blue() const {return _b;}
    62   ///Set the color components
    62     ///Set the color components
    63   void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
    63     void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
    64 };
    64   };
    65 
    65 
    66   /// White color constant
    66   /// White color constant
    67   extern const Color WHITE;  
    67   extern const Color WHITE;  
    68   /// Black color constant
    68   /// Black color constant
    69   extern const Color BLACK;
    69   extern const Color BLACK;
    92   /// Dark magenta color constant
    92   /// Dark magenta color constant
    93   extern const Color DARK_MAGENTA;
    93   extern const Color DARK_MAGENTA;
    94   /// Dark cyan color constant
    94   /// Dark cyan color constant
    95   extern const Color DARK_CYAN;
    95   extern const Color DARK_CYAN;
    96 
    96 
    97 ///Map <tt>int</tt>s to different \ref Color "Color"s
    97   ///Map <tt>int</tt>s to different \ref Color "Color"s
    98 
    98 
    99 ///This map assigns one of the predefined \ref Color "Color"s
    99   ///This map assigns one of the predefined \ref Color "Color"s to
   100 ///to each <tt>int</tt>. It is possible to change the colors as well as their
   100   ///each <tt>int</tt>. It is possible to change the colors as well as
   101 ///number. The integer range is cyclically mapped to the provided set of colors.
   101   ///their number. The integer range is cyclically mapped to the
   102 ///
   102   ///provided set of colors.
   103 ///This is a true \ref concepts::ReferenceMap "reference map", so you can also
   103   ///
   104 ///change the actual colors.
   104   ///This is a true \ref concepts::ReferenceMap "reference map", so
   105 
   105   ///you can also change the actual colors.
   106 class Palette : public MapBase<int,Color>
   106 
   107 {
   107   class Palette : public MapBase<int,Color>
   108   std::vector<Color> colors;
       
   109 public:
       
   110   ///Constructor
       
   111 
       
   112   ///Constructor
       
   113   ///\param have_white indicates whether white is
       
   114   ///amongst the provided color (\c true) or not (\c false). If it is true,
       
   115   ///white will be assigned to \c 0.
       
   116   ///\param num the number of the allocated colors. If it is \c 0,
       
   117   ///the default color configuration is set up (26 color plus the white).
       
   118   ///If \c num is less then 26/27 then the default color list is cut. Otherwise
       
   119   ///the color list is filled repeatedly with the default color list.
       
   120   ///(The colors can be changed later on.)
       
   121   Palette(bool have_white=false,int num=0)
       
   122   {
   108   {
   123     do {
   109     std::vector<Color> colors;
   124       if(have_white) colors.push_back(Color(1,1,1));
   110   public:
   125 
   111     ///Constructor
   126       colors.push_back(Color(0,0,0));
   112 
   127       colors.push_back(Color(1,0,0));
   113     ///Constructor 
   128       colors.push_back(Color(0,1,0));
   114     ///\param num the number of the allocated colors. If it is \c -1,
   129       colors.push_back(Color(0,0,1));
   115     ///the default color configuration is set up (26 color plus the
   130       colors.push_back(Color(1,1,0));
   116     ///white).  If \c num is less then 26/27 then the default color
   131       colors.push_back(Color(1,0,1));
   117     ///list is cut. Otherwise the color list is filled repeatedly with
   132       colors.push_back(Color(0,1,1));
   118     ///the default color list.  (The colors can be changed later on.)
       
   119     ///\param have_white indicates whether white is amongst the
       
   120     ///provided color (\c true) or not (\c false). If it is true,
       
   121     ///white will be assigned to \c 0.
       
   122     Palette(int num=-1,bool have_white=false)
       
   123     {
       
   124       if (num==0) return;
       
   125       do {
       
   126         if(have_white) colors.push_back(Color(1,1,1));
       
   127 
       
   128         colors.push_back(Color(0,0,0));
       
   129         colors.push_back(Color(1,0,0));
       
   130         colors.push_back(Color(0,1,0));
       
   131         colors.push_back(Color(0,0,1));
       
   132         colors.push_back(Color(1,1,0));
       
   133         colors.push_back(Color(1,0,1));
       
   134         colors.push_back(Color(0,1,1));
   133       
   135       
   134       colors.push_back(Color(.5,0,0));
   136         colors.push_back(Color(.5,0,0));
   135       colors.push_back(Color(0,.5,0));
   137         colors.push_back(Color(0,.5,0));
   136       colors.push_back(Color(0,0,.5));
   138         colors.push_back(Color(0,0,.5));
   137       colors.push_back(Color(.5,.5,0));
   139         colors.push_back(Color(.5,.5,0));
   138       colors.push_back(Color(.5,0,.5));
   140         colors.push_back(Color(.5,0,.5));
   139       colors.push_back(Color(0,.5,.5));
   141         colors.push_back(Color(0,.5,.5));
   140       
   142       
   141       colors.push_back(Color(.5,.5,.5));
   143         colors.push_back(Color(.5,.5,.5));
   142       colors.push_back(Color(1,.5,.5));
   144         colors.push_back(Color(1,.5,.5));
   143       colors.push_back(Color(.5,1,.5));
   145         colors.push_back(Color(.5,1,.5));
   144       colors.push_back(Color(.5,.5,1));
   146         colors.push_back(Color(.5,.5,1));
   145       colors.push_back(Color(1,1,.5));
   147         colors.push_back(Color(1,1,.5));
   146       colors.push_back(Color(1,.5,1));
   148         colors.push_back(Color(1,.5,1));
   147       colors.push_back(Color(.5,1,1));
   149         colors.push_back(Color(.5,1,1));
   148       
   150       
   149       colors.push_back(Color(1,.5,0));
   151         colors.push_back(Color(1,.5,0));
   150       colors.push_back(Color(.5,1,0));
   152         colors.push_back(Color(.5,1,0));
   151       colors.push_back(Color(1,0,.5));
   153         colors.push_back(Color(1,0,.5));
   152       colors.push_back(Color(0,1,.5));
   154         colors.push_back(Color(0,1,.5));
   153       colors.push_back(Color(0,.5,1));
   155         colors.push_back(Color(0,.5,1));
   154       colors.push_back(Color(.5,0,1));
   156         colors.push_back(Color(.5,0,1));
   155     } while(int(colors.size())<num);
   157       } while(int(colors.size())<num);
   156     //    colors.push_back(Color(1,1,1));
   158       //    colors.push_back(Color(1,1,1));
   157     if(num>0) colors.resize(num);
   159       if(num>=0) colors.resize(num);
       
   160     }
       
   161     ///\e
       
   162     Color &operator[](int i)
       
   163     {
       
   164       return colors[i%colors.size()];
       
   165     }
       
   166     ///\e
       
   167     const Color &operator[](int i) const
       
   168     {
       
   169       return colors[i%colors.size()];
       
   170     }
       
   171     ///\e
       
   172     void set(int i,const Color &c)
       
   173     {
       
   174       colors[i%colors.size()]=c;
       
   175     }
       
   176     ///\e
       
   177     void add(const Color &c) 
       
   178     {
       
   179       colors.push_back(c);
       
   180     }
       
   181 
       
   182     ///Sets the number of the exiting colors.
       
   183     void resize(int s) { colors.resize(s);}
       
   184     ///Returns the number of the existing colors.
       
   185     int size() const { return int(colors.size());}
       
   186   };
       
   187 
       
   188   ///Returns a visible distinct \ref Color
       
   189 
       
   190   ///Returns a \ref Color which is as different from the given parameter
       
   191   ///as it is possible.
       
   192   inline Color distantColor(const Color &c) 
       
   193   {
       
   194     return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
   158   }
   195   }
   159   ///\e
   196   ///Returns black for light colors and white for the dark ones.
   160   Color &operator[](int i)
   197 
   161   {
   198   ///Returns black for light colors and white for the dark ones.
   162     return colors[i%colors.size()];
   199   inline Color distantBW(const Color &c){
       
   200     return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
   163   }
   201   }
   164   ///\e
   202 
   165   const Color &operator[](int i) const
   203   /// @}
   166   {
       
   167     return colors[i%colors.size()];
       
   168   }
       
   169   ///\e
       
   170   void set(int i,const Color &c)
       
   171   {
       
   172     colors[i%colors.size()]=c;
       
   173   }
       
   174   ///Sets the number of the exiting colors.
       
   175   void resize(int s) { colors.resize(s);}
       
   176   ///Returns the number of the existing colors.
       
   177   std::size_t size() const { return colors.size();}
       
   178 };
       
   179 
       
   180 ///Returns a visible distinct \ref Color
       
   181 
       
   182 ///Returns a \ref Color which is as different from the given parameter
       
   183 ///as it is possible.
       
   184 inline Color distantColor(const Color &c) 
       
   185 {
       
   186   return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
       
   187 }
       
   188 ///Returns black for light colors and white for the dark ones.
       
   189 
       
   190 ///Returns black for light colors and white for the dark ones.
       
   191 inline Color distantBW(const Color &c){
       
   192   return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
       
   193 }
       
   194 
       
   195 /// @}
       
   196 
   204 
   197 } //END OF NAMESPACE LEMON
   205 } //END OF NAMESPACE LEMON
   198 
   206 
   199 #endif // LEMON_COLOR_H
   207 #endif // LEMON_COLOR_H