comparison layer/ColourMapper.cpp @ 1012:2a85ab180d08 colourschemes

Experimentation with colour schemes and mappings
author Chris Cannam
date Wed, 02 Dec 2015 15:01:38 +0000
parents 6e2a034f7117
children ac69043f82b6
comparison
equal deleted inserted replaced
1011:7242fe160c19 1012:2a85ab180d08
19 19
20 #include <cmath> 20 #include <cmath>
21 21
22 #include "base/Debug.h" 22 #include "base/Debug.h"
23 23
24 #include <vector>
25
26 using namespace std;
27
28 static vector<QString> ylGnBuS {
29 // this is ylGnBu 9
30 // "#f7fcf0","#e0f3db","#ccebc5","#a8ddb5","#7bccc4","#4eb3d3","#2b8cbe","#0868ac","#084081"
31
32 "#ffffff", "#ffff00", "#f7fcf0","#e0f3db","#ccebc5","#a8ddb5","#7bccc4","#4eb3d3","#2b8cbe","#0868ac","#084081","#042040"
33
34 // this is PuOr 11
35 // "#7f3b08","#b35806","#e08214","#fdb863","#fee0b6","#f7f7f7","#d8daeb","#b2abd2","#8073ac","#542788","#2d004b"
36 };
37
38
39 static vector<QColor> convertStrings(const vector<QString> &strs)
40 {
41 vector<QColor> converted;
42 for (const auto &s: strs) converted.push_back(QColor(s));
43 reverse(converted.begin(), converted.end());
44 return converted;
45 }
46
47 static vector<QColor> ylGnBu = convertStrings(ylGnBuS);
48
49 static void
50 mapDiscrete(double norm, vector<QColor> &colours, double &r, double &g, double &b)
51 {
52 int n = colours.size();
53 double m = norm * (n-1);
54 if (m >= n-1) { colours[n-1].getRgbF(&r, &g, &b, 0); return; }
55 if (m <= 0) { colours[0].getRgbF(&r, &g, &b, 0); return; }
56 int base(int(floor(m)));
57 double prop0 = (base + 1.0) - m, prop1 = m - base;
58 QColor c0(colours[base]), c1(colours[base+1]);
59 r = c0.redF() * prop0 + c1.redF() * prop1;
60 g = c0.greenF() * prop0 + c1.greenF() * prop1;
61 b = c0.blueF() * prop0 + c1.blueF() * prop1;
62 cerr << "mapDiscrete: norm " << norm << " -> " << r << "," << g << "," << b << endl;
63 }
64
24 ColourMapper::ColourMapper(int map, double min, double max) : 65 ColourMapper::ColourMapper(int map, double min, double max) :
25 QObject(), 66 QObject(),
26 m_map(map), 67 m_map(map),
27 m_min(min), 68 m_min(min),
28 m_max(max) 69 m_max(max)
39 } 80 }
40 81
41 int 82 int
42 ColourMapper::getColourMapCount() 83 ColourMapper::getColourMapCount()
43 { 84 {
44 return 12; 85 return 13;
45 } 86 }
46 87
47 QString 88 QString
48 ColourMapper::getColourMapName(int n) 89 ColourMapper::getColourMapName(int n)
49 { 90 {
61 case FruitSalad: return tr("Fruit Salad"); 102 case FruitSalad: return tr("Fruit Salad");
62 case Banded: return tr("Banded"); 103 case Banded: return tr("Banded");
63 case Highlight: return tr("Highlight"); 104 case Highlight: return tr("Highlight");
64 case Printer: return tr("Printer"); 105 case Printer: return tr("Printer");
65 case HighGain: return tr("High Gain"); 106 case HighGain: return tr("High Gain");
107 case YlGnBu: return tr("YlGnBu");
66 } 108 }
67 109
68 return tr("<unknown>"); 110 return tr("<unknown>");
69 } 111 }
70 112
205 if (r > 1.0) r = 1.0; 247 if (r > 1.0) r = 1.0;
206 r = g = b = 1.0 - r; 248 r = g = b = 1.0 - r;
207 hsv = false; 249 hsv = false;
208 */ 250 */
209 break; 251 break;
252
253 case YlGnBu:
254 hsv = false;
255 mapDiscrete(norm, ylGnBu, r, g, b);
210 } 256 }
211 257
212 if (hsv) { 258 if (hsv) {
213 return QColor::fromHsvF(h, s, v); 259 return QColor::fromHsvF(h, s, v);
214 } else { 260 } else {