Debian Codegraph @

cvd/colourmap.h

raw symbols SCIP
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
97 ///Handy class for generating a colourscale.
98 ///Expected range is \f$[0, 1)\f$, and clamping is performed.
99 ///Currently only RGB scales are provided. TODO: RGBA scales.
100 ///@ingroup gGraphics
101 ///Examples in examples/colourmaps.cc
102 ///@include colourmaps.cc
103 template <class C>
104 struct Colourmap<Rgb<C>>
105 {
106
107 ///Glow/Hot colourscale (red-yellow-white)
108 ///@param d Value in \f$[0, 1)\f$ to map
109 static Rgb<C> hot(double d) { return Internal::conv<C>(Internal::hot, d); }
110
111 ///Jet colourscale (red-yellow-green-cyan-blue)
112 ///@param d Value in \f$[0, 1)\f$ to map
113 static Rgb<C> jet(double d) { return Internal::conv<C>(Internal::jet, d); }
114
115 ///Green-black-red colourscale
116 ///@param d Value in \f$[0, 1)\f$ to map
117 static Rgb<C> gkr(double d) { return Internal::conv<C>(Internal::gkr, d); }
118 ///Gray colourscale
119 ///@param d Value in \f$[0, 1)\f$ to map
120 static Rgb<C> grey(double d) { return Internal::conv<C>(Internal::grey, d); }
121 };
122
123 ///@example colourmaps.cc
124 ///Example of how to use the CVD::Colourmap class for visualisation.
125
126 };
127
128 #endif