Chris@1068: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1068: Chris@1068: /* Chris@1068: Sonic Visualiser Chris@1068: An audio file viewer and annotation editor. Chris@1068: Centre for Digital Music, Queen Mary, University of London. Chris@1068: This file copyright 2006-2016 Chris Cannam and QMUL. Chris@1068: Chris@1068: This program is free software; you can redistribute it and/or Chris@1068: modify it under the terms of the GNU General Public License as Chris@1068: published by the Free Software Foundation; either version 2 of the Chris@1068: License, or (at your option) any later version. See the file Chris@1068: COPYING included with this distribution for more information. Chris@1068: */ Chris@1068: Chris@1068: #ifndef COLOUR_SCALE_H Chris@1068: #define COLOUR_SCALE_H Chris@1068: Chris@1068: #include "ColourMapper.h" Chris@1068: Chris@1105: enum class ColourScaleType { Chris@1105: Linear, Chris@1105: Meter, Chris@1105: Log, Chris@1105: Phase, Chris@1105: PlusMinusOne, Chris@1105: Absolute Chris@1105: }; Chris@1105: Chris@1068: /** Chris@1068: * Map values within a range onto a set of colours, with a given Chris@1068: * distribution (linear, log etc) and optional colourmap rotation. Chris@1068: */ Chris@1068: class ColourScale Chris@1068: { Chris@1068: public: Chris@1070: struct Parameters { Chris@1266: Parameters() : colourMap(0), scaleType(ColourScaleType::Linear), Chris@1266: minValue(0.0), maxValue(1.0), Chris@1266: threshold(0.0), gain(1.0), multiple(1.0) { } Chris@1070: Chris@1266: /** A colour map index as used by ColourMapper */ Chris@1266: int colourMap; Chris@1266: Chris@1266: /** Distribution for the scale */ Chris@1266: ColourScaleType scaleType; Chris@1266: Chris@1266: /** Minimum value in source range */ Chris@1266: double minValue; Chris@1266: Chris@1266: /** Maximum value in source range. Must be > minValue */ Chris@1266: double maxValue; Chris@1070: Chris@1266: /** Threshold below which every value is mapped to background Chris@1266: pixel 0 */ Chris@1266: double threshold; Chris@1070: Chris@1266: /** Gain to apply before thresholding, mapping, and clamping */ Chris@1266: double gain; Chris@1137: Chris@1137: /** Multiple to apply after thresholding and mapping. In most Chris@1137: * cases the gain parameter is the one you want instead of Chris@1137: * this, but this can be used for example with Log scale to Chris@1137: * produce the log of some power of the original value, Chris@1137: * e.g. multiple = 2 gives log(x^2). */ Chris@1137: double multiple; Chris@1070: }; Chris@1070: Chris@1068: /** Chris@1070: * Create a ColourScale with the given parameters. Chris@1068: * Chris@1068: * Note that some parameters may be ignored for some scale Chris@1068: * distribution settings. For example, min and max are ignored for Chris@1068: * PlusMinusOneScale and PhaseColourScale and threshold and gain Chris@1068: * are ignored for PhaseColourScale. Chris@1068: */ Chris@1070: ColourScale(Parameters parameters); Chris@1071: ~ColourScale(); Chris@1068: Chris@1071: ColourScale(const ColourScale &) = default; Chris@1071: ColourScale &operator=(const ColourScale &) = default; Chris@1079: Chris@1079: /** Chris@1079: * Return the general type of scale this is. Chris@1079: */ Chris@1105: ColourScaleType getScale() const; Chris@1071: Chris@1068: /** Chris@1068: * Return a pixel number (in the range 0-255 inclusive) Chris@1068: * corresponding to the given value. The pixel 0 is used only for Chris@1068: * values below the threshold supplied in the constructor. All Chris@1068: * other values are mapped onto the range 1-255. Chris@1068: */ Chris@1079: int getPixel(double value) const; Chris@1068: Chris@1068: /** Chris@1068: * Return the colour for the given pixel number (which must be in Chris@1068: * the range 0-255). The pixel 0 is always the background Chris@1068: * colour. Other pixels are mapped taking into account the given Chris@1068: * colourmap rotation (which is also a value in the range 0-255). Chris@1068: */ Chris@1079: QColor getColourForPixel(int pixel, int rotation) const; Chris@1068: Chris@1068: /** Chris@1068: * Return the colour corresponding to the given value. This is Chris@1068: * equivalent to getColourForPixel(getPixel(value), rotation). Chris@1068: */ Chris@1079: QColor getColour(double value, int rotation) const { Chris@1266: return getColourForPixel(getPixel(value), rotation); Chris@1068: } Chris@1068: Chris@1068: private: Chris@1070: Parameters m_params; Chris@1068: ColourMapper m_mapper; Chris@1068: double m_mappedMin; Chris@1068: double m_mappedMax; Chris@1068: static int m_maxPixel; Chris@1068: }; Chris@1068: Chris@1068: #endif