DGtal  0.9.2
testColorMaps.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/io/Color.h"
34 #include "DGtal/io/colormaps/CColorMap.h"
35 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
36 #include "DGtal/io/colormaps/HueShadeColorMap.h"
37 #include "DGtal/io/colormaps/ColorBrightnessColorMap.h"
38 #include "DGtal/io/colormaps/GradientColorMap.h"
39 #include "DGtal/io/colormaps/RandomColorMap.h"
40 #include "DGtal/io/boards/Board2D.h"
41 #include "Board/PSFonts.h"
43 
44 using namespace std;
45 using namespace DGtal;
46 using namespace LibBoard;
47 
49 // Function template for testing ColorMap classes.
51 template <typename TColorMap>
52 void addColorMapSample( const char * name,
53  const TColorMap & aColorMap,
54  const typename TColorMap::Value step,
55  Board & board )
56 {
57  BOOST_CONCEPT_ASSERT(( concepts::CColorMap<TColorMap> ));
58 
59  typedef typename TColorMap::Value Value;
60  board.translate( 0, 15 );
61  board.setPenColor(Color::Black);
62  board.setFont( LibBoard::Fonts::Courier, 12 ); // todo with Cairo
63  board.drawText( -150, 0, name ); // todo with Cairo
64  board.setPenColor(Color::None);
65  for ( Value x = aColorMap.min(); x <= aColorMap.max(); x += step ) {
66  board.setFillColor( aColorMap( x ) );
67  board.drawRectangle( static_cast<double>( x ),
68  10,
69  static_cast<double>( step ),
70  10 );
71  }
72 }
73 
74 bool testGrayscaleColorMap()
75 {
76  unsigned int nbok = 0;
77  unsigned int nb = 6;
78 
79  trace.beginBlock("Colormap 0..255");
80  {
82 
83  Color c0 = cmap(0);
84  trace.info();
85  cerr << int(c0.red())
86  << "," << int(c0.green()) << "," << int(c0.blue()) << std::endl;
87  nbok += ( c0 == Color::Black );
88 
89  Color c128 = cmap(128);
90  trace.info();
91  cerr << int(c128.red())
92  << "," << int(c128.green()) << "," << int(c128.blue()) << std::endl;
93  nbok += ( c128 == Color(128,128,128) );
94 
95  Color c255 = cmap(255);
96  trace.info();
97  cerr << int(c255.red())
98  << "," << int(c255.green()) << "," << int(c255.blue()) << std::endl;
99  nbok += ( c255 == Color::White );
100  }
101  trace.endBlock();
102 
103  trace.beginBlock("Colormap 64..128");
104  {
106  Color c0 = cmap(64);
107  trace.info();
108  cerr << int(c0.red())
109  << "," << int(c0.green()) << "," << int(c0.blue()) << std::endl;
110  nbok += ( c0 == Color::Black );
111 
112  Color c255 = cmap(128);
113  trace.info();
114  cerr << int(c255.red())
115  << "," << int(c255.green()) << "," << int(c255.blue()) << std::endl;
116  nbok += ( c255 == Color::White );
117  }
118  trace.endBlock();
119 
120  trace.beginBlock("Static method");
121  {
123  trace.info() << "Should be white: ";
124  cerr << int(c.red())
125  << "," << int(c.green()) << "," << int(c.blue()) << std::endl;
126  nbok += (c == Color::White);
127 
129  trace.info() << "Should be around 127,127,127: ";
130  cerr << int(c.red())
131  << "," << int(c.green()) << "," << int(c.blue()) << std::endl;
132 
133  trace.endBlock();
134  }
135  return nbok == nb;
136 }
137 
138 int main()
139 {
140  Board board;
141  trace.beginBlock ( "Testing GrayscaleColorMap" );
142  bool res1 = testGrayscaleColorMap();
143  trace.emphase() << ( res1 ? "Passed." : "Error." ) << endl;
144  trace.endBlock();
145 
146  GrayscaleColorMap<int> cmap_gray( 0, 500);
147  addColorMapSample( "Grayscale", cmap_gray, 1, board );
148 
149  ColorBrightnessColorMap<int> cmap_red( 0, 500, Color::Red );
150  addColorMapSample( "Brightness", cmap_red, 1, board );
151 
153  addColorMapSample( "Bright (Blue)", cmap_blue, 1, board );
154 
155  HueShadeColorMap<int,2> cmap_hsv( 0, 500);
156  addColorMapSample( "HueShade", cmap_hsv, 1, board );
157 
158  HueShadeColorMap<int,2> cmap_cyclic5( 0, 500 );
159  addColorMapSample( "HueShade (2x)", cmap_cyclic5, 1, board );
160 
161  HueShadeColorMap<int, 10> cmap_cyclic10( 0, 500 );
162  addColorMapSample( "HueShade (10x)", cmap_cyclic10, 1, board );
163 
164  RandomColorMap cmap_random(0,500, Color::Green, Color::Red);
165  cmap_random.addColor(Color::Red);
166  cmap_random.addColor(Color::Blue);
167  cmap_random.addColor(Color::Yellow);
168 
169  const int yellow = DGTAL_RGB2INT(255,255,0);
170  const int red = DGTAL_RGB2INT(255,0,0);
171  GradientColorMap<int, CMAP_CUSTOM, yellow, red> cmap_gradient( 0, 500 );
172  addColorMapSample( "Gradient (Y->R)", cmap_gradient, 1, board );
173 
174  GradientColorMap<int> cmap_grad3( 0, 500 );
175  cmap_grad3.addColor( Color::Green );
176  cmap_grad3.addColor( Color::Yellow );
177  cmap_grad3.addColor( Color::Red );
178  addColorMapSample( "Gradient (G->Y->R)", cmap_grad3, 1, board );
179 
180  cmap_grad3.clearColors();
181  cmap_grad3.addColor( Color::Blue );
182  cmap_grad3.addColor( Color::White );
183  cmap_grad3.addColor( Color::Red );
184  addColorMapSample( "Gradient (B->W->R)", cmap_grad3, 1, board );
185 
186  GradientColorMap<int> cool_gradient( 0, 500, CMAP_COOL );
187  addColorMapSample( "Gradient (Cool)", cool_gradient, 1, board );
188 
189  GradientColorMap<int> copper_gradient( 0, 500, CMAP_COPPER );
190  addColorMapSample( "Gradient (Copper)", copper_gradient, 1, board );
191 
192  GradientColorMap<int> hot_gradient( 0, 500, CMAP_HOT );
193  addColorMapSample( "Gradient (Hot)", hot_gradient, 1, board );
194 
195  GradientColorMap<int,CMAP_JET> jet_gradient( 0, 500 );
196  addColorMapSample( "Gradient (Jet)", jet_gradient, 1, board );
197 
198  addColorMapSample( "Gradient (Spring)",
200  1,
201  board );
202  addColorMapSample( "Gradient (Summer)",
203  GradientColorMap<int>( 0, 500, CMAP_SUMMER ),
204  1,
205  board );
206  addColorMapSample( "Gradient (Autumn)",
207  GradientColorMap<int>( 0, 500, CMAP_AUTUMN ),
208  1,
209  board );
210  addColorMapSample( "Gradient (Winter)",
211  GradientColorMap<int>( 0, 500, CMAP_WINTER ),
212  1,
213  board );
214  addColorMapSample( "Random", cmap_random, 1, board );
215 
216  board.saveEPS( "colormaps.eps" );
217  board.saveSVG( "colormaps.svg" );
218  board.saveTikZ( "colormaps.tikz" );
219 
220 #ifdef WITH_CAIRO
221  board.saveCairo("colormaps-cairo.ps", Board2D::CairoPS);
222 #endif
223 
224  return ( res1 ) ? 0 : 1;
225 }
226 // //
void beginBlock(const std::string &keyword="")
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Trace trace
Definition: Common.h:130
Class for EPS, FIG or SVG drawings.
Definition: Board.h:35
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: access to random color from a gradientColorMap.
STL namespace.
double endBlock()
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void drawText(double x, double y, const char *text, int depthValue=-1)
Definition: Board.cpp:693
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
void saveTikZ(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1219
std::ostream & emphase()
void drawRectangle(double x, double y, double width, double height, int depthValue=-1)
Definition: Board.cpp:417
DGtal is the top-level namespace which contains all DGtal functions and types.
Board & setFillColor(const DGtal::Color &color)
Definition: Board.cpp:322
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
std::ostream & info()
Shape & translate(double dx, double dy)
Definition: Board.cpp:176
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
Board & setFont(const Fonts::Font font, double fontSize)
Definition: Board.cpp:336
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Board & setPenColor(const DGtal::Color &color)
Definition: Board.cpp:298
Aim: Defines the concept describing a color map. A color map converts a value within a given range in...
Definition: CColorMap.h:93
void green(const unsigned char aGreenValue)
void red(const unsigned char aRedValue)
void blue(const unsigned char aBlueValue)