annotate layer/ColourMapper.cpp @ 201:1ab31723825d

...
author Chris Cannam
date Fri, 09 Feb 2007 09:32:04 +0000
parents 6b023411087b
children 28c8e8e3c537
rev   line source
Chris@196 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@196 2
Chris@196 3 /*
Chris@196 4 Sonic Visualiser
Chris@196 5 An audio file viewer and annotation editor.
Chris@196 6 Centre for Digital Music, Queen Mary, University of London.
Chris@196 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@196 8
Chris@196 9 This program is free software; you can redistribute it and/or
Chris@196 10 modify it under the terms of the GNU General Public License as
Chris@196 11 published by the Free Software Foundation; either version 2 of the
Chris@196 12 License, or (at your option) any later version. See the file
Chris@196 13 COPYING included with this distribution for more information.
Chris@196 14 */
Chris@196 15
Chris@196 16 #include "ColourMapper.h"
Chris@196 17
Chris@197 18 #include <iostream>
Chris@197 19
Chris@201 20 #include <cmath>
Chris@201 21
Chris@196 22 ColourMapper::ColourMapper(int map, float min, float max) :
Chris@196 23 QObject(),
Chris@196 24 m_map(map),
Chris@196 25 m_min(min),
Chris@196 26 m_max(max)
Chris@196 27 {
Chris@197 28 if (m_min == m_max) {
Chris@197 29 std::cerr << "WARNING: ColourMapper: min == max (== " << m_min
Chris@197 30 << "), adjusting" << std::endl;
Chris@197 31 m_max = m_min + 1;
Chris@197 32 }
Chris@196 33 }
Chris@196 34
Chris@196 35 ColourMapper::~ColourMapper()
Chris@196 36 {
Chris@196 37 }
Chris@196 38
Chris@196 39 int
Chris@196 40 ColourMapper::getColourMapCount()
Chris@196 41 {
Chris@197 42 return 10;
Chris@196 43 }
Chris@196 44
Chris@196 45 QString
Chris@196 46 ColourMapper::getColourMapName(int n)
Chris@196 47 {
Chris@197 48 if (n >= getColourMapCount()) return tr("<unknown>");
Chris@196 49 StandardMap map = (StandardMap)n;
Chris@196 50
Chris@196 51 switch (map) {
Chris@196 52 case DefaultColours: return tr("Default");
Chris@196 53 case WhiteOnBlack: return tr("White on Black");
Chris@196 54 case BlackOnWhite: return tr("Black on White");
Chris@196 55 case RedOnBlue: return tr("Red on Blue");
Chris@196 56 case YellowOnBlack: return tr("Yellow on Black");
Chris@196 57 case BlueOnBlack: return tr("Blue on Black");
Chris@196 58 case Sunset: return tr("Sunset");
Chris@196 59 case FruitSalad: return tr("Fruit Salad");
Chris@197 60 case Banded: return tr("Banded");
Chris@197 61 case Highlight: return tr("Highlight");
Chris@196 62 }
Chris@196 63
Chris@196 64 return tr("<unknown>");
Chris@196 65 }
Chris@196 66
Chris@196 67 QColor
Chris@196 68 ColourMapper::map(float value) const
Chris@196 69 {
Chris@196 70 float norm = (value - m_min) / (m_max - m_min);
Chris@196 71 if (norm < 0.f) norm = 0.f;
Chris@196 72 if (norm > 1.f) norm = 1.f;
Chris@196 73
Chris@196 74 float h = 0.f, s = 0.f, v = 0.f, r = 0.f, g = 0.f, b = 0.f;
Chris@196 75 bool hsv = true;
Chris@196 76
Chris@196 77 float red = 0.f, green = 0.3333f, blue = 0.6666f, pieslice = 0.3333f;
Chris@196 78
Chris@197 79 if (m_map >= getColourMapCount()) return Qt::black;
Chris@196 80 StandardMap map = (StandardMap)m_map;
Chris@196 81
Chris@196 82 switch (map) {
Chris@196 83
Chris@196 84 case DefaultColours:
Chris@196 85 h = blue - norm * 2.f * pieslice;
Chris@196 86 s = 0.5f + norm/2.f;
Chris@196 87 v = norm;
Chris@196 88 break;
Chris@196 89
Chris@196 90 case WhiteOnBlack:
Chris@196 91 r = g = b = norm;
Chris@196 92 hsv = false;
Chris@196 93 break;
Chris@196 94
Chris@196 95 case BlackOnWhite:
Chris@196 96 r = g = b = 1.f - norm;
Chris@196 97 hsv = false;
Chris@196 98 break;
Chris@196 99
Chris@196 100 case RedOnBlue:
Chris@196 101 h = blue - pieslice/4.f + norm * (pieslice + pieslice/4.f);
Chris@196 102 s = 1.f;
Chris@196 103 v = norm;
Chris@196 104 break;
Chris@196 105
Chris@196 106 case YellowOnBlack:
Chris@196 107 h = 0.15f;
Chris@196 108 s = 1.f;
Chris@196 109 v = norm;
Chris@196 110 break;
Chris@196 111
Chris@196 112 case BlueOnBlack:
Chris@197 113 h = blue;
Chris@196 114 s = 1.f;
Chris@196 115 v = norm * 2.f;
Chris@197 116 if (v > 1.f) {
Chris@196 117 v = 1.f;
Chris@197 118 s = 1.f - (sqrtf(norm) - 0.707f) * 3.413f;
Chris@197 119 if (s < 0.f) s = 0.f;
Chris@197 120 if (s > 1.f) s = 1.f;
Chris@196 121 }
Chris@196 122 break;
Chris@196 123
Chris@196 124 case Sunset:
Chris@196 125 r = (norm - 0.24f) * 2.38f;
Chris@196 126 if (r > 1.f) r = 1.f;
Chris@196 127 if (r < 0.f) r = 0.f;
Chris@196 128 g = (norm - 0.64f) * 2.777f;
Chris@196 129 if (g > 1.f) g = 1.f;
Chris@196 130 if (g < 0.f) g = 0.f;
Chris@196 131 b = (3.6f * norm);
Chris@196 132 if (norm > 0.277f) b = 2.f - b;
Chris@196 133 if (b > 1.f) b = 1.f;
Chris@196 134 if (b < 0.f) b = 0.f;
Chris@196 135 hsv = false;
Chris@196 136 break;
Chris@196 137
Chris@196 138 case FruitSalad:
Chris@197 139 h = blue + (pieslice/6.f) - norm;
Chris@196 140 if (h < 0.f) h += 1.f;
Chris@196 141 s = 1.f;
Chris@196 142 v = 1.f;
Chris@196 143 break;
Chris@197 144
Chris@197 145 case Banded:
Chris@197 146 if (norm < 0.125) return Qt::darkGreen;
Chris@197 147 else if (norm < 0.25) return Qt::green;
Chris@197 148 else if (norm < 0.375) return Qt::darkBlue;
Chris@197 149 else if (norm < 0.5) return Qt::blue;
Chris@197 150 else if (norm < 0.625) return Qt::darkYellow;
Chris@197 151 else if (norm < 0.75) return Qt::yellow;
Chris@197 152 else if (norm < 0.875) return Qt::darkRed;
Chris@197 153 else return Qt::red;
Chris@197 154 break;
Chris@197 155
Chris@197 156 case Highlight:
Chris@197 157 if (norm > 0.99) return Qt::white;
Chris@197 158 else return Qt::darkBlue;
Chris@196 159 }
Chris@196 160
Chris@196 161 if (hsv) {
Chris@196 162 return QColor::fromHsvF(h, s, v);
Chris@196 163 } else {
Chris@196 164 return QColor::fromRgbF(r, g, b);
Chris@196 165 }
Chris@196 166 }
Chris@196 167
Chris@196 168 QColor
Chris@196 169 ColourMapper::getContrastingColour() const
Chris@196 170 {
Chris@197 171 if (m_map >= getColourMapCount()) return Qt::white;
Chris@196 172 StandardMap map = (StandardMap)m_map;
Chris@196 173
Chris@196 174 switch (map) {
Chris@196 175
Chris@196 176 case DefaultColours:
Chris@196 177 return QColor(255, 150, 50);
Chris@196 178
Chris@196 179 case WhiteOnBlack:
Chris@196 180 return Qt::red;
Chris@196 181
Chris@196 182 case BlackOnWhite:
Chris@196 183 return Qt::darkGreen;
Chris@196 184
Chris@196 185 case RedOnBlue:
Chris@196 186 return Qt::green;
Chris@196 187
Chris@196 188 case YellowOnBlack:
Chris@196 189 return QColor::fromHsv(240, 255, 255);
Chris@196 190
Chris@196 191 case BlueOnBlack:
Chris@196 192 return Qt::red;
Chris@196 193
Chris@196 194 case Sunset:
Chris@196 195 return Qt::white;
Chris@196 196
Chris@196 197 case FruitSalad:
Chris@196 198 return Qt::white;
Chris@197 199
Chris@197 200 case Banded:
Chris@197 201 return Qt::cyan;
Chris@197 202
Chris@197 203 case Highlight:
Chris@197 204 return Qt::red;
Chris@196 205 }
Chris@196 206
Chris@196 207 return Qt::white;
Chris@196 208 }
Chris@196 209
Chris@196 210