KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i_colormap.h
Go to the documentation of this file.
1 /* $Id: i_colormap.h 16764 2014-05-09 23:17:12Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef KJB_I_CPP_I_COLORMAP_H
23 #define KJB_I_CPP_I_COLORMAP_H
24 
25 #include <string>
26 #include <vector>
27 #include <map>
28 #include <iterator>
29 #include <l_cpp/l_algorithm.h>
30 #include <i/i_float.h>
31 #include <l_cpp/l_exception.h>
32 #include <i_cpp/i_pixel.h>
33 
34 namespace kjb
35 {
36 
42 class Colormap
43 {
44 public:
45 
46  Colormap(const std::string& name = "jet", int n = 64)
47  {
48  preset(name, n);
49  }
50 
51  Colormap(const char* name, int n = 64)
52  {
53  preset(name, n);
54  }
55 
56  void preset(const std::string& name, int n = 64)
57  {
58  if(presets_.count(name) == 0)
59  {
60  KJB_THROW_3(Illegal_argument, "Unknown colormap preset: %s", (name.c_str()));
61  }
62 
63  colors_ = presets_[name](n);
64  }
65 
70  PixelRGBA operator()(double x) const
71  {
72  return lerp(colors_.begin(), colors_.end(), x);
73  }
74 
75  size_t size() const
76  {
77  return colors_.size();
78  }
79 
80  typedef std::vector<PixelRGBA> (*Preset_map_func)(int);
81 
82  static std::vector<PixelRGBA> jet(int n);
83  static std::vector<PixelRGBA> hot(int n);
84  static std::vector<PixelRGBA> hsv(int n);
85  static std::vector<PixelRGBA> gray(int n);
86  static std::vector<PixelRGBA> cool(int n);
87  static std::vector<PixelRGBA> lines(int n);
88  static std::map<std::string, Preset_map_func> presets_;
89 
90  std::vector<PixelRGBA> colors_;
91 };
92 
94 
95 } // namespace kjb
96 #endif
static std::vector< PixelRGBA > lines(int n)
Definition: i_colormap.cpp:196
static std::vector< PixelRGBA > cool(int n)
Definition: i_colormap.cpp:222
static std::vector< PixelRGBA > jet(int n)
Definition: i_colormap.cpp:40
Colormap(const std::string &name="jet", int n=64)
Definition: i_colormap.h:46
std::iterator_traits< Iterator >::value_type lerp(Iterator begin, Iterator end, Real x)
Definition: l_algorithm.h:69
static std::vector< PixelRGBA > gray(int n)
Definition: i_colormap.cpp:174
std::vector< PixelRGBA > colors_
Definition: i_colormap.h:90
x
Definition: APPgetLargeConnectedEdges.m:100
static std::vector< PixelRGBA > hsv(int n)
Definition: i_colormap.cpp:147
Code for a wrapper class around the C struct Pixel.
void preset(const std::string &name, int n=64)
Definition: i_colormap.h:56
Wrapped version of the C struct Pixel, with Alpha (opacity).
Definition: i_pixel.h:57
PixelRGBA operator()(double x) const
Definition: i_colormap.h:70
#define KJB_THROW_3(ex, fmt, params)
Definition: l_exception.h:56
Definition: i_colormap.h:42
static std::map< std::string, Preset_map_func > presets_
Definition: i_colormap.h:88
size_t size() const
Definition: i_colormap.h:75
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
static std::vector< PixelRGBA > hot(int n)
Definition: i_colormap.cpp:96
Colormap(const char *name, int n=64)
Definition: i_colormap.h:51
Support for error handling exception classes in libKJB.