qwt_color_map.h 5.38 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_COLOR_MAP_H
#define QWT_COLOR_MAP_H

#include <qglobal.h>
#include <qcolor.h>
#if QT_VERSION < 0x040000
#include <qvaluevector.h>
#else
#include <qvector.h>
#endif
#include "qwt_array.h"
#include "qwt_double_interval.h"

#if defined(QWT_TEMPLATEDLL)
// MOC_SKIP_BEGIN
template class QWT_EXPORT QwtArray<double>;
// MOC_SKIP_END
#endif

/*!
30
  \brief QwtColorMap is used to map values into colors.
pixhawk's avatar
pixhawk committed
31 32

  For displaying 3D data on a 2D plane the 3rd dimension is often
33
  displayed using colors, like f.e in a spectrogram.
pixhawk's avatar
pixhawk committed
34 35 36 37 38 39 40 41 42 43 44 45 46

  Each color map is optimized to return colors for only one of the
  following image formats:

  - QImage::Format_Indexed8\n
  - QImage::Format_ARGB32\n

  \sa QwtPlotSpectrogram, QwtScaleWidget
*/

class QWT_EXPORT QwtColorMap
{
public:
47
    /*!
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53 54 55 56
        - RGB\n
        The map is intended to map into QRgb values.
        - Indexed\n
        The map is intended to map into 8 bit values, that
        are indices into the color table.

        \sa rgb(), colorIndex(), colorTable()
    */

57
    enum Format {
pixhawk's avatar
pixhawk committed
58 59 60 61 62 63 64 65 66 67 68 69
        RGB,
        Indexed
    };

    QwtColorMap(Format = QwtColorMap::RGB );
    virtual ~QwtColorMap();

    inline Format format() const;

    //! Clone the color map
    virtual QwtColorMap *copy() const = 0;

70
    /*!
pixhawk's avatar
pixhawk committed
71 72 73 74 75 76 77 78
       Map a value of a given interval into a rgb value.
       \param interval Range for the values
       \param value Value
       \return rgb value, corresponding to value
    */
    virtual QRgb rgb(
        const QwtDoubleInterval &interval, double value) const = 0;

79
    /*!
pixhawk's avatar
pixhawk committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
       Map a value of a given interval into a color index
       \param interval Range for the values
       \param value Value
       \return color index, corresponding to value
     */
    virtual unsigned char colorIndex(
        const QwtDoubleInterval &interval, double value) const = 0;

    QColor color(const QwtDoubleInterval &, double value) const;
#if QT_VERSION < 0x040000
    virtual QValueVector<QRgb> colorTable(const QwtDoubleInterval &) const;
#else
    virtual QVector<QRgb> colorTable(const QwtDoubleInterval &) const;
#endif

private:
    Format d_format;
};


/*!
  \brief QwtLinearColorMap builds a color map from color stops.
102

pixhawk's avatar
pixhawk committed
103 104
  A color stop is a color at a specific position. The valid
  range for the positions is [0.0, 1.0]. When mapping a value
105
  into a color it is translated into this interval. If
pixhawk's avatar
pixhawk committed
106 107
  mode() == FixedColors the color is calculated from the next lower
  color stop. If mode() == ScaledColors the color is calculated
108
  by interpolating the colors of the adjacent stops.
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116
*/
class QWT_EXPORT QwtLinearColorMap: public QwtColorMap
{
public:
    /*!
       Mode of color map
       \sa setMode(), mode()
    */
117
    enum Mode {
pixhawk's avatar
pixhawk committed
118 119 120 121 122 123
        FixedColors,
        ScaledColors
    };

    QwtLinearColorMap(QwtColorMap::Format = QwtColorMap::RGB);
    QwtLinearColorMap( const QColor &from, const QColor &to,
124
                       QwtColorMap::Format = QwtColorMap::RGB);
pixhawk's avatar
pixhawk committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    QwtLinearColorMap(const QwtLinearColorMap &);

    virtual ~QwtLinearColorMap();

    QwtLinearColorMap &operator=(const QwtLinearColorMap &);

    virtual QwtColorMap *copy() const;

    void setMode(Mode);
    Mode mode() const;

    void setColorInterval(const QColor &color1, const QColor &color2);
    void addColorStop(double value, const QColor&);
    QwtArray<double> colorStops() const;

    QColor color1() const;
    QColor color2() const;

    virtual QRgb rgb(const QwtDoubleInterval &, double value) const;
    virtual unsigned char colorIndex(
        const QwtDoubleInterval &, double value) const;

    class ColorStops;

private:
    class PrivateData;
    PrivateData *d_data;
};

/*!
  \brief QwtAlphaColorMap variies the alpha value of a color
*/
class QWT_EXPORT QwtAlphaColorMap: public QwtColorMap
{
public:
    QwtAlphaColorMap(const QColor & = QColor(Qt::gray));
    QwtAlphaColorMap(const QwtAlphaColorMap &);

    virtual ~QwtAlphaColorMap();

    QwtAlphaColorMap &operator=(const QwtAlphaColorMap &);

    virtual QwtColorMap *copy() const;

    void setColor(const QColor &);
    QColor color() const;

    virtual QRgb rgb(const QwtDoubleInterval &, double value) const;

private:
    virtual unsigned char colorIndex(
        const QwtDoubleInterval &, double value) const;

    class PrivateData;
    PrivateData *d_data;
};


/*!
   Map a value into a color

   \param interval Valid interval for values
   \param value Value

   \return Color corresponding to value

   \warning This method is slow for Indexed color maps. If it is
            necessary to map many values, its better to get the
            color table once and find the color using colorIndex().
*/
inline QColor QwtColorMap::color(
    const QwtDoubleInterval &interval, double value) const
{
199
    if ( d_format == RGB ) {
pixhawk's avatar
pixhawk committed
200
        return QColor( rgb(interval, value) );
201
    } else {
pixhawk's avatar
pixhawk committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        const unsigned int index = colorIndex(interval, value);
        return colorTable(interval)[index]; // slow
    }
}

/*!
   \return Intended format of the color map
   \sa Format
*/
inline QwtColorMap::Format QwtColorMap::format() const
{
    return d_format;
}

#endif