comparison layer/ColourMapper.cpp @ 1148:c0d841cb8ab9 tony-2.0-integration

Merge latest SV 3.0 branch code
author Chris Cannam
date Fri, 19 Aug 2016 15:58:57 +0100
parents 65b183494331
children 73d43e410a6b
comparison
equal deleted inserted replaced
1009:96cf499fad62 1148:c0d841cb8ab9
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(),
26 m_map(map), 63 m_map(map),
27 m_min(min), 64 m_min(min),
28 m_max(max) 65 m_max(max)
29 { 66 {
30 if (m_min == m_max) { 67 if (m_min == m_max) {
45 } 82 }
46 83
47 QString 84 QString
48 ColourMapper::getColourMapName(int n) 85 ColourMapper::getColourMapName(int n)
49 { 86 {
50 if (n >= getColourMapCount()) return tr("<unknown>"); 87 if (n >= getColourMapCount()) return QObject::tr("<unknown>");
51 StandardMap map = (StandardMap)n; 88 StandardMap map = (StandardMap)n;
52 89
53 switch (map) { 90 switch (map) {
54 case DefaultColours: return tr("Default"); 91 case Green: return QObject::tr("Green");
55 case WhiteOnBlack: return tr("White on Black"); 92 case WhiteOnBlack: return QObject::tr("White on Black");
56 case BlackOnWhite: return tr("Black on White"); 93 case BlackOnWhite: return QObject::tr("Black on White");
57 case RedOnBlue: return tr("Red on Blue"); 94 case Cherry: return QObject::tr("Cherry");
58 case YellowOnBlack: return tr("Yellow on Black"); 95 case Wasp: return QObject::tr("Wasp");
59 case BlueOnBlack: return tr("Blue on Black"); 96 case Ice: return QObject::tr("Ice");
60 case Sunset: return tr("Sunset"); 97 case Sunset: return QObject::tr("Sunset");
61 case FruitSalad: return tr("Fruit Salad"); 98 case FruitSalad: return QObject::tr("Fruit Salad");
62 case Banded: return tr("Banded"); 99 case Banded: return QObject::tr("Banded");
63 case Highlight: return tr("Highlight"); 100 case Highlight: return QObject::tr("Highlight");
64 case Printer: return tr("Printer"); 101 case Printer: return QObject::tr("Printer");
65 case HighGain: return tr("High Gain"); 102 case HighGain: return QObject::tr("High Gain");
66 } 103 }
67 104
68 return tr("<unknown>"); 105 return QObject::tr("<unknown>");
69 } 106 }
70 107
71 QColor 108 QColor
72 ColourMapper::map(double value) const 109 ColourMapper::map(double value) const
73 { 110 {
83 if (m_map >= getColourMapCount()) return Qt::black; 120 if (m_map >= getColourMapCount()) return Qt::black;
84 StandardMap map = (StandardMap)m_map; 121 StandardMap map = (StandardMap)m_map;
85 122
86 switch (map) { 123 switch (map) {
87 124
88 case DefaultColours: 125 case Green:
89 h = blue - norm * 2.0 * pieslice; 126 h = blue - norm * 2.0 * pieslice;
90 s = 0.5f + norm/2.0; 127 s = 0.5f + norm/2.0;
91 v = norm; 128 v = norm;
92 break; 129 break;
93 130
99 case BlackOnWhite: 136 case BlackOnWhite:
100 r = g = b = 1.0 - norm; 137 r = g = b = 1.0 - norm;
101 hsv = false; 138 hsv = false;
102 break; 139 break;
103 140
104 case RedOnBlue: 141 case Cherry:
105 h = blue - pieslice/4.0 + norm * (pieslice + pieslice/4.0); 142 hsv = false;
106 s = 1.0; 143 mapDiscrete(norm, cherry, r, g, b);
107 v = norm; 144 break;
108 break; 145
109 146 case Wasp:
110 case YellowOnBlack:
111 h = 0.15; 147 h = 0.15;
112 s = 1.0; 148 s = 1.0;
113 v = norm; 149 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; 150 break;
127 151
128 case Sunset: 152 case Sunset:
129 r = (norm - 0.24) * 2.38; 153 r = (norm - 0.24) * 2.38;
130 if (r > 1.0) r = 1.0; 154 if (r > 1.0) r = 1.0;
205 if (r > 1.0) r = 1.0; 229 if (r > 1.0) r = 1.0;
206 r = g = b = 1.0 - r; 230 r = g = b = 1.0 - r;
207 hsv = false; 231 hsv = false;
208 */ 232 */
209 break; 233 break;
234
235 case Ice:
236 hsv = false;
237 mapDiscrete(norm, ice, r, g, b);
210 } 238 }
211 239
212 if (hsv) { 240 if (hsv) {
213 return QColor::fromHsvF(h, s, v); 241 return QColor::fromHsvF(h, s, v);
214 } else { 242 } else {
222 if (m_map >= getColourMapCount()) return Qt::white; 250 if (m_map >= getColourMapCount()) return Qt::white;
223 StandardMap map = (StandardMap)m_map; 251 StandardMap map = (StandardMap)m_map;
224 252
225 switch (map) { 253 switch (map) {
226 254
227 case DefaultColours: 255 case Green:
228 return QColor(255, 150, 50); 256 return QColor(255, 150, 50);
229 257
230 case WhiteOnBlack: 258 case WhiteOnBlack:
231 return Qt::red; 259 return Qt::red;
232 260
233 case BlackOnWhite: 261 case BlackOnWhite:
234 return Qt::darkGreen; 262 return Qt::darkGreen;
235 263
236 case RedOnBlue: 264 case Cherry:
237 return Qt::green; 265 return Qt::green;
238 266
239 case YellowOnBlack: 267 case Wasp:
240 return QColor::fromHsv(240, 255, 255); 268 return QColor::fromHsv(240, 255, 255);
241 269
242 case BlueOnBlack: 270 case Ice:
243 return Qt::red; 271 return Qt::red;
244 272
245 case Sunset: 273 case Sunset:
246 return Qt::white; 274 return Qt::white;
247 275
275 case BlackOnWhite: 303 case BlackOnWhite:
276 case Printer: 304 case Printer:
277 case HighGain: 305 case HighGain:
278 return true; 306 return true;
279 307
280 case DefaultColours: 308 case Green:
281 case Sunset: 309 case Sunset:
282 case WhiteOnBlack: 310 case WhiteOnBlack:
283 case RedOnBlue: 311 case Cherry:
284 case YellowOnBlack: 312 case Wasp:
285 case BlueOnBlack: 313 case Ice:
286 case FruitSalad: 314 case FruitSalad:
287 case Banded: 315 case Banded:
288 case Highlight: 316 case Highlight:
289 317
290 default: 318 default: