lemon/color.h
branchlemon-1.0
changeset 2655 3aa8ee4853dc
child 2656 eb1c5d1333d1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/color.h	Thu Aug 10 14:56:36 2006 +0000
     1.3 @@ -0,0 +1,211 @@
     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-2006
     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 <sys/time.h>
    1.26 +
    1.27 +#include<iostream>
    1.28 +#include<fstream>
    1.29 +#include<sstream>
    1.30 +#include<algorithm>
    1.31 +#include<vector>
    1.32 +
    1.33 +#include <ctime>
    1.34 +#include <cmath>
    1.35 +
    1.36 +#include<lemon/bits/invalid.h>
    1.37 +#include<lemon/xy.h>
    1.38 +#include<lemon/maps.h>
    1.39 +#include<lemon/bezier.h>
    1.40 +
    1.41 +
    1.42 +///\ingroup misc
    1.43 +///\file
    1.44 +///\brief Tools to manage RGB colors.
    1.45 +///
    1.46 +///\author Alpar Juttner
    1.47 +
    1.48 +namespace lemon {
    1.49 +
    1.50 +  /// \addtogroup misc
    1.51 +  /// @{
    1.52 +
    1.53 +///Data structure representing RGB colors.
    1.54 +
    1.55 +///Data structure representing RGB colors.
    1.56 +///\ingroup misc
    1.57 +class Color
    1.58 +{
    1.59 +  double _r,_g,_b;
    1.60 +public:
    1.61 +  ///Default constructor
    1.62 +  Color() {}
    1.63 +  ///Constructor
    1.64 +  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
    1.65 +  ///Set the red component
    1.66 +  double & red() {return _r;}
    1.67 +  ///Return the red component
    1.68 +  const double & red() const {return _r;}
    1.69 +  ///Set the green component
    1.70 +  double & green() {return _g;}
    1.71 +  ///Return the green component
    1.72 +  const double & green() const {return _g;}
    1.73 +  ///Set the blue component
    1.74 +  double & blue() {return _b;}
    1.75 +  ///Return the blue component
    1.76 +  const double & blue() const {return _b;}
    1.77 +  ///Set the color components
    1.78 +  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
    1.79 +};
    1.80 +
    1.81 +  /// White color constant
    1.82 +  extern const Color WHITE;  
    1.83 +  /// Black color constant
    1.84 +  extern const Color BLACK;
    1.85 +  /// Red color constant
    1.86 +  extern const Color RED;
    1.87 +  /// Green color constant
    1.88 +  extern const Color GREEN;
    1.89 +  /// Blue color constant
    1.90 +  extern const Color BLUE;
    1.91 +  /// Yellow color constant
    1.92 +  extern const Color YELLOW;
    1.93 +  /// Magenta color constant
    1.94 +  extern const Color MAGENTA;
    1.95 +  /// Cyan color constant
    1.96 +  extern const Color CYAN;
    1.97 +  /// Grey color constant
    1.98 +  extern const Color GREY;
    1.99 +  /// Dark red color constant
   1.100 +  extern const Color DARK_RED;
   1.101 +  /// Dark green color constant
   1.102 +  extern const Color DARK_GREEN;
   1.103 +  /// Drak blue color constant
   1.104 +  extern const Color DARK_BLUE;
   1.105 +  /// Dark yellow color constant
   1.106 +  extern const Color DARK_YELLOW;
   1.107 +  /// Dark magenta color constant
   1.108 +  extern const Color DARK_MAGENTA;
   1.109 +  /// Dark cyan color constant
   1.110 +  extern const Color DARK_CYAN;
   1.111 +
   1.112 +///Maps <tt>int</tt>s to different \ref Color "Color"s
   1.113 +
   1.114 +///This map assigns one of the predefined \ref Color "Color"s
   1.115 +///to each <tt>int</tt>. It is possible to change the colors as well as their
   1.116 +///number. The integer range is cyclically mapped to the provided set of colors.
   1.117 +///
   1.118 +///This is a true \ref concept::ReferenceMap "reference map", so you can also
   1.119 +///change the actual colors.
   1.120 +
   1.121 +class Palette : public MapBase<int,Color>
   1.122 +{
   1.123 +  std::vector<Color> colors;
   1.124 +public:
   1.125 +  ///Constructor
   1.126 +
   1.127 +  ///Constructor
   1.128 +  ///\param have_white indicates whether white is
   1.129 +  ///amongst the provided color (\c true) or not (\c false). If it is true,
   1.130 +  ///white will be assigned to \c 0.
   1.131 +  ///\param num the number of the allocated colors. If it is \c 0,
   1.132 +  ///the default color configuration is set up (26 color plus the white).
   1.133 +  ///If \c num is less then 26/27 then the default color list is cut. Otherwise
   1.134 +  ///the color list is filled repeatedly with the default color list.
   1.135 +  ///(The colors can be changed later on.)
   1.136 +  Palette(bool have_white=false,int num=0)
   1.137 +  {
   1.138 +    do {
   1.139 +      if(have_white) colors.push_back(Color(1,1,1));
   1.140 +
   1.141 +      colors.push_back(Color(0,0,0));
   1.142 +      colors.push_back(Color(1,0,0));
   1.143 +      colors.push_back(Color(0,1,0));
   1.144 +      colors.push_back(Color(0,0,1));
   1.145 +      colors.push_back(Color(1,1,0));
   1.146 +      colors.push_back(Color(1,0,1));
   1.147 +      colors.push_back(Color(0,1,1));
   1.148 +      
   1.149 +      colors.push_back(Color(.5,0,0));
   1.150 +      colors.push_back(Color(0,.5,0));
   1.151 +      colors.push_back(Color(0,0,.5));
   1.152 +      colors.push_back(Color(.5,.5,0));
   1.153 +      colors.push_back(Color(.5,0,.5));
   1.154 +      colors.push_back(Color(0,.5,.5));
   1.155 +      
   1.156 +      colors.push_back(Color(.5,.5,.5));
   1.157 +      colors.push_back(Color(1,.5,.5));
   1.158 +      colors.push_back(Color(.5,1,.5));
   1.159 +      colors.push_back(Color(.5,.5,1));
   1.160 +      colors.push_back(Color(1,1,.5));
   1.161 +      colors.push_back(Color(1,.5,1));
   1.162 +      colors.push_back(Color(.5,1,1));
   1.163 +      
   1.164 +      colors.push_back(Color(1,.5,0));
   1.165 +      colors.push_back(Color(.5,1,0));
   1.166 +      colors.push_back(Color(1,0,.5));
   1.167 +      colors.push_back(Color(0,1,.5));
   1.168 +      colors.push_back(Color(0,.5,1));
   1.169 +      colors.push_back(Color(.5,0,1));
   1.170 +    } while(int(colors.size())<num);
   1.171 +    //    colors.push_back(Color(1,1,1));
   1.172 +    if(num>0) colors.resize(num);
   1.173 +  }
   1.174 +  ///\e
   1.175 +  Color &operator[](int i)
   1.176 +  {
   1.177 +    return colors[i%colors.size()];
   1.178 +  }
   1.179 +  ///\e
   1.180 +  const Color &operator[](int i) const
   1.181 +  {
   1.182 +    return colors[i%colors.size()];
   1.183 +  }
   1.184 +  ///\e
   1.185 +  void set(int i,const Color &c)
   1.186 +  {
   1.187 +    colors[i%colors.size()]=c;
   1.188 +  }
   1.189 +  ///Sets the number of the exiting colors.
   1.190 +  void resize(int s) { colors.resize(s);}
   1.191 +  ///Returns the number of the existing colors.
   1.192 +  std::size_t size() const { return colors.size();}
   1.193 +};
   1.194 +
   1.195 +///Returns a visible distinct \ref Color
   1.196 +
   1.197 +///Returns a \ref Color which is as different from the given parameter
   1.198 +///as it is possible.
   1.199 +inline Color distantColor(const Color &c) 
   1.200 +{
   1.201 +  return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
   1.202 +}
   1.203 +///Returns black for light colors and white for the dark ones.
   1.204 +
   1.205 +///Returns black for light colors and white for the dark ones.
   1.206 +inline Color distantBW(const Color &c){
   1.207 +  return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
   1.208 +}
   1.209 +
   1.210 +/// @}
   1.211 +
   1.212 +} //END OF NAMESPACE LEMON
   1.213 +
   1.214 +#endif // LEMON_COLOR_H