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