annotate 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
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@196 18 ColourMapper::ColourMapper(int map, float min, float max) :
Chris@196 19 QObject(),
Chris@196 20 m_map(map),
Chris@196 21 m_min(min),
Chris@196 22 m_max(max)
Chris@196 23 {
Chris@196 24 }
Chris@196 25
Chris@196 26 ColourMapper::~ColourMapper()
Chris@196 27 {
Chris@196 28 }
Chris@196 29
Chris@196 30 int
Chris@196 31 ColourMapper::getColourMapCount()
Chris@196 32 {
Chris@196 33 return 8;
Chris@196 34 }
Chris@196 35
Chris@196 36 QString
Chris@196 37 ColourMapper::getColourMapName(int n)
Chris@196 38 {
Chris@196 39 if (n >= 8) return tr("<unknown>");
Chris@196 40 StandardMap map = (StandardMap)n;
Chris@196 41
Chris@196 42 switch (map) {
Chris@196 43 case DefaultColours: return tr("Default");
Chris@196 44 case WhiteOnBlack: return tr("White on Black");
Chris@196 45 case BlackOnWhite: return tr("Black on White");
Chris@196 46 case RedOnBlue: return tr("Red on Blue");
Chris@196 47 case YellowOnBlack: return tr("Yellow on Black");
Chris@196 48 case BlueOnBlack: return tr("Blue on Black");
Chris@196 49 case Sunset: return tr("Sunset");
Chris@196 50 case FruitSalad: return tr("Fruit Salad");
Chris@196 51 }
Chris@196 52
Chris@196 53 return tr("<unknown>");
Chris@196 54 }
Chris@196 55
Chris@196 56 QColor
Chris@196 57 ColourMapper::map(float value) const
Chris@196 58 {
Chris@196 59 float norm = (value - m_min) / (m_max - m_min);
Chris@196 60 if (norm < 0.f) norm = 0.f;
Chris@196 61 if (norm > 1.f) norm = 1.f;
Chris@196 62
Chris@196 63 float h = 0.f, s = 0.f, v = 0.f, r = 0.f, g = 0.f, b = 0.f;
Chris@196 64 bool hsv = true;
Chris@196 65
Chris@196 66 float red = 0.f, green = 0.3333f, blue = 0.6666f, pieslice = 0.3333f;
Chris@196 67
Chris@196 68 if (m_map >= 8) return Qt::black;
Chris@196 69 StandardMap map = (StandardMap)m_map;
Chris@196 70
Chris@196 71 switch (map) {
Chris@196 72
Chris@196 73 case DefaultColours:
Chris@196 74 h = blue - norm * 2.f * pieslice;
Chris@196 75 s = 0.5f + norm/2.f;
Chris@196 76 v = norm;
Chris@196 77 break;
Chris@196 78
Chris@196 79 case WhiteOnBlack:
Chris@196 80 r = g = b = norm;
Chris@196 81 hsv = false;
Chris@196 82 break;
Chris@196 83
Chris@196 84 case BlackOnWhite:
Chris@196 85 r = g = b = 1.f - norm;
Chris@196 86 hsv = false;
Chris@196 87 break;
Chris@196 88
Chris@196 89 case RedOnBlue:
Chris@196 90 h = blue - pieslice/4.f + norm * (pieslice + pieslice/4.f);
Chris@196 91 s = 1.f;
Chris@196 92 v = norm;
Chris@196 93 break;
Chris@196 94
Chris@196 95 case YellowOnBlack:
Chris@196 96 h = 0.15f;
Chris@196 97 s = 1.f;
Chris@196 98 v = norm;
Chris@196 99 break;
Chris@196 100
Chris@196 101 case BlueOnBlack:
Chris@196 102 h = blue / 1.f;
Chris@196 103 s = 1.f;
Chris@196 104 v = norm * 2.f;
Chris@196 105 if (v > 1) {
Chris@196 106 v = 1.f;
Chris@196 107 s = 1.f - (sqrtf(norm) - 0.707f) * 3.414f;
Chris@196 108 }
Chris@196 109 break;
Chris@196 110
Chris@196 111 case Sunset:
Chris@196 112 r = (norm - 0.24f) * 2.38f;
Chris@196 113 if (r > 1.f) r = 1.f;
Chris@196 114 if (r < 0.f) r = 0.f;
Chris@196 115 g = (norm - 0.64f) * 2.777f;
Chris@196 116 if (g > 1.f) g = 1.f;
Chris@196 117 if (g < 0.f) g = 0.f;
Chris@196 118 b = (3.6f * norm);
Chris@196 119 if (norm > 0.277f) b = 2.f - b;
Chris@196 120 if (b > 1.f) b = 1.f;
Chris@196 121 if (b < 0.f) b = 0.f;
Chris@196 122 hsv = false;
Chris@196 123 break;
Chris@196 124
Chris@196 125 case FruitSalad:
Chris@196 126 h = blue + (pieslice/2.f) - norm;
Chris@196 127 if (h < 0.f) h += 1.f;
Chris@196 128 s = 1.f;
Chris@196 129 v = 1.f;
Chris@196 130 break;
Chris@196 131 }
Chris@196 132
Chris@196 133 if (hsv) {
Chris@196 134 return QColor::fromHsvF(h, s, v);
Chris@196 135 } else {
Chris@196 136 return QColor::fromRgbF(r, g, b);
Chris@196 137 }
Chris@196 138 }
Chris@196 139
Chris@196 140 QColor
Chris@196 141 ColourMapper::getContrastingColour() const
Chris@196 142 {
Chris@196 143 if (m_map >= 8) return Qt::white;
Chris@196 144 StandardMap map = (StandardMap)m_map;
Chris@196 145
Chris@196 146 switch (map) {
Chris@196 147
Chris@196 148 case DefaultColours:
Chris@196 149 return QColor(255, 150, 50);
Chris@196 150
Chris@196 151 case WhiteOnBlack:
Chris@196 152 return Qt::red;
Chris@196 153
Chris@196 154 case BlackOnWhite:
Chris@196 155 return Qt::darkGreen;
Chris@196 156
Chris@196 157 case RedOnBlue:
Chris@196 158 return Qt::green;
Chris@196 159
Chris@196 160 case YellowOnBlack:
Chris@196 161 return QColor::fromHsv(240, 255, 255);
Chris@196 162
Chris@196 163 case BlueOnBlack:
Chris@196 164 return Qt::red;
Chris@196 165
Chris@196 166 case Sunset:
Chris@196 167 return Qt::white;
Chris@196 168
Chris@196 169 case FruitSalad:
Chris@196 170 return Qt::white;
Chris@196 171 }
Chris@196 172
Chris@196 173 return Qt::white;
Chris@196 174 }
Chris@196 175
Chris@196 176