Skip to content
qwt_color_map.h 4.92 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- 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
#include "qwt_global.h"
#include "qwt_interval.h"
pixhawk's avatar
pixhawk committed
#include <qcolor.h>
#include <qvector.h>

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

  For displaying 3D data on a 2D plane the 3rd dimension is often
  displayed using colors, like f.e in a spectrogram.
pixhawk's avatar
pixhawk committed

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

Bryant's avatar
Bryant committed
    enum Format
    {
        //! The map is intended to map into RGB values.
pixhawk's avatar
pixhawk committed
        RGB,
Bryant's avatar
Bryant committed

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

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

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

Bryant's avatar
Bryant committed
       Map a value of a given interval into a RGB value.

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

pixhawk's avatar
pixhawk committed
       Map a value of a given interval into a color index
Bryant's avatar
Bryant committed

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

Bryant's avatar
Bryant committed
    QColor color( const QwtInterval &, double value ) const;
    virtual QVector<QRgb> colorTable( const QwtInterval & ) const;
pixhawk's avatar
pixhawk committed

private:
    Format d_format;
};

/*!
  \brief QwtLinearColorMap builds a color map from color stops.
pixhawk's avatar
pixhawk committed
  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
  into a color it is translated into this interval according to mode().
pixhawk's avatar
pixhawk committed
*/
class QWT_EXPORT QwtLinearColorMap: public QwtColorMap
{
public:
    /*!
       Mode of color map
       \sa setMode(), mode()
    */
Bryant's avatar
Bryant committed
    enum Mode
    {
        //! Return the color from the next lower color stop
pixhawk's avatar
pixhawk committed
        FixedColors,
Bryant's avatar
Bryant committed

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

Bryant's avatar
Bryant committed
    QwtLinearColorMap( QwtColorMap::Format = QwtColorMap::RGB );
pixhawk's avatar
pixhawk committed
    QwtLinearColorMap( const QColor &from, const QColor &to,
Bryant's avatar
Bryant committed
        QwtColorMap::Format = QwtColorMap::RGB );
pixhawk's avatar
pixhawk committed

    virtual ~QwtLinearColorMap();

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

Bryant's avatar
Bryant committed
    void setColorInterval( const QColor &color1, const QColor &color2 );
    void addColorStop( double value, const QColor& );
    QVector<double> colorStops() const;
pixhawk's avatar
pixhawk committed

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

Bryant's avatar
Bryant committed
    virtual QRgb rgb( const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed
    virtual unsigned char colorIndex(
Bryant's avatar
Bryant committed
        const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed

    class ColorStops;

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

pixhawk's avatar
pixhawk committed
    class PrivateData;
    PrivateData *d_data;
};

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

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

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

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

pixhawk's avatar
pixhawk committed
    virtual unsigned char colorIndex(
Bryant's avatar
Bryant committed
        const QwtInterval &, double value ) const;
pixhawk's avatar
pixhawk committed

    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
    const QwtInterval &interval, double value ) const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    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
    }
}

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

#endif