lemon/color.h
changeset 1977 8ef02f0c4245
child 1993 2115143eceea
equal deleted inserted replaced
-1:000000000000 0:08149517c9aa
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2006
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 #ifndef LEMON_COLOR_H
       
    20 #define LEMON_COLOR_H
       
    21 
       
    22 #include <sys/time.h>
       
    23 
       
    24 #include<iostream>
       
    25 #include<fstream>
       
    26 #include<sstream>
       
    27 #include<algorithm>
       
    28 #include<vector>
       
    29 
       
    30 #include <ctime>
       
    31 #include <cmath>
       
    32 
       
    33 #include<lemon/invalid.h>
       
    34 #include<lemon/xy.h>
       
    35 #include<lemon/maps.h>
       
    36 #include<lemon/bezier.h>
       
    37 
       
    38 
       
    39 ///\ingroup misc
       
    40 ///\file
       
    41 ///\brief Tools to manage RGB colors.
       
    42 ///
       
    43 ///\author Alpar Juttner
       
    44 
       
    45 namespace lemon {
       
    46 
       
    47 ///Data structure representing RGB colors.
       
    48 
       
    49 ///Data structure representing RGB colors.
       
    50 ///\ingroup misc
       
    51 class Color
       
    52 {
       
    53   double _r,_g,_b;
       
    54 public:
       
    55   ///Default constructor
       
    56   Color() {}
       
    57   ///Constructor
       
    58   Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
       
    59   ///Returns the red component
       
    60   double & red() {return _r;}
       
    61   ///Returns the red component
       
    62   const double & red() const {return _r;}
       
    63   ///Returns the green component
       
    64   double & green() {return _g;}
       
    65   ///Returns the green component
       
    66   const double & green() const {return _g;}
       
    67   ///Returns the blue component
       
    68   double & blue() {return _b;}
       
    69   ///Returns the blue component
       
    70   const double & blue() const {return _b;}
       
    71   ///Set the color components
       
    72   void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
       
    73 };
       
    74 
       
    75 ///Maps <tt>int</tt>s to different \ref Color "Color"s
       
    76 
       
    77 ///This map assigns one of the predefined \ref Color "Color"s
       
    78 ///to each <tt>int</tt>. It is possible to change the colors as well as their
       
    79 ///number. The integer range is cyclically mapped to the provided set of colors.
       
    80 ///
       
    81 ///This is a true \ref concept::ReferenceMap "reference map", so you can also
       
    82 ///change the actual colors.
       
    83 
       
    84 class ColorSet : public MapBase<int,Color>
       
    85 {
       
    86   std::vector<Color> colors;
       
    87 public:
       
    88   ///Constructor
       
    89 
       
    90   ///Constructor
       
    91   ///\param have_white indicates whether white is
       
    92   ///amongst the provided color (\c true) or not (\c false). If it is true,
       
    93   ///white will be assigned to \c 0.
       
    94   ///\param num the number of the allocated colors. If it is \c 0
       
    95   ///the default color configuration is set up (26 color plus the while).
       
    96   ///If \c num is less then 26/27 then the default color list is cut. Otherwise
       
    97   ///the color list is filled repeatedly with the default color list.
       
    98   ///(The colors can be changed later on.)
       
    99   ColorSet(bool have_white=false,int num=0)
       
   100   {
       
   101     do {
       
   102       if(have_white) colors.push_back(Color(1,1,1));
       
   103 
       
   104       colors.push_back(Color(0,0,0));
       
   105       colors.push_back(Color(1,0,0));
       
   106       colors.push_back(Color(0,1,0));
       
   107       colors.push_back(Color(0,0,1));
       
   108       colors.push_back(Color(1,1,0));
       
   109       colors.push_back(Color(1,0,1));
       
   110       colors.push_back(Color(0,1,1));
       
   111       
       
   112       colors.push_back(Color(.5,0,0));
       
   113       colors.push_back(Color(0,.5,0));
       
   114       colors.push_back(Color(0,0,.5));
       
   115       colors.push_back(Color(.5,.5,0));
       
   116       colors.push_back(Color(.5,0,.5));
       
   117       colors.push_back(Color(0,.5,.5));
       
   118       
       
   119       colors.push_back(Color(.5,.5,.5));
       
   120       colors.push_back(Color(1,.5,.5));
       
   121       colors.push_back(Color(.5,1,.5));
       
   122       colors.push_back(Color(.5,.5,1));
       
   123       colors.push_back(Color(1,1,.5));
       
   124       colors.push_back(Color(1,.5,1));
       
   125       colors.push_back(Color(.5,1,1));
       
   126       
       
   127       colors.push_back(Color(1,.5,0));
       
   128       colors.push_back(Color(.5,1,0));
       
   129       colors.push_back(Color(1,0,.5));
       
   130       colors.push_back(Color(0,1,.5));
       
   131       colors.push_back(Color(0,.5,1));
       
   132       colors.push_back(Color(.5,0,1));
       
   133     } while(int(colors.size())<num);
       
   134     //    colors.push_back(Color(1,1,1));
       
   135     if(num>0) colors.resize(num);
       
   136   }
       
   137   ///\e
       
   138   Color &operator[](int i)
       
   139   {
       
   140     return colors[i%colors.size()];
       
   141   }
       
   142   ///\e
       
   143   const Color &operator[](int i) const
       
   144   {
       
   145     return colors[i%colors.size()];
       
   146   }
       
   147   ///\e
       
   148   void set(int i,const Color &c)
       
   149   {
       
   150     colors[i%colors.size()]=c;
       
   151   }
       
   152   ///Sets the number of the exiting colors.
       
   153   void resize(int s) { colors.resize(s);}
       
   154   ///Returns the number of the existing colors.
       
   155   std::size_t size() const { return colors.size();}
       
   156 };
       
   157 
       
   158 ///Returns a visible distinct \ref Color
       
   159 
       
   160 ///Returns a \ref Color which is as different from the given parameter
       
   161 ///as it is possible.
       
   162 inline Color distantColor(const Color &c) 
       
   163 {
       
   164   return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
       
   165 }
       
   166 ///Returns black for light colors and white for the dark ones.
       
   167 
       
   168 ///Returns black for light colors and white for the dark ones.
       
   169 inline Color distantBW(const Color &c){
       
   170   double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
       
   171   return Color(v,v,v);
       
   172 }
       
   173 
       
   174 } //END OF NAMESPACE LEMON
       
   175 
       
   176 #endif // LEMON_COLOR_H