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