libcvd
colourmap.h
1 #ifndef CVD_INC_COLOURMAPS_H
2 #define CVD_INC_COLOURMAPS_H
3 
4 #include <cvd/internal/convert_pixel_types.h>
5 #include <cvd/internal/rgb_components.h>
6 #include <cvd/rgb.h>
7 #include <cvd/rgba.h>
8 
9 #include <algorithm>
10 
11 namespace CVD
12 {
13 
14 namespace Internal
15 {
16  Rgb<float> grey(float d)
17  {
18  using std::max;
19  using std::min;
20  d = max(0.f, min(d, 1.0f));
21  return Rgb<float>(d, d, d);
22  }
23 
24  Rgb<float> hot(float d)
25  {
26  using std::max;
27  using std::min;
28  d = max(0.f, min(d, 1.0f));
29 
30  if(d < 1. / 3.)
31  return Rgb<float>(d * 3, 0, 0);
32  else if(d < 2. / 3.)
33  return Rgb<float>(1, (d - 1. / 3.) * 3, 0);
34  else
35  return Rgb<float>(1, 1, (d - 2. / 3.) * 3);
36  }
37 
38  Rgb<float> jet(float d)
39  {
40  using std::max;
41  using std::min;
42  d = max(0.f, min(d, 1.0f));
43  double r = 0, g = 0, b = 0;
44 
45  if(d < 1. / 4.) // Red to yello
46  {
47  r = 1;
48  g = d / (1. / 4.);
49  b = 0;
50  }
51  else if(d < 2. / 4.) //Yello to green
52  {
53  g = 1;
54  r = 1 - (d - 1. / 4.) / (1. / 4.);
55  b = 0;
56  }
57  else if(d < 3. / 4.) // Green to cyan
58  {
59  r = 0;
60  g = 1;
61  b = (d - 2. / 4.) / (1. / 4.);
62  }
63  else //cyan to blue
64  {
65  b = 1;
66  g = 1 - (d - 3. / 4.) / (1. / 4.);
67  r = 0;
68  }
69 
70  return Rgb<float>(r, g, b);
71  }
72 
73  Rgb<float> gkr(float d)
74  {
75  using std::max;
76  using std::min;
77 
78  if(d < 1. / 2.)
79  return Rgb<float>(1 - d * 2, 0, 0);
80  else
81  return Rgb<float>(0, (d - 1. / 2.) * 2, 0);
82  }
83 
84  template <class C, class D>
85  Rgb<C> conv(const D& func, float d)
86  {
87  Rgb<float> col = func(d);
88  Rgb<C> r;
89  Pixel::ConvertPixels<Rgb<float>, Rgb<C>>::convert(&col, &r, 1);
90  return r;
91  }
92 };
93 
94 template <class C>
95 struct Colourmap;
96 
103 template <class C>
104 struct Colourmap<Rgb<C>>
105 {
106 
109  static Rgb<C> hot(double d) { return Internal::conv<C>(Internal::hot, d); }
110 
113  static Rgb<C> jet(double d) { return Internal::conv<C>(Internal::jet, d); }
114 
117  static Rgb<C> gkr(double d) { return Internal::conv<C>(Internal::gkr, d); }
120  static Rgb<C> grey(double d) { return Internal::conv<C>(Internal::grey, d); }
121 };
122 
125 
126 };
127 
128 #endif
A colour consisting of red, green and blue components.
Definition: rgb.h:25
static Rgb< C > jet(double d)
Jet colourscale (red-yellow-green-cyan-blue)
Definition: colourmap.h:113
All classes and functions are within the CVD namespace.
Definition: argb.h:6
Definition: colourmap.h:95
static Rgb< C > grey(double d)
Gray colourscale.
Definition: colourmap.h:120
static Rgb< C > hot(double d)
Glow/Hot colourscale (red-yellow-white)
Definition: colourmap.h:109
static Rgb< C > gkr(double d)
Green-black-red colourscale.
Definition: colourmap.h:117