annotate layer/ColourMapper.cpp @ 1015:352fa17af9f0 colourschemes

Rearrange colours, make some of them more attractive, give some of them nicer names
author Chris Cannam
date Tue, 19 Jan 2016 16:30:33 +0000
parents 6338d7dc3b6d
children 6a8084c72e7a
rev   line source
Chris@376 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@376 2
Chris@376 3 /*
Chris@376 4 Sonic Visualiser
Chris@376 5 An audio file viewer and annotation editor.
Chris@376 6 Centre for Digital Music, Queen Mary, University of London.
Chris@376 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@376 8
Chris@376 9 This program is free software; you can redistribute it and/or
Chris@376 10 modify it under the terms of the GNU General Public License as
Chris@376 11 published by the Free Software Foundation; either version 2 of the
Chris@376 12 License, or (at your option) any later version. See the file
Chris@376 13 COPYING included with this distribution for more information.
Chris@376 14 */
Chris@376 15
Chris@376 16 #include "ColourMapper.h"
Chris@376 17
Chris@376 18 #include <iostream>
Chris@376 19
Chris@376 20 #include <cmath>
Chris@376 21
Chris@682 22 #include "base/Debug.h"
Chris@682 23
Chris@1012 24 #include <vector>
Chris@1012 25
Chris@1012 26 using namespace std;
Chris@1012 27
Chris@1012 28 static vector<QColor> convertStrings(const vector<QString> &strs)
Chris@1012 29 {
Chris@1012 30 vector<QColor> converted;
Chris@1012 31 for (const auto &s: strs) converted.push_back(QColor(s));
Chris@1012 32 reverse(converted.begin(), converted.end());
Chris@1012 33 return converted;
Chris@1012 34 }
Chris@1012 35
Chris@1013 36 static vector<QColor> xRay = convertStrings({
Chris@1015 37 // Based on ColorBrewer ylGnBu
Chris@1015 38 "#ffffff", "#ffff00", "#f7fcf0", "#e0f3db", "#ccebc5", "#a8ddb5",
Chris@1015 39 "#7bccc4", "#4eb3d3", "#2b8cbe", "#0868ac", "#084081", "#042040"
Chris@1013 40 });
Chris@1012 41
Chris@1015 42 static vector<QColor> cherryPie = convertStrings({
Chris@1015 43 "#f7f7f7",
Chris@1015 44 "#fddbc7",
Chris@1015 45 "#f4a582",
Chris@1015 46 "#d6604d",
Chris@1015 47 "#b2182b",
Chris@1015 48 "#dd3497",
Chris@1015 49 "#ae017e",
Chris@1015 50 "#7a0177",
Chris@1015 51 "#49006a"
Chris@1015 52 // "#d1e5f0",
Chris@1015 53 // "#92c5de",
Chris@1015 54 // "#4393c3",
Chris@1015 55 // "#2166ac",
Chris@1015 56
Chris@1015 57 });
Chris@1015 58
Chris@1012 59 static void
Chris@1012 60 mapDiscrete(double norm, vector<QColor> &colours, double &r, double &g, double &b)
Chris@1012 61 {
Chris@1015 62 int n = int(colours.size());
Chris@1012 63 double m = norm * (n-1);
Chris@1012 64 if (m >= n-1) { colours[n-1].getRgbF(&r, &g, &b, 0); return; }
Chris@1012 65 if (m <= 0) { colours[0].getRgbF(&r, &g, &b, 0); return; }
Chris@1012 66 int base(int(floor(m)));
Chris@1012 67 double prop0 = (base + 1.0) - m, prop1 = m - base;
Chris@1012 68 QColor c0(colours[base]), c1(colours[base+1]);
Chris@1012 69 r = c0.redF() * prop0 + c1.redF() * prop1;
Chris@1012 70 g = c0.greenF() * prop0 + c1.greenF() * prop1;
Chris@1012 71 b = c0.blueF() * prop0 + c1.blueF() * prop1;
Chris@1012 72 }
Chris@1012 73
Chris@902 74 ColourMapper::ColourMapper(int map, double min, double max) :
Chris@376 75 QObject(),
Chris@376 76 m_map(map),
Chris@376 77 m_min(min),
Chris@376 78 m_max(max)
Chris@376 79 {
Chris@376 80 if (m_min == m_max) {
Chris@682 81 cerr << "WARNING: ColourMapper: min == max (== " << m_min
Chris@682 82 << "), adjusting" << endl;
Chris@376 83 m_max = m_min + 1;
Chris@376 84 }
Chris@376 85 }
Chris@376 86
Chris@376 87 ColourMapper::~ColourMapper()
Chris@376 88 {
Chris@376 89 }
Chris@376 90
Chris@376 91 int
Chris@376 92 ColourMapper::getColourMapCount()
Chris@376 93 {
Chris@1015 94 return 12;
Chris@376 95 }
Chris@376 96
Chris@376 97 QString
Chris@376 98 ColourMapper::getColourMapName(int n)
Chris@376 99 {
Chris@376 100 if (n >= getColourMapCount()) return tr("<unknown>");
Chris@376 101 StandardMap map = (StandardMap)n;
Chris@376 102
Chris@376 103 switch (map) {
Chris@1015 104 case DefaultColours: return tr("Green");
Chris@376 105 case WhiteOnBlack: return tr("White on Black");
Chris@376 106 case BlackOnWhite: return tr("Black on White");
Chris@1015 107 case CherryPie: return tr("Cherry");
Chris@1015 108 case YellowOnBlack: return tr("Wasp");
Chris@1015 109 case XRay: return tr("X-Ray");
Chris@376 110 case Sunset: return tr("Sunset");
Chris@376 111 case FruitSalad: return tr("Fruit Salad");
Chris@376 112 case Banded: return tr("Banded");
Chris@376 113 case Highlight: return tr("Highlight");
Chris@376 114 case Printer: return tr("Printer");
Chris@536 115 case HighGain: return tr("High Gain");
Chris@376 116 }
Chris@376 117
Chris@376 118 return tr("<unknown>");
Chris@376 119 }
Chris@376 120
Chris@376 121 QColor
Chris@902 122 ColourMapper::map(double value) const
Chris@376 123 {
Chris@902 124 double norm = (value - m_min) / (m_max - m_min);
Chris@902 125 if (norm < 0.0) norm = 0.0;
Chris@902 126 if (norm > 1.0) norm = 1.0;
Chris@376 127
Chris@902 128 double h = 0.0, s = 0.0, v = 0.0, r = 0.0, g = 0.0, b = 0.0;
Chris@376 129 bool hsv = true;
Chris@376 130
Chris@911 131 double blue = 0.6666, pieslice = 0.3333;
Chris@376 132
Chris@376 133 if (m_map >= getColourMapCount()) return Qt::black;
Chris@376 134 StandardMap map = (StandardMap)m_map;
Chris@376 135
Chris@376 136 switch (map) {
Chris@376 137
Chris@376 138 case DefaultColours:
Chris@902 139 h = blue - norm * 2.0 * pieslice;
Chris@902 140 s = 0.5f + norm/2.0;
Chris@376 141 v = norm;
Chris@376 142 break;
Chris@376 143
Chris@376 144 case WhiteOnBlack:
Chris@376 145 r = g = b = norm;
Chris@376 146 hsv = false;
Chris@376 147 break;
Chris@376 148
Chris@376 149 case BlackOnWhite:
Chris@902 150 r = g = b = 1.0 - norm;
Chris@376 151 hsv = false;
Chris@376 152 break;
Chris@376 153
Chris@1015 154 case CherryPie:
Chris@1015 155 hsv = false;
Chris@1015 156 mapDiscrete(norm, cherryPie, r, g, b);
Chris@376 157 break;
Chris@376 158
Chris@376 159 case YellowOnBlack:
Chris@902 160 h = 0.15;
Chris@902 161 s = 1.0;
Chris@376 162 v = norm;
Chris@376 163 break;
Chris@376 164
Chris@376 165 case Sunset:
Chris@902 166 r = (norm - 0.24) * 2.38;
Chris@902 167 if (r > 1.0) r = 1.0;
Chris@902 168 if (r < 0.0) r = 0.0;
Chris@902 169 g = (norm - 0.64) * 2.777;
Chris@902 170 if (g > 1.0) g = 1.0;
Chris@902 171 if (g < 0.0) g = 0.0;
Chris@376 172 b = (3.6f * norm);
Chris@902 173 if (norm > 0.277) b = 2.0 - b;
Chris@902 174 if (b > 1.0) b = 1.0;
Chris@902 175 if (b < 0.0) b = 0.0;
Chris@376 176 hsv = false;
Chris@376 177 break;
Chris@376 178
Chris@376 179 case FruitSalad:
Chris@902 180 h = blue + (pieslice/6.0) - norm;
Chris@902 181 if (h < 0.0) h += 1.0;
Chris@902 182 s = 1.0;
Chris@902 183 v = 1.0;
Chris@376 184 break;
Chris@376 185
Chris@376 186 case Banded:
Chris@376 187 if (norm < 0.125) return Qt::darkGreen;
Chris@376 188 else if (norm < 0.25) return Qt::green;
Chris@376 189 else if (norm < 0.375) return Qt::darkBlue;
Chris@376 190 else if (norm < 0.5) return Qt::blue;
Chris@376 191 else if (norm < 0.625) return Qt::darkYellow;
Chris@376 192 else if (norm < 0.75) return Qt::yellow;
Chris@376 193 else if (norm < 0.875) return Qt::darkRed;
Chris@376 194 else return Qt::red;
Chris@376 195 break;
Chris@376 196
Chris@376 197 case Highlight:
Chris@376 198 if (norm > 0.99) return Qt::white;
Chris@376 199 else return Qt::darkBlue;
Chris@376 200
Chris@376 201 case Printer:
Chris@376 202 if (norm > 0.8) {
Chris@902 203 r = 1.0;
Chris@376 204 } else if (norm > 0.7) {
Chris@902 205 r = 0.9;
Chris@376 206 } else if (norm > 0.6) {
Chris@902 207 r = 0.8;
Chris@376 208 } else if (norm > 0.5) {
Chris@902 209 r = 0.7;
Chris@376 210 } else if (norm > 0.4) {
Chris@902 211 r = 0.6;
Chris@376 212 } else if (norm > 0.3) {
Chris@902 213 r = 0.5;
Chris@376 214 } else if (norm > 0.2) {
Chris@902 215 r = 0.4;
Chris@376 216 } else {
Chris@902 217 r = 0.0;
Chris@376 218 }
Chris@902 219 r = g = b = 1.0 - r;
Chris@376 220 hsv = false;
Chris@376 221 break;
Chris@536 222
Chris@536 223 case HighGain:
Chris@902 224 if (norm <= 1.0 / 256.0) {
Chris@902 225 norm = 0.0;
Chris@536 226 } else {
Chris@904 227 norm = 0.1f + (pow(((norm - 0.5) * 2.0), 3.0) + 1.0) / 2.081;
Chris@536 228 }
Chris@536 229 // now as for Sunset
Chris@902 230 r = (norm - 0.24) * 2.38;
Chris@902 231 if (r > 1.0) r = 1.0;
Chris@902 232 if (r < 0.0) r = 0.0;
Chris@902 233 g = (norm - 0.64) * 2.777;
Chris@902 234 if (g > 1.0) g = 1.0;
Chris@902 235 if (g < 0.0) g = 0.0;
Chris@536 236 b = (3.6f * norm);
Chris@902 237 if (norm > 0.277) b = 2.0 - b;
Chris@902 238 if (b > 1.0) b = 1.0;
Chris@902 239 if (b < 0.0) b = 0.0;
Chris@536 240 hsv = false;
Chris@536 241 /*
Chris@902 242 if (r > 1.0) r = 1.0;
Chris@902 243 r = g = b = 1.0 - r;
Chris@536 244 hsv = false;
Chris@536 245 */
Chris@536 246 break;
Chris@1012 247
Chris@1013 248 case XRay:
Chris@1012 249 hsv = false;
Chris@1013 250 mapDiscrete(norm, xRay, r, g, b);
Chris@376 251 }
Chris@376 252
Chris@376 253 if (hsv) {
Chris@376 254 return QColor::fromHsvF(h, s, v);
Chris@376 255 } else {
Chris@376 256 return QColor::fromRgbF(r, g, b);
Chris@376 257 }
Chris@376 258 }
Chris@376 259
Chris@376 260 QColor
Chris@376 261 ColourMapper::getContrastingColour() const
Chris@376 262 {
Chris@376 263 if (m_map >= getColourMapCount()) return Qt::white;
Chris@376 264 StandardMap map = (StandardMap)m_map;
Chris@376 265
Chris@376 266 switch (map) {
Chris@376 267
Chris@376 268 case DefaultColours:
Chris@376 269 return QColor(255, 150, 50);
Chris@376 270
Chris@376 271 case WhiteOnBlack:
Chris@376 272 return Qt::red;
Chris@376 273
Chris@376 274 case BlackOnWhite:
Chris@376 275 return Qt::darkGreen;
Chris@376 276
Chris@1015 277 case CherryPie:
Chris@376 278 return Qt::green;
Chris@376 279
Chris@376 280 case YellowOnBlack:
Chris@376 281 return QColor::fromHsv(240, 255, 255);
Chris@376 282
Chris@1015 283 case XRay:
Chris@376 284 return Qt::red;
Chris@376 285
Chris@376 286 case Sunset:
Chris@376 287 return Qt::white;
Chris@376 288
Chris@376 289 case FruitSalad:
Chris@376 290 return Qt::white;
Chris@376 291
Chris@376 292 case Banded:
Chris@376 293 return Qt::cyan;
Chris@376 294
Chris@376 295 case Highlight:
Chris@376 296 return Qt::red;
Chris@376 297
Chris@376 298 case Printer:
Chris@376 299 return Qt::red;
Chris@536 300
Chris@536 301 case HighGain:
Chris@536 302 return Qt::red;
Chris@376 303 }
Chris@376 304
Chris@376 305 return Qt::white;
Chris@376 306 }
Chris@376 307
Chris@376 308 bool
Chris@376 309 ColourMapper::hasLightBackground() const
Chris@376 310 {
Chris@376 311 if (m_map >= getColourMapCount()) return false;
Chris@376 312 StandardMap map = (StandardMap)m_map;
Chris@376 313
Chris@376 314 switch (map) {
Chris@376 315
Chris@376 316 case BlackOnWhite:
Chris@376 317 case Printer:
Chris@536 318 case HighGain:
Chris@376 319 return true;
Chris@376 320
Chris@805 321 case DefaultColours:
Chris@805 322 case Sunset:
Chris@805 323 case WhiteOnBlack:
Chris@1015 324 case CherryPie:
Chris@805 325 case YellowOnBlack:
Chris@1015 326 case XRay:
Chris@805 327 case FruitSalad:
Chris@805 328 case Banded:
Chris@805 329 case Highlight:
Chris@805 330
Chris@376 331 default:
Chris@376 332 return false;
Chris@376 333 }
Chris@376 334 }
Chris@376 335
Chris@376 336