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