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