File: colormap.h

package info (click to toggle)
aoflagger 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,232 kB
  • sloc: cpp: 61,805; python: 60; sh: 23; makefile: 8
file content (557 lines) | stat: -rw-r--r-- 18,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/** @file
 * This is the header file for the ColorMap base class and several specific color map classes.
 * @author André Offringa <offringa@gmail.com>
 */
#ifndef COLORMAP_H
#define COLORMAP_H

#include <cmath>
#include <string>
#include <memory>

/**
 * The ColorMap class turns a value between -1 and 1 into a gradient color scale.
 */
class ColorMap {
	public:
		/**
		 * Destructor.
		 */
		virtual ~ColorMap()
		{ }
		/**
		 * Maps a double value to the red component of the color map.
		 * @param value Value to be mapped  (-1 to 1).
		 * @return The red color value (0 - 255).
		 */
		virtual unsigned char ValueToColorR(long double value) const noexcept = 0;
		/**
		 * Maps a double value to the green component of the color map.
		 * @param value Value to be mapped (-1 to 1).
		 * @return The green color value (0 - 255).
		 */
		virtual unsigned char ValueToColorG(long double value) const noexcept = 0;
		/**
		 * Maps a double value to the blue component of the color map.
		 * @param value Value to be mapped (-1 to 1).
		 * @return The blue color value (0 - 255).
		 */
		virtual unsigned char ValueToColorB(long double value) const noexcept = 0;
		/**
		 * Maps a double value to the alfa (transparency) component of the color map.
		 * @param value Value to be mapped (-1 to 1).
		 * @return The alfa (transparency) color value (0 - 255). 255=fully opaque, 0=fully transparent.
		 */
		virtual unsigned char ValueToColorA(long double value) const noexcept = 0;
		/**
		 * Convert the input value to a RGB value.
		 * @param value Value to be mapped (-1 to 1).
		 * @param r Red component (0-255)
		 * @param g Green component (0-255)
		 * @param b Blue component (0-255)
		 */
		void Convert(double value, unsigned char& r, unsigned char& g, unsigned char& b) const  noexcept
		{
			r = ValueToColorR(value);
			g = ValueToColorG(value);
			b = ValueToColorB(value);
		}
		/**
		 * Create a color map given its name. The human readable list of names can be retrieved with GetColorMapsString().
		 * @param typeStr name of the color map type.
		 * @return The new create color map. The caller is responsible for @c delete -ing the color map after usage.
		 * @see GetColorMapsString().
		 */
		static std::unique_ptr<ColorMap> CreateColorMap(const std::string& typeStr);
		/**
		 * Returns a string containing a description of the color map names. These names can be used to
		 * create the color map with CreateColorMap().
		 * @return 
		 * @see CreateColorMap().
		 */
		static const std::string &GetColorMapsString() noexcept;
	private:
		static const std::string _colorMapsString;
};

/**
 * ColorMap that turns a value into a gray scale value. High values represent whiter colors.
 */
class MonochromeMap : public ColorMap {
	public:
		MonochromeMap() { }
		~MonochromeMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return (unsigned char) (value*127.5+127.5); }
		unsigned char ValueToColorG(long double value) const noexcept override { return (unsigned char) (value*127.5+127.5); }
		unsigned char ValueToColorB(long double value) const noexcept override { return (unsigned char) (value*127.5+127.5); }
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns a value into a gray scale value. High values represent whiter colors.
 */
class InvertedMap : public ColorMap {
	public:
		InvertedMap() { }
		~InvertedMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return (unsigned char) (127.5-value*127.5); }
		unsigned char ValueToColorG(long double value) const noexcept override { return (unsigned char) (127.5-value*127.5); }
		unsigned char ValueToColorB(long double value) const noexcept override { return (unsigned char) (127.5-value*127.5); }
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns negative values into blue and positive values into red. Zero is represented with black.
 */
class RedBlueMap : public ColorMap {
	public:
		RedBlueMap() { }
		~RedBlueMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value>0.0)
				return (unsigned char) (value*255.0);
			else
				return 0;
		}
		unsigned char ValueToColorG(long double) const noexcept override { return 0; }
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value<0.0)
				return (unsigned char) (value*-255.0);
			else
				return 0;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns negative values into red and positive values into black. Zero is represented with white.
 */
class BlackRedMap : public ColorMap {
	public:
		BlackRedMap() { }
		~BlackRedMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value>0.0)
				return (unsigned char) (255-value*255.0);
			else
				return 255;
		}
		unsigned char ValueToColorG(long double value) const noexcept override
		{
			if(value>0.0)
				return (unsigned char) (255-value*255.0);
			else
				return 255+value*255.0;
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value>0.0)
				return (unsigned char) (255-value*255.0);
			else
				return 255+value*255.0;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns negative values into blue and positive values into red. Zero is represented with white.
 */
class RedWhiteBlueMap : public ColorMap {
	public:
		RedWhiteBlueMap() { }
		~RedWhiteBlueMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value<0.0)
				return (unsigned char) (255.0 + (value*255.0));
			else
				return 255;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			if(value<0.0)
				return (unsigned char) (255.0 + (value*255.0));
			else
				return (unsigned char) (255.0 - (value*255.0));
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value>0.0)
				return (unsigned char) (255.0 - (value*255.0));
			else
				return 255;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that passes several colors.
 */
class ColdHotMap : public ColorMap {
	public:
		ColdHotMap() { }
		~ColdHotMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value >= 0.5)
				return (unsigned char) ((1.5 - value) * 255.0);
			else if(value < -0.5)
				return 0;
			else if(value < 0.0)
				return (unsigned char) (((value + 0.5)*2.0) * 255.0);
			else
				return 255;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			if(value < -0.5)
				return (unsigned char) (((value + 1.0)*2.0) * 255.0);
			else if(value < 0.0)
				return 255;
			else if(value < 0.5)
				return (unsigned char) (((0.5 - value)*2.0)*255.0);
			else
				return 0;
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value < -0.5)
				return (unsigned char) ((value + 1.5) * 255.0);
			else if(value < 0.0)
				return (unsigned char) (((-value)*2.0)*255.0);
			else
				return 0;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that uses another color map and increases its contrast.
 */
class ContrastMap : public ColorMap {
	private:
		const std::unique_ptr<ColorMap> _map;
	public:
		explicit ContrastMap(const std::string& type) :
			_map(CreateColorMap(type)) { }
		unsigned char ValueToColorR(long double value) const noexcept override { return _map->ValueToColorR(value>=0.0 ? sqrt(value) : -sqrt(-value)); }
		unsigned char ValueToColorG(long double value) const noexcept override { return _map->ValueToColorG(value>=0.0 ? sqrt(value) : -sqrt(-value)); }
		unsigned char ValueToColorB(long double value) const noexcept override { return _map->ValueToColorB(value>=0.0 ? sqrt(value) : -sqrt(-value)); }
		unsigned char ValueToColorA(long double value) const noexcept override { return _map->ValueToColorA(value>=0.0 ? sqrt(value) : -sqrt(-value)); }
};

/**
 * ColorMap that uses another color map and applies logarithmic scaling to it. Expects inputs
 * between 0 and 1, will output to map between -1 and 1.
 */
class PosLogMap : public ColorMap {
	private:
		const ColorMap &_map;
	public:
		explicit PosLogMap(const ColorMap &map) :
			_map(map) { }
		~PosLogMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return value >= 0.0  ? _map.ValueToColorR((log10(value*0.9999+0.0001)+2.0)/2.0) : _map.ValueToColorR(-1.0); }
		unsigned char ValueToColorG(long double value) const noexcept override { return value >= 0.0  ? _map.ValueToColorG((log10(value*0.9999+0.0001)+2.0)/2.0) : _map.ValueToColorG(-1.0);  }
		unsigned char ValueToColorB(long double value) const noexcept override { return value >= 0.0  ? _map.ValueToColorB((log10(value*0.9999+0.0001)+2.0)/2.0) : _map.ValueToColorB(-1.0);  }
		unsigned char ValueToColorA(long double value) const noexcept override { return value >= 0.0  ? _map.ValueToColorA((log10(value*0.9999+0.0001)+2.0)/2.0) : _map.ValueToColorA(-1.0);  }
};

class PosMonochromeLogMap : public PosLogMap {
	private:
		const MonochromeMap _monochromeMap;
	public:
		PosMonochromeLogMap() : PosLogMap(_monochromeMap) { }
};

/**
 * ColorMap that uses another color map and applies logarithmic scaling to it. Expects inputs
 * between -1 and 1, will output to map between -1 and 1.
 */
class FullLogMap : public ColorMap {
	private:
		const ColorMap &_map;
	public:
		explicit FullLogMap(const ColorMap &map) :
			_map(map) { }
		~FullLogMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return _map.ValueToColorR((log10(value*0.49995+0.50005)+2.0)/2.0); }
		unsigned char ValueToColorG(long double value) const noexcept override { return _map.ValueToColorG((log10(value*0.49995+0.50005)+2.0)/2.0);  }
		unsigned char ValueToColorB(long double value) const noexcept override { return _map.ValueToColorB((log10(value*0.49995+0.50005)+2.0)/2.0);  }
		unsigned char ValueToColorA(long double value) const noexcept override { return _map.ValueToColorA((log10(value*0.49995+0.50005)+2.0)/2.0);  }
};

/**
 * ColorMap that turns negative values into blue and positive values into red. Zero is represented with yellow.
 */
class RedYellowBlueMap : public ColorMap {
	public:
		RedYellowBlueMap() { }
		~RedYellowBlueMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value >= 1.0/3.0)
				return 255;
			else if(value >= -1.0/3.0)
				return (unsigned char) (value*(255.0 * 3.0/2.0)) + 128;
			else
				return 0;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			if(value >= 1.0/3.0)
				return 255 - (unsigned char) ((value-1.0/3.0)*(255.0*3.0/2.0));
			else if(value >= 0.0)
				return 255;
			else if(value >= -1.0/3.0)
				return (unsigned char) ((value+1.0/3.0)*(255.0*6.0/2.0));
			else 
				return 0;
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value >= 1.0/3.0)
				return 0;
			else if(value >= -1.0/3.0)
				return 255 - (unsigned char) ((value+1.0/3.0)*(255.0*3.0/2.0));
			else
				return (unsigned char) ((value+1.0)*(255.0*3.0/2.0));
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

class FireMap : public ColorMap {
	public:
		FireMap() { }
		~FireMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value < -1.0)
				return 0;
			else if(value < 0)
				return (unsigned char) ((value + 1.0) * 255.0);
			else
				return 255;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			if(value < -0.5)
				return 0;
			else if(value < 0.5)
				return (unsigned char) ((value + 0.5) * 255.0);
			else
				return 255;
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value < 0)
				return 0;
			else if(value < 1.0)
				return(unsigned char) (value * 255.0);
			else
				return 255;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns negative values into blue and positive values into red. Zero is represented with black.
 */
class RedYellowBlackBlueMap : public ColorMap {
	public:
		RedYellowBlackBlueMap() { }
		~RedYellowBlackBlueMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override {
			if(value < 0.0)
				return 0;
			else if(value < 0.5)
				return (unsigned char) (value*2.0*255.0);
			else
				return 255;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			if(value < -0.5)
				return (unsigned char) ((value+1.0)*255.0*2.0);
			else if(value >= 0.5)
				return (unsigned char) ((-value+1.0)*255.0*2.0);
			else if(value < 0.0)
				return (unsigned char) (-value*2.0*255.0);
			else
				return (unsigned char) (value*2.0*255.0);
		}
		unsigned char ValueToColorB(long double value) const noexcept override {
			if(value >= 0.0)
				return 0;
			else if(value >= -0.5)
				return (unsigned char) (-value*2.0*255.0);
			else
				return 255;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns all negative values into black and positive values into a gray scale.
 */
class PositiveMap : public ColorMap {
	public:
		PositiveMap() { }
		~PositiveMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return (unsigned char) (value>0.0 ? value*255.0 : 0); }
		unsigned char ValueToColorG(long double value) const noexcept override { return (unsigned char) (value>0.0 ? value*255.0 : 0); }
		unsigned char ValueToColorB(long double value) const noexcept override { return (unsigned char) (value>0.0 ? value*255.0 : 0); }
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that is the negation of PositiveMap.
 */
class InvPositiveMap : public ColorMap {
	public:
		InvPositiveMap() { }
		~InvPositiveMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return (unsigned char) (value>0.0 ? 255.0-value*255.0 : 255.0); }
		unsigned char ValueToColorG(long double value) const noexcept override { return (unsigned char) (value>0.0 ? 255.0-value*255.0 : 255.0); }
		unsigned char ValueToColorB(long double value) const noexcept override { return (unsigned char) (value>0.0 ? 255.0-value*255.0 : 255.0); }
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that is the logarithmic negation of PositiveMap.
 */
class LogInvPositiveMap : public ColorMap {
	public:
		LogInvPositiveMap() { }
		~LogInvPositiveMap() { }
		unsigned char ValueToColorR(long double value) const noexcept override { return (unsigned char) (value>0.0 ? -log10(value*0.9+0.1)*255.0 : 255.0); }
		unsigned char ValueToColorG(long double value) const noexcept override { return (unsigned char) (value>0.0 ? -log10(value*0.9+0.1)*255.0 : 255.0); }
		unsigned char ValueToColorB(long double value) const noexcept override { return (unsigned char) (value>0.0 ? -log10(value*0.9+0.1)*255.0 : 255.0); }
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
};

/**
 * ColorMap that turns different integer values into different colours.
 */
class IntMap {
	public:
		static unsigned char R(int value) {
			switch (value%16)
			{
				case 0:
				case 3:
				case 5:
				case 6:
					return 255;
				case 8:
				case 11:
				case 13:
				case 14:
					return 127;
				default:
					return 0;	
			}
		}
		static unsigned char G(int value) {
			switch (value%16)
			{
				case 1:
				case 3:
				case 4:
				case 6:
					return 255;
				case 9:
				case 11:
				case 12:
				case 14:
					return 127;
				default:
					return 0;	
			}
		}
		static unsigned char B(int value) {
			switch (value%16)
			{
				case 2:
				case 4:
				case 5:
				case 6:
					return 255;
				case 10:
				case 12:
				case 13:
				case 14:
					return 127;
				default:
					return 0;	
			}
		}
		static unsigned char A(int) { return 255; }
};

/**
 * ColorMap that is equivalent with the Viridis colormap as used by Python Matplotlib.
 */
class ViridisMap : public ColorMap {
	public:
		unsigned char ValueToColorR(long double value) const noexcept override {
			return (unsigned char) (DATA_R[index(value)]*255.0);
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			return (unsigned char) (DATA_G[index(value)]*255.0); 
		}
		unsigned char ValueToColorB(long double value) const noexcept override { 
			return (unsigned char) (DATA_B[index(value)]*255.0);
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }

private:
	static size_t index(long double value)
	{
		double d = round(value*127.5+127.5);
		if(d <= 0.0)
			d = 0.0;
		if(d >= 255.0)
			d = 255.0;
		return (size_t) d;
	}
	
	const static double DATA_R[256], DATA_G[256], DATA_B[256];
};

template<int Saturation>
class CubeHelixMapBase : public ColorMap {
public:
		unsigned char ValueToColorR(long double value) const noexcept override {
			unsigned char r, g, b;
			convertColour(value, r, g, b);
			return r;
		}
		unsigned char ValueToColorG(long double value) const noexcept override {
			unsigned char r, g, b;
			convertColour(value, r, g, b);
			return g;
		}
		unsigned char ValueToColorB(long double value) const noexcept override { 
			unsigned char r, g, b;
			convertColour(value, r, g, b);
			return b;
		}
		unsigned char ValueToColorA(long double) const noexcept override { return 255; }
private:
  void convertColour(long double value, unsigned char& rv, unsigned char& gv, unsigned char& bv) const noexcept
	{
		value = value*0.5 + 0.5;
		double start = 0.5, cycles = -1.5, gamma =1.5;
		double phi = 2. * 3.14 * (start/3.0 + value * cycles);
		if ( gamma != 1.0 )
			value = pow(value, 1./gamma);
		double a = (0.01*Saturation) * value * (1. - value) / 2.;
		double r = value + a * (-0.14861 * cos(phi) + 1.78277 * sin(phi));
		double g = value + a * (-0.29227 * cos(phi) - 0.90649 * sin(phi));
		double b = value + a * ( 1.97294 * cos(phi));
		int ri = r * 256;
		int gi = g * 256;
		int bi = b * 256;
		if ( ri > 255 ) ri = 255;
		if ( ri < 0 ) ri = 0;
		if ( gi > 255 ) gi = 255;
		if ( gi < 0 ) gi = 0;
		if ( bi > 255 ) bi = 255;
		if ( bi < 0 ) bi = 0;
		rv = (unsigned char) ri;
		gv = (unsigned char) gi;
		bv = (unsigned char) bi;
	}
};

class CubeHelixMap : public CubeHelixMapBase<100> { };
class CubeHelixColourfulMap : public CubeHelixMapBase<150> { };

#endif