lemon/color.h
author alpar
Mon, 14 Aug 2006 15:15:57 +0000
changeset 2178 0d7c0f96a5ee
parent 2172 4b25e7003868
child 2179 a3bb30be417c
permissions -rw-r--r--
- bezier.h went to lemon/bits/
- new graphToEps() option: absolute/relative node size/link width scaling.
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@2174
    47
  /// \addtogroup misc
alpar@2174
    48
  /// @{
alpar@2174
    49
alpar@1971
    50
///Data structure representing RGB colors.
alpar@1971
    51
alpar@1971
    52
///Data structure representing RGB colors.
alpar@1971
    53
///\ingroup misc
alpar@1971
    54
class Color
alpar@1971
    55
{
alpar@1971
    56
  double _r,_g,_b;
alpar@1971
    57
public:
alpar@1971
    58
  ///Default constructor
alpar@1971
    59
  Color() {}
alpar@1971
    60
  ///Constructor
alpar@1971
    61
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@2159
    62
  ///Set the red component
alpar@1971
    63
  double & red() {return _r;}
alpar@2159
    64
  ///Return the red component
alpar@1971
    65
  const double & red() const {return _r;}
alpar@2159
    66
  ///Set the green component
alpar@1971
    67
  double & green() {return _g;}
alpar@2159
    68
  ///Return the green component
alpar@1971
    69
  const double & green() const {return _g;}
alpar@2159
    70
  ///Set the blue component
alpar@1971
    71
  double & blue() {return _b;}
alpar@2159
    72
  ///Return the blue component
alpar@1971
    73
  const double & blue() const {return _b;}
alpar@1971
    74
  ///Set the color components
alpar@1971
    75
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1971
    76
};
alpar@1971
    77
alpar@2174
    78
  /// White color constant
alpar@2174
    79
  extern const Color WHITE;  
alpar@2174
    80
  /// Black color constant
alpar@2174
    81
  extern const Color BLACK;
alpar@2174
    82
  /// Red color constant
alpar@2174
    83
  extern const Color RED;
alpar@2174
    84
  /// Green color constant
alpar@2174
    85
  extern const Color GREEN;
alpar@2174
    86
  /// Blue color constant
alpar@2174
    87
  extern const Color BLUE;
alpar@2174
    88
  /// Yellow color constant
alpar@2174
    89
  extern const Color YELLOW;
alpar@2174
    90
  /// Magenta color constant
alpar@2174
    91
  extern const Color MAGENTA;
alpar@2174
    92
  /// Cyan color constant
alpar@2174
    93
  extern const Color CYAN;
alpar@2174
    94
  /// Grey color constant
alpar@2174
    95
  extern const Color GREY;
alpar@2174
    96
  /// Dark red color constant
alpar@2174
    97
  extern const Color DARK_RED;
alpar@2174
    98
  /// Dark green color constant
alpar@2174
    99
  extern const Color DARK_GREEN;
alpar@2174
   100
  /// Drak blue color constant
alpar@2174
   101
  extern const Color DARK_BLUE;
alpar@2174
   102
  /// Dark yellow color constant
alpar@2174
   103
  extern const Color DARK_YELLOW;
alpar@2174
   104
  /// Dark magenta color constant
alpar@2174
   105
  extern const Color DARK_MAGENTA;
alpar@2174
   106
  /// Dark cyan color constant
alpar@2174
   107
  extern const Color DARK_CYAN;
alpar@2174
   108
alpar@1971
   109
///Maps <tt>int</tt>s to different \ref Color "Color"s
alpar@1971
   110
alpar@1971
   111
///This map assigns one of the predefined \ref Color "Color"s
alpar@1971
   112
///to each <tt>int</tt>. It is possible to change the colors as well as their
alpar@1971
   113
///number. The integer range is cyclically mapped to the provided set of colors.
alpar@1971
   114
///
alpar@1971
   115
///This is a true \ref concept::ReferenceMap "reference map", so you can also
alpar@1971
   116
///change the actual colors.
alpar@1971
   117
alpar@2172
   118
class Palette : public MapBase<int,Color>
alpar@1971
   119
{
alpar@1971
   120
  std::vector<Color> colors;
alpar@1971
   121
public:
alpar@1971
   122
  ///Constructor
alpar@1971
   123
alpar@1971
   124
  ///Constructor
alpar@1971
   125
  ///\param have_white indicates whether white is
alpar@1971
   126
  ///amongst the provided color (\c true) or not (\c false). If it is true,
alpar@1971
   127
  ///white will be assigned to \c 0.
alpar@2174
   128
  ///\param num the number of the allocated colors. If it is \c 0,
alpar@2174
   129
  ///the default color configuration is set up (26 color plus the white).
alpar@1971
   130
  ///If \c num is less then 26/27 then the default color list is cut. Otherwise
alpar@1971
   131
  ///the color list is filled repeatedly with the default color list.
alpar@1971
   132
  ///(The colors can be changed later on.)
alpar@2172
   133
  Palette(bool have_white=false,int num=0)
alpar@1971
   134
  {
alpar@1971
   135
    do {
alpar@1971
   136
      if(have_white) colors.push_back(Color(1,1,1));
alpar@1971
   137
alpar@1971
   138
      colors.push_back(Color(0,0,0));
alpar@1971
   139
      colors.push_back(Color(1,0,0));
alpar@1971
   140
      colors.push_back(Color(0,1,0));
alpar@1971
   141
      colors.push_back(Color(0,0,1));
alpar@1971
   142
      colors.push_back(Color(1,1,0));
alpar@1971
   143
      colors.push_back(Color(1,0,1));
alpar@1971
   144
      colors.push_back(Color(0,1,1));
alpar@1971
   145
      
alpar@1971
   146
      colors.push_back(Color(.5,0,0));
alpar@1971
   147
      colors.push_back(Color(0,.5,0));
alpar@1971
   148
      colors.push_back(Color(0,0,.5));
alpar@1971
   149
      colors.push_back(Color(.5,.5,0));
alpar@1971
   150
      colors.push_back(Color(.5,0,.5));
alpar@1971
   151
      colors.push_back(Color(0,.5,.5));
alpar@1971
   152
      
alpar@1971
   153
      colors.push_back(Color(.5,.5,.5));
alpar@1971
   154
      colors.push_back(Color(1,.5,.5));
alpar@1971
   155
      colors.push_back(Color(.5,1,.5));
alpar@1971
   156
      colors.push_back(Color(.5,.5,1));
alpar@1971
   157
      colors.push_back(Color(1,1,.5));
alpar@1971
   158
      colors.push_back(Color(1,.5,1));
alpar@1971
   159
      colors.push_back(Color(.5,1,1));
alpar@1971
   160
      
alpar@1971
   161
      colors.push_back(Color(1,.5,0));
alpar@1971
   162
      colors.push_back(Color(.5,1,0));
alpar@1971
   163
      colors.push_back(Color(1,0,.5));
alpar@1971
   164
      colors.push_back(Color(0,1,.5));
alpar@1971
   165
      colors.push_back(Color(0,.5,1));
alpar@1971
   166
      colors.push_back(Color(.5,0,1));
alpar@1971
   167
    } while(int(colors.size())<num);
alpar@1971
   168
    //    colors.push_back(Color(1,1,1));
alpar@1971
   169
    if(num>0) colors.resize(num);
alpar@1971
   170
  }
alpar@1971
   171
  ///\e
alpar@1971
   172
  Color &operator[](int i)
alpar@1971
   173
  {
alpar@1971
   174
    return colors[i%colors.size()];
alpar@1971
   175
  }
alpar@1971
   176
  ///\e
alpar@1971
   177
  const Color &operator[](int i) const
alpar@1971
   178
  {
alpar@1971
   179
    return colors[i%colors.size()];
alpar@1971
   180
  }
alpar@1971
   181
  ///\e
alpar@1971
   182
  void set(int i,const Color &c)
alpar@1971
   183
  {
alpar@1971
   184
    colors[i%colors.size()]=c;
alpar@1971
   185
  }
alpar@1971
   186
  ///Sets the number of the exiting colors.
alpar@1971
   187
  void resize(int s) { colors.resize(s);}
alpar@1971
   188
  ///Returns the number of the existing colors.
alpar@1971
   189
  std::size_t size() const { return colors.size();}
alpar@1971
   190
};
alpar@1971
   191
alpar@1971
   192
///Returns a visible distinct \ref Color
alpar@1971
   193
alpar@1971
   194
///Returns a \ref Color which is as different from the given parameter
alpar@1971
   195
///as it is possible.
alpar@1971
   196
inline Color distantColor(const Color &c) 
alpar@1971
   197
{
alpar@1971
   198
  return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
alpar@1971
   199
}
alpar@1971
   200
///Returns black for light colors and white for the dark ones.
alpar@1971
   201
alpar@1971
   202
///Returns black for light colors and white for the dark ones.
alpar@1971
   203
inline Color distantBW(const Color &c){
alpar@2174
   204
  return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
alpar@1971
   205
}
alpar@1971
   206
alpar@2174
   207
/// @}
alpar@2174
   208
alpar@1971
   209
} //END OF NAMESPACE LEMON
alpar@1971
   210
alpar@1971
   211
#endif // LEMON_COLOR_H