lemon/color.h
changeset 1971 9a59a6cacfd9
child 1993 2115143eceea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/color.h	Mon Feb 20 06:41:12 2006 +0000
     1.3 @@ -0,0 +1,176 @@
     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/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 +///Data structure representing RGB colors.
    1.51 +
    1.52 +///Data structure representing RGB colors.
    1.53 +///\ingroup misc
    1.54 +class Color
    1.55 +{
    1.56 +  double _r,_g,_b;
    1.57 +public:
    1.58 +  ///Default constructor
    1.59 +  Color() {}
    1.60 +  ///Constructor
    1.61 +  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
    1.62 +  ///Returns the red component
    1.63 +  double & red() {return _r;}
    1.64 +  ///Returns the red component
    1.65 +  const double & red() const {return _r;}
    1.66 +  ///Returns the green component
    1.67 +  double & green() {return _g;}
    1.68 +  ///Returns the green component
    1.69 +  const double & green() const {return _g;}
    1.70 +  ///Returns the blue component
    1.71 +  double & blue() {return _b;}
    1.72 +  ///Returns the blue component
    1.73 +  const double & blue() const {return _b;}
    1.74 +  ///Set the color components
    1.75 +  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
    1.76 +};
    1.77 +
    1.78 +///Maps <tt>int</tt>s to different \ref Color "Color"s
    1.79 +
    1.80 +///This map assigns one of the predefined \ref Color "Color"s
    1.81 +///to each <tt>int</tt>. It is possible to change the colors as well as their
    1.82 +///number. The integer range is cyclically mapped to the provided set of colors.
    1.83 +///
    1.84 +///This is a true \ref concept::ReferenceMap "reference map", so you can also
    1.85 +///change the actual colors.
    1.86 +
    1.87 +class ColorSet : public MapBase<int,Color>
    1.88 +{
    1.89 +  std::vector<Color> colors;
    1.90 +public:
    1.91 +  ///Constructor
    1.92 +
    1.93 +  ///Constructor
    1.94 +  ///\param have_white indicates whether white is
    1.95 +  ///amongst the provided color (\c true) or not (\c false). If it is true,
    1.96 +  ///white will be assigned to \c 0.
    1.97 +  ///\param num the number of the allocated colors. If it is \c 0
    1.98 +  ///the default color configuration is set up (26 color plus the while).
    1.99 +  ///If \c num is less then 26/27 then the default color list is cut. Otherwise
   1.100 +  ///the color list is filled repeatedly with the default color list.
   1.101 +  ///(The colors can be changed later on.)
   1.102 +  ColorSet(bool have_white=false,int num=0)
   1.103 +  {
   1.104 +    do {
   1.105 +      if(have_white) colors.push_back(Color(1,1,1));
   1.106 +
   1.107 +      colors.push_back(Color(0,0,0));
   1.108 +      colors.push_back(Color(1,0,0));
   1.109 +      colors.push_back(Color(0,1,0));
   1.110 +      colors.push_back(Color(0,0,1));
   1.111 +      colors.push_back(Color(1,1,0));
   1.112 +      colors.push_back(Color(1,0,1));
   1.113 +      colors.push_back(Color(0,1,1));
   1.114 +      
   1.115 +      colors.push_back(Color(.5,0,0));
   1.116 +      colors.push_back(Color(0,.5,0));
   1.117 +      colors.push_back(Color(0,0,.5));
   1.118 +      colors.push_back(Color(.5,.5,0));
   1.119 +      colors.push_back(Color(.5,0,.5));
   1.120 +      colors.push_back(Color(0,.5,.5));
   1.121 +      
   1.122 +      colors.push_back(Color(.5,.5,.5));
   1.123 +      colors.push_back(Color(1,.5,.5));
   1.124 +      colors.push_back(Color(.5,1,.5));
   1.125 +      colors.push_back(Color(.5,.5,1));
   1.126 +      colors.push_back(Color(1,1,.5));
   1.127 +      colors.push_back(Color(1,.5,1));
   1.128 +      colors.push_back(Color(.5,1,1));
   1.129 +      
   1.130 +      colors.push_back(Color(1,.5,0));
   1.131 +      colors.push_back(Color(.5,1,0));
   1.132 +      colors.push_back(Color(1,0,.5));
   1.133 +      colors.push_back(Color(0,1,.5));
   1.134 +      colors.push_back(Color(0,.5,1));
   1.135 +      colors.push_back(Color(.5,0,1));
   1.136 +    } while(int(colors.size())<num);
   1.137 +    //    colors.push_back(Color(1,1,1));
   1.138 +    if(num>0) colors.resize(num);
   1.139 +  }
   1.140 +  ///\e
   1.141 +  Color &operator[](int i)
   1.142 +  {
   1.143 +    return colors[i%colors.size()];
   1.144 +  }
   1.145 +  ///\e
   1.146 +  const Color &operator[](int i) const
   1.147 +  {
   1.148 +    return colors[i%colors.size()];
   1.149 +  }
   1.150 +  ///\e
   1.151 +  void set(int i,const Color &c)
   1.152 +  {
   1.153 +    colors[i%colors.size()]=c;
   1.154 +  }
   1.155 +  ///Sets the number of the exiting colors.
   1.156 +  void resize(int s) { colors.resize(s);}
   1.157 +  ///Returns the number of the existing colors.
   1.158 +  std::size_t size() const { return colors.size();}
   1.159 +};
   1.160 +
   1.161 +///Returns a visible distinct \ref Color
   1.162 +
   1.163 +///Returns a \ref Color which is as different from the given parameter
   1.164 +///as it is possible.
   1.165 +inline Color distantColor(const Color &c) 
   1.166 +{
   1.167 +  return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
   1.168 +}
   1.169 +///Returns black for light colors and white for the dark ones.
   1.170 +
   1.171 +///Returns black for light colors and white for the dark ones.
   1.172 +inline Color distantBW(const Color &c){
   1.173 +  double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
   1.174 +  return Color(v,v,v);
   1.175 +}
   1.176 +
   1.177 +} //END OF NAMESPACE LEMON
   1.178 +
   1.179 +#endif // LEMON_COLOR_H