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