Chris@196: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@196: Chris@196: /* Chris@196: Sonic Visualiser Chris@196: An audio file viewer and annotation editor. Chris@196: Centre for Digital Music, Queen Mary, University of London. Chris@196: This file copyright 2006-2007 Chris Cannam and QMUL. Chris@196: Chris@196: This program is free software; you can redistribute it and/or Chris@196: modify it under the terms of the GNU General Public License as Chris@196: published by the Free Software Foundation; either version 2 of the Chris@196: License, or (at your option) any later version. See the file Chris@196: COPYING included with this distribution for more information. Chris@196: */ Chris@196: Chris@196: #include "ColourMapper.h" Chris@196: Chris@197: #include Chris@197: Chris@201: #include Chris@201: Chris@196: ColourMapper::ColourMapper(int map, float min, float max) : Chris@196: QObject(), Chris@196: m_map(map), Chris@196: m_min(min), Chris@196: m_max(max) Chris@196: { Chris@197: if (m_min == m_max) { Chris@197: std::cerr << "WARNING: ColourMapper: min == max (== " << m_min Chris@197: << "), adjusting" << std::endl; Chris@197: m_max = m_min + 1; Chris@197: } Chris@196: } Chris@196: Chris@196: ColourMapper::~ColourMapper() Chris@196: { Chris@196: } Chris@196: Chris@196: int Chris@196: ColourMapper::getColourMapCount() Chris@196: { Chris@274: return 11; Chris@196: } Chris@196: Chris@196: QString Chris@196: ColourMapper::getColourMapName(int n) Chris@196: { Chris@197: if (n >= getColourMapCount()) return tr(""); Chris@196: StandardMap map = (StandardMap)n; Chris@196: Chris@196: switch (map) { Chris@196: case DefaultColours: return tr("Default"); Chris@196: case WhiteOnBlack: return tr("White on Black"); Chris@196: case BlackOnWhite: return tr("Black on White"); Chris@196: case RedOnBlue: return tr("Red on Blue"); Chris@196: case YellowOnBlack: return tr("Yellow on Black"); Chris@196: case BlueOnBlack: return tr("Blue on Black"); Chris@196: case Sunset: return tr("Sunset"); Chris@196: case FruitSalad: return tr("Fruit Salad"); Chris@197: case Banded: return tr("Banded"); Chris@197: case Highlight: return tr("Highlight"); Chris@274: case Printer: return tr("Printer"); Chris@196: } Chris@196: Chris@196: return tr(""); Chris@196: } Chris@196: Chris@196: QColor Chris@196: ColourMapper::map(float value) const Chris@196: { Chris@196: float norm = (value - m_min) / (m_max - m_min); Chris@196: if (norm < 0.f) norm = 0.f; Chris@196: if (norm > 1.f) norm = 1.f; Chris@196: Chris@196: float h = 0.f, s = 0.f, v = 0.f, r = 0.f, g = 0.f, b = 0.f; Chris@196: bool hsv = true; Chris@196: Chris@248: // float red = 0.f, green = 0.3333f; Chris@248: float blue = 0.6666f, pieslice = 0.3333f; Chris@196: Chris@197: if (m_map >= getColourMapCount()) return Qt::black; Chris@196: StandardMap map = (StandardMap)m_map; Chris@196: Chris@196: switch (map) { Chris@196: Chris@196: case DefaultColours: Chris@196: h = blue - norm * 2.f * pieslice; Chris@196: s = 0.5f + norm/2.f; Chris@196: v = norm; Chris@196: break; Chris@196: Chris@196: case WhiteOnBlack: Chris@196: r = g = b = norm; Chris@196: hsv = false; Chris@196: break; Chris@196: Chris@196: case BlackOnWhite: Chris@196: r = g = b = 1.f - norm; Chris@196: hsv = false; Chris@196: break; Chris@196: Chris@196: case RedOnBlue: Chris@196: h = blue - pieslice/4.f + norm * (pieslice + pieslice/4.f); Chris@196: s = 1.f; Chris@196: v = norm; Chris@196: break; Chris@196: Chris@196: case YellowOnBlack: Chris@196: h = 0.15f; Chris@196: s = 1.f; Chris@196: v = norm; Chris@196: break; Chris@196: Chris@196: case BlueOnBlack: Chris@197: h = blue; Chris@196: s = 1.f; Chris@196: v = norm * 2.f; Chris@197: if (v > 1.f) { Chris@196: v = 1.f; Chris@197: s = 1.f - (sqrtf(norm) - 0.707f) * 3.413f; Chris@197: if (s < 0.f) s = 0.f; Chris@197: if (s > 1.f) s = 1.f; Chris@196: } Chris@196: break; Chris@196: Chris@196: case Sunset: Chris@196: r = (norm - 0.24f) * 2.38f; Chris@196: if (r > 1.f) r = 1.f; Chris@196: if (r < 0.f) r = 0.f; Chris@196: g = (norm - 0.64f) * 2.777f; Chris@196: if (g > 1.f) g = 1.f; Chris@196: if (g < 0.f) g = 0.f; Chris@196: b = (3.6f * norm); Chris@196: if (norm > 0.277f) b = 2.f - b; Chris@196: if (b > 1.f) b = 1.f; Chris@196: if (b < 0.f) b = 0.f; Chris@196: hsv = false; Chris@196: break; Chris@196: Chris@196: case FruitSalad: Chris@197: h = blue + (pieslice/6.f) - norm; Chris@196: if (h < 0.f) h += 1.f; Chris@196: s = 1.f; Chris@196: v = 1.f; Chris@196: break; Chris@197: Chris@197: case Banded: Chris@197: if (norm < 0.125) return Qt::darkGreen; Chris@197: else if (norm < 0.25) return Qt::green; Chris@197: else if (norm < 0.375) return Qt::darkBlue; Chris@197: else if (norm < 0.5) return Qt::blue; Chris@197: else if (norm < 0.625) return Qt::darkYellow; Chris@197: else if (norm < 0.75) return Qt::yellow; Chris@197: else if (norm < 0.875) return Qt::darkRed; Chris@197: else return Qt::red; Chris@197: break; Chris@197: Chris@197: case Highlight: Chris@197: if (norm > 0.99) return Qt::white; Chris@197: else return Qt::darkBlue; Chris@274: Chris@274: case Printer: Chris@274: if (norm > 0.8) { Chris@274: r = 1.f; Chris@274: } else if (norm > 0.7) { Chris@274: r = 0.9f; Chris@274: } else if (norm > 0.6) { Chris@274: r = 0.8f; Chris@274: } else if (norm > 0.5) { Chris@274: r = 0.7f; Chris@274: } else if (norm > 0.4) { Chris@274: r = 0.6f; Chris@274: } else if (norm > 0.3) { Chris@274: r = 0.5f; Chris@274: } else if (norm > 0.2) { Chris@274: r = 0.4f; Chris@274: } else { Chris@274: r = 0.f; Chris@274: } Chris@274: r = g = b = 1.f - r; Chris@274: hsv = false; Chris@274: break; Chris@196: } Chris@196: Chris@196: if (hsv) { Chris@196: return QColor::fromHsvF(h, s, v); Chris@196: } else { Chris@196: return QColor::fromRgbF(r, g, b); Chris@196: } Chris@196: } Chris@196: Chris@196: QColor Chris@196: ColourMapper::getContrastingColour() const Chris@196: { Chris@197: if (m_map >= getColourMapCount()) return Qt::white; Chris@196: StandardMap map = (StandardMap)m_map; Chris@196: Chris@196: switch (map) { Chris@196: Chris@196: case DefaultColours: Chris@196: return QColor(255, 150, 50); Chris@196: Chris@196: case WhiteOnBlack: Chris@196: return Qt::red; Chris@196: Chris@196: case BlackOnWhite: Chris@196: return Qt::darkGreen; Chris@196: Chris@196: case RedOnBlue: Chris@196: return Qt::green; Chris@196: Chris@196: case YellowOnBlack: Chris@196: return QColor::fromHsv(240, 255, 255); Chris@196: Chris@196: case BlueOnBlack: Chris@196: return Qt::red; Chris@196: Chris@196: case Sunset: Chris@196: return Qt::white; Chris@196: Chris@196: case FruitSalad: Chris@196: return Qt::white; Chris@197: Chris@197: case Banded: Chris@197: return Qt::cyan; Chris@197: Chris@197: case Highlight: Chris@197: return Qt::red; Chris@274: Chris@274: case Printer: Chris@274: return Qt::red; Chris@196: } Chris@196: Chris@196: return Qt::white; Chris@196: } Chris@196: Chris@196: