lemon/color.h
author Balazs Dezso <deba@inf.elte.hu>
Mon, 14 Apr 2008 19:27:36 +0200
changeset 134 0775d2ba2afb
parent 129 e99385bca9fe
child 157 2ccc1afc2c52
permissions -rw-r--r--
MinGW compatibility for graph_to_eps.h
alpar@128
     1
/* -*- C++ -*-
alpar@128
     2
 *
alpar@128
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@128
     4
 *
alpar@128
     5
 * Copyright (C) 2003-2008
alpar@128
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@128
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@128
     8
 *
alpar@128
     9
 * Permission to use, modify and distribute this software is granted
alpar@128
    10
 * provided that this copyright notice appears in all copies. For
alpar@128
    11
 * precise terms see the accompanying LICENSE file.
alpar@128
    12
 *
alpar@128
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@128
    14
 * express or implied, and with no claim as to its suitability for any
alpar@128
    15
 * purpose.
alpar@128
    16
 *
alpar@128
    17
 */
alpar@128
    18
alpar@128
    19
#ifndef LEMON_COLOR_H
alpar@128
    20
#define LEMON_COLOR_H
alpar@128
    21
alpar@128
    22
#include<vector>
alpar@128
    23
#include<lemon/math.h>
alpar@128
    24
#include<lemon/maps.h>
alpar@128
    25
alpar@128
    26
alpar@128
    27
///\ingroup misc
alpar@128
    28
///\file
alpar@128
    29
///\brief Tools to manage RGB colors.
alpar@128
    30
///
alpar@128
    31
///\author Alpar Juttner
alpar@128
    32
alpar@128
    33
namespace lemon {
alpar@128
    34
alpar@128
    35
alpar@128
    36
  /// \addtogroup misc
alpar@128
    37
  /// @{
alpar@128
    38
alpar@128
    39
  ///Data structure representing RGB colors.
alpar@128
    40
alpar@128
    41
  ///Data structure representing RGB colors.
alpar@128
    42
  class Color
alpar@128
    43
  {
alpar@128
    44
    double _r,_g,_b;
alpar@128
    45
  public:
alpar@128
    46
    ///Default constructor
alpar@128
    47
    Color() {}
alpar@128
    48
    ///Constructor
alpar@128
    49
    Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@128
    50
    ///Set the red component
alpar@128
    51
    double & red() {return _r;}
alpar@128
    52
    ///Return the red component
alpar@128
    53
    const double & red() const {return _r;}
alpar@128
    54
    ///Set the green component
alpar@128
    55
    double & green() {return _g;}
alpar@128
    56
    ///Return the green component
alpar@128
    57
    const double & green() const {return _g;}
alpar@128
    58
    ///Set the blue component
alpar@128
    59
    double & blue() {return _b;}
alpar@128
    60
    ///Return the blue component
alpar@128
    61
    const double & blue() const {return _b;}
alpar@128
    62
    ///Set the color components
alpar@128
    63
    void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@128
    64
  };
alpar@128
    65
alpar@128
    66
  /// White color constant
alpar@128
    67
  extern const Color WHITE;  
alpar@128
    68
  /// Black color constant
alpar@128
    69
  extern const Color BLACK;
alpar@128
    70
  /// Red color constant
alpar@128
    71
  extern const Color RED;
alpar@128
    72
  /// Green color constant
alpar@128
    73
  extern const Color GREEN;
alpar@128
    74
  /// Blue color constant
alpar@128
    75
  extern const Color BLUE;
alpar@128
    76
  /// Yellow color constant
alpar@128
    77
  extern const Color YELLOW;
alpar@128
    78
  /// Magenta color constant
alpar@128
    79
  extern const Color MAGENTA;
alpar@128
    80
  /// Cyan color constant
alpar@128
    81
  extern const Color CYAN;
alpar@128
    82
  /// Grey color constant
alpar@128
    83
  extern const Color GREY;
alpar@128
    84
  /// Dark red color constant
alpar@128
    85
  extern const Color DARK_RED;
alpar@128
    86
  /// Dark green color constant
alpar@128
    87
  extern const Color DARK_GREEN;
alpar@128
    88
  /// Drak blue color constant
alpar@128
    89
  extern const Color DARK_BLUE;
alpar@128
    90
  /// Dark yellow color constant
alpar@128
    91
  extern const Color DARK_YELLOW;
alpar@128
    92
  /// Dark magenta color constant
alpar@128
    93
  extern const Color DARK_MAGENTA;
alpar@128
    94
  /// Dark cyan color constant
alpar@128
    95
  extern const Color DARK_CYAN;
alpar@128
    96
alpar@128
    97
  ///Map <tt>int</tt>s to different \ref Color "Color"s
alpar@128
    98
alpar@128
    99
  ///This map assigns one of the predefined \ref Color "Color"s to
alpar@128
   100
  ///each <tt>int</tt>. It is possible to change the colors as well as
alpar@128
   101
  ///their number. The integer range is cyclically mapped to the
alpar@128
   102
  ///provided set of colors.
alpar@128
   103
  ///
alpar@128
   104
  ///This is a true \ref concepts::ReferenceMap "reference map", so
alpar@128
   105
  ///you can also change the actual colors.
alpar@128
   106
alpar@128
   107
  class Palette : public MapBase<int,Color>
alpar@128
   108
  {
alpar@128
   109
    std::vector<Color> colors;
alpar@128
   110
  public:
alpar@128
   111
    ///Constructor
alpar@128
   112
alpar@128
   113
    ///Constructor 
alpar@133
   114
    ///\param have_white indicates whether white is amongst the
alpar@133
   115
    ///provided initial colors (\c true) or not (\c false). If it is true,
alpar@133
   116
    ///white will be assigned to \c 0.
alpar@128
   117
    ///\param num the number of the allocated colors. If it is \c -1,
alpar@133
   118
    ///the default color configuration is set up (26 color plus optionaly the
alpar@128
   119
    ///white).  If \c num is less then 26/27 then the default color
alpar@128
   120
    ///list is cut. Otherwise the color list is filled repeatedly with
alpar@128
   121
    ///the default color list.  (The colors can be changed later on.)
alpar@129
   122
    Palette(bool have_white=false,int num=-1)
alpar@128
   123
    {
alpar@128
   124
      if (num==0) return;
alpar@128
   125
      do {
alpar@128
   126
        if(have_white) colors.push_back(Color(1,1,1));
alpar@128
   127
alpar@128
   128
        colors.push_back(Color(0,0,0));
alpar@128
   129
        colors.push_back(Color(1,0,0));
alpar@128
   130
        colors.push_back(Color(0,1,0));
alpar@128
   131
        colors.push_back(Color(0,0,1));
alpar@128
   132
        colors.push_back(Color(1,1,0));
alpar@128
   133
        colors.push_back(Color(1,0,1));
alpar@128
   134
        colors.push_back(Color(0,1,1));
alpar@128
   135
      
alpar@128
   136
        colors.push_back(Color(.5,0,0));
alpar@128
   137
        colors.push_back(Color(0,.5,0));
alpar@128
   138
        colors.push_back(Color(0,0,.5));
alpar@128
   139
        colors.push_back(Color(.5,.5,0));
alpar@128
   140
        colors.push_back(Color(.5,0,.5));
alpar@128
   141
        colors.push_back(Color(0,.5,.5));
alpar@128
   142
      
alpar@128
   143
        colors.push_back(Color(.5,.5,.5));
alpar@128
   144
        colors.push_back(Color(1,.5,.5));
alpar@128
   145
        colors.push_back(Color(.5,1,.5));
alpar@128
   146
        colors.push_back(Color(.5,.5,1));
alpar@128
   147
        colors.push_back(Color(1,1,.5));
alpar@128
   148
        colors.push_back(Color(1,.5,1));
alpar@128
   149
        colors.push_back(Color(.5,1,1));
alpar@128
   150
      
alpar@128
   151
        colors.push_back(Color(1,.5,0));
alpar@128
   152
        colors.push_back(Color(.5,1,0));
alpar@128
   153
        colors.push_back(Color(1,0,.5));
alpar@128
   154
        colors.push_back(Color(0,1,.5));
alpar@128
   155
        colors.push_back(Color(0,.5,1));
alpar@128
   156
        colors.push_back(Color(.5,0,1));
alpar@128
   157
      } while(int(colors.size())<num);
alpar@128
   158
      //    colors.push_back(Color(1,1,1));
alpar@128
   159
      if(num>=0) colors.resize(num);
alpar@128
   160
    }
alpar@128
   161
    ///\e
alpar@128
   162
    Color &operator[](int i)
alpar@128
   163
    {
alpar@128
   164
      return colors[i%colors.size()];
alpar@128
   165
    }
alpar@128
   166
    ///\e
alpar@128
   167
    const Color &operator[](int i) const
alpar@128
   168
    {
alpar@128
   169
      return colors[i%colors.size()];
alpar@128
   170
    }
alpar@128
   171
    ///\e
alpar@128
   172
    void set(int i,const Color &c)
alpar@128
   173
    {
alpar@128
   174
      colors[i%colors.size()]=c;
alpar@128
   175
    }
alpar@133
   176
    ///Add a new color to the end of the color list.
alpar@128
   177
    void add(const Color &c) 
alpar@128
   178
    {
alpar@128
   179
      colors.push_back(c);
alpar@128
   180
    }
alpar@128
   181
alpar@128
   182
    ///Sets the number of the exiting colors.
alpar@128
   183
    void resize(int s) { colors.resize(s);}
alpar@128
   184
    ///Returns the number of the existing colors.
alpar@128
   185
    int size() const { return int(colors.size());}
alpar@128
   186
  };
alpar@128
   187
alpar@133
   188
  ///Returns a visibly distinct \ref Color
alpar@128
   189
alpar@128
   190
  ///Returns a \ref Color which is as different from the given parameter
alpar@128
   191
  ///as it is possible.
alpar@128
   192
  inline Color distantColor(const Color &c) 
alpar@128
   193
  {
alpar@128
   194
    return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
alpar@128
   195
  }
alpar@128
   196
  ///Returns black for light colors and white for the dark ones.
alpar@128
   197
alpar@128
   198
  ///Returns black for light colors and white for the dark ones.
alpar@128
   199
  inline Color distantBW(const Color &c){
alpar@128
   200
    return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
alpar@128
   201
  }
alpar@128
   202
alpar@128
   203
  /// @}
alpar@128
   204
alpar@128
   205
} //END OF NAMESPACE LEMON
alpar@128
   206
alpar@128
   207
#endif // LEMON_COLOR_H