comparison layer/ColourMapper.cpp @ 1043:fccee028a522 3.0-integration

Merge from branch "spectrogram-minor-refactor"
author Chris Cannam
date Thu, 04 Feb 2016 11:18:08 +0000
parents 9c890b7dfa83
children 65b183494331
comparison
equal deleted inserted replaced
1042:cd9e76e755bf 1043:fccee028a522
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<QColor> convertStrings(const vector<QString> &strs)
29 {
30 vector<QColor> converted;
31 for (const auto &s: strs) converted.push_back(QColor(s));
32 reverse(converted.begin(), converted.end());
33 return converted;
34 }
35
36 static vector<QColor> ice = convertStrings({
37 // Based on ColorBrewer ylGnBu
38 "#ffffff", "#ffff00", "#f7fcf0", "#e0f3db", "#ccebc5", "#a8ddb5",
39 "#7bccc4", "#4eb3d3", "#2b8cbe", "#0868ac", "#084081", "#042040"
40 });
41
42 static vector<QColor> cherry = convertStrings({
43 "#f7f7f7", "#fddbc7", "#f4a582", "#d6604d", "#b2182b", "#dd3497",
44 "#ae017e", "#7a0177", "#49006a"
45 });
46
47 static void
48 mapDiscrete(double norm, vector<QColor> &colours, double &r, double &g, double &b)
49 {
50 int n = int(colours.size());
51 double m = norm * (n-1);
52 if (m >= n-1) { colours[n-1].getRgbF(&r, &g, &b, 0); return; }
53 if (m <= 0) { colours[0].getRgbF(&r, &g, &b, 0); return; }
54 int base(int(floor(m)));
55 double prop0 = (base + 1.0) - m, prop1 = m - base;
56 QColor c0(colours[base]), c1(colours[base+1]);
57 r = c0.redF() * prop0 + c1.redF() * prop1;
58 g = c0.greenF() * prop0 + c1.greenF() * prop1;
59 b = c0.blueF() * prop0 + c1.blueF() * prop1;
60 }
61
24 ColourMapper::ColourMapper(int map, double min, double max) : 62 ColourMapper::ColourMapper(int map, double min, double max) :
25 QObject(), 63 QObject(),
26 m_map(map), 64 m_map(map),
27 m_min(min), 65 m_min(min),
28 m_max(max) 66 m_max(max)
49 { 87 {
50 if (n >= getColourMapCount()) return tr("<unknown>"); 88 if (n >= getColourMapCount()) return tr("<unknown>");
51 StandardMap map = (StandardMap)n; 89 StandardMap map = (StandardMap)n;
52 90
53 switch (map) { 91 switch (map) {
54 case DefaultColours: return tr("Default"); 92 case Green: return tr("Green");
55 case WhiteOnBlack: return tr("White on Black"); 93 case WhiteOnBlack: return tr("White on Black");
56 case BlackOnWhite: return tr("Black on White"); 94 case BlackOnWhite: return tr("Black on White");
57 case RedOnBlue: return tr("Red on Blue"); 95 case Cherry: return tr("Cherry");
58 case YellowOnBlack: return tr("Yellow on Black"); 96 case Wasp: return tr("Wasp");
59 case BlueOnBlack: return tr("Blue on Black"); 97 case Ice: return tr("Ice");
60 case Sunset: return tr("Sunset"); 98 case Sunset: return tr("Sunset");
61 case FruitSalad: return tr("Fruit Salad"); 99 case FruitSalad: return tr("Fruit Salad");
62 case Banded: return tr("Banded"); 100 case Banded: return tr("Banded");
63 case Highlight: return tr("Highlight"); 101 case Highlight: return tr("Highlight");
64 case Printer: return tr("Printer"); 102 case Printer: return tr("Printer");
83 if (m_map >= getColourMapCount()) return Qt::black; 121 if (m_map >= getColourMapCount()) return Qt::black;
84 StandardMap map = (StandardMap)m_map; 122 StandardMap map = (StandardMap)m_map;
85 123
86 switch (map) { 124 switch (map) {
87 125
88 case DefaultColours: 126 case Green:
89 h = blue - norm * 2.0 * pieslice; 127 h = blue - norm * 2.0 * pieslice;
90 s = 0.5f + norm/2.0; 128 s = 0.5f + norm/2.0;
91 v = norm; 129 v = norm;
92 break; 130 break;
93 131
99 case BlackOnWhite: 137 case BlackOnWhite:
100 r = g = b = 1.0 - norm; 138 r = g = b = 1.0 - norm;
101 hsv = false; 139 hsv = false;
102 break; 140 break;
103 141
104 case RedOnBlue: 142 case Cherry:
105 h = blue - pieslice/4.0 + norm * (pieslice + pieslice/4.0); 143 hsv = false;
106 s = 1.0; 144 mapDiscrete(norm, cherry, r, g, b);
107 v = norm; 145 break;
108 break; 146
109 147 case Wasp:
110 case YellowOnBlack:
111 h = 0.15; 148 h = 0.15;
112 s = 1.0; 149 s = 1.0;
113 v = norm; 150 v = norm;
114 break;
115
116 case BlueOnBlack:
117 h = blue;
118 s = 1.0;
119 v = norm * 2.0;
120 if (v > 1.0) {
121 v = 1.0;
122 s = 1.0 - (sqrt(norm) - 0.707) * 3.413;
123 if (s < 0.0) s = 0.0;
124 if (s > 1.0) s = 1.0;
125 }
126 break; 151 break;
127 152
128 case Sunset: 153 case Sunset:
129 r = (norm - 0.24) * 2.38; 154 r = (norm - 0.24) * 2.38;
130 if (r > 1.0) r = 1.0; 155 if (r > 1.0) r = 1.0;
205 if (r > 1.0) r = 1.0; 230 if (r > 1.0) r = 1.0;
206 r = g = b = 1.0 - r; 231 r = g = b = 1.0 - r;
207 hsv = false; 232 hsv = false;
208 */ 233 */
209 break; 234 break;
235
236 case Ice:
237 hsv = false;
238 mapDiscrete(norm, ice, r, g, b);
210 } 239 }
211 240
212 if (hsv) { 241 if (hsv) {
213 return QColor::fromHsvF(h, s, v); 242 return QColor::fromHsvF(h, s, v);
214 } else { 243 } else {
222 if (m_map >= getColourMapCount()) return Qt::white; 251 if (m_map >= getColourMapCount()) return Qt::white;
223 StandardMap map = (StandardMap)m_map; 252 StandardMap map = (StandardMap)m_map;
224 253
225 switch (map) { 254 switch (map) {
226 255
227 case DefaultColours: 256 case Green:
228 return QColor(255, 150, 50); 257 return QColor(255, 150, 50);
229 258
230 case WhiteOnBlack: 259 case WhiteOnBlack:
231 return Qt::red; 260 return Qt::red;
232 261
233 case BlackOnWhite: 262 case BlackOnWhite:
234 return Qt::darkGreen; 263 return Qt::darkGreen;
235 264
236 case RedOnBlue: 265 case Cherry:
237 return Qt::green; 266 return Qt::green;
238 267
239 case YellowOnBlack: 268 case Wasp:
240 return QColor::fromHsv(240, 255, 255); 269 return QColor::fromHsv(240, 255, 255);
241 270
242 case BlueOnBlack: 271 case Ice:
243 return Qt::red; 272 return Qt::red;
244 273
245 case Sunset: 274 case Sunset:
246 return Qt::white; 275 return Qt::white;
247 276
275 case BlackOnWhite: 304 case BlackOnWhite:
276 case Printer: 305 case Printer:
277 case HighGain: 306 case HighGain:
278 return true; 307 return true;
279 308
280 case DefaultColours: 309 case Green:
281 case Sunset: 310 case Sunset:
282 case WhiteOnBlack: 311 case WhiteOnBlack:
283 case RedOnBlue: 312 case Cherry:
284 case YellowOnBlack: 313 case Wasp:
285 case BlueOnBlack: 314 case Ice:
286 case FruitSalad: 315 case FruitSalad:
287 case Banded: 316 case Banded:
288 case Highlight: 317 case Highlight:
289 318
290 default: 319 default: