qwt_color_map.h 4.92 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12
/* -*- 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

Bryant's avatar
Bryant committed
13 14
#include "qwt_global.h"
#include "qwt_interval.h"
pixhawk's avatar
pixhawk committed
15 16 17 18
#include <qcolor.h>
#include <qvector.h>

/*!
19
  \brief QwtColorMap is used to map values into colors.
pixhawk's avatar
pixhawk committed
20 21

  For displaying 3D data on a 2D plane the 3rd dimension is often
22
  displayed using colors, like f.e in a spectrogram.
pixhawk's avatar
pixhawk committed
23 24 25 26 27 28 29 30 31 32 33 34 35

  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:
36
    /*!
Bryant's avatar
Bryant committed
37
        Format for color mapping
pixhawk's avatar
pixhawk committed
38 39 40
        \sa rgb(), colorIndex(), colorTable()
    */

Bryant's avatar
Bryant committed
41 42 43
    enum Format
    {
        //! The map is intended to map into RGB values.
pixhawk's avatar
pixhawk committed
44
        RGB,
Bryant's avatar
Bryant committed
45 46 47 48 49

        /*!
          The map is intended to map into 8 bit values, that
          are indices into the color table.
         */
pixhawk's avatar
pixhawk committed
50 51 52
        Indexed
    };

Bryant's avatar
Bryant committed
53
    QwtColorMap( Format = QwtColorMap::RGB );
pixhawk's avatar
pixhawk committed
54 55
    virtual ~QwtColorMap();

Bryant's avatar
Bryant committed
56
    Format format() const;
pixhawk's avatar
pixhawk committed
57

58
    /*!
Bryant's avatar
Bryant committed
59 60
       Map a value of a given interval into a RGB value.

pixhawk's avatar
pixhawk committed
61 62
       \param interval Range for the values
       \param value Value
Bryant's avatar
Bryant committed
63
       \return RGB value, corresponding to value
pixhawk's avatar
pixhawk committed
64
    */
Bryant's avatar
Bryant committed
65 66
    virtual QRgb rgb( const QwtInterval &interval,
        double value ) const = 0;
pixhawk's avatar
pixhawk committed
67

68
    /*!
pixhawk's avatar
pixhawk committed
69
       Map a value of a given interval into a color index
Bryant's avatar
Bryant committed
70

pixhawk's avatar
pixhawk committed
71 72 73 74 75
       \param interval Range for the values
       \param value Value
       \return color index, corresponding to value
     */
    virtual unsigned char colorIndex(
Bryant's avatar
Bryant committed
76
        const QwtInterval &interval, double value ) const = 0;
pixhawk's avatar
pixhawk committed
77

Bryant's avatar
Bryant committed
78 79
    QColor color( const QwtInterval &, double value ) const;
    virtual QVector<QRgb> colorTable( const QwtInterval & ) const;
pixhawk's avatar
pixhawk committed
80 81 82 83 84 85 86

private:
    Format d_format;
};

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

pixhawk's avatar
pixhawk committed
88 89
  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
Bryant's avatar
Bryant committed
90
  into a color it is translated into this interval according to mode().
pixhawk's avatar
pixhawk committed
91 92 93 94 95 96 97 98
*/
class QWT_EXPORT QwtLinearColorMap: public QwtColorMap
{
public:
    /*!
       Mode of color map
       \sa setMode(), mode()
    */
Bryant's avatar
Bryant committed
99 100 101
    enum Mode
    {
        //! Return the color from the next lower color stop
pixhawk's avatar
pixhawk committed
102
        FixedColors,
Bryant's avatar
Bryant committed
103 104

        //! Interpolating the colors of the adjacent stops.
pixhawk's avatar
pixhawk committed
105 106 107
        ScaledColors
    };

Bryant's avatar
Bryant committed
108
    QwtLinearColorMap( QwtColorMap::Format = QwtColorMap::RGB );
pixhawk's avatar
pixhawk committed
109
    QwtLinearColorMap( const QColor &from, const QColor &to,
Bryant's avatar
Bryant committed
110
        QwtColorMap::Format = QwtColorMap::RGB );
pixhawk's avatar
pixhawk committed
111 112 113

    virtual ~QwtLinearColorMap();

Bryant's avatar
Bryant committed
114
    void setMode( Mode );
pixhawk's avatar
pixhawk committed
115 116
    Mode mode() const;

Bryant's avatar
Bryant committed
117 118 119
    void setColorInterval( const QColor &color1, const QColor &color2 );
    void addColorStop( double value, const QColor& );
    QVector<double> colorStops() const;
pixhawk's avatar
pixhawk committed
120 121 122 123

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

Bryant's avatar
Bryant committed
124
    virtual QRgb rgb( const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed
125
    virtual unsigned char colorIndex(
Bryant's avatar
Bryant committed
126
        const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed
127 128 129 130

    class ColorStops;

private:
Bryant's avatar
Bryant committed
131 132 133 134
    // Disabled copy constructor and operator=
    QwtLinearColorMap( const QwtLinearColorMap & );
    QwtLinearColorMap &operator=( const QwtLinearColorMap & );

pixhawk's avatar
pixhawk committed
135 136 137 138 139
    class PrivateData;
    PrivateData *d_data;
};

/*!
Bryant's avatar
Bryant committed
140
  \brief QwtAlphaColorMap varies the alpha value of a color
pixhawk's avatar
pixhawk committed
141 142 143 144
*/
class QWT_EXPORT QwtAlphaColorMap: public QwtColorMap
{
public:
Bryant's avatar
Bryant committed
145
    QwtAlphaColorMap( const QColor & = QColor( Qt::gray ) );
pixhawk's avatar
pixhawk committed
146 147
    virtual ~QwtAlphaColorMap();

Bryant's avatar
Bryant committed
148
    void setColor( const QColor & );
pixhawk's avatar
pixhawk committed
149 150
    QColor color() const;

Bryant's avatar
Bryant committed
151
    virtual QRgb rgb( const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed
152 153

private:
Bryant's avatar
Bryant committed
154 155 156
    QwtAlphaColorMap( const QwtAlphaColorMap & );
    QwtAlphaColorMap &operator=( const QwtAlphaColorMap & );

pixhawk's avatar
pixhawk committed
157
    virtual unsigned char colorIndex(
Bryant's avatar
Bryant committed
158
        const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    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(
Bryant's avatar
Bryant committed
178
    const QwtInterval &interval, double value ) const
pixhawk's avatar
pixhawk committed
179
{
Bryant's avatar
Bryant committed
180 181 182 183 184 185 186 187
    if ( d_format == RGB )
    {
        return QColor( rgb( interval, value ) );
    }
    else
    {
        const unsigned int index = colorIndex( interval, value );
        return colorTable( interval )[index]; // slow
pixhawk's avatar
pixhawk committed
188 189 190 191 192 193 194 195 196 197 198 199 200
    }
}

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

#endif