1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997   Josef Wilgen
4  * Copyright (C) 2002   Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #ifndef QWT_COLOR_MAP_H
11 #define QWT_COLOR_MAP_H
12 
13 #include "qwt_global.h"
14 #include "qwt_interval.h"
15 #include <qcolor.h>
16 #include <qvector.h>
17 
18 /*!
19   \brief QwtColorMap is used to map values into colors.
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.
23 
24   Each color map is optimized to return colors for only one of the
25   following image formats:
26 
27   - QImage::Format_Indexed8\n
28   - QImage::Format_ARGB32\n
29 
30   \sa QwtPlotSpectrogram, QwtScaleWidget
31 */
32 
33 class QWT_EXPORT QwtColorMap
34 {
35 public:
36     /*!
37         Format for color mapping
38         \sa rgb(), colorIndex(), colorTable()
39     */
40 
41     enum Format
42     {
43         //! The map is intended to map into RGB values.
44         RGB,
45 
46         /*!
47           The map is intended to map into 8 bit values, that
48           are indices into the color table.
49          */
50         Indexed
51     };
52 
53     QwtColorMap( Format = QwtColorMap::RGB );
54     virtual ~QwtColorMap();
55 
56     Format format() const;
57 
58     /*!
59        Map a value of a given interval into a RGB value.
60 
61        \param interval Range for the values
62        \param value Value
63        \return RGB value, corresponding to value
64     */
65     virtual QRgb rgb( const QwtInterval &interval,
66         double value ) const = 0;
67 
68     /*!
69        Map a value of a given interval into a color index
70 
71        \param interval Range for the values
72        \param value Value
73        \return color index, corresponding to value
74      */
75     virtual unsigned char colorIndex(
76         const QwtInterval &interval, double value ) const = 0;
77 
78     QColor color( const QwtInterval &, double value ) const;
79     virtual QVector<QRgb> colorTable( const QwtInterval & ) const;
80 
81 private:
82     Format d_format;
83 };
84 
85 /*!
86   \brief QwtLinearColorMap builds a color map from color stops.
87 
88   A color stop is a color at a specific position. The valid
89   range for the positions is [0.0, 1.0]. When mapping a value
90   into a color it is translated into this interval according to mode().
91 */
92 class QWT_EXPORT QwtLinearColorMap: public QwtColorMap
93 {
94 public:
95     /*!
96        Mode of color map
97        \sa setMode(), mode()
98     */
99     enum Mode
100     {
101         //! Return the color from the next lower color stop
102         FixedColors,
103 
104         //! Interpolating the colors of the adjacent stops.
105         ScaledColors
106     };
107 
108     QwtLinearColorMap( QwtColorMap::Format = QwtColorMap::RGB );
109     QwtLinearColorMap( const QColor &color1, const QColor &color2,
110         QwtColorMap::Format = QwtColorMap::RGB );
111 
112     virtual ~QwtLinearColorMap();
113 
114     void setMode( Mode );
115     Mode mode() const;
116 
117     void setColorInterval( const QColor &color1, const QColor &color2 );
118     void addColorStop( double value, const QColor& );
119     QVector<double> colorStops() const;
120 
121     QColor color1() const;
122     QColor color2() const;
123 
124     virtual QRgb rgb( const QwtInterval &, double value ) const;
125     virtual unsigned char colorIndex(
126         const QwtInterval &, double value ) const;
127 
128     class ColorStops;
129 
130 private:
131     // Disabled copy constructor and operator=
132     QwtLinearColorMap( const QwtLinearColorMap & );
133     QwtLinearColorMap &operator=( const QwtLinearColorMap & );
134 
135     class PrivateData;
136     PrivateData *d_data;
137 };
138 
139 /*!
140   \brief QwtAlphaColorMap varies the alpha value of a color
141 */
142 class QWT_EXPORT QwtAlphaColorMap: public QwtColorMap
143 {
144 public:
145     QwtAlphaColorMap( const QColor & = QColor( Qt::gray ) );
146     virtual ~QwtAlphaColorMap();
147 
148     void setColor( const QColor & );
149     QColor color() const;
150 
151     virtual QRgb rgb( const QwtInterval &, double value ) const;
152 
153 private:
154     QwtAlphaColorMap( const QwtAlphaColorMap & );
155     QwtAlphaColorMap &operator=( const QwtAlphaColorMap & );
156 
157     virtual unsigned char colorIndex(
158         const QwtInterval &, double value ) const;
159 
160     class PrivateData;
161     PrivateData *d_data;
162 };
163 
164 
165 /*!
166    Map a value into a color
167 
168    \param interval Valid interval for values
169    \param value Value
170 
171    \return Color corresponding to value
172 
173    \warning This method is slow for Indexed color maps. If it is
174             necessary to map many values, its better to get the
175             color table once and find the color using colorIndex().
176 */
color(const QwtInterval & interval,double value)177 inline QColor QwtColorMap::color(
178     const QwtInterval &interval, double value ) const
179 {
180     if ( d_format == RGB )
181     {
182         return QColor::fromRgba( rgb( interval, value ) );
183     }
184     else
185     {
186         const unsigned int index = colorIndex( interval, value );
187 
188         const QVector<QRgb> rgbTable = colorTable( interval );
189         return rgbTable[index]; // slow
190     }
191 }
192 
193 /*!
194    \return Intended format of the color map
195    \sa Format
196 */
format()197 inline QwtColorMap::Format QwtColorMap::format() const
198 {
199     return d_format;
200 }
201 
202 #endif
203