lemon/color.h
changeset 128 7cd965d2257f
child 129 e99385bca9fe
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/color.h	Thu Apr 03 11:10:49 2008 +0100
     1.3 @@ -0,0 +1,207 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_COLOR_H
    1.23 +#define LEMON_COLOR_H
    1.24 +
    1.25 +#include<vector>
    1.26 +#include<lemon/math.h>
    1.27 +#include<lemon/maps.h>
    1.28 +
    1.29 +
    1.30 +///\ingroup misc
    1.31 +///\file
    1.32 +///\brief Tools to manage RGB colors.
    1.33 +///
    1.34 +///\author Alpar Juttner
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +
    1.39 +  /// \addtogroup misc
    1.40 +  /// @{
    1.41 +
    1.42 +  ///Data structure representing RGB colors.
    1.43 +
    1.44 +  ///Data structure representing RGB colors.
    1.45 +  class Color
    1.46 +  {
    1.47 +    double _r,_g,_b;
    1.48 +  public:
    1.49 +    ///Default constructor
    1.50 +    Color() {}
    1.51 +    ///Constructor
    1.52 +    Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
    1.53 +    ///Set the red component
    1.54 +    double & red() {return _r;}
    1.55 +    ///Return the red component
    1.56 +    const double & red() const {return _r;}
    1.57 +    ///Set the green component
    1.58 +    double & green() {return _g;}
    1.59 +    ///Return the green component
    1.60 +    const double & green() const {return _g;}
    1.61 +    ///Set the blue component
    1.62 +    double & blue() {return _b;}
    1.63 +    ///Return the blue component
    1.64 +    const double & blue() const {return _b;}
    1.65 +    ///Set the color components
    1.66 +    void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
    1.67 +  };
    1.68 +
    1.69 +  /// White color constant
    1.70 +  extern const Color WHITE;  
    1.71 +  /// Black color constant
    1.72 +  extern const Color BLACK;
    1.73 +  /// Red color constant
    1.74 +  extern const Color RED;
    1.75 +  /// Green color constant
    1.76 +  extern const Color GREEN;
    1.77 +  /// Blue color constant
    1.78 +  extern const Color BLUE;
    1.79 +  /// Yellow color constant
    1.80 +  extern const Color YELLOW;
    1.81 +  /// Magenta color constant
    1.82 +  extern const Color MAGENTA;
    1.83 +  /// Cyan color constant
    1.84 +  extern const Color CYAN;
    1.85 +  /// Grey color constant
    1.86 +  extern const Color GREY;
    1.87 +  /// Dark red color constant
    1.88 +  extern const Color DARK_RED;
    1.89 +  /// Dark green color constant
    1.90 +  extern const Color DARK_GREEN;
    1.91 +  /// Drak blue color constant
    1.92 +  extern const Color DARK_BLUE;
    1.93 +  /// Dark yellow color constant
    1.94 +  extern const Color DARK_YELLOW;
    1.95 +  /// Dark magenta color constant
    1.96 +  extern const Color DARK_MAGENTA;
    1.97 +  /// Dark cyan color constant
    1.98 +  extern const Color DARK_CYAN;
    1.99 +
   1.100 +  ///Map <tt>int</tt>s to different \ref Color "Color"s
   1.101 +
   1.102 +  ///This map assigns one of the predefined \ref Color "Color"s to
   1.103 +  ///each <tt>int</tt>. It is possible to change the colors as well as
   1.104 +  ///their number. The integer range is cyclically mapped to the
   1.105 +  ///provided set of colors.
   1.106 +  ///
   1.107 +  ///This is a true \ref concepts::ReferenceMap "reference map", so
   1.108 +  ///you can also change the actual colors.
   1.109 +
   1.110 +  class Palette : public MapBase<int,Color>
   1.111 +  {
   1.112 +    std::vector<Color> colors;
   1.113 +  public:
   1.114 +    ///Constructor
   1.115 +
   1.116 +    ///Constructor 
   1.117 +    ///\param num the number of the allocated colors. If it is \c -1,
   1.118 +    ///the default color configuration is set up (26 color plus the
   1.119 +    ///white).  If \c num is less then 26/27 then the default color
   1.120 +    ///list is cut. Otherwise the color list is filled repeatedly with
   1.121 +    ///the default color list.  (The colors can be changed later on.)
   1.122 +    ///\param have_white indicates whether white is amongst the
   1.123 +    ///provided color (\c true) or not (\c false). If it is true,
   1.124 +    ///white will be assigned to \c 0.
   1.125 +    Palette(int num=-1,bool have_white=false)
   1.126 +    {
   1.127 +      if (num==0) return;
   1.128 +      do {
   1.129 +        if(have_white) colors.push_back(Color(1,1,1));
   1.130 +
   1.131 +        colors.push_back(Color(0,0,0));
   1.132 +        colors.push_back(Color(1,0,0));
   1.133 +        colors.push_back(Color(0,1,0));
   1.134 +        colors.push_back(Color(0,0,1));
   1.135 +        colors.push_back(Color(1,1,0));
   1.136 +        colors.push_back(Color(1,0,1));
   1.137 +        colors.push_back(Color(0,1,1));
   1.138 +      
   1.139 +        colors.push_back(Color(.5,0,0));
   1.140 +        colors.push_back(Color(0,.5,0));
   1.141 +        colors.push_back(Color(0,0,.5));
   1.142 +        colors.push_back(Color(.5,.5,0));
   1.143 +        colors.push_back(Color(.5,0,.5));
   1.144 +        colors.push_back(Color(0,.5,.5));
   1.145 +      
   1.146 +        colors.push_back(Color(.5,.5,.5));
   1.147 +        colors.push_back(Color(1,.5,.5));
   1.148 +        colors.push_back(Color(.5,1,.5));
   1.149 +        colors.push_back(Color(.5,.5,1));
   1.150 +        colors.push_back(Color(1,1,.5));
   1.151 +        colors.push_back(Color(1,.5,1));
   1.152 +        colors.push_back(Color(.5,1,1));
   1.153 +      
   1.154 +        colors.push_back(Color(1,.5,0));
   1.155 +        colors.push_back(Color(.5,1,0));
   1.156 +        colors.push_back(Color(1,0,.5));
   1.157 +        colors.push_back(Color(0,1,.5));
   1.158 +        colors.push_back(Color(0,.5,1));
   1.159 +        colors.push_back(Color(.5,0,1));
   1.160 +      } while(int(colors.size())<num);
   1.161 +      //    colors.push_back(Color(1,1,1));
   1.162 +      if(num>=0) colors.resize(num);
   1.163 +    }
   1.164 +    ///\e
   1.165 +    Color &operator[](int i)
   1.166 +    {
   1.167 +      return colors[i%colors.size()];
   1.168 +    }
   1.169 +    ///\e
   1.170 +    const Color &operator[](int i) const
   1.171 +    {
   1.172 +      return colors[i%colors.size()];
   1.173 +    }
   1.174 +    ///\e
   1.175 +    void set(int i,const Color &c)
   1.176 +    {
   1.177 +      colors[i%colors.size()]=c;
   1.178 +    }
   1.179 +    ///\e
   1.180 +    void add(const Color &c) 
   1.181 +    {
   1.182 +      colors.push_back(c);
   1.183 +    }
   1.184 +
   1.185 +    ///Sets the number of the exiting colors.
   1.186 +    void resize(int s) { colors.resize(s);}
   1.187 +    ///Returns the number of the existing colors.
   1.188 +    int size() const { return int(colors.size());}
   1.189 +  };
   1.190 +
   1.191 +  ///Returns a visible distinct \ref Color
   1.192 +
   1.193 +  ///Returns a \ref Color which is as different from the given parameter
   1.194 +  ///as it is possible.
   1.195 +  inline Color distantColor(const Color &c) 
   1.196 +  {
   1.197 +    return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
   1.198 +  }
   1.199 +  ///Returns black for light colors and white for the dark ones.
   1.200 +
   1.201 +  ///Returns black for light colors and white for the dark ones.
   1.202 +  inline Color distantBW(const Color &c){
   1.203 +    return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
   1.204 +  }
   1.205 +
   1.206 +  /// @}
   1.207 +
   1.208 +} //END OF NAMESPACE LEMON
   1.209 +
   1.210 +#endif // LEMON_COLOR_H