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