lemon/color.h
author deba
Fri, 03 Nov 2006 15:21:52 +0000
changeset 2292 38d985e82205
parent 2230 67af33b34394
child 2391 14a343be7a5a
permissions -rw-r--r--
General mapping based variant type
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<vector>
alpar@2179
    23
#include<cmath>
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@2230
    36
/// \addtogroup misc
deba@2230
    37
/// @{
alpar@2174
    38
alpar@1971
    39
///Data structure representing RGB colors.
alpar@1971
    40
alpar@1971
    41
///Data structure representing RGB colors.
alpar@1971
    42
class Color
alpar@1971
    43
{
alpar@1971
    44
  double _r,_g,_b;
alpar@1971
    45
public:
alpar@1971
    46
  ///Default constructor
alpar@1971
    47
  Color() {}
alpar@1971
    48
  ///Constructor
alpar@1971
    49
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@2159
    50
  ///Set the red component
alpar@1971
    51
  double & red() {return _r;}
alpar@2159
    52
  ///Return the red component
alpar@1971
    53
  const double & red() const {return _r;}
alpar@2159
    54
  ///Set the green component
alpar@1971
    55
  double & green() {return _g;}
alpar@2159
    56
  ///Return the green component
alpar@1971
    57
  const double & green() const {return _g;}
alpar@2159
    58
  ///Set the blue component
alpar@1971
    59
  double & blue() {return _b;}
alpar@2159
    60
  ///Return the blue component
alpar@1971
    61
  const double & blue() const {return _b;}
alpar@1971
    62
  ///Set the color components
alpar@1971
    63
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1971
    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
alpar@2179
    97
///Map <tt>int</tt>s to different \ref Color "Color"s
alpar@1971
    98
alpar@1971
    99
///This map assigns one of the predefined \ref Color "Color"s
alpar@1971
   100
///to each <tt>int</tt>. It is possible to change the colors as well as their
alpar@1971
   101
///number. The integer range is cyclically mapped to the provided set of colors.
alpar@1971
   102
///
alpar@2260
   103
///This is a true \ref concepts::ReferenceMap "reference map", so you can also
alpar@1971
   104
///change the actual colors.
alpar@1971
   105
alpar@2172
   106
class Palette : public MapBase<int,Color>
alpar@1971
   107
{
alpar@1971
   108
  std::vector<Color> colors;
alpar@1971
   109
public:
alpar@1971
   110
  ///Constructor
alpar@1971
   111
alpar@1971
   112
  ///Constructor
alpar@1971
   113
  ///\param have_white indicates whether white is
alpar@1971
   114
  ///amongst the provided color (\c true) or not (\c false). If it is true,
alpar@1971
   115
  ///white will be assigned to \c 0.
alpar@2174
   116
  ///\param num the number of the allocated colors. If it is \c 0,
alpar@2174
   117
  ///the default color configuration is set up (26 color plus the white).
alpar@1971
   118
  ///If \c num is less then 26/27 then the default color list is cut. Otherwise
alpar@1971
   119
  ///the color list is filled repeatedly with the default color list.
alpar@1971
   120
  ///(The colors can be changed later on.)
alpar@2172
   121
  Palette(bool have_white=false,int num=0)
alpar@1971
   122
  {
alpar@1971
   123
    do {
alpar@1971
   124
      if(have_white) colors.push_back(Color(1,1,1));
alpar@1971
   125
alpar@1971
   126
      colors.push_back(Color(0,0,0));
alpar@1971
   127
      colors.push_back(Color(1,0,0));
alpar@1971
   128
      colors.push_back(Color(0,1,0));
alpar@1971
   129
      colors.push_back(Color(0,0,1));
alpar@1971
   130
      colors.push_back(Color(1,1,0));
alpar@1971
   131
      colors.push_back(Color(1,0,1));
alpar@1971
   132
      colors.push_back(Color(0,1,1));
alpar@1971
   133
      
alpar@1971
   134
      colors.push_back(Color(.5,0,0));
alpar@1971
   135
      colors.push_back(Color(0,.5,0));
alpar@1971
   136
      colors.push_back(Color(0,0,.5));
alpar@1971
   137
      colors.push_back(Color(.5,.5,0));
alpar@1971
   138
      colors.push_back(Color(.5,0,.5));
alpar@1971
   139
      colors.push_back(Color(0,.5,.5));
alpar@1971
   140
      
alpar@1971
   141
      colors.push_back(Color(.5,.5,.5));
alpar@1971
   142
      colors.push_back(Color(1,.5,.5));
alpar@1971
   143
      colors.push_back(Color(.5,1,.5));
alpar@1971
   144
      colors.push_back(Color(.5,.5,1));
alpar@1971
   145
      colors.push_back(Color(1,1,.5));
alpar@1971
   146
      colors.push_back(Color(1,.5,1));
alpar@1971
   147
      colors.push_back(Color(.5,1,1));
alpar@1971
   148
      
alpar@1971
   149
      colors.push_back(Color(1,.5,0));
alpar@1971
   150
      colors.push_back(Color(.5,1,0));
alpar@1971
   151
      colors.push_back(Color(1,0,.5));
alpar@1971
   152
      colors.push_back(Color(0,1,.5));
alpar@1971
   153
      colors.push_back(Color(0,.5,1));
alpar@1971
   154
      colors.push_back(Color(.5,0,1));
alpar@1971
   155
    } while(int(colors.size())<num);
alpar@1971
   156
    //    colors.push_back(Color(1,1,1));
alpar@1971
   157
    if(num>0) colors.resize(num);
alpar@1971
   158
  }
alpar@1971
   159
  ///\e
alpar@1971
   160
  Color &operator[](int i)
alpar@1971
   161
  {
alpar@1971
   162
    return colors[i%colors.size()];
alpar@1971
   163
  }
alpar@1971
   164
  ///\e
alpar@1971
   165
  const Color &operator[](int i) const
alpar@1971
   166
  {
alpar@1971
   167
    return colors[i%colors.size()];
alpar@1971
   168
  }
alpar@1971
   169
  ///\e
alpar@1971
   170
  void set(int i,const Color &c)
alpar@1971
   171
  {
alpar@1971
   172
    colors[i%colors.size()]=c;
alpar@1971
   173
  }
alpar@1971
   174
  ///Sets the number of the exiting colors.
alpar@1971
   175
  void resize(int s) { colors.resize(s);}
alpar@1971
   176
  ///Returns the number of the existing colors.
alpar@1971
   177
  std::size_t size() const { return colors.size();}
alpar@1971
   178
};
alpar@1971
   179
alpar@1971
   180
///Returns a visible distinct \ref Color
alpar@1971
   181
alpar@1971
   182
///Returns a \ref Color which is as different from the given parameter
alpar@1971
   183
///as it is possible.
alpar@1971
   184
inline Color distantColor(const Color &c) 
alpar@1971
   185
{
alpar@1971
   186
  return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
alpar@1971
   187
}
alpar@1971
   188
///Returns black for light colors and white for the dark ones.
alpar@1971
   189
alpar@1971
   190
///Returns black for light colors and white for the dark ones.
alpar@1971
   191
inline Color distantBW(const Color &c){
alpar@2174
   192
  return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
alpar@1971
   193
}
alpar@1971
   194
alpar@2174
   195
/// @}
alpar@2174
   196
alpar@1971
   197
} //END OF NAMESPACE LEMON
alpar@1971
   198
alpar@1971
   199
#endif // LEMON_COLOR_H