comparison layer/ColourMapper.cpp @ 196:22c99c8aa1e0

* Add separate colour mapping unit; use it in spectrogram (colour 3d plot to follow) * Add another colour scheme resembling that of a noted commercial application
author Chris Cannam
date Wed, 31 Jan 2007 12:13:47 +0000
parents
children 6b023411087b
comparison
equal deleted inserted replaced
195:4a3bdde1ef13 196:22c99c8aa1e0
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2007 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "ColourMapper.h"
17
18 ColourMapper::ColourMapper(int map, float min, float max) :
19 QObject(),
20 m_map(map),
21 m_min(min),
22 m_max(max)
23 {
24 }
25
26 ColourMapper::~ColourMapper()
27 {
28 }
29
30 int
31 ColourMapper::getColourMapCount()
32 {
33 return 8;
34 }
35
36 QString
37 ColourMapper::getColourMapName(int n)
38 {
39 if (n >= 8) return tr("<unknown>");
40 StandardMap map = (StandardMap)n;
41
42 switch (map) {
43 case DefaultColours: return tr("Default");
44 case WhiteOnBlack: return tr("White on Black");
45 case BlackOnWhite: return tr("Black on White");
46 case RedOnBlue: return tr("Red on Blue");
47 case YellowOnBlack: return tr("Yellow on Black");
48 case BlueOnBlack: return tr("Blue on Black");
49 case Sunset: return tr("Sunset");
50 case FruitSalad: return tr("Fruit Salad");
51 }
52
53 return tr("<unknown>");
54 }
55
56 QColor
57 ColourMapper::map(float value) const
58 {
59 float norm = (value - m_min) / (m_max - m_min);
60 if (norm < 0.f) norm = 0.f;
61 if (norm > 1.f) norm = 1.f;
62
63 float h = 0.f, s = 0.f, v = 0.f, r = 0.f, g = 0.f, b = 0.f;
64 bool hsv = true;
65
66 float red = 0.f, green = 0.3333f, blue = 0.6666f, pieslice = 0.3333f;
67
68 if (m_map >= 8) return Qt::black;
69 StandardMap map = (StandardMap)m_map;
70
71 switch (map) {
72
73 case DefaultColours:
74 h = blue - norm * 2.f * pieslice;
75 s = 0.5f + norm/2.f;
76 v = norm;
77 break;
78
79 case WhiteOnBlack:
80 r = g = b = norm;
81 hsv = false;
82 break;
83
84 case BlackOnWhite:
85 r = g = b = 1.f - norm;
86 hsv = false;
87 break;
88
89 case RedOnBlue:
90 h = blue - pieslice/4.f + norm * (pieslice + pieslice/4.f);
91 s = 1.f;
92 v = norm;
93 break;
94
95 case YellowOnBlack:
96 h = 0.15f;
97 s = 1.f;
98 v = norm;
99 break;
100
101 case BlueOnBlack:
102 h = blue / 1.f;
103 s = 1.f;
104 v = norm * 2.f;
105 if (v > 1) {
106 v = 1.f;
107 s = 1.f - (sqrtf(norm) - 0.707f) * 3.414f;
108 }
109 break;
110
111 case Sunset:
112 r = (norm - 0.24f) * 2.38f;
113 if (r > 1.f) r = 1.f;
114 if (r < 0.f) r = 0.f;
115 g = (norm - 0.64f) * 2.777f;
116 if (g > 1.f) g = 1.f;
117 if (g < 0.f) g = 0.f;
118 b = (3.6f * norm);
119 if (norm > 0.277f) b = 2.f - b;
120 if (b > 1.f) b = 1.f;
121 if (b < 0.f) b = 0.f;
122 hsv = false;
123 break;
124
125 case FruitSalad:
126 h = blue + (pieslice/2.f) - norm;
127 if (h < 0.f) h += 1.f;
128 s = 1.f;
129 v = 1.f;
130 break;
131 }
132
133 if (hsv) {
134 return QColor::fromHsvF(h, s, v);
135 } else {
136 return QColor::fromRgbF(r, g, b);
137 }
138 }
139
140 QColor
141 ColourMapper::getContrastingColour() const
142 {
143 if (m_map >= 8) return Qt::white;
144 StandardMap map = (StandardMap)m_map;
145
146 switch (map) {
147
148 case DefaultColours:
149 return QColor(255, 150, 50);
150
151 case WhiteOnBlack:
152 return Qt::red;
153
154 case BlackOnWhite:
155 return Qt::darkGreen;
156
157 case RedOnBlue:
158 return Qt::green;
159
160 case YellowOnBlack:
161 return QColor::fromHsv(240, 255, 255);
162
163 case BlueOnBlack:
164 return Qt::red;
165
166 case Sunset:
167 return Qt::white;
168
169 case FruitSalad:
170 return Qt::white;
171 }
172
173 return Qt::white;
174 }
175
176