DGtal  1.3.beta
testColorMaps.cpp
Go to the documentation of this file.
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/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 // //
LibBoard::Board::setFont
Board & setFont(const Fonts::Font font, double fontSize)
Definition: Board.cpp:336
DGtal::GrayscaleColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
Definition: GrayscaleColorMap.h:93
DGtal::RandomColorMap::addColor
void addColor(const Color &aColor)
DGtal::CMAP_SUMMER
@ CMAP_SUMMER
Definition: GradientColorMap.h:63
LibBoard::Board::setPenColor
Board & setPenColor(const DGtal::Color &color)
Definition: Board.cpp:298
DGtal::Trace::endBlock
double endBlock()
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
LibBoard::Board::drawText
void drawText(double x, double y, const char *text, int depthValue=-1)
Definition: Board.cpp:693
LibBoard::Board
Class for EPS, FIG or SVG drawings.
Definition: Board.h:35
DGtal::HueShadeColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: HueShadeColorMap.h:90
DGtal::Trace::emphase
std::ostream & emphase()
LibBoard::Board::saveCairo
void saveCairo(const char *filename, CairoType type=CairoPNG, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1139
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::RandomColorMap
Aim: access to random color from a gradientColorMap.
Definition: RandomColorMap.h:59
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
LibBoard::Board::drawRectangle
void drawRectangle(double x, double y, double width, double height, int depthValue=-1)
Definition: Board.cpp:417
DGtal::CMAP_HOT
@ CMAP_HOT
Definition: GradientColorMap.h:68
DGtal::SimpleDistanceColorMap
Aim: simple blue to red colormap for distance information for instance.
Definition: SimpleDistanceColorMap.h:64
DGtal::Color::red
void red(const unsigned char aRedValue)
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
LibBoard::Fonts::Courier
@ Courier
Definition: PSFonts.h:50
DGtal::concepts::CColorMap
Aim: Defines the concept describing a color map. A color map converts a value within a given range in...
Definition: CColorMap.h:93
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
DGtal::CMAP_COOL
@ CMAP_COOL
Definition: GradientColorMap.h:66
LibBoard
Definition: Board.cpp:87
testGrayscaleColorMap
bool testGrayscaleColorMap()
Definition: testColorMaps.cpp:75
DGtal::CMAP_AUTUMN
@ CMAP_AUTUMN
Definition: GradientColorMap.h:64
DGtal::GradientColorMap::clearColors
void clearColors()
DGtal::Color::green
void green(const unsigned char aGreenValue)
DGtal::ColorBrightnessColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: ColorBrightnessColorMap.h:89
DGtal::Color::blue
void blue(const unsigned char aBlueValue)
DGtal::GradientColorMap::addColor
void addColor(const Color &color)
LibBoard::Board::saveEPS
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
LibBoard::Board::saveTikZ
void saveTikZ(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1219
DGtal::CMAP_COPPER
@ CMAP_COPPER
Definition: GradientColorMap.h:67
main
int main()
Definition: testColorMaps.cpp:139
LibBoard::Board::setFillColor
Board & setFillColor(const DGtal::Color &color)
Definition: Board.cpp:322
LibBoard::Board::translate
Shape & translate(double dx, double dy)
Definition: Board.cpp:176
DGtal::GradientColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: GradientColorMap.h:119
DGtal::CMAP_WINTER
@ CMAP_WINTER
Definition: GradientColorMap.h:65
Value
double Value
Definition: testSimpleRandomAccessRangeFromPoint.cpp:38
addColorMapSample
void addColorMapSample(const char *name, const TColorMap &aColorMap, const typename TColorMap::Value step, Board &board)
Definition: testColorMaps.cpp:53